Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,35 @@ void SQLTagStore::SizeGetter(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(static_cast<double>(store->sql_tags_.Size()));
}

bool SQLTagStore::ResetAndBindStatement(
Environment* env,
StatementSync* stmt,
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = env->isolate();
int r = stmt->ResetStatement();
CHECK_ERROR_OR_THROW(isolate, stmt->db_.get(), r, SQLITE_OK, false);

r = sqlite3_clear_bindings(stmt->statement_);
CHECK_ERROR_OR_THROW(isolate, stmt->db_.get(), r, SQLITE_OK, false);

uint32_t n_params = args.Length() - 1;
int param_count = sqlite3_bind_parameter_count(stmt->statement_);
if (param_count != static_cast<int>(n_params)) {
THROW_ERR_INVALID_ARG_VALUE(
env,
"SQLite parameters must be bound using template literal placeholders.");
return false;
}
Comment on lines +3423 to +3430

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.


for (int i = 0; i < param_count; ++i) {
if (!stmt->BindValue(args[i + 1], i + 1)) {
return false;
}
}

return true;
}

void SQLTagStore::Run(const FunctionCallbackInfo<Value>& args) {
SQLTagStore* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
Expand All @@ -3423,15 +3452,8 @@ void SQLTagStore::Run(const FunctionCallbackInfo<Value>& args) {
return;
}

uint32_t n_params = args.Length() - 1;
int r = stmt->ResetStatement();
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
int param_count = sqlite3_bind_parameter_count(stmt->statement_);
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
Local<Value> value = args[i + 1];
if (!stmt->BindValue(value, i + 1)) {
return;
}
if (!ResetAndBindStatement(env, stmt.get(), args)) {
return;
}

Local<Object> result;
Expand All @@ -3456,15 +3478,8 @@ void SQLTagStore::Iterate(const FunctionCallbackInfo<Value>& args) {
return;
}

uint32_t n_params = args.Length() - 1;
int r = stmt->ResetStatement();
CHECK_ERROR_OR_THROW(env->isolate(), stmt->db_.get(), r, SQLITE_OK, void());
int param_count = sqlite3_bind_parameter_count(stmt->statement_);
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
Local<Value> value = args[i + 1];
if (!stmt->BindValue(value, i + 1)) {
return;
}
if (!ResetAndBindStatement(env, stmt.get(), args)) {
return;
}

BaseObjectPtr<StatementSyncIterator> iter = StatementExecutionHelper::Iterate(
Expand All @@ -3491,18 +3506,8 @@ void SQLTagStore::Get(const FunctionCallbackInfo<Value>& args) {
return;
}

uint32_t n_params = args.Length() - 1;
Isolate* isolate = env->isolate();

int r = stmt->ResetStatement();
CHECK_ERROR_OR_THROW(isolate, stmt->db_.get(), r, SQLITE_OK, void());

int param_count = sqlite3_bind_parameter_count(stmt->statement_);
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
Local<Value> value = args[i + 1];
if (!stmt->BindValue(value, i + 1)) {
return;
}
if (!ResetAndBindStatement(env, stmt.get(), args)) {
return;
}

Local<Value> result;
Expand Down Expand Up @@ -3530,18 +3535,8 @@ void SQLTagStore::All(const FunctionCallbackInfo<Value>& args) {
return;
}

uint32_t n_params = args.Length() - 1;
Isolate* isolate = env->isolate();

int r = stmt->ResetStatement();
CHECK_ERROR_OR_THROW(isolate, stmt->db_.get(), r, SQLITE_OK, void());

int param_count = sqlite3_bind_parameter_count(stmt->statement_);
for (int i = 0; i < static_cast<int>(n_params) && i < param_count; ++i) {
Local<Value> value = args[i + 1];
if (!stmt->BindValue(value, i + 1)) {
return;
}
if (!ResetAndBindStatement(env, stmt.get(), args)) {
return;
}

auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); });
Expand Down
4 changes: 4 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ class SQLTagStore : public BaseObject {
private:
static BaseObjectPtr<StatementSync> PrepareStatement(
const v8::FunctionCallbackInfo<v8::Value>& args);
static bool ResetAndBindStatement(
Environment* env,
StatementSync* stmt,
const v8::FunctionCallbackInfo<v8::Value>& args);
BaseObjectWeakPtr<DatabaseSync> database_;
LRUCache<std::string, BaseObjectPtr<StatementSync>> sql_tags_;
friend class StatementExecutionHelper;
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-sqlite-template-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ test('queries with no results', () => {
assert.strictEqual(count, 0);
});

test('rejects parameters outside of template expressions', () => {
const ldb = new DatabaseSync(':memory:');
const lsql = ldb.createTagStore();
ldb.exec(`
CREATE TABLE secrets(owner TEXT, token TEXT);
INSERT INTO secrets VALUES ('victim', 'secret');
CREATE TABLE transfers(from_user TEXT, amount INTEGER);
`);

const expectedError = {
code: 'ERR_INVALID_ARG_VALUE',
message: /must be bound using template literal placeholders/,
};

for (const method of ['get', 'all', 'iterate']) {
// Prime the cached statement with a bound value before each attempt.
// eslint-disable-next-line no-unused-expressions
lsql.all`SELECT token FROM secrets WHERE owner = ${'victim'}`;
assert.throws(() => {
// eslint-disable-next-line no-unused-expressions
lsql[method]`SELECT token FROM secrets WHERE owner = ?`;
}, expectedError);
}

// eslint-disable-next-line no-unused-expressions
lsql.run`INSERT INTO transfers VALUES (${'victim'},${100})`;
assert.throws(() => {
// eslint-disable-next-line no-unused-expressions
lsql.run`INSERT INTO transfers VALUES (?,?)`;
}, expectedError);
assert.strictEqual(
ldb.prepare('SELECT COUNT(*) AS count FROM transfers').get().count,
1);

ldb.close();
});

test('TagStore capacity, size, and clear', () => {
assert.strictEqual(sql.capacity, 10);
assert.strictEqual(sql.size, 0);
Expand Down
Loading