Skip to content

Commit

Permalink
Reset statement before reusing it
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Mar 10, 2023
1 parent a8e9989 commit 2522ce6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions sqlite3/lib/src/ffi/impl/statement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class _FinalizableStatement extends FinalizablePart {
final Bindings _bindings;
final Pointer<sqlite3_stmt> _stmt;

bool _variablesBound = false;
bool _inResetState = true;
bool _closed = false;
final List<Pointer> _allocatedWhileBinding = [];

Expand All @@ -20,9 +20,9 @@ class _FinalizableStatement extends FinalizablePart {
}

void _reset() {
if (_variablesBound) {
if (!_inResetState) {
_bindings.sqlite3_reset(_stmt);
_variablesBound = false;
_inResetState = true;
}

for (final pointer in _allocatedWhileBinding) {
Expand Down Expand Up @@ -83,6 +83,7 @@ class PreparedStatementImpl implements PreparedStatement {
void _execute() {
int result;

_finalizable._inResetState = false;
// Users should be able to execute statements returning rows, so we should
// call _step() to skip past rows.
do {
Expand Down Expand Up @@ -149,6 +150,7 @@ class PreparedStatementImpl implements PreparedStatement {
final tableNames = _tableNames;
final columnCount = names.length;
final rows = <List<Object?>>[];
_finalizable._inResetState = false;

int resultCode;
while ((resultCode = _step()) == SqlError.SQLITE_ROW) {
Expand Down Expand Up @@ -209,7 +211,6 @@ class PreparedStatementImpl implements PreparedStatement {
}

_latestArguments = params;
_finalizable._variablesBound = true;
}

void _bindMapParams(Map<String, Object?> params) {
Expand Down Expand Up @@ -249,7 +250,6 @@ class PreparedStatementImpl implements PreparedStatement {
params, 'params', 'Expected $expectedLength parameters');
}

_finalizable._variablesBound = true;
_latestArguments = paramsAsList;
}

Expand Down Expand Up @@ -351,7 +351,9 @@ class _ActiveCursorIterator extends IteratingCursor {
List<String> columnNames,
List<String?>? tableNames,
) : columnCount = columnNames.length,
super(columnNames, tableNames);
super(columnNames, tableNames) {
statement._finalizable._inResetState = false;
}

@override
bool moveNext() {
Expand Down
8 changes: 5 additions & 3 deletions sqlite3/test/common/prepared_statement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ void testPreparedStatements(
test('prepared statements without parameters can be used multiple times', () {
final opened = sqlite3.openInMemory();
addTearDown(opened.dispose);
opened.execute('CREATE TABLE tbl (a TEXT);');
opened
..execute('CREATE TABLE tbl (a TEXT);')
..execute('INSERT INTO tbl DEFAULT VALUES;');

final stmt = opened.prepare('SELECT * FROM tbl');
stmt.select();
stmt.select();
expect(stmt.select(), hasLength(1));
expect(stmt.select(), hasLength(1));
});

test('prepared statements cannot be used after close', () {
Expand Down

0 comments on commit 2522ce6

Please sign in to comment.