diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index ea5b46d5..1a8d53b2 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -2,27 +2,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index c4439c19..2b38e11d 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,25 +1,25 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/sally/lib/src/runtime/executor/executor.dart b/sally/lib/src/runtime/executor/executor.dart index 18cd643b..87c425f1 100644 --- a/sally/lib/src/runtime/executor/executor.dart +++ b/sally/lib/src/runtime/executor/executor.dart @@ -33,7 +33,7 @@ abstract class GeneratedDatabase { return migration.onUpgrade(migrator, from, to); } - InsertStatement insert(TableInfo table) => + InsertStatement into(TableInfo table) => InsertStatement(this, table); SelectStatement select( diff --git a/sally/lib/src/runtime/statements/insert.dart b/sally/lib/src/runtime/statements/insert.dart index 6b0db5c2..b7ef276f 100644 --- a/sally/lib/src/runtime/statements/insert.dart +++ b/sally/lib/src/runtime/statements/insert.dart @@ -1,5 +1,6 @@ import 'package:meta/meta.dart'; import 'package:sally/sally.dart'; +import 'package:sally/src/runtime/components/component.dart'; class InsertStatement { @@ -14,10 +15,33 @@ class InsertStatement { table.validateIntegrity(entity, true); final map = table - ..entityToSql(entity) - .removeWhere((_, value) => value == null); + .entityToSql(entity) + ..removeWhere((_, value) => value == null); - print(map); + final ctx = GenerationContext(database); + ctx.buffer + ..write('INSERT INTO ') + ..write(table.$tableName) + ..write(' (') + ..write(map.keys.join(', ')) + ..write(') ') + ..write('VALUES ('); + + var first = true; + for (var variable in map.values) { + if (!first) { + ctx.buffer.write(', '); + } + first = false; + + variable.writeInto(ctx); + } + + ctx.buffer.write(')'); + + return database.executor.runInsert(ctx.sql, ctx.boundVariables); } + // TODO insert multiple values + } \ No newline at end of file diff --git a/sally/sally.iml b/sally/sally.iml index ae9af975..d3275515 100644 --- a/sally/sally.iml +++ b/sally/sally.iml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/sally/test/queries_test.dart b/sally/test/queries_test.dart index d231897d..2668147c 100644 --- a/sally/test/queries_test.dart +++ b/sally/test/queries_test.dart @@ -72,4 +72,12 @@ void main() { [100])); }); }); + + group('Generates INSERT statements', () { + test('with full data', () { + db.into(db.users).insert(UserDataObject(10, 'User')); + + verify(executor.runInsert('INSERT INTO users (id, name) VALUES (?, ?)', [10, 'User'])); + }); + }); } diff --git a/sally_generator/sally_generator.iml b/sally_generator/sally_generator.iml index ae9af975..0cf94bdc 100644 --- a/sally_generator/sally_generator.iml +++ b/sally_generator/sally_generator.iml @@ -9,7 +9,5 @@ - - \ No newline at end of file