Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for numbered placeholders #725

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/better_sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,8 @@ void Binder::Fail (void (* Throw) (char const *), char const * message)
int Binder::NextAnonIndex ()
#line 52 "./src/util/binder.lzz"
{
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
const char* name;
while ((name = sqlite3_bind_parameter_name(handle, ++anon_index)) != NULL && name[0] != '?') {}
return anon_index;
}
#line 58 "./src/util/binder.lzz"
Expand Down
3 changes: 2 additions & 1 deletion src/util/binder.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ private:
}

int NextAnonIndex() {
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
const char* name;
while ((name = sqlite3_bind_parameter_name(handle, ++anon_index)) != NULL && name[0] != '?') {}
return anon_index;
}

Expand Down
16 changes: 16 additions & 0 deletions test/24.statement.bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ describe('Statement#bind()', function () {
expect(result).to.be.a('string');
expect(result.length).to.equal(0);
});
it('should accept numbered placeholders', function () {
const result = this.db.prepare('SELECT ?, ?, ?1, ?').bind(1, 2, 3).raw().get();
expect(result).to.deep.equal([1, 2, 1, 3]);

const result2 = this.db.prepare('SELECT ?3, ?2, ?1').bind('a', 'b', 'c').raw().get();
expect(result2).to.deep.equal(['c', 'b', 'a']);

const result3 = this.db.prepare('SELECT ?2, ?1, ?, ?5').bind(1, 2, 3, 4, 5).raw().get();
expect(result3).to.deep.equal([2, 1, 3, 5]);

const result4 = this.db.prepare('SELECT ?2, ?, ?5').bind([1, 2, 3, 4, 5]).raw().get();
expect(result4).to.deep.equal([2, 3, 5]);

const result5 = this.db.prepare('SELECT ?2, ?, ?5').bind([1, 2, 3], [4, 5]).raw().get();
expect(result5).to.deep.equal([2, 3, 5]);
});
});