1 /*
2 
3 mysqlclient binding to D.
4 API overview here http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html
5 
6 Some method and structures are missing due to deprecatedions or not needed yet
7 
8 */
9 
10 module mysql.binding;
11 
12 import core.stdc.config;
13 import std.string;
14 import std.array;
15 
16 auto cstr2dstr(inout(char)* cstr, uint str_size) {
17     import core.stdc.string: strlen;
18     return cstr ? cstr[0 .. strlen(cstr)] : "";
19 }
20 
21 cstring toCstring(string c) {
22     return cast(cstring) toStringz(c);
23 }
24 
25 string fromCstring(cstring c, int len = -1) {
26     string ret;
27     if (c is null)
28         return null;
29     if (len == 0)
30         return "";
31     if (len == -1) {
32         auto iterator = c;
33         while(*iterator)
34             iterator++;
35 
36         // note they are both byte pointers, so this is sane
37         len = cast(int) iterator - cast(int) c;
38         assert(len >= 0);
39     }
40 
41     ret = cast(string) (c[0 .. len].idup);
42 
43     return ret;
44 }
45 
46 version(Windows) {
47     pragma(lib, "libmysql");
48 } else {
49     pragma(lib, "mysqlclient");
50 }
51 
52 extern(System) {
53     struct MYSQL;
54 
55     struct MYSQL_RES {
56         ulong           row_count;
57         MYSQL_FIELD     *fields;
58         MYSQL_DATA      *data;
59         MYSQL_ROWS      *data_cursor;
60         ulong           *lengths;        /* column lengths of current row */
61         MYSQL           *handle;        /* for unbuffered reads */
62         //const struct st_mysql_methods *methods;
63         MYSQL_ROW       row;            /* If unbuffered read */
64         MYSQL_ROW       current_row;    /* buffer to current row */
65         ///MEM_ROOT     field_alloc;
66         uint            field_count, current_field;
67         char            eof;            /* Used by mysql_fetch_row */
68         /* mysql_stmt_close() had to cancel this result */
69         char            unbuffered_fetch_cancelled;
70         void            *extension;
71     }
72 
73     struct MYSQL_ROWS {
74         //struct st_mysql_rows *next;        /* list of rows */
75         MYSQL_ROW data;
76         ulong length;
77     }
78 
79     struct MYSQL_DATA {
80         MYSQL_ROWS *data;
81         //struct embedded_query_result *embedded_info;
82         //MEM_ROOT alloc;
83         ulong rows;
84         uint fields;
85         /* extra info for embedded library */
86         void *extension;
87     }
88 
89     /* typedef */ alias const(ubyte)* cstring;
90 
91     struct MYSQL_FIELD {
92         cstring name;                 /* Name of column */
93         cstring org_name;             /* Original column name, if an alias */
94         cstring table;                /* Table of column if column was a field */
95         cstring org_table;            /* Org table name, if table was an alias */
96         cstring db;                   /* Database for table */
97         cstring catalog;          /* Catalog for table */
98         cstring def;                  /* Default value (set by mysql_list_fields) */
99         size_t length;       /* Width of column (create length) */
100         size_t max_length;   /* Max width for selected set */
101         uint name_length;
102         uint org_name_length;
103         uint table_length;
104         uint org_table_length;
105         uint db_length;
106         uint catalog_length;
107         uint def_length;
108         uint flags;         /* Div flags */
109         uint decimals;      /* Number of decimals in field */
110         uint charsetnr;     /* Character set */
111         uint type; /* Type of field. See mysql_com.h for types */
112         // type is actually an enum btw
113         void* extension;
114     }
115 
116     alias ubyte my_bool;
117     alias char** MYSQL_ROW;
118 
119     cstring mysql_get_client_info();
120     ulong mysql_get_client_version();
121 
122     MYSQL* mysql_init(MYSQL*);
123     uint mysql_errno(MYSQL*);
124     cstring mysql_error(MYSQL*);
125 
126     MYSQL* mysql_real_connect(MYSQL*,
127         cstring host,
128         cstring user,
129         cstring password,
130         cstring db,
131         uint port,
132         cstring unix_socket,
133         c_ulong clientflag
134     );
135     int mysql_select_db(MYSQL*, cstring);
136 
137     int mysql_query(MYSQL*, cstring);
138 
139     void mysql_close(MYSQL*);
140     int mysql_ping(MYSQL *mysql);
141     cstring mysql_stat(MYSQL *mysql);
142 
143     ulong mysql_num_rows(MYSQL_RES*);
144     uint mysql_num_fields(MYSQL_RES*);
145     bool mysql_eof(MYSQL_RES*);
146 
147     ulong mysql_affected_rows(MYSQL*);
148     ulong mysql_insert_id(MYSQL*);
149 
150     MYSQL_RES* mysql_store_result(MYSQL*);
151     MYSQL_RES* mysql_use_result(MYSQL*);
152 
153     MYSQL_ROW mysql_fetch_row(MYSQL_RES *);
154     uint* mysql_fetch_lengths(MYSQL_RES*);
155     MYSQL_FIELD* mysql_fetch_field(MYSQL_RES*);
156     MYSQL_FIELD* mysql_fetch_fields(MYSQL_RES*);
157     MYSQL_FIELD mysql_fetch_field_direct(MYSQL_RES*, uint);
158 
159     uint mysql_real_escape_string(MYSQL*, ubyte* to, cstring from, uint length);
160 
161     void mysql_free_result(MYSQL_RES*);
162 
163     enum mysql_option {
164       MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS, MYSQL_OPT_NAMED_PIPE,
165       MYSQL_INIT_COMMAND, MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP,
166       MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME, MYSQL_OPT_LOCAL_INFILE,
167       MYSQL_OPT_PROTOCOL, MYSQL_SHARED_MEMORY_BASE_NAME, MYSQL_OPT_READ_TIMEOUT,
168       MYSQL_OPT_WRITE_TIMEOUT, MYSQL_OPT_USE_RESULT,
169       MYSQL_OPT_USE_REMOTE_CONNECTION, MYSQL_OPT_USE_EMBEDDED_CONNECTION,
170       MYSQL_OPT_GUESS_CONNECTION, MYSQL_SET_CLIENT_IP, MYSQL_SECURE_AUTH,
171       MYSQL_REPORT_DATA_TRUNCATION, MYSQL_OPT_RECONNECT,
172       MYSQL_OPT_SSL_VERIFY_SERVER_CERT, MYSQL_PLUGIN_DIR, MYSQL_DEFAULT_AUTH,
173       MYSQL_ENABLE_CLEARTEXT_PLUGIN
174     };
175 
176     int mysql_options(MYSQL* mysql, mysql_option option, const void* arg);
177 }