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

Fix binding of explicitly numbered parameters #1032

Open
wants to merge 2 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
29 changes: 18 additions & 11 deletions src/better_sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ BindMap * Statement::GetBindMap (v8::Isolate * isolate)
int param_count = sqlite3_bind_parameter_count(handle);
for (int i = 1; i <= param_count; ++i) {
const char* name = sqlite3_bind_parameter_name(handle, i);
if (name != NULL) bind_map->Add(isolate, name + 1, i);
if (name != NULL && name[0] != '?') bind_map->Add(isolate, name + 1, i);
}
has_bind_map = true;
return bind_map;
Expand Down Expand Up @@ -1983,15 +1983,22 @@ void Binder::Fail (void (* Throw) (char const *), char const * message)
success = false;
}
#line 63 "./src/util/binder.lzz"
int Binder::NextAnonIndex ()
bool Binder::IsAnonIndex (int index)
#line 63 "./src/util/binder.lzz"
{
const char* name = sqlite3_bind_parameter_name(handle, index);
return name == NULL || name[0] == '?';
}
#line 68 "./src/util/binder.lzz"
int Binder::NextAnonIndex ()
#line 68 "./src/util/binder.lzz"
{
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
while (!IsAnonIndex(++anon_index)) {}
return anon_index;
}
#line 69 "./src/util/binder.lzz"
#line 74 "./src/util/binder.lzz"
void Binder::BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int index)
#line 69 "./src/util/binder.lzz"
#line 74 "./src/util/binder.lzz"
{
int status = Data::BindValueFromJS(isolate, handle, index, value);
if (status != SQLITE_OK) {
Expand All @@ -2010,9 +2017,9 @@ void Binder::BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int
assert(false);
}
}
#line 90 "./src/util/binder.lzz"
#line 95 "./src/util/binder.lzz"
int Binder::BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr)
#line 90 "./src/util/binder.lzz"
#line 95 "./src/util/binder.lzz"
{
v8 :: Local < v8 :: Context > ctx = isolate -> GetCurrentContext ( ) ;
uint32_t length = arr->Length();
Expand All @@ -2034,9 +2041,9 @@ int Binder::BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr)
}
return len;
}
#line 116 "./src/util/binder.lzz"
#line 121 "./src/util/binder.lzz"
int Binder::BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, Statement * stmt)
#line 116 "./src/util/binder.lzz"
#line 121 "./src/util/binder.lzz"
{
v8 :: Local < v8 :: Context > ctx = isolate -> GetCurrentContext ( ) ;
BindMap* bind_map = stmt->GetBindMap(isolate);
Expand Down Expand Up @@ -2073,9 +2080,9 @@ int Binder::BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, State

return len;
}
#line 160 "./src/util/binder.lzz"
#line 165 "./src/util/binder.lzz"
Binder::Result Binder::BindArgs (v8::FunctionCallbackInfo <v8 :: Value> const & info, int argc, Statement * stmt)
#line 160 "./src/util/binder.lzz"
#line 165 "./src/util/binder.lzz"
{
v8 :: Isolate * isolate = info . GetIsolate ( ) ;
int count = 0;
Expand Down
18 changes: 10 additions & 8 deletions src/better_sqlite3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,22 +757,24 @@ class Binder
#line 55 "./src/util/binder.lzz"
void Fail (void (* Throw) (char const *), char const * message);
#line 63 "./src/util/binder.lzz"
bool IsAnonIndex (int index);
#line 68 "./src/util/binder.lzz"
int NextAnonIndex ();
#line 69 "./src/util/binder.lzz"
#line 74 "./src/util/binder.lzz"
void BindValue (v8::Isolate * isolate, v8::Local <v8::Value> value, int index);
#line 90 "./src/util/binder.lzz"
#line 95 "./src/util/binder.lzz"
int BindArray (v8::Isolate * isolate, v8::Local <v8::Array> arr);
#line 116 "./src/util/binder.lzz"
#line 121 "./src/util/binder.lzz"
int BindObject (v8::Isolate * isolate, v8::Local <v8::Object> obj, Statement * stmt);
#line 160 "./src/util/binder.lzz"
#line 165 "./src/util/binder.lzz"
Result BindArgs (v8::FunctionCallbackInfo <v8 :: Value> const & info, int argc, Statement * stmt);
#line 200 "./src/util/binder.lzz"
#line 205 "./src/util/binder.lzz"
sqlite3_stmt * handle;
#line 201 "./src/util/binder.lzz"
#line 206 "./src/util/binder.lzz"
int param_count;
#line 202 "./src/util/binder.lzz"
#line 207 "./src/util/binder.lzz"
int anon_index;
#line 203 "./src/util/binder.lzz"
#line 208 "./src/util/binder.lzz"
bool success;
};
#line 34 "./src/better_sqlite3.lzz"
Expand Down
2 changes: 1 addition & 1 deletion src/objects/statement.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public:
int param_count = sqlite3_bind_parameter_count(handle);
for (int i = 1; i <= param_count; ++i) {
const char* name = sqlite3_bind_parameter_name(handle, i);
if (name != NULL) bind_map->Add(isolate, name + 1, i);
if (name != NULL && name[0] != '?') bind_map->Add(isolate, name + 1, i);
}
has_bind_map = true;
return bind_map;
Expand Down
7 changes: 6 additions & 1 deletion src/util/binder.lzz
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ private:
success = false;
}

bool IsAnonIndex(int index) {
const char* name = sqlite3_bind_parameter_name(handle, index);
return name == NULL || name[0] == '?';
}

int NextAnonIndex() {
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
while (!IsAnonIndex(++anon_index)) {}
return anon_index;
}

Expand Down
5 changes: 4 additions & 1 deletion test/20.statement.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ describe('Statement#run()', function () {
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run('foo', 25, 25, Buffer.alloc(8).fill(0xdd));
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25, 25, Buffer.alloc(8).fill(0xdd)]);
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?, ?)').run(['foo', 25], [25], Buffer.alloc(8).fill(0xdd));
this.db.prepare('INSERT INTO entries VALUES (?4, ?3, ?2, ?1)').run(Buffer.alloc(8).fill(0xdd), 25, 25, 'foo');
this.db.prepare('INSERT INTO entries VALUES (?, ?, ?2, ?)').run('foo', 25, Buffer.alloc(8).fill(0xdd));
this.db.prepare('INSERT INTO entries VALUES (?1, ?2, ?4, ?5)').run('foo', 25, null, 25, Buffer.alloc(8).fill(0xdd));
this.db.prepare('INSERT INTO entries VALUES (@a, @b, @c, @d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
this.db.prepare('INSERT INTO entries VALUES ($a, $b, $c, $d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
this.db.prepare('INSERT INTO entries VALUES (:a, :b, :c, :d)').run({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
Expand Down Expand Up @@ -165,6 +168,6 @@ describe('Statement#run()', function () {
while (row = this.db.prepare(`SELECT * FROM entries WHERE rowid=${++i}`).get()) {
expect(row).to.deep.equal({ a: 'foo', b: 25, c: 25, d: Buffer.alloc(8).fill(0xdd) });
}
expect(i).to.equal(11);
expect(i).to.equal(14);
});
});