Working version of sqlite

This commit is contained in:
Tee Teoh
2008-06-04 23:14:51 -04:00
parent 91ff5f6a88
commit 7a4ae6f539
3 changed files with 209 additions and 33 deletions

View File

@@ -1,7 +1,8 @@
OTP_DIR=/opt/local/lib/erlang/lib/erl_interface-3.5.5.3
LIB=-lsqlite3 -lerl_interface -lei
FLAGS=-I$(OTP_DIR)/include -L$(OTP_DIR)/lib
GCC=/opt/local/bin/gcc-apple-4.2
#GCC=/opt/local/bin/gcc-apple-4.2
GCC=/usr/bin/gcc
all: erl_comm.c sqlite_port.c
$(GCC) -Wall -o sqlite_port erl_comm.c sqlite_port.c $(FLAGS) $(LIB)

View File

@@ -7,18 +7,61 @@
#include "erl_interface.h"
#include "ei.h"
static FILE *log;
typedef struct {
ETERM *result;
} Result;
static Result r;
void send_error(char *err_msg);
void send_result();
void send_ok();
static int callback(void *notUsed, int argc, char **argv, char **azColName) {
ETERM **record_list;
int i;
if (r.result == 0) {
r.result = erl_mk_empty_list();
}
record_list = malloc(argc * sizeof(ETERM *));
fprintf(log, "runs %d\n", argc);
for (i = 0; i < argc; i++) {
fprintf(log, "%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
if (argv[i]) {
record_list[i] = erl_mk_string(argv[i]);
}
else {
record_list[i] = erl_mk_empty_list();
}
}
fprintf(log, "\n");
fflush(log);
r.result = erl_cons(erl_mk_tuple(record_list, argc), r.result);
free(record_list);
return 0;
}
int main(int argc, char **argv)
{
ETERM *tuplep, *intp;
ETERM *tuplep;
ETERM *fnp, *argp;
int res;
byte buf[100];
long allocated, freed;
byte buf[1024];
sqlite3 *db;
char *zErrMsg = 0;
int rc;
log = fopen("/tmp/sqlite_port.log", "a+");
fprintf(log, "******start log (%s)******\n", argv[1]);
fflush(log);
rc = sqlite3_open(argv[1], &db);
if (rc) {
sqlite3_close(db);
@@ -33,42 +76,89 @@ int main(int argc, char **argv)
argp = erl_element(2, tuplep);
if (strncmp((const char *)ERL_ATOM_PTR(fnp), "close", 5) == 0) {
fprintf(log, "closing sqlite3_close\n");
fflush(log);
sqlite3_close(db);
break;
} else if (strncmp((const char *)ERL_ATOM_PTR(fnp), "bar", 3) == 0) {
res = ERL_INT_VALUE(argp);
}
} else if (strncmp((const char *)ERL_ATOM_PTR(fnp), "sql_exec", 8) == 0) {
fprintf(log, "calling sqlite3_exec %s\n", erl_iolist_to_string(argp));
intp = erl_mk_int(res);
erl_encode(intp, buf);
write_cmd(buf, erl_term_len(intp));
r.result = 0;
rc = sqlite3_exec(db, erl_iolist_to_string(argp), callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
send_error(zErrMsg);
sqlite3_free(zErrMsg);
}
else if (r.result != 0) {
send_result();
}
else {
// not an error and no results. still need to return something
send_ok();
}
fflush(log);
}
erl_free_compound(tuplep);
erl_free_term(fnp);
erl_free_term(argp);
erl_free_term(intp);
}
fprintf(log, "******end log******\n");
fclose(log);
return 0;
}
void send_error(char *err_msg) {
ETERM **tup_list;
ETERM *result;
byte buf[1024];
tup_list = malloc(sizeof(ETERM *) * 2);
tup_list[0] = erl_mk_atom("sql_error");
tup_list[1] = erl_mk_string(err_msg);
result = erl_mk_tuple(tup_list, 2);
static int callback(void *notUsed, int argc, char **argv, char **azColName) {
int i;
printf("runs %d\n", argc);
for (i = 1; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[1] ? argv[1] : "NULL");
}
printf("\n");
return 0;
bzero(buf, 1024);
erl_encode(result, buf);
write_cmd(buf, erl_term_len(result));
fprintf(log, "SQL Error: %s\n", err_msg);
erl_free_term(tup_list[0]);
erl_free_term(tup_list[1]);
free(tup_list);
erl_free_compound(result);
}
void send_result() {
byte buf[2048];
bzero(buf, 2048);
erl_encode(r.result, buf);
write_cmd(buf, erl_term_len(r.result));
fprintf(log, "returning at len %d\n", erl_term_len(r.result));
erl_free_compound(r.result);
r.result = 0;
}
void send_ok() {
ETERM *result;
byte buf[48];
result = erl_mk_atom("ok");
bzero(buf, 48);
erl_encode(result, buf);
write_cmd(buf, erl_term_len(result));
fprintf(log, "returning ok at len %d\n", erl_term_len(result));
erl_free_term(result);
}
/*
rc = sqlite3_exec(db, "SELECT * from t1;", callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
*/