Commit 16189614 by Orlando Vazquez

get step working and clean up the tests slightly

parent f3663bdf
...@@ -16,6 +16,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -16,6 +16,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <v8.h> #include <v8.h>
#include <node.h> #include <node.h>
...@@ -316,8 +317,6 @@ protected: ...@@ -316,8 +317,6 @@ protected:
int rc = sqlite3_prepare_v2(db, prep_req->sql, -1, int rc = sqlite3_prepare_v2(db, prep_req->sql, -1,
&(prep_req->stmt), &(prep_req->tail)); &(prep_req->stmt), &(prep_req->tail));
printf("rc = %d, stmt = %p\n", rc, prep_req->stmt);
req->result = rc; req->result = rc;
return 0; return 0;
...@@ -427,7 +426,6 @@ protected: ...@@ -427,7 +426,6 @@ protected:
HandleScope scope; HandleScope scope;
struct bind_request *bind_req = (struct bind_request *)(req->data); struct bind_request *bind_req = (struct bind_request *)(req->data);
printf("EIO_AfterBind %d\n", (int) req->result);
Statement *sto = bind_req->sto; Statement *sto = bind_req->sto;
...@@ -468,7 +466,6 @@ protected: ...@@ -468,7 +466,6 @@ protected:
break; break;
case KEY_STRING: case KEY_STRING:
printf("stmt = %p\n", (sqlite3_stmt*)*sto);
index = sqlite3_bind_parameter_index( index = sqlite3_bind_parameter_index(
*sto, (char*)(bind_req->key)); *sto, (char*)(bind_req->key));
printf("key was %s (index %d)\n", (char*)(bind_req->key), index); printf("key was %s (index %d)\n", (char*)(bind_req->key), index);
...@@ -541,7 +538,7 @@ protected: ...@@ -541,7 +538,7 @@ protected:
strcpy(key, *keyValue); strcpy(key, *keyValue);
bind_req->key = key; bind_req->key = key;
printf("created key %s\n", key); printf("attempting to bind to key %s\n", key);
} }
else if (args[0]->IsInt32()) { else if (args[0]->IsInt32()) {
bind_req->key_type = KEY_INT; bind_req->key_type = KEY_INT;
...@@ -551,26 +548,23 @@ protected: ...@@ -551,26 +548,23 @@ protected:
// don't forget to `free` this // don't forget to `free` this
bind_req->key = index; bind_req->key = index;
printf("created value %d\n", (int) *index); printf("attempting to bind to index %d\n", (int) *index);
} }
// setup value // setup value
if (args[1]->IsInt32()) { if (args[1]->IsInt32()) {
printf("setting up int\n");
bind_req->value_type = VALUE_INT; bind_req->value_type = VALUE_INT;
int *value = (int *) malloc(sizeof(int)); int *value = (int *) malloc(sizeof(int));
*value = args[1]->Int32Value(); *value = args[1]->Int32Value();
bind_req->value = value; bind_req->value = value;
} }
else if (args[1]->IsNumber()) { else if (args[1]->IsNumber()) {
printf("setting up double\n");
bind_req->value_type = VALUE_DOUBLE; bind_req->value_type = VALUE_DOUBLE;
double *value = (double *) malloc(sizeof(double)); double *value = (double *) malloc(sizeof(double));
*value = args[1]->NumberValue(); *value = args[1]->NumberValue();
bind_req->value = value; bind_req->value = value;
} }
else if (args[1]->IsString()) { else if (args[1]->IsString()) {
printf("setting up string\n");
bind_req->value_type = VALUE_STRING; bind_req->value_type = VALUE_STRING;
String::Utf8Value text(args[1]); String::Utf8Value text(args[1]);
char *value = (char *) calloc(text.length()+1, sizeof(char*)); char *value = (char *) calloc(text.length()+1, sizeof(char*));
...@@ -579,7 +573,6 @@ protected: ...@@ -579,7 +573,6 @@ protected:
bind_req->value_size = text.length()+1; bind_req->value_size = text.length()+1;
} }
else if (args[1]->IsNull() || args[1]->IsUndefined()) { else if (args[1]->IsNull() || args[1]->IsUndefined()) {
printf("setting up null\n");
bind_req->value_type = VALUE_NULL; bind_req->value_type = VALUE_NULL;
bind_req->value = NULL; bind_req->value = NULL;
} }
...@@ -600,34 +593,7 @@ protected: ...@@ -600,34 +593,7 @@ protected:
return Undefined(); return Undefined();
} }
static Handle<Value> Bind2(const Arguments& args) { // XXX TODO
HandleScope scope;
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
REQ_ARGS(2);
if (!args[0]->IsString() && !args[0]->IsInt32())
return ThrowException(Exception::TypeError(
String::New("First argument must be a string or integer")));
int index = args[0]->IsString() ?
sqlite3_bind_parameter_index(*stmt, *String::Utf8Value(args[0])) :
args[0]->Int32Value();
if (args[1]->IsInt32()) {
sqlite3_bind_int(*stmt, index, args[1]->Int32Value());
} else if (args[1]->IsNumber()) {
sqlite3_bind_double(*stmt, index, args[1]->NumberValue());
} else if (args[1]->IsString()) {
String::Utf8Value text(args[1]);
sqlite3_bind_text(*stmt, index, *text, text.length(),SQLITE_TRANSIENT);
} else if (args[1]->IsNull() || args[1]->IsUndefined()) {
sqlite3_bind_null(*stmt, index);
} else {
return ThrowException(Exception::TypeError(
String::New("Unable to bind value of this type")));
}
return args.This();
}
static Handle<Value> BindParameterCount(const Arguments& args) { static Handle<Value> BindParameterCount(const Arguments& args) {
HandleScope scope; HandleScope scope;
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This()); Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
...@@ -670,30 +636,48 @@ protected: ...@@ -670,30 +636,48 @@ protected:
}; };
static int EIO_AfterStep(eio_req *req) { static int EIO_AfterStep(eio_req *req) {
ev_unref(EV_DEFAULT_UC);
HandleScope scope; HandleScope scope;
struct step_request *step_req = (struct step_request *)(req->data); struct step_request *step_req = (struct step_request *)(req->data);
ev_unref(EV_DEFAULT_UC);
void **data = step_req->column_data; void **data = step_req->column_data;
printf("column_data addr was %p\n", ((void**)data)+1);
printf("there were %d columns\n", step_req->column_count); printf("there were %d columns\n", step_req->column_count);
Local<Value> argv[2];
argv[0] = Local<Value>::New(Undefined());
Local<Array> row = Array::New(step_req->column_count);
for (int i = 0; i < step_req->column_count; i++) { for (int i = 0; i < step_req->column_count; i++) {
printf("value addr was %p\n", (step_req->column_data[i]));
switch(step_req->column_types[i]) { switch(step_req->column_types[i]) {
case SQLITE_INTEGER: case SQLITE_INTEGER:
printf("type was an integer\n"); printf("integer value was %d\n", *(int*)(step_req->column_data[i]));
printf("value was %d\n", *(int*)(step_req->column_data[i])); row->Set(Number::New(i), Int32::New(*(int*) (step_req->column_data[i])));
free((int*)(step_req->column_data[i]));
break; break;
case SQLITE_TEXT: case SQLITE_TEXT:
printf("type was as string\n"); printf("string value was %s\n", (char *) (step_req->column_data[i]));
printf("value was %s\n", (char*)(step_req->column_data[i])); row->Set(Number::New(i),
String::New((char *) (step_req->column_data[i])));
// don't free this pointer, it's owned by sqlite3
break; break;
default: default:
printf("dunno\n"); printf("Unknown type error!\n");
} }
} }
argv[1] = row;
TryCatch try_catch;
step_req->cb->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
step_req->cb.Dispose(); step_req->cb.Dispose();
free(step_req->column_data);
free(step_req->column_types);
free(step_req); free(step_req);
step_req->sto->Unref(); step_req->sto->Unref();
...@@ -741,7 +725,7 @@ protected: ...@@ -741,7 +725,7 @@ protected:
printf("result of step %d\n", rc); printf("result of step %d\n", rc);
if (rc == SQLITE_ROW) { if (rc == SQLITE_ROW) {
// would be nice to not have to fetch and return the columns always // would be nice to cache the column type data somewhere
step_req->column_count = sqlite3_column_count(stmt); step_req->column_count = sqlite3_column_count(stmt);
step_req->column_types = step_req->column_types =
(int *) calloc(step_req->column_count, sizeof(int)); (int *) calloc(step_req->column_count, sizeof(int));
...@@ -753,7 +737,7 @@ protected: ...@@ -753,7 +737,7 @@ protected:
switch(type) { switch(type) {
case SQLITE_INTEGER: { case SQLITE_INTEGER: {
int *value = (int*) malloc(sizeof(int)); int *value = (int *) malloc(sizeof(int));
*value = sqlite3_column_int(stmt, i); *value = sqlite3_column_int(stmt, i);
step_req->column_data[i] = value; step_req->column_data[i] = value;
printf("serialized int %d\n", *(int*)(step_req->column_data[i])); printf("serialized int %d\n", *(int*)(step_req->column_data[i]));
...@@ -761,7 +745,7 @@ protected: ...@@ -761,7 +745,7 @@ protected:
break; break;
case SQLITE_FLOAT: { case SQLITE_FLOAT: {
double *value = (double*) malloc(sizeof(double)); double *value = (double *) malloc(sizeof(double));
*value = sqlite3_column_double(stmt, i); *value = sqlite3_column_double(stmt, i);
step_req->column_data[i] = value; step_req->column_data[i] = value;
printf("serialized float\n"); printf("serialized float\n");
...@@ -774,7 +758,7 @@ protected: ...@@ -774,7 +758,7 @@ protected:
// it will be reclaimed on the next step, reset, or finalize. // it will be reclaimed on the next step, reset, or finalize.
// I'm going to assume it's okay to keep this pointer around // I'm going to assume it's okay to keep this pointer around
// until it is used in `EIO_AfterStep` // until it is used in `EIO_AfterStep`
char *value = (char *)sqlite3_column_text(stmt, i); char *value = (char *) sqlite3_column_text(stmt, i);
step_req->column_data[i] = value; step_req->column_data[i] = value;
printf("serialized text %s\n", step_req->column_data[i]); printf("serialized text %s\n", step_req->column_data[i]);
} }
......
...@@ -18,7 +18,7 @@ throws(function () { ...@@ -18,7 +18,7 @@ throws(function () {
db.open("foo.db"); db.open("foo.db");
}); });
function testBinds() { function test_prepare() {
db.open("mydatabase.db", function (err) { db.open("mydatabase.db", function (err) {
puts(inspect(arguments)); puts(inspect(arguments));
if (err) { if (err) {
...@@ -32,11 +32,15 @@ function testBinds() { ...@@ -32,11 +32,15 @@ function testBinds() {
db.prepare("SELECT bar FROM foo; SELECT baz FROM foo;", function (error, statement) { db.prepare("SELECT bar FROM foo; SELECT baz FROM foo;", function (error, statement) {
puts("prepare callback"); puts("prepare callback");
db.prepare("SELECT bar FROM foo WHERE bar = $x and (baz = $y or baz = $z)", function (error, statement) { db.prepare("SELECT bar FROM foo WHERE bar = $x and (baz = $y or baz = $z)", function (error, statement) {
test_bind();
}); });
db.prepare("SELECT bar FROM foo WHERE bar = $x and (baz = $y or baz = $z)", function (error, statement) { });
puts("prepare callback"); });
}
function test_bind() {
db.prepare("SELECT bar FROM foo WHERE bar = $x and (baz = $y or baz = $z)", function (error, statement) {
statement.bind(0, 666, function () { statement.bind(0, 666, function () {
puts("bind callback"); puts("bind callback");
...@@ -60,19 +64,30 @@ function testBinds() { ...@@ -60,19 +64,30 @@ function testBinds() {
}); });
}); });
}); });
}); puts("prepare callback");
});
}); });
} }
db.open("mydatabase.db", function () { function test_simple() {
db.open("mydatabase.db", function () {
db.prepare("SELECT bar, baz FROM foo WHERE baz > 5", function (error, statement) { db.prepare("SELECT bar, baz FROM foo WHERE baz > 5", function (error, statement) {
puts('prepare callback');
puts(inspect(arguments)); puts(inspect(arguments));
// assertCallsFunction(statement.step, function (err, statement) {
// });
statement.step(function () { statement.step(function () {
puts('query callback'); puts('query callback');
puts(inspect(arguments));
statement.step(function () {
puts('query callback');
puts(inspect(arguments));
test_prepare();
}); });
}); });
}); });
});
}
puts("done"); test_simple();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment