fix
[libdbw.git] / include / mysql / mysql.h
blob6e7c3c2ee21c367c81f221224a101cbcbf0e6e03
1 #ifndef __MYSQLW_H__
2 #define __MYSQLW_H__
4 #include <stdlib.h>
5 #include <time.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <mysql.h>
10 #include <libex/str.h>
11 #include <libex/list.h>
13 typedef struct {
14 time_t lastm;
15 list_item_t *node;
16 int status;
17 const char *msg;
18 char *cmd;
19 MYSQL db;
20 } myconn_t;
21 myconn_t *maria_connect (const char *host, const char *dbname, const char *user, const char *pass);
22 myconn_t *maria_connect_str (const char *conn_info);
23 void maria_disconnect (myconn_t *conn);
25 typedef struct {
26 int status;
27 const char *msg;
28 MYSQL_STMT *stmt;
29 MYSQL_RES *res;
30 MYSQL_BIND *params;
31 MYSQL_BIND *result;
32 int nflds;
33 int nparams;
34 my_bool *param_nulls;
35 my_bool *nulls;
36 my_bool *errors;
37 unsigned long *lengths;
38 unsigned long *param_lengths;
39 } myres_t;
40 void maria_res_close (myres_t *res);
42 typedef union {
43 int8_t i1;
44 uint8_t ui1;
45 int16_t i2;
46 uint16_t ui2;
47 int32_t i4;
48 uint32_t ui4;
49 int64_t i8;
50 uint64_t ui8;
51 float f4;
52 double f8;
53 strptr_t s;
54 } myval_t;
56 typedef struct {
57 int is_null;
58 int type;
59 myval_t data;
60 } myfld_t;
61 void maria_get_tiny (myres_t *res, int col, myfld_t *fld);
62 void maria_get_smallint (myres_t *res, int col, myfld_t *fld);
63 void maria_get_int (myres_t *res, int col, myfld_t *fld);
64 void maria_get_bigint (myres_t *res, int col, myfld_t *fld);
65 void maria_get_float (myres_t *res, int col, myfld_t *fld);
66 void maria_get_double (myres_t *res, int col, myfld_t *fld);
67 void maria_get_varchar (myres_t *res, int col, myfld_t *fld);
69 #define MARIA_DEF_NULL { .is_null = 1 }
70 #define MARIA_DEF_TINY(V) { .is_null = 0, .type = MYSQL_TYPE_TINY, .data.i1 = V }
71 #define MARIA_DEF_SMALLINT(V) { .is_null = 0, .type = MYSQL_TYPE_SHORT, .data.i2 = V }
72 #define MARIA_DEF_INT(V) { .is_null = 0, .type = MYSQL_TYPE_LONG, .data.i4 = V }
73 #define MARIA_DEF_BIGINT(V) { .is_null = 0, .type = MYSQL_TYPE_LONGLONG, .data.i8 = V }
74 #define MARIA_DEF_FLOAT(V) { .is_null = 0, .type = MYSQL_TYPE_TINY, .data.f4 = V }
75 #define MARIA_DEF_DOUBLE(V) { .is_null = 0, .type = MYSQL_TYPE_DOUBLE, .data.f8 = V }
76 #define MARIA_DEF_VARCHAR(V,L) { .is_null = 0, .type = MYSQL_TYPE_VAR_STRING, .data.s.ptr = V, .data.s.len = L }
78 myres_t *maria_prepare (myconn_t *conn, const char *sql, size_t sql_len);
79 myres_t *maria_exec (myconn_t *conn, const char *sql, size_t sql_len, myfld_t *arg, ...);
80 int maria_pexec (myres_t *res, myfld_t *arg, ...);
81 int maria_exec_sql (myconn_t *conn, const char *sql, size_t sql_len);
82 int maria_fetch (myres_t *res);
83 static inline int maria_start (myconn_t *conn) { return maria_exec_sql(conn, CONST_STR_LEN("start transaction")); }
84 static inline int maria_commit (myconn_t *conn) { return maria_exec_sql(conn, CONST_STR_LEN("commit")); }
85 static inline int maria_rollback (myconn_t *conn) { return maria_exec_sql(conn, CONST_STR_LEN("rollback")); }
87 #endif // __MYSQLW_H__