Skip to content

Commit

Permalink
added binder, partially implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Wise committed Mar 22, 2016
1 parent e0c7d4c commit 5681268
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"src/workers/get.cc",
"src/workers/all.cc",
"src/workers/each.cc",
"src/binder/binder.cc",
"src/sqlite3_plus.cc"
]
}
Expand Down
6 changes: 6 additions & 0 deletions src/binder/advance-anon-index.cc
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) {}
}
8 changes: 8 additions & 0 deletions src/binder/bind-buffer.cc
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);
}
6 changes: 6 additions & 0 deletions src/binder/bind-null.cc
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);
}
6 changes: 6 additions & 0 deletions src/binder/bind-number.cc
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);
}
9 changes: 9 additions & 0 deletions src/binder/bind-string.cc
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);
}
13 changes: 13 additions & 0 deletions src/binder/bind-value.cc
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.";
}
}
19 changes: 19 additions & 0 deletions src/binder/binder.cc
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) {}

27 changes: 27 additions & 0 deletions src/binder/binder.h
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
17 changes: 17 additions & 0 deletions src/binder/set-binding-error.cc
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.";
}
}
}

0 comments on commit 5681268

Please sign in to comment.