diff --git a/src/better_sqlite3.cpp b/src/better_sqlite3.cpp index 93fc14b7..57ceb4f1 100644 --- a/src/better_sqlite3.cpp +++ b/src/better_sqlite3.cpp @@ -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" diff --git a/src/util/binder.lzz b/src/util/binder.lzz index c7deb1c9..f3b16f95 100644 --- a/src/util/binder.lzz +++ b/src/util/binder.lzz @@ -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; } diff --git a/test/24.statement.bind.js b/test/24.statement.bind.js index 404b27d0..6f9596bc 100644 --- a/test/24.statement.bind.js +++ b/test/24.statement.bind.js @@ -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]); + }); });