sqlite/src/wrapper.c

157 lines
3.0 KiB
C
Raw Normal View History

2019-07-19 15:12:49 +03:00
#include <stdlib.h>
#include "../sdk/logger.h"
#include "sqliteInt.h"
sqlite3 *state;
2020-09-14 21:00:27 +03:00
char *RESULT_PTR;
int RESULT_SIZE;
2019-07-19 15:12:49 +03:00
int init() {
const int rc = sqlite3_initialize();
if(rc != 0) {
return rc;
}
return sqlite3_open(":memory:", &state);
}
int g_isInited = 0;
2020-04-17 20:44:55 +03:00
void* allocate(size_t size) {
2019-07-19 15:12:49 +03:00
return malloc(size + 1);
}
2020-04-17 20:44:55 +03:00
void deallocate(void *ptr, int size) {
2019-07-19 15:12:49 +03:00
free(ptr);
}
2020-09-14 21:00:27 +03:00
void set_result_ptr(char *ptr) {
RESULT_PTR = ptr;
}
2019-07-19 15:12:49 +03:00
2020-09-14 21:00:27 +03:00
void set_result_size(int size) {
RESULT_SIZE = size;
}
int get_result_size(void) {
return RESULT_SIZE;
}
2019-07-19 15:12:49 +03:00
2020-09-14 21:00:27 +03:00
char *get_result_ptr() {
return RESULT_PTR;
2019-07-19 15:12:49 +03:00
}
typedef struct ShellText ShellText;
struct ShellText {
2020-04-28 21:58:11 +03:00
char *z;
int n;
int nAlloc;
2019-07-19 15:12:49 +03:00
};
static void initText(ShellText *p){
memset(p, 0, sizeof(*p));
}
static void freeText(ShellText *p){
free(p->z);
initText(p);
}
static int strlen30(const char *z){
const char *z2 = z;
while( *z2 ){ z2++; }
return 0x3fffffff & (int)(z2 - z);
}
static void appendText(ShellText *p, char const *zAppend, char quote){
int len;
int i;
int nAppend = strlen30(zAppend);
len = nAppend+p->n+1;
if( quote ){
len += 2;
for(i=0; i<nAppend; i++){
if( zAppend[i]==quote ) len++;
}
}
if( p->n+len>=p->nAlloc ){
p->nAlloc = p->nAlloc*2 + len + 20;
p->z = realloc(p->z, p->nAlloc);
// TODO: more solid work with OOM
if( p->z==0 ) __builtin_unreachable();
}
if( quote ){
char *zCsr = p->z+p->n;
*zCsr++ = quote;
for(i=0; i<nAppend; i++){
*zCsr++ = zAppend[i];
if( zAppend[i]==quote ) *zCsr++ = quote;
}
*zCsr++ = quote;
p->n = (int)(zCsr - p->z);
*zCsr = '\0';
}else{
memcpy(p->z+p->n, zAppend, nAppend);
p->n += nAppend;
p->z[p->n] = '\0';
}
}
static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
ShellText *p = (ShellText*)pArg;
int i;
UNUSED_PARAMETER(az);
if( azArg==0 ) return 0;
if( p->n ) appendText(p, "|", 0);
for(i=0; i<nArg; i++){
if( i ) appendText(p, ",", 0);
if( azArg[i] ) appendText(p, azArg[i], 0);
}
return 0;
}
2020-09-14 21:00:27 +03:00
void invoke(char *request, int request_size) {
2019-07-19 15:12:49 +03:00
if(g_isInited == 0) {
// TODO: check the return code
init();
2020-04-17 23:28:01 +03:00
#if LOG_ENABLED
2020-04-28 21:58:11 +03:00
const char successInitMessage[] = "Sqlite has been initialized\n";
2020-04-17 23:19:01 +03:00
log_utf8_string(successInitMessage, sizeof(successInitMessage));
2020-04-17 23:28:01 +03:00
#endif
2019-07-19 15:12:49 +03:00
g_isInited = 1;
}
2020-04-17 20:44:55 +03:00
request[request_size] = 0;
2019-07-19 15:12:49 +03:00
2020-04-17 23:28:01 +03:00
#if LOG_ENABLED
2020-04-17 23:19:01 +03:00
log_utf8_string(request, request_size);
2020-04-17 23:28:01 +03:00
#endif
2019-08-16 12:58:24 +03:00
2019-07-19 15:12:49 +03:00
ShellText str;
initText(&str);
char *errorMessage = 0;
int rc = sqlite3_exec(state, request, captureOutputCallback, &str, &errorMessage);
char *response = 0;
if(rc || errorMessage) {
2020-09-14 21:00:27 +03:00
RESULT_PTR = errorMessage;
RESULT_SIZE = strlen(errorMessage);
2019-07-19 15:12:49 +03:00
}
else {
if(str.n != 0) {
2020-09-14 21:00:27 +03:00
RESULT_PTR = str.z;
RESULT_SIZE = str.n;
2019-07-19 15:12:49 +03:00
} else {
// if a request was successfull, sqlite doesn't return anything as the result string
2020-09-14 21:00:27 +03:00
RESULT_PTR = strdup("OK");
RESULT_SIZE = 2;
2019-07-19 15:12:49 +03:00
}
}
}