Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/features/mysql/mysql_table_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,22 @@ class _MysqlTableViewState extends material.State<MysqlTableView> {
);
if (!mounted) return;
if (outcome.isApplied) {
if (buffer.insertedRowCount > 0) {
// Inserted rows have no database-generated keys / defaults yet, so a
// follow-up UPDATE / DELETE on them would match 0 rows. Reload the page.
buffer.dispose();
setState(() {
_stagingBuffer = null;
_isSaving = false;
});
showAppToast(
context: context,
message: '${outcome.statementCount} change(s) saved',
variant: AppToastVariant.success,
);
await _fetch(refreshCount: true);
return;
}
final newRows = buffer.committedRows;
buffer.dispose();
setState(() {
Expand Down
16 changes: 16 additions & 0 deletions lib/features/postgresql/postgres_table_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,22 @@ class _PostgresTableViewState extends material.State<PostgresTableView> {
);
if (!mounted) return;
if (outcome.isApplied) {
if (buffer.insertedRowCount > 0) {
// Inserted rows have no database-generated keys / defaults yet, so a
// follow-up UPDATE / DELETE on them would match 0 rows. Reload the page.
buffer.dispose();
setState(() {
_stagingBuffer = null;
_isSaving = false;
});
showAppToast(
context: context,
message: '${outcome.statementCount} change(s) saved',
variant: AppToastVariant.success,
);
await _fetch(refreshCount: true);
return;
}
final newRows = buffer.committedRows;
buffer.dispose();
setState(() {
Expand Down
16 changes: 16 additions & 0 deletions lib/features/sqlite/sqlite_table_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,22 @@ class _SqliteTableViewState extends material.State<SqliteTableView> {
);
if (!mounted) return;
if (outcome.isApplied) {
if (buffer.insertedRowCount > 0) {
// Inserted rows have no database-generated keys / defaults yet, so a
// follow-up UPDATE / DELETE on them would match 0 rows. Reload the page.
buffer.dispose();
setState(() {
_stagingBuffer = null;
_isSaving = false;
});
showAppToast(
context: context,
message: '${outcome.statementCount} change(s) saved',
variant: AppToastVariant.success,
);
await _fetch();
return;
}
final newRows = buffer.committedRows;
buffer.dispose();
setState(() {
Expand Down
93 changes: 86 additions & 7 deletions test/core/database/sqlite_connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:querya_desktop/core/storage/local_db.dart';
import 'package:querya_desktop/core/database/sqlite_connection.dart';
import 'package:querya_desktop/core/database/sqlite_service.dart';
import 'package:querya_desktop/core/database/table_mutation_engine.dart';
import 'package:querya_desktop/core/database/table_schema_meta.dart';
import 'package:querya_desktop/features/sqlite/sqlite_table_utils.dart';
import 'package:querya_desktop/features/workspace/data_grid_staging_buffer.dart';
import 'package:querya_desktop/features/workspace/table_view_staging.dart';
Expand Down Expand Up @@ -216,8 +217,7 @@ void main() {
expect(cols, contains('name'));

final rows = [
for (final row in rs)
[for (final c in cols) '${row[c]}'],
for (final row in rs) [for (final c in cols) '${row[c]}'],
];
final buffer = DataGridStagingBuffer(columns: cols, rows: rows);
addTearDown(buffer.dispose);
Expand All @@ -241,6 +241,80 @@ void main() {
expect(after.first['name'], 'Grace');
});

test('row inserted with a generated key is editable only after a reload',
() async {
await conn.connect();
await conn.execute(
'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)',
);
await conn.execute("INSERT INTO t (id, name) VALUES (1, 'Ada')");

const columnMeta = {
'id': TableColumnMeta(
name: 'id',
dataType: 'INTEGER',
isPrimaryKey: true,
hasServerDefault: true,
),
};
const types = {'id': 'INTEGER', 'name': 'TEXT'};

Future<List<List<String>>> browse() async {
final rs = await conn.execute('SELECT id, name FROM t ORDER BY id');
return [
for (final row in rs) ['${row['id']}', '${row['name']}'],
];
}

DataGridStagingBuffer bufferFor(List<List<String>> rows) =>
DataGridStagingBuffer(
columns: const ['id', 'name'],
rows: rows,
primaryKeys: const ['id'],
);

// Insert a new row with a blank (generated) key.
final insertBuffer = bufferFor(await browse());
addTearDown(insertBuffer.dispose);
insertBuffer.addRow(['', 'Grace']);
final insertPlan = insertBuffer.generateMutationPlan(
dialect: SqlDialect.sqlite,
tableName: 't',
primaryKeys: const ['id'],
columnDataTypes: types,
columnMeta: columnMeta,
);
expect(insertPlan.statements.single.sql, isNot(contains('"id"')));
expectDmlMatchedRows(
await conn.executeAffected(insertPlan.statements.single.sql),
);

Future<int> renameLastRow(List<List<String>> baseline) async {
final buffer = bufferFor(baseline);
addTearDown(buffer.dispose);
buffer.setCell(baseline.length - 1, 1, 'Grace Hopper');
final plan = buffer.generateMutationPlan(
dialect: SqlDialect.sqlite,
tableName: 't',
primaryKeys: const ['id'],
columnDataTypes: types,
columnMeta: columnMeta,
);
return conn.executeAffected(plan.statements.single.sql);
}

// The locally committed baseline still has the blank key: 0 rows match.
final staleBaseline = insertBuffer.committedRows;
expect(staleBaseline.last, ['', 'Grace']);
expect(await renameLastRow(staleBaseline), 0);

// A reload from the database resolves the key, so the edit applies.
final reloaded = await browse();
expect(reloaded.last, ['2', 'Grace']);
expect(await renameLastRow(reloaded), 1);
expect((await browse()).last, ['2', 'Grace Hopper']);
});

test('WITHOUT ROWID tables keep the declared PK, not implicit rowid',
() async {
await conn.connect();
Expand Down Expand Up @@ -600,23 +674,28 @@ void main() {
);
});

test('inferQueryColumns returns column names when query returns zero rows', () async {
test('inferQueryColumns returns column names when query returns zero rows',
() async {
final conn = SqliteConnection(
id: 99,
name: 'mem',
path: ':memory:',
);
addTearDown(conn.disconnect);
await conn.connect();
await conn.execute('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);');
await conn.execute(
'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);');

final colsAll = await conn.inferQueryColumns('SELECT * FROM users WHERE 1=0;');
final colsAll =
await conn.inferQueryColumns('SELECT * FROM users WHERE 1=0;');
expect(colsAll, ['id', 'name', 'email']);

final colsProjected = await conn.inferQueryColumns('SELECT id, name AS full_name FROM users WHERE id = -1;');
final colsProjected = await conn.inferQueryColumns(
'SELECT id, name AS full_name FROM users WHERE id = -1;');
expect(colsProjected, ['id', 'full_name']);

final colsComputed = await conn.inferQueryColumns('SELECT 1 AS flag, COUNT(*) AS cnt WHERE 1=0;');
final colsComputed = await conn
.inferQueryColumns('SELECT 1 AS flag, COUNT(*) AS cnt WHERE 1=0;');
expect(colsComputed, ['flag', 'cnt']);
});
});
Expand Down
Loading