mirror of https://github.com/AMT-Cheif/drift.git
moor_ffi: Assert right parameter count (#608)
This commit is contained in:
parent
ee6e09699e
commit
cbde252b50
|
@ -90,6 +90,7 @@ class _SQLiteBindings {
|
|||
int length,
|
||||
Pointer<Void> disposeCb) sqlite3_bind_blob;
|
||||
int Function(Pointer<Statement> statement, int columnIndex) sqlite3_bind_null;
|
||||
sqlite3_bind_parameter_count_dart sqlite3_bind_parameter_count;
|
||||
|
||||
int Function(
|
||||
Pointer<Database> db,
|
||||
|
@ -136,6 +137,9 @@ class _SQLiteBindings {
|
|||
sqlite3_bind_null = sqlite
|
||||
.lookup<NativeFunction<sqlite3_bind_null_native>>('sqlite3_bind_null')
|
||||
.asFunction();
|
||||
sqlite3_bind_parameter_count = sqlite.lookupFunction<
|
||||
sqlite3_bind_parameter_count_native,
|
||||
sqlite3_bind_parameter_count_dart>('sqlite3_bind_parameter_count');
|
||||
sqlite3_open_v2 = sqlite
|
||||
.lookup<NativeFunction<sqlite3_open_v2_native_t>>('sqlite3_open_v2')
|
||||
.asFunction();
|
||||
|
|
|
@ -93,6 +93,10 @@ typedef sqlite3_bind_blob_native = Int32 Function(
|
|||
Pointer<Void> callback);
|
||||
typedef sqlite3_bind_null_native = Int32 Function(
|
||||
Pointer<Statement> statement, Int32 columnIndex);
|
||||
typedef sqlite3_bind_parameter_count_dart = int Function(
|
||||
Pointer<Statement> stmt);
|
||||
typedef sqlite3_bind_parameter_count_native = Int32 Function(
|
||||
Pointer<Statement> statement);
|
||||
|
||||
typedef sqlite3_function_handler = Void Function(
|
||||
Pointer<FunctionContext> context,
|
||||
|
|
|
@ -21,6 +21,12 @@ class PreparedStatement {
|
|||
_closed = true;
|
||||
}
|
||||
|
||||
/// Returns the amount of parameters in this prepared statement.
|
||||
///
|
||||
/// See also:
|
||||
/// - `sqlite3_bind_parameter_count`: https://www.sqlite.org/c3ref/bind_parameter_count.html
|
||||
int get parameterCount => bindings.sqlite3_bind_parameter_count(_stmt);
|
||||
|
||||
void _ensureNotFinalized() {
|
||||
if (_closed) {
|
||||
throw StateError('Tried to operate on a released prepared statement');
|
||||
|
@ -30,6 +36,11 @@ class PreparedStatement {
|
|||
/// Executes this prepared statement as a select statement. The returned rows
|
||||
/// will be returned.
|
||||
Result select([List<dynamic> params]) {
|
||||
assert(
|
||||
(params?.length ?? 0) == parameterCount,
|
||||
'Expected $parameterCount params, but got ${params?.length ?? 0}.',
|
||||
);
|
||||
|
||||
_ensureNotFinalized();
|
||||
_reset();
|
||||
_bindParams(params);
|
||||
|
|
|
@ -118,6 +118,22 @@ void main() {
|
|||
expect(() => stmt.execute([false]), throwsArgumentError);
|
||||
db.close();
|
||||
});
|
||||
|
||||
group('asserts that the amount of parameters are correct', () {
|
||||
final db = Database.memory();
|
||||
|
||||
test('when no parameters are set', () {
|
||||
final stmt = db.prepare('SELECT ?');
|
||||
expect(stmt.select, throwsA(isA<AssertionError>()));
|
||||
});
|
||||
|
||||
test('when the wrong amount of parameters are set', () {
|
||||
final stmt = db.prepare('SELECT ?, ?');
|
||||
expect(() => stmt.select([1]), throwsA(isA<AssertionError>()));
|
||||
});
|
||||
|
||||
tearDownAll(db.close);
|
||||
});
|
||||
}
|
||||
|
||||
void _raiseIfTwo(Pointer<FunctionContext> ctx, int argCount,
|
||||
|
|
Loading…
Reference in New Issue