Skip to content

Commit

Permalink
Allocate once and reuse stored column count, names and type data
Browse files Browse the repository at this point in the history
  • Loading branch information
orlandov committed Mar 22, 2010
1 parent f1fbcd9 commit edf899c
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions sqlite3_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,6 @@ class Sqlite3Db : public EventEmitter {
if (req->result == SQLITE_ROW && step_req->column_count) {
free((void**)step_req->column_data);
step_req->column_data = NULL;
free((int*)step_req->column_types);
step_req->column_types = NULL;
free((char**)step_req->column_names);
step_req->column_names = NULL;
}

step_req->sto->Unref();
Expand All @@ -875,23 +871,35 @@ class Sqlite3Db : public EventEmitter {
assert(step_req);

if (rc == SQLITE_ROW) {
// would be nice to cache the column names and type data somewhere
if (step_req->column_count = sqlite3_column_count(stmt)) {
// If these pointers are NULL, look up and store the number of columns
// their names and types.
// Otherwise that means we have already looked up the column types and
// names so we can simply re-use that info.
if ( !step_req->column_types
&& !step_req->column_names) {
step_req->column_count = sqlite3_column_count(stmt);
step_req->column_types =
(int *) calloc(step_req->column_count, sizeof(int));
step_req->column_data =
(void **) calloc(step_req->column_count, sizeof(void *));
step_req->column_names =
(char **) calloc(step_req->column_count, sizeof(char *));

for (int i = 0; i < step_req->column_count; i++) {
step_req->column_types[i] = sqlite3_column_type(stmt, i);
step_req->column_names[i] = (char *) sqlite3_column_name(stmt, i);
}
}

if (step_req->column_count) {
step_req->column_data =
(void **) calloc(step_req->column_count, sizeof(void *));
}

assert(step_req->column_types
&& step_req->column_data
&& step_req->column_names);

for (int i = 0; i < step_req->column_count; i++) {
int type = step_req->column_types[i] = sqlite3_column_type(stmt, i);
step_req->column_names[i] = (char *) sqlite3_column_name(stmt, i);
int type = step_req->column_types[i];

switch(type) {
case SQLITE_INTEGER: {
Expand Down Expand Up @@ -951,6 +959,9 @@ class Sqlite3Db : public EventEmitter {
if (!step_req) {
sto->step_req = step_req = (struct step_request *)
calloc(1, sizeof(struct step_request));
step_req->column_types = NULL;
step_req->column_names = NULL;
step_req->column_data = NULL;
}

if (!step_req) {
Expand Down

0 comments on commit edf899c

Please sign in to comment.