Annotation of sql/mysql/parser3mysql.C, revision 1.28
1.1 parser 1: /** @file
2: Parser MySQL driver.
3:
1.11 paf 4: Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.1 parser 5:
1.7 paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 7:
8: 2001.07.30 using MySQL 3.23.22b
1.3 paf 9:
10: 2001.11.06 numrows on "HP-UX istok1 B.11.00 A 9000/869 448594332 two-user license"
11: 3.23.42 & 4.0.0.alfa never worked, both subst & .sl version returned 0
1.1 parser 12: */
1.28 ! misha 13: static const char *RCSId="$Id: parser3mysql.C,v 1.27 2007/08/27 14:49:34 misha Exp $";
1.1 parser 14:
15: #include "config_includes.h"
16:
17: #include "pa_sql_driver.h"
18:
19: #define NO_CLIENT_LONG_LONG
20: #include "mysql.h"
21: #include "ltdl.h"
22:
23: #define MAX_STRING 0x400
24: #define MAX_NUMBER 20
25:
26: #if _MSC_VER
27: # define snprintf _snprintf
1.2 parser 28: # define strcasecmp _stricmp
1.1 parser 29: #endif
30:
31: static char *lsplit(char *string, char delim) {
32: if(string) {
33: char *v=strchr(string, delim);
34: if(v) {
35: *v=0;
36: return v+1;
37: }
38: }
39: return 0;
40: }
41:
1.2 parser 42: static char *lsplit(char **string_ref, char delim) {
43: char *result=*string_ref;
44: char *next=lsplit(*string_ref, delim);
45: *string_ref=next;
46: return result;
47: }
48:
1.26 paf 49: static char* rsplit(char* string, char delim) {
50: if(string) {
51: char* v=strrchr(string, delim);
52: if(v) {
53: *v=0;
54: return v+1;
55: }
56: }
57: return NULL;
58: }
59:
1.21 paf 60: static void toupper_str(char *out, const char *in, size_t size) {
1.19 paf 61: while(size--)
62: *out++=(char)toupper(*in++);
63: }
64:
1.14 paf 65: struct Connection {
1.16 paf 66: SQL_Driver_services* services;
1.15 paf 67:
1.14 paf 68: MYSQL* handle;
1.19 paf 69: const char* cstrClientCharset;
1.14 paf 70: bool autocommit;
71: };
72:
1.1 parser 73: /**
74: MySQL server driver
75: */
76: class MySQL_Driver : public SQL_Driver {
77: public:
78:
79: MySQL_Driver() : SQL_Driver() {
80: }
81:
82: /// get api version
83: int api_version() { return SQL_DRIVER_API_VERSION; }
84: /// initialize driver by loading sql dynamic link library
1.4 paf 85: const char *initialize(char *dlopen_file_spec) {
1.1 parser 86: return dlopen_file_spec?
87: dlink(dlopen_file_spec):"client library column is empty";
88: }
89: /** connect
1.23 paf 90: @param url
1.2 parser 91: format: @b user:pass@host[:port]|[/unix/socket]/database?
92: charset=cp1251_koi8&
93: timeout=3&
94: compress=1&
95: named_pipe=1
1.1 parser 96: 3.23.22b
1.28 ! misha 97: Currently the only option for @b character_set_name is cp1251_koi8.
! 98: WARNING: must be used only to connect, for buffer doesn't live long
! 99: 4.1+ accept not only 'cp1251_koi8' but 'cp1251', 'utf8' and much more
! 100: it can be usable for transcoding using sql server
1.1 parser 101: */
102: void connect(
1.23 paf 103: char *url,
1.1 parser 104: SQL_Driver_services& services,
1.14 paf 105: void **connection_ref ///< output: Connection*
1.1 parser 106: ) {
1.23 paf 107: char *user=url;
1.26 paf 108: char *s=rsplit(user, '@');
1.1 parser 109: char *host=0;
110: char *unix_socket=0;
111: if(s && s[0]=='[') { // unix socket
112: unix_socket=1+s;
113: s=lsplit(unix_socket, ']');
114: } else { // IP
115: host=s;
116: }
117: char *db=lsplit(s, '/');
118: char *pwd=lsplit(user, ':');
119: char *error_pos=0;
120: char *port_cstr=lsplit(host, ':');
121: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
1.2 parser 122: char *options=lsplit(db, '?');
123:
1.19 paf 124: char *cstrBackwardCompAskServerToTranscode=0;
1.1 parser 125:
1.18 paf 126: Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.15 paf 127: connection.services=&services;
1.14 paf 128: connection.handle=mysql_init(NULL);
1.19 paf 129: connection.cstrClientCharset=0;
1.14 paf 130: connection.autocommit=true;
1.18 paf 131: *connection_ref=&connection;
1.2 parser 132:
133: while(options) {
134: if(char *key=lsplit(&options, '&')) {
135: if(*key) {
136: if(char *value=lsplit(key, '=')) {
1.19 paf 137: if(strcmp(key, "ClientCharset" ) == 0) {
1.21 paf 138: toupper_str(value, value, strlen(value));
1.19 paf 139: connection.cstrClientCharset=value;
140: } else if(strcasecmp(key, "charset")==0) { // left for backward compatibility, consider using ClientCharset
141: cstrBackwardCompAskServerToTranscode=value;
1.2 parser 142: } else if(strcasecmp(key, "timeout")==0) {
143: unsigned int timeout=(unsigned int)atoi(value);
1.14 paf 144: if(mysql_options(connection.handle, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout)!=0)
145: services._throw(mysql_error(connection.handle));
1.2 parser 146: } else if(strcasecmp(key, "compress")==0) {
147: if(atoi(value))
1.14 paf 148: if(mysql_options(connection.handle, MYSQL_OPT_COMPRESS, 0)!=0)
149: services._throw(mysql_error(connection.handle));
1.2 parser 150: } else if(strcasecmp(key, "named_pipe")==0) {
151: if(atoi(value))
1.14 paf 152: if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE , 0)!=0)
153: services._throw(mysql_error(connection.handle));
154: } else if(strcasecmp(key, "autocommit")==0) {
155: if(atoi(value)==0) {
156: connection.autocommit=false;
157: }
1.2 parser 158: } else
159: services._throw("unknown connect option" /*key*/);
160: } else
161: services._throw("connect option without =value" /*key*/);
162: }
163: }
164: }
165:
1.28 ! misha 166: // if(connection.cstrClientCharset && cstrBackwardCompAskServerToTranscode)
! 167: // services._throw("use 'ClientCharset' option only, "
! 168: // "'charset' option is obsolete and should not be used with new 'ClientCharset' option");
1.19 paf 169:
1.14 paf 170: if(!mysql_real_connect(connection.handle,
1.27 misha 171: host, user, pwd, db, port?port:MYSQL_PORT, unix_socket, CLIENT_MULTI_STATEMENTS))
1.14 paf 172: services._throw(mysql_error(connection.handle));
1.1 parser 173:
1.19 paf 174: if(cstrBackwardCompAskServerToTranscode) {
1.1 parser 175: // set charset
1.28 ! misha 176: char statement[MAX_STRING]="set CHARACTER SET ";
1.19 paf 177: strncat(statement, cstrBackwardCompAskServerToTranscode, MAX_STRING);
1.1 parser 178:
1.15 paf 179: exec(connection, statement);
1.1 parser 180: }
181:
1.14 paf 182: if(!connection.autocommit)
1.15 paf 183: exec(connection, "set autocommit=0");
1.14 paf 184: }
185:
1.15 paf 186: void exec(Connection& connection, const char* statement) {
1.14 paf 187: if(mysql_query(connection.handle, statement))
1.15 paf 188: connection.services->_throw(mysql_error(connection.handle));
1.14 paf 189: (*mysql_store_result)(connection.handle); // throw out the result [don't need but must call]
1.1 parser 190: }
1.14 paf 191:
192: void disconnect(void *aconnection) {
193: Connection& connection=*static_cast<Connection*>(aconnection);
194:
195: mysql_close(connection.handle);
1.17 paf 196: connection.handle=0;
1.1 parser 197: }
1.15 paf 198: void commit(void *aconnection) {
1.22 paf 199: //_asm int 3;
1.14 paf 200: Connection& connection=*static_cast<Connection*>(aconnection);
1.1 parser 201:
1.14 paf 202: if(!connection.autocommit)
1.15 paf 203: exec(connection, "commit");
1.14 paf 204: }
1.15 paf 205: void rollback(void *aconnection) {
1.14 paf 206: Connection& connection=*static_cast<Connection*>(aconnection);
207:
208: if(!connection.autocommit)
1.15 paf 209: exec(connection, "rollback");
1.14 paf 210: }
211:
1.15 paf 212: bool ping(void *aconnection) {
1.14 paf 213: Connection& connection=*static_cast<Connection*>(aconnection);
214:
215: return mysql_ping(connection.handle)==0;
1.1 parser 216: }
217:
1.15 paf 218: const char* quote(void *aconnection, const char *from, unsigned int length) {
219: Connection& connection=*static_cast<Connection*>(aconnection);
1.1 parser 220: /*
221: 3.23.22b
222: You must allocate the to buffer to be at least length*2+1 bytes long.
223: (In the worse case, each character may need to be encoded as using two bytes,
224: and you need room for the terminating null byte.)
225: */
1.15 paf 226: char *result=(char*)connection.services->malloc_atomic(length*2+1);
1.13 paf 227: mysql_escape_string(result, from, length);
228: return result;
1.1 parser 229: }
1.24 paf 230: void query(void *aconnection,
231: const char *astatement,
232: size_t placeholders_count, Placeholder* placeholders,
233: unsigned long offset, unsigned long limit,
1.1 parser 234: SQL_Driver_query_event_handlers& handlers) {
1.14 paf 235: Connection& connection=*static_cast<Connection*>(aconnection);
1.15 paf 236: SQL_Driver_services& services=*connection.services;
1.20 paf 237: const char* cstrClientCharset=connection.cstrClientCharset;
1.1 parser 238: MYSQL_RES *res=NULL;
1.24 paf 239:
240: if(placeholders_count>0)
241: services._throw("bind variables not supported (yet)");
1.1 parser 242:
1.19 paf 243: // transcode from $request:charset to connect-string?client_charset
1.20 paf 244: if(cstrClientCharset) {
1.19 paf 245: size_t transcoded_statement_size;
246: services.transcode(astatement, strlen(astatement),
247: astatement, transcoded_statement_size,
248: services.request_charset(),
249: cstrClientCharset);
250: }
251:
1.1 parser 252: const char *statement;
253: if(offset || limit) {
254: size_t statement_size=strlen(astatement);
1.13 paf 255: char *statement_limited=(char *)services.malloc_atomic(
1.1 parser 256: statement_size+MAX_NUMBER*2+8/* limit #,#*/+1);
257: char *cur=statement_limited;
258: memcpy(cur, astatement, statement_size); cur+=statement_size;
259: cur+=sprintf(cur, " limit ");
260: if(offset)
261: cur+=snprintf(cur, MAX_NUMBER+1, "%u,", offset);
262: if(limit)
263: cur+=snprintf(cur, MAX_NUMBER, "%u", limit);
264: statement=statement_limited;
265: } else
266: statement=astatement;
267:
1.14 paf 268: if(mysql_query(connection.handle, statement))
269: services._throw(mysql_error(connection.handle));
270: if(!(res=mysql_store_result(connection.handle)) && mysql_field_count(connection.handle))
271: services._throw(mysql_error(connection.handle));
1.1 parser 272: if(!res) // empty result: insert|delete|update|...
273: return;
274:
275: int column_count=mysql_num_fields(res);
276: if(!column_count) // old client
1.14 paf 277: column_count=mysql_field_count(connection.handle);
1.1 parser 278:
279: if(!column_count) {
280: mysql_free_result(res);
281: services._throw("result contains no columns");
282: }
1.8 paf 283:
1.9 paf 284: bool failed=false;
285: SQL_Error sql_error;
286: #define CHECK(afailed) \
287: if(afailed) { \
288: failed=true; \
289: goto cleanup; \
290: }
291:
292: for(int i=0; i<column_count; i++){
1.12 paf 293: if(MYSQL_FIELD *field=mysql_fetch_field(res)) {
1.20 paf 294: size_t length=strlen(field->name);
295: char* strm=(char*)services.malloc_atomic(length+1);
296: memcpy(strm, field->name, length+1);
297: const char* str=strm;
1.19 paf 298:
299: // transcode to $request:charset from connect-string?client_charset
1.20 paf 300: if(cstrClientCharset) {
1.19 paf 301: services.transcode(str, length,
302: str, length,
303: cstrClientCharset,
304: services.request_charset());
305: }
306:
307: CHECK(handlers.add_column(sql_error, str, length));
1.12 paf 308: } else {
309: // seen some broken client,
310: // which reported "44" for column count of response to "select 2+2"
311: column_count=i;
312: break;
313: }
1.9 paf 314: }
1.1 parser 315:
1.9 paf 316: CHECK(handlers.before_rows(sql_error));
317:
318: while(MYSQL_ROW mysql_row=mysql_fetch_row(res)) {
319: CHECK(handlers.add_row(sql_error));
320: unsigned long *lengths=mysql_fetch_lengths(res);
321: for(int i=0; i<column_count; i++){
1.13 paf 322: size_t length=(size_t)lengths[i];
1.20 paf 323: const char* str;
1.13 paf 324: if(length) {
1.20 paf 325: char *strm=(char*)services.malloc_atomic(length+1);
326: memcpy(strm, mysql_row[i], length);
327: strm[length]=0;
328: str=strm;
1.19 paf 329:
330: // transcode to $request:charset from connect-string?client_charset
1.20 paf 331: if(cstrClientCharset)
1.19 paf 332: services.transcode(str, length,
333: str, length,
334: cstrClientCharset,
335: services.request_charset());
1.9 paf 336: } else
1.13 paf 337: str=0;
338: CHECK(handlers.add_row_cell(sql_error, str, length));
1.3 paf 339: }
1.9 paf 340: }
341: cleanup:
1.1 parser 342: mysql_free_result(res);
1.9 paf 343: if(failed)
344: services._throw(sql_error);
1.1 parser 345: }
346:
347: private: // mysql client library funcs
348:
349: typedef MYSQL* (STDCALL *t_mysql_init)(MYSQL *); t_mysql_init mysql_init;
350:
1.2 parser 351: typedef int (STDCALL *t_mysql_options)(MYSQL *mysql, enum mysql_option option, const char *arg); t_mysql_options mysql_options;
352:
1.1 parser 353: typedef MYSQL_RES* (STDCALL *t_mysql_store_result)(MYSQL *); t_mysql_store_result mysql_store_result;
354:
355: typedef int (STDCALL *t_mysql_query)(MYSQL *, const char *q); t_mysql_query mysql_query;
356:
357: typedef char * (STDCALL *t_mysql_error)(MYSQL *); t_mysql_error mysql_error;
358: static char* STDCALL subst_mysql_error(MYSQL *mysql) { return (mysql)->net.last_error; }
359:
360: typedef MYSQL* (STDCALL *t_mysql_real_connect)(MYSQL *, const char *host,
361: const char *user,
362: const char *passwd,
363: const char *db,
364: unsigned int port,
365: const char *unix_socket,
366: unsigned int clientflag); t_mysql_real_connect mysql_real_connect;
367:
368: typedef void (STDCALL *t_mysql_close)(MYSQL *); t_mysql_close mysql_close;
369:
370: typedef int (STDCALL *t_mysql_ping)(MYSQL *); t_mysql_ping mysql_ping;
371:
372: typedef unsigned long (STDCALL *t_mysql_escape_string)(char *to,const char *from,
373: unsigned long from_length); t_mysql_escape_string mysql_escape_string;
374:
375: typedef void (STDCALL *t_mysql_free_result)(MYSQL_RES *result); t_mysql_free_result mysql_free_result;
376:
377: typedef unsigned long* (STDCALL *t_mysql_fetch_lengths)(MYSQL_RES *result); t_mysql_fetch_lengths mysql_fetch_lengths;
378:
379: typedef MYSQL_ROW (STDCALL *t_mysql_fetch_row)(MYSQL_RES *result); t_mysql_fetch_row mysql_fetch_row;
380:
381: typedef MYSQL_FIELD* (STDCALL *t_mysql_fetch_field)(MYSQL_RES *result); t_mysql_fetch_field mysql_fetch_field;
382:
383: typedef unsigned int (STDCALL *t_mysql_num_fields)(MYSQL_RES *); t_mysql_num_fields mysql_num_fields;
384: static unsigned int STDCALL subst_mysql_num_fields(MYSQL_RES *res) { return res->field_count; }
385:
386: typedef unsigned int (STDCALL *t_mysql_field_count)(MYSQL *); t_mysql_field_count mysql_field_count;
387: static unsigned int STDCALL subst_mysql_field_count(MYSQL *mysql) { return mysql->field_count; }
388:
389: private: // mysql client library funcs linking
390:
391: const char *dlink(const char *dlopen_file_spec) {
1.10 paf 392: if(lt_dlinit())
393: return lt_dlerror();
1.1 parser 394: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
1.25 paf 395: if (!handle) {
396: if(const char* result=lt_dlerror())
397: return result;
398:
1.1 parser 399: return "can not open the dynamic link module";
1.25 paf 400: }
1.1 parser 401:
402: #define DSLINK(name, action) \
403: name=(t_##name)lt_dlsym(handle, #name); \
404: if(!name) \
405: action;
406:
407: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
408: #define SLINK(name) DSLINK(name, name=subst_##name)
409:
410: DLINK(mysql_init);
1.2 parser 411: DLINK(mysql_options);
1.1 parser 412: DLINK(mysql_store_result);
413: DLINK(mysql_query);
414: SLINK(mysql_error);
415: DLINK(mysql_real_connect);
416: DLINK(mysql_close);
417: DLINK(mysql_ping);
418: DLINK(mysql_escape_string);
419: DLINK(mysql_free_result);
420: DLINK(mysql_fetch_lengths);
421: DLINK(mysql_fetch_row);
422: DLINK(mysql_fetch_field);
423: SLINK(mysql_num_fields);
424: SLINK(mysql_field_count);
425: return 0;
426: }
427:
428: };
429:
430: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
431: return new MySQL_Driver();
432: }
E-mail: