forked from WiseLibs/better-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joshua Wise
committed
Mar 22, 2016
1 parent
e0c7d4c
commit 5681268
Showing
10 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Increments anon_index until it is out of range, or a nameless parameter | ||
// index is reached. It is incremented at least once. | ||
|
||
void Binder::AdvanceAnonIndex() { | ||
while (sqlite3_bind_parameter_name(++anon_index) != NULL) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
void Binder::BindBuffer(v8::Local<v8::Object> value, int index = 0) { | ||
if (!index) {index = anon_index;} | ||
AdvanceAnonIndex(); | ||
|
||
int status = sqlite3_bind_text(handle, index, node::Buffer::Data(value), node::Buffer::Length(value), SQLITE_TRANSIENT); | ||
|
||
SetBindingError(status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
void Binder::BindNull(int index = 0) { | ||
if (!index) {index = anon_index;} | ||
AdvanceAnonIndex(); | ||
int status = sqlite3_bind_null(handle, index); | ||
SetBindingError(status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
void Binder::BindNumber(v8::Local<v8::Number> value, int index = 0) { | ||
if (!index) {index = anon_index;} | ||
AdvanceAnonIndex(); | ||
int status = sqlite3_bind_double(handle, index, value->Value()); | ||
SetBindingError(status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
void Binder::BindString(v8::Local<v8::String> value, int index = 0) { | ||
if (!index) {index = anon_index;} | ||
AdvanceAnonIndex(); | ||
|
||
Nan::Utf8String utf8(value); | ||
int status = sqlite3_bind_text(handle, index, *utf8, utf8.length(), SQLITE_TRANSIENT); | ||
|
||
SetBindingError(status); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
void Binder::BindValue(v8::Local<v8::Value> value, int index = 0) { | ||
if (value->IsNumber()) { | ||
BindNumber(v8::Local<v8::Number>::Cast(value), index); | ||
} else if (value->IsString()) { | ||
BindString(v8::Local<v8::String>::Cast(value), index); | ||
} else if (value->IsNull() || value->IsUndefined()) { | ||
BindNull(index); | ||
} else if (node::Buffer::HasInstance(value)) { | ||
BindBuffer(v8::Local<v8::Object>::Cast(value), index); | ||
} else { | ||
error = "SQLite3 can only bind numbers, strings, Buffers, and null."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <sqlite3.h> | ||
#include <nan.h> | ||
#include "binder.h" | ||
|
||
#include "advance-anon-index.cc" | ||
#include "set-binding-error.cc" | ||
#include "bind-number.cc" | ||
#include "bind-string.cc" | ||
#include "bind-buffer.cc" | ||
#include "bind-null.cc" | ||
#include "bind-value.cc" | ||
|
||
Binder::Binder(sqlite3_stmt* handle) | ||
: handle(handle) | ||
, param_count(sqlite3_bind_parameter_count(handle)) | ||
, bound_args(0) | ||
, anon_index(1) | ||
, error(NULL) {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef NODE_SQLITE3_PLUS_WORKER_STATEMENT_WORKER_H | ||
#define NODE_SQLITE3_PLUS_WORKER_STATEMENT_WORKER_H | ||
|
||
#include <sqlite3.h> | ||
|
||
class Binder { | ||
public: | ||
Binder(sqlite3_stmt*); | ||
|
||
private: | ||
void AdvanceAnonIndex(); | ||
void SetBindingError(int); | ||
void BindNumber(v8::Local<v8::Number>, int); | ||
void BindString(v8::Local<v8::String>, int); | ||
void BindBuffer(v8::Local<v8::Object>, int); | ||
void BindNull(int); | ||
void BindValue(v8::Local<v8::Value>, int); | ||
|
||
sqlite3_stmt* const handle; | ||
int const param_count; | ||
|
||
int bound_args; | ||
int anon_index; | ||
const char* error; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
void Binder::SetBindingError(int status) { | ||
if (status != SQLITE_OK) { | ||
switch (status) { | ||
case SQLITE_RANGE: | ||
error = "Too many parameters were provided."; | ||
break; | ||
case SQLITE_TOOBIG: | ||
error = "The bound string or Buffer is too big."; | ||
break; | ||
case SQLITE_NOMEM: | ||
error = "Out of memory."; | ||
break; | ||
default: | ||
error = "An unexpected error occured while trying to bind parameters."; | ||
} | ||
} | ||
} |