Skip to content

Commit b8f81b0

Browse files
committed
use query() instead of execute() for insert statements.
1 parent 2a9f03e commit b8f81b0

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

lib/src/database_access.dart

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
3636
final entries = values.entries.toList();
3737
final columnList = entries.map((e) => e.key).join(',');
3838
final bindList = entries.map((e) => _bindForEntry(e)).join(',');
39-
return await execute('INSERT INTO $table ($columnList) VALUES ($bindList)',
40-
values: values.map((key, value) =>
41-
MapEntry(key, value is CustomBind ? value.value : value)),
42-
expectedResultCount: 1);
39+
return await execute(
40+
'INSERT INTO $table ($columnList) VALUES ($bindList)',
41+
values: values.map((key, value) =>
42+
MapEntry(key, value is CustomBind ? value.value : value)),
43+
expectedResultCount: 1,
44+
useExtendedQuery: true,
45+
);
4346
}
4447

4548
String _bindForEntry(MapEntry<String, Object?> entry) {
4649
final value = entry.value;
4750
if (value is CustomBind) {
48-
return value.bind;
51+
return value.formatString(entry.key);
4952
}
5053
return '@${entry.key}';
5154
}
@@ -116,12 +119,21 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
116119
Map<String, Object?>? values,
117120
int? timeoutInSeconds,
118121
int? expectedResultCount,
122+
bool useExtendedQuery = false,
119123
}) async {
120124
try {
121125
assert(_assertCorrectValues(values));
122126
_logger.finest('Executing query: $fmtString with values: $values');
123-
final result = await _conn.execute(fmtString,
124-
substitutionValues: values, timeoutInSeconds: timeoutInSeconds);
127+
128+
final int result;
129+
if (useExtendedQuery) {
130+
final sqlResult = await _conn.query(fmtString,
131+
substitutionValues: values, timeoutInSeconds: timeoutInSeconds);
132+
result = sqlResult.affectedRowCount;
133+
} else {
134+
result = await _conn.execute(fmtString,
135+
substitutionValues: values, timeoutInSeconds: timeoutInSeconds);
136+
}
125137
if (expectedResultCount != null && result != expectedResultCount) {
126138
throw StateError(
127139
'Expected result: $expectedResultCount but got $result. '
@@ -155,9 +167,29 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
155167
}
156168

157169
class CustomBind {
158-
CustomBind(this.bind, this.value);
159-
final String bind;
170+
CustomBind(this._bind, this.value, {this.type});
171+
final String _bind;
160172
final Object value;
173+
final PostgreSQLDataType? type;
174+
175+
String formatString(String bindName) => _bind;
176+
}
177+
178+
class CustomTypeBind extends CustomBind {
179+
factory CustomTypeBind(PostgreSQLDataType type, Object value) {
180+
// _bindCount.to
181+
return CustomTypeBind._(
182+
'',
183+
value,
184+
type,
185+
);
186+
}
187+
CustomTypeBind._(String bind, Object value, PostgreSQLDataType type)
188+
: super(bind, value, type: type);
189+
190+
@override
191+
String formatString(String bindName) =>
192+
'${PostgreSQLFormat.id(bindName)}::jsonb';
161193
}
162194

163195
abstract class DatabaseAccessBase<TX extends DatabaseTransactionBase<TABLES>,

0 commit comments

Comments
 (0)