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