Export substitute method out of the lib
darkstarx opened this issue · 2 comments
What if I want to make multiple insertions within single query? I should write something like this:
var sql = 'insert into my_table (id, name, create_date) values';
for (var obj in objects) {
sql += "(${obj.id}, '${obj.name}', '${obj.createDate.toIso8601String()}'),";
}
But what if create_date field has type date (not timestamp)? In such cases I should write formatter in each insertion. It is annoying.
I wonder, what are you thinking about exporting Connection.substitute outside the library? Like this for instance:
var sql = 'insert into my_table (id, name, create_date) values';
for (var obj in objects) {
sql += substitute(
'(@id, @name, @create_date:date),',
obj
);
}
Sorry, I don't get it. Why don't you use the values argument?
c.execute('insert ... values(@a, @b, @c)', {'a': date1, 'b': date2, 'c': date3});
To extend the conversion, you can implement a type converter and customize it via Pool
:
pool = new Pool(... typeConverter: yourTypeConverter);
Can you see the difference between this queries?
insert into mytable (a, b, c) values (1, 2, 3);
insert into mytable (a, b, c) values (1, 2, 3), (4, 5, 6), (7, 8, 9), ...;
How can you execute the second one using Connection.execute
, if tuples (1, 2, 3), (4, 5, 6), (7, 8, 9)...
are objects in some collection?
Ok. I write this clearly in dart for you.
class A
{
final int a;
final String b;
final DateTime c;
const A(this.a, this.b, this.c);
}
List<A> objects = [A(1, '1', DateTime.now()), A(2, '2', DateTime.now()), A(3, '3', DateTime.now())];
final connection = await pool.connect();
final values = ???;
connection.execute('insert into mytable (a, b, c) values ???, values);
What will you write in ???
places to execute query 2?