Moved LoadPlugin_sqlite3_plugin into SQLite3DB

In this way we can initialize it from other applications, for example TAP tests
pull/2821/head
René Cannaò 6 years ago
parent 659471128a
commit 35c851b9fa

@ -178,6 +178,7 @@ class SQLite3DB {
bool build_table(char *table_name, char *table_def, bool dropit);
bool check_and_build_table(char *table_name, char *table_def);
int prepare_v2(const char *, sqlite3_stmt **);
static void LoadPlugin(const char *);
};
#endif /* __CLASS_SQLITE3DB_H */

@ -1,6 +1,10 @@
#include "proxysql.h"
#include "cpp.h"
#include "SpookyV2.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define USLEEP_SQLITE_LOCKED 100
@ -654,3 +658,148 @@ SQLite3_result::~SQLite3_result() {
SQLite3_result::SQLite3_result() {
columns=0;
}
void SQLite3DB::LoadPlugin(const char *plugin_name) {
proxy_sqlite3_config = NULL;
proxy_sqlite3_bind_double = NULL;
proxy_sqlite3_bind_int = NULL;
proxy_sqlite3_bind_int64 = NULL;
proxy_sqlite3_bind_null = NULL;
proxy_sqlite3_bind_text = NULL;
proxy_sqlite3_column_name = NULL;
proxy_sqlite3_column_text = NULL;
proxy_sqlite3_column_bytes = NULL;
proxy_sqlite3_column_type = NULL;
proxy_sqlite3_column_count = NULL;
proxy_sqlite3_column_int = NULL;
proxy_sqlite3_errmsg = NULL;
proxy_sqlite3_finalize = NULL;
proxy_sqlite3_reset = NULL;
proxy_sqlite3_clear_bindings = NULL;
proxy_sqlite3_close_v2 = NULL;
proxy_sqlite3_get_autocommit = NULL;
proxy_sqlite3_free = NULL;
proxy_sqlite3_status = NULL;
proxy_sqlite3_changes = NULL;
proxy_sqlite3_step = NULL;
proxy_sqlite3_shutdown = NULL;
proxy_sqlite3_prepare_v2 = NULL;
proxy_sqlite3_open_v2 = NULL;
proxy_sqlite3_exec = NULL;
if (plugin_name) {
int fd = -1;
fd = ::open(plugin_name, O_RDONLY);
char binary_sha1_sqlite3[SHA_DIGEST_LENGTH*2+1];
if(fd >= 0) {
struct stat statbuf;
if(fstat(fd, &statbuf) == 0) {
unsigned char *fb = (unsigned char *)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (fb != MAP_FAILED) {
unsigned char temp[SHA_DIGEST_LENGTH];
SHA1(fb, statbuf.st_size, temp);
memset(binary_sha1_sqlite3, 0, SHA_DIGEST_LENGTH*2+1);
char buf[SHA_DIGEST_LENGTH*2];
for (int i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
}
memcpy(binary_sha1_sqlite3, buf, SHA_DIGEST_LENGTH*2);
munmap(fb,statbuf.st_size);
} else {
proxy_error("Unable to mmap %s: %s\n", plugin_name, strerror(errno));
}
} else {
proxy_error("Unable to fstat %s: %s\n", plugin_name, strerror(errno));
}
} else {
proxy_error("Unable to open %s: %s\n", plugin_name, strerror(errno));
}
close(fd);
proxy_info("SQLite3 Plugin SHA1 checksum: %s\n", binary_sha1_sqlite3);
dlerror();
char * dlsym_error = NULL;
dlerror();
dlsym_error=NULL;
void * handle_sqlite3_plugin = dlopen(plugin_name, RTLD_NOW);
if (!handle_sqlite3_plugin) {
cerr << "Cannot load SQLite3 plugin from library: " << dlerror() << '\n';
exit(EXIT_FAILURE);
} else {
dlerror();
int (*proxy_load_sqlite3)();
proxy_load_sqlite3 = (int (*)()) dlsym(handle_sqlite3_plugin, "proxy_load_sqlite3"); dlsym_error = dlerror();
if (dlsym_error!=NULL) { cerr << "Cannot load symbol proxy_load_sqlite3: " << dlsym_error << '\n'; exit(EXIT_FAILURE); }
(*proxy_load_sqlite3)();
}
if (handle_sqlite3_plugin==NULL || dlsym_error) {
proxy_error("Unable to load SQLite3 plugin from %s\n", plugin_name);
exit(EXIT_FAILURE);
}
proxy_info("Loaded SQLite3 from plugin\n");
} else {
proxy_sqlite3_config = sqlite3_config;
proxy_sqlite3_bind_double = sqlite3_bind_double;
proxy_sqlite3_bind_int = sqlite3_bind_int;
proxy_sqlite3_bind_int64 = sqlite3_bind_int64;
proxy_sqlite3_bind_null = sqlite3_bind_null;
proxy_sqlite3_bind_text = sqlite3_bind_text;
proxy_sqlite3_column_name = sqlite3_column_name;
proxy_sqlite3_column_text = sqlite3_column_text;
proxy_sqlite3_column_bytes = sqlite3_column_bytes;
proxy_sqlite3_column_type = sqlite3_column_type;
proxy_sqlite3_column_count = sqlite3_column_count;
proxy_sqlite3_column_int = sqlite3_column_int;
proxy_sqlite3_errmsg = sqlite3_errmsg;
proxy_sqlite3_finalize = sqlite3_finalize;
proxy_sqlite3_reset = sqlite3_reset;
proxy_sqlite3_clear_bindings = sqlite3_clear_bindings;
proxy_sqlite3_close_v2 = sqlite3_close_v2;
proxy_sqlite3_get_autocommit = sqlite3_get_autocommit;
proxy_sqlite3_free = sqlite3_free;
proxy_sqlite3_status = sqlite3_status;
proxy_sqlite3_changes = sqlite3_changes;
proxy_sqlite3_step = sqlite3_step;
proxy_sqlite3_shutdown = sqlite3_shutdown;
proxy_sqlite3_prepare_v2 = sqlite3_prepare_v2;
proxy_sqlite3_open_v2 = sqlite3_open_v2;
proxy_sqlite3_exec = sqlite3_exec;
proxy_info("Loaded built-in SQLite3\n");
}
assert(proxy_sqlite3_config);
assert(proxy_sqlite3_bind_double);
assert(proxy_sqlite3_bind_int);
assert(proxy_sqlite3_bind_int64);
assert(proxy_sqlite3_bind_null);
assert(proxy_sqlite3_bind_text);
assert(proxy_sqlite3_column_name);
assert(proxy_sqlite3_column_text);
assert(proxy_sqlite3_column_bytes);
assert(proxy_sqlite3_column_type);
assert(proxy_sqlite3_column_count);
assert(proxy_sqlite3_column_int);
assert(proxy_sqlite3_errmsg);
assert(proxy_sqlite3_finalize);
assert(proxy_sqlite3_reset);
assert(proxy_sqlite3_clear_bindings);
assert(proxy_sqlite3_close_v2);
assert(proxy_sqlite3_get_autocommit);
assert(proxy_sqlite3_free);
assert(proxy_sqlite3_status);
assert(proxy_sqlite3_changes);
assert(proxy_sqlite3_step);
assert(proxy_sqlite3_shutdown);
assert(proxy_sqlite3_prepare_v2);
assert(proxy_sqlite3_open_v2);
assert(proxy_sqlite3_exec);
{
/* moved here, so if needed by multiple modules it applies to all of them */
int i=(*proxy_sqlite3_config)(SQLITE_CONFIG_URI, 1);
if (i!=SQLITE_OK) {
fprintf(stderr,"SQLITE: Error on (*proxy_sqlite3_config)(SQLITE_CONFIG_URI,1)\n");
assert(i==SQLITE_OK);
exit(EXIT_FAILURE);
}
}
}

@ -1218,152 +1218,14 @@ void ProxySQL_Main_init() {
// __thr_sfp=l_mem_init();
}
static void LoadPlugin_sqlite3_plugin() {
proxy_sqlite3_config = NULL;
proxy_sqlite3_bind_double = NULL;
proxy_sqlite3_bind_int = NULL;
proxy_sqlite3_bind_int64 = NULL;
proxy_sqlite3_bind_null = NULL;
proxy_sqlite3_bind_text = NULL;
proxy_sqlite3_column_name = NULL;
proxy_sqlite3_column_text = NULL;
proxy_sqlite3_column_bytes = NULL;
proxy_sqlite3_column_type = NULL;
proxy_sqlite3_column_count = NULL;
proxy_sqlite3_column_int = NULL;
proxy_sqlite3_errmsg = NULL;
proxy_sqlite3_finalize = NULL;
proxy_sqlite3_reset = NULL;
proxy_sqlite3_clear_bindings = NULL;
proxy_sqlite3_close_v2 = NULL;
proxy_sqlite3_get_autocommit = NULL;
proxy_sqlite3_free = NULL;
proxy_sqlite3_status = NULL;
proxy_sqlite3_changes = NULL;
proxy_sqlite3_step = NULL;
proxy_sqlite3_shutdown = NULL;
proxy_sqlite3_prepare_v2 = NULL;
proxy_sqlite3_open_v2 = NULL;
proxy_sqlite3_exec = NULL;
if (GloVars.sqlite3_plugin) {
int fd = -1;
fd = open(GloVars.sqlite3_plugin, O_RDONLY);
char binary_sha1_sqlite3[SHA_DIGEST_LENGTH*2+1];
if(fd >= 0) {
struct stat statbuf;
if(fstat(fd, &statbuf) == 0) {
unsigned char *fb = (unsigned char *)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (fb != MAP_FAILED) {
unsigned char temp[SHA_DIGEST_LENGTH];
SHA1(fb, statbuf.st_size, temp);
memset(binary_sha1_sqlite3, 0, SHA_DIGEST_LENGTH*2+1);
char buf[SHA_DIGEST_LENGTH*2];
for (int i=0; i < SHA_DIGEST_LENGTH; i++) {
sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
}
memcpy(binary_sha1_sqlite3, buf, SHA_DIGEST_LENGTH*2);
munmap(fb,statbuf.st_size);
} else {
proxy_error("Unable to mmap %s: %s\n", GloVars.sqlite3_plugin, strerror(errno));
}
} else {
proxy_error("Unable to fstat %s: %s\n", GloVars.sqlite3_plugin, strerror(errno));
}
} else {
proxy_error("Unable to open %s: %s\n", GloVars.sqlite3_plugin, strerror(errno));
}
close(fd);
proxy_info("SQLite3 Plugin SHA1 checksum: %s\n", binary_sha1_sqlite3);
dlerror();
char * dlsym_error = NULL;
dlerror();
dlsym_error=NULL;
void * handle_sqlite3_plugin = dlopen(GloVars.sqlite3_plugin, RTLD_NOW);
if (!handle_sqlite3_plugin) {
cerr << "Cannot load SQLite3 plugin from library: " << dlerror() << '\n';
exit(EXIT_FAILURE);
} else {
dlerror();
int (*proxy_load_sqlite3)();
proxy_load_sqlite3 = (int (*)()) dlsym(handle_sqlite3_plugin, "proxy_load_sqlite3"); dlsym_error = dlerror();
if (dlsym_error!=NULL) { cerr << "Cannot load symbol proxy_load_sqlite3: " << dlsym_error << '\n'; exit(EXIT_FAILURE); }
(*proxy_load_sqlite3)();
}
if (handle_sqlite3_plugin==NULL || dlsym_error) {
proxy_error("Unable to load SQLite3 plugin from %s\n", GloVars.sqlite3_plugin);
exit(EXIT_FAILURE);
}
proxy_info("Loaded SQLite3 from plugin\n");
} else {
proxy_sqlite3_config = sqlite3_config;
proxy_sqlite3_bind_double = sqlite3_bind_double;
proxy_sqlite3_bind_int = sqlite3_bind_int;
proxy_sqlite3_bind_int64 = sqlite3_bind_int64;
proxy_sqlite3_bind_null = sqlite3_bind_null;
proxy_sqlite3_bind_text = sqlite3_bind_text;
proxy_sqlite3_column_name = sqlite3_column_name;
proxy_sqlite3_column_text = sqlite3_column_text;
proxy_sqlite3_column_bytes = sqlite3_column_bytes;
proxy_sqlite3_column_type = sqlite3_column_type;
proxy_sqlite3_column_count = sqlite3_column_count;
proxy_sqlite3_column_int = sqlite3_column_int;
proxy_sqlite3_errmsg = sqlite3_errmsg;
proxy_sqlite3_finalize = sqlite3_finalize;
proxy_sqlite3_reset = sqlite3_reset;
proxy_sqlite3_clear_bindings = sqlite3_clear_bindings;
proxy_sqlite3_close_v2 = sqlite3_close_v2;
proxy_sqlite3_get_autocommit = sqlite3_get_autocommit;
proxy_sqlite3_free = sqlite3_free;
proxy_sqlite3_status = sqlite3_status;
proxy_sqlite3_changes = sqlite3_changes;
proxy_sqlite3_step = sqlite3_step;
proxy_sqlite3_shutdown = sqlite3_shutdown;
proxy_sqlite3_prepare_v2 = sqlite3_prepare_v2;
proxy_sqlite3_open_v2 = sqlite3_open_v2;
proxy_sqlite3_exec = sqlite3_exec;
proxy_info("Loaded built-in SQLite3\n");
}
assert(proxy_sqlite3_config);
assert(proxy_sqlite3_bind_double);
assert(proxy_sqlite3_bind_int);
assert(proxy_sqlite3_bind_int64);
assert(proxy_sqlite3_bind_null);
assert(proxy_sqlite3_bind_text);
assert(proxy_sqlite3_column_name);
assert(proxy_sqlite3_column_text);
assert(proxy_sqlite3_column_bytes);
assert(proxy_sqlite3_column_type);
assert(proxy_sqlite3_column_count);
assert(proxy_sqlite3_column_int);
assert(proxy_sqlite3_errmsg);
assert(proxy_sqlite3_finalize);
assert(proxy_sqlite3_reset);
assert(proxy_sqlite3_clear_bindings);
assert(proxy_sqlite3_close_v2);
assert(proxy_sqlite3_get_autocommit);
assert(proxy_sqlite3_free);
assert(proxy_sqlite3_status);
assert(proxy_sqlite3_changes);
assert(proxy_sqlite3_step);
assert(proxy_sqlite3_shutdown);
assert(proxy_sqlite3_prepare_v2);
assert(proxy_sqlite3_open_v2);
assert(proxy_sqlite3_exec);
{
/* moved here, so if needed by multiple modules it applies to all of them */
int i=(*proxy_sqlite3_config)(SQLITE_CONFIG_URI, 1);
if (i!=SQLITE_OK) {
fprintf(stderr,"SQLITE: Error on (*proxy_sqlite3_config)(SQLITE_CONFIG_URI,1)\n");
assert(i==SQLITE_OK);
exit(EXIT_FAILURE);
}
}
}
static void LoadPlugins() {
LoadPlugin_sqlite3_plugin();
//LoadPlugin_sqlite3_plugin();
SQLite3DB::LoadPlugin(GloVars.sqlite3_plugin);
if (GloVars.web_interface_plugin) {
dlerror();
char * dlsym_error = NULL;

@ -12,7 +12,7 @@
int main() {
plan(8);
SQLite3DB::LoadPlugin(NULL);
SQLite3DB *db; // in memory
db = new SQLite3DB();
db->open((char *)"file:mem_db?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX);

Loading…
Cancel
Save