-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackendmysql.c
90 lines (75 loc) · 2.4 KB
/
backendmysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef HAVE_MYSQL
#include <mysql.h>
#endif
#include <dlfcn.h>
#include "utilities.h"
#include "backend.h"
#ifdef HAVE_MYSQL
#include "schema_mysql.h"
#endif
#ifdef HAVE_MYSQL
struct connection_struct {
void* dlhandle;
int (*library_init)(int,char**,char**);
int (*library_end)(void);
MYSQL*(*init)(MYSQL*);
MYSQL*(*connect)(MYSQL*,const char* host,const char* user,const char* passwd,const char* db,unsigned int port,const char* unix_socket,unsigned long flags);
void(*close)(MYSQL*);
};
struct session_struct {
connection_type connection;
MYSQL* handle;
};
static int
opensession(connection_type connection, session_type* resultSession)
{
session_type session;
MYSQL* newhandle;
*resultSession = NULL;
session = malloc(sizeof(struct session_struct));
session->connection = connection;
session->handle = NULL;
newhandle = connection->init(session->handle);
session->handle = connection->connect(newhandle, "localhost","root","poslww","ods",0,NULL,CLIENT_MULTI_RESULTS | CLIENT_MULTI_STATEMENTS);
return 0;
}
static int
closesession(session_type session)
{
session->connection->close(session->handle);
return 0;
}
struct backend
mysqlRegister(void)
{
int (*threadsafefunc)(void);
struct connection_struct* connection;
struct backend backend;
connection = malloc(sizeof(struct connection_struct));
connection->dlhandle = dlopen("libsqlite3.so", RTLD_LAZY);
connection->init = (MYSQL*(*)(MYSQL*))functioncast(dlsym(connection->dlhandle, "mysql_init"));
connection->connect = (MYSQL*(*)(MYSQL *,const char *,const char *,const char *,const char *,unsigned int,const char*,unsigned long))functioncast(dlsym(connection->dlhandle, "mysql_real_connect"));
connection->close = (void (*)(MYSQL*))functioncast(dlsym(connection->dlhandle, "mysql_close"));
backend.connection = connection;
backend.opensession = opensession;
backend.closesession = closesession;
threadsafefunc = (int (*)(void))functioncast(dlsym(connection->dlhandle, "sqlite3_threadsafe"));
if (!threadsafefunc()) {
fprintf(stderr, "SQLite not thread safe\n");
}
return backend;
}
#else
struct backend
mysqlRegister(void)
{
struct backend backend;
memset(&backend, 0, sizeof(struct backend));
return backend;
}
#endif