Annotation of sql/mysql/parser3mysql.C, revision 1.53
1.1 parser 1: /** @file
2: Parser MySQL driver.
3:
1.48 moko 4: Copyright (c) 2001-2015 Art. Lebedev Studio (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: */
13:
14: #include "config_includes.h"
15:
16: #include "pa_sql_driver.h"
17:
1.53 ! moko 18: volatile const char * IDENT_PARSER3MYSQL_C="$Id: parser3mysql.C,v 1.52 2017/08/01 16:50:13 moko Exp $" IDENT_PA_SQL_DRIVER_H;
1.40 moko 19:
1.1 parser 20: #define NO_CLIENT_LONG_LONG
21: #include "mysql.h"
22: #include "ltdl.h"
23:
24: #define MAX_STRING 0x400
25: #define MAX_NUMBER 20
26:
27: #if _MSC_VER
28: # define snprintf _snprintf
1.2 parser 29: # define strcasecmp _stricmp
1.1 parser 30: #endif
31:
1.34 misha 32: static char *lsplit(char *string, char delim){
1.32 misha 33: if(string) {
1.34 misha 34: if(char *v=strchr(string, delim)){
1.1 parser 35: *v=0;
36: return v+1;
37: }
1.32 misha 38: }
39: return 0;
1.1 parser 40: }
41:
1.34 misha 42: static char *lsplit(char **string_ref, char delim){
1.32 misha 43: char *result=*string_ref;
1.2 parser 44: char *next=lsplit(*string_ref, delim);
1.32 misha 45: *string_ref=next;
46: return result;
1.2 parser 47: }
48:
1.34 misha 49: static char* rsplit(char* string, char delim){
50: if(string){
51: if(char* v=strrchr(string, delim)){
1.26 paf 52: *v=0;
53: return v+1;
54: }
1.32 misha 55: }
56: return NULL;
1.26 paf 57: }
58:
1.34 misha 59: static void toupper_str(char *out, const char *in, size_t size){
1.19 paf 60: while(size--)
61: *out++=(char)toupper(*in++);
62: }
63:
1.38 misha 64: inline static bool is_column_transcode_required(enum_field_types type) {
65: switch(type) {
66: case MYSQL_TYPE_NULL:
67:
1.45 moko 68: #ifdef FIELD_TYPE_NEWDECIMAL
69: case MYSQL_TYPE_NEWDECIMAL:
70: #endif
1.38 misha 71: case MYSQL_TYPE_DECIMAL:
72: case MYSQL_TYPE_FLOAT:
73: case MYSQL_TYPE_DOUBLE:
74:
75: case MYSQL_TYPE_TINY:
76: case MYSQL_TYPE_SHORT:
77: case MYSQL_TYPE_LONG:
78: case MYSQL_TYPE_LONGLONG:
79: case MYSQL_TYPE_INT24:
1.45 moko 80: #ifdef FIELD_TYPE_BIT
1.38 misha 81: case MYSQL_TYPE_BIT:
1.45 moko 82: #endif
1.38 misha 83:
84: case MYSQL_TYPE_DATE:
85: case MYSQL_TYPE_NEWDATE:
86: case MYSQL_TYPE_TIME:
87: case MYSQL_TYPE_DATETIME:
88: case MYSQL_TYPE_YEAR:
89: case MYSQL_TYPE_TIMESTAMP:
90:
91: case MYSQL_TYPE_BLOB:
92: case MYSQL_TYPE_TINY_BLOB:
93: case MYSQL_TYPE_MEDIUM_BLOB:
94: case MYSQL_TYPE_LONG_BLOB:
95: return false;
96: break;
1.49 moko 97: default:
98: return true;
1.38 misha 99: }
100: }
101:
1.53 ! moko 102: inline static char* strdup(SQL_Driver_services& services, char* str, size_t length) {
1.38 misha 103: char *strm=(char*)services.malloc_atomic(length+1);
104: memcpy(strm, str, length);
105: strm[length]=0;
1.53 ! moko 106: return strm;
1.38 misha 107: }
108:
1.14 paf 109: struct Connection {
1.16 paf 110: SQL_Driver_services* services;
1.15 paf 111:
1.14 paf 112: MYSQL* handle;
1.33 misha 113: const char* client_charset;
1.14 paf 114: bool autocommit;
115: };
116:
1.29 misha 117:
1.1 parser 118: /**
119: MySQL server driver
120: */
121: class MySQL_Driver : public SQL_Driver {
122: public:
123:
1.29 misha 124: MySQL_Driver() : SQL_Driver() {}
125:
1.1 parser 126: /// get api version
127: int api_version() { return SQL_DRIVER_API_VERSION; }
1.33 misha 128:
1.1 parser 129: /// initialize driver by loading sql dynamic link library
1.4 paf 130: const char *initialize(char *dlopen_file_spec) {
1.1 parser 131: return dlopen_file_spec?
132: dlink(dlopen_file_spec):"client library column is empty";
133: }
1.33 misha 134:
1.1 parser 135: /** connect
1.23 paf 136: @param url
1.2 parser 137: format: @b user:pass@host[:port]|[/unix/socket]/database?
1.47 moko 138: charset=value& // transcode by server with command 'SET NAMES value'
1.32 misha 139: ClientCharset=charset& // transcode by parser
1.2 parser 140: timeout=3&
1.32 misha 141: compress=0&
142: named_pipe=1&
1.33 misha 143: autocommit=1&
1.35 misha 144: multi_statements=0 // allows more then one statement in one query
1.52 moko 145: 4.1+ accept not 'cp1251_koi8' but 'cp1251', 'utf8' and much more
1.33 misha 146: it is usable for transcoding using sql server
1.1 parser 147: */
1.52 moko 148: void connect(char *url, SQL_Driver_services& services, void **connection_ref /*< output: Connection* */){
1.23 paf 149: char *user=url;
1.26 paf 150: char *s=rsplit(user, '@');
1.1 parser 151: char *host=0;
152: char *unix_socket=0;
153: if(s && s[0]=='[') { // unix socket
154: unix_socket=1+s;
155: s=lsplit(unix_socket, ']');
156: } else { // IP
157: host=s;
158: }
159: char *db=lsplit(s, '/');
160: char *pwd=lsplit(user, ':');
161: char *error_pos=0;
1.2 parser 162: char *options=lsplit(db, '?');
1.33 misha 163: char *charset=0;
1.34 misha 164: int client_flag=CLIENT_MULTI_RESULTS;
1.1 parser 165:
1.33 misha 166: Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
167: *connection_ref=&connection;
1.15 paf 168: connection.services=&services;
1.32 misha 169: connection.handle=mysql_init(NULL);
1.52 moko 170: connection.client_charset=0;
1.14 paf 171: connection.autocommit=true;
1.2 parser 172:
1.53 ! moko 173: while(1){
! 174: char *next_host=lsplit(host, ',');
! 175: char *host_options=next_host && options ? strdup(services, options, strlen(options)) : options;
! 176:
! 177: char *port_cstr=lsplit(host, ':');
! 178: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
! 179:
! 180: while(host_options){
! 181: char *key=lsplit(&host_options, '&');
! 182: if(key && *key){
1.32 misha 183: if(char *value=lsplit(key, '=')){
1.33 misha 184: if(strcmp(key, "ClientCharset")==0){ // transcoding with parser
1.21 paf 185: toupper_str(value, value, strlen(value));
1.33 misha 186: connection.client_charset=value;
1.32 misha 187: } else if(strcasecmp(key, "charset")==0){ // transcoding with server
1.33 misha 188: charset=value;
1.32 misha 189: } else if(strcasecmp(key, "timeout")==0){
1.2 parser 190: unsigned int timeout=(unsigned int)atoi(value);
1.14 paf 191: if(mysql_options(connection.handle, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout)!=0)
192: services._throw(mysql_error(connection.handle));
1.32 misha 193: } else if(strcasecmp(key, "compress")==0){
1.2 parser 194: if(atoi(value))
1.14 paf 195: if(mysql_options(connection.handle, MYSQL_OPT_COMPRESS, 0)!=0)
196: services._throw(mysql_error(connection.handle));
1.32 misha 197: } else if(strcasecmp(key, "named_pipe")==0){
1.2 parser 198: if(atoi(value))
1.33 misha 199: if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE, 0)!=0)
1.14 paf 200: services._throw(mysql_error(connection.handle));
1.39 misha 201: } else if(strcasecmp(key, "local_infile")==0){
202: if(atoi(value))
203: if(mysql_options(connection.handle, MYSQL_OPT_LOCAL_INFILE, 0)!=0)
204: services._throw(mysql_error(connection.handle));
1.32 misha 205: } else if(strcasecmp(key, "autocommit")==0){
1.31 misha 206: if(atoi(value)==0)
1.14 paf 207: connection.autocommit=false;
1.34 misha 208: } else if(strcasecmp(key, "multi_statements")==0){
209: if(atoi(value)!=0)
210: client_flag=CLIENT_MULTI_STATEMENTS;
1.2 parser 211: } else
212: services._throw("unknown connect option" /*key*/);
1.53 ! moko 213: } else
1.2 parser 214: services._throw("connect option without =value" /*key*/);
215: }
216: }
217:
1.53 ! moko 218: if(mysql_real_connect(connection.handle, host, user, pwd, db, port, unix_socket, client_flag))
! 219: break;
! 220:
! 221: if(!next_host)
! 222: services._throw(mysql_error(connection.handle));
! 223:
! 224: host=next_host;
1.33 misha 225: }
1.1 parser 226:
1.33 misha 227: if(charset){
1.47 moko 228: char statement[MAX_STRING+1]="SET NAMES ";
1.33 misha 229: strncat(statement, charset, MAX_STRING);
230: _exec(connection, statement);
1.1 parser 231: }
232:
1.14 paf 233: if(!connection.autocommit)
1.33 misha 234: _exec(connection, "SET AUTOCOMMIT=0");
1.1 parser 235: }
1.14 paf 236:
237: void disconnect(void *aconnection) {
238: Connection& connection=*static_cast<Connection*>(aconnection);
239: mysql_close(connection.handle);
1.17 paf 240: connection.handle=0;
1.1 parser 241: }
1.32 misha 242:
1.15 paf 243: void commit(void *aconnection) {
1.14 paf 244: Connection& connection=*static_cast<Connection*>(aconnection);
245: if(!connection.autocommit)
1.33 misha 246: _exec(connection, "COMMIT");
1.14 paf 247: }
1.32 misha 248:
1.15 paf 249: void rollback(void *aconnection) {
1.14 paf 250: Connection& connection=*static_cast<Connection*>(aconnection);
251: if(!connection.autocommit)
1.33 misha 252: _exec(connection, "ROLLBACK");
1.14 paf 253: }
254:
1.15 paf 255: bool ping(void *aconnection) {
1.14 paf 256: Connection& connection=*static_cast<Connection*>(aconnection);
257: return mysql_ping(connection.handle)==0;
1.1 parser 258: }
259:
1.37 moko 260: // charset here is services.request_charset(), not connection.client_charset
261: // thus we can't use the sql server quoting support
262: const char* quote(void *aconnection, const char *str, unsigned int length) {
263: const char* from;
264: const char* from_end=str+length;
265:
266: size_t quoted=0;
267:
268: for(from=str; from<from_end; from++){
269: switch (*from) {
270: case 0:
271: case '\n':
272: case '\r':
273: case '\032':
274: case '\\':
275: case '\'':
276: case '"':
277: quoted++;
278: }
279: }
280:
281: if(!quoted)
282: return str;
283:
1.15 paf 284: Connection& connection=*static_cast<Connection*>(aconnection);
1.37 moko 285: char *result=(char*)connection.services->malloc_atomic(length + quoted + 1);
286: char *to = result;
287:
288: for(from=str; from<from_end; from++){
289: char escape;
290: switch (*from) {
291: case 0:
292: escape= '0';
293: break;
294: case '\n':
295: escape= 'n';
296: break;
297: case '\r':
298: escape= 'r';
299: break;
300: case '\032':
301: escape= 'Z';
302: break;
303: case '\\':
304: case '\'':
305: case '"':
306: escape= *from;
307: break;
308: default:
309: *to++=*from;
310: continue;
311: }
312: *to++= '\\';
313: *to++= escape;
314: }
315:
316: *to=0;
1.13 paf 317: return result;
1.1 parser 318: }
1.33 misha 319:
1.24 paf 320: void query(void *aconnection,
1.33 misha 321: const char *astatement,
322: size_t placeholders_count, Placeholder* placeholders,
323: unsigned long offset, unsigned long limit,
324: SQL_Driver_query_event_handlers& handlers
325: ){
1.14 paf 326: Connection& connection=*static_cast<Connection*>(aconnection);
1.15 paf 327: SQL_Driver_services& services=*connection.services;
1.1 parser 328: MYSQL_RES *res=NULL;
1.24 paf 329:
330: if(placeholders_count>0)
331: services._throw("bind variables not supported (yet)");
1.1 parser 332:
1.33 misha 333: bool transcode_needed=_transcode_required(connection);
334:
1.38 misha 335: size_t statement_size=0;
336:
337: if(transcode_needed) {
338: statement_size=strlen(astatement);
339: // transcode query from $request:charset to ?ClientCharset
340: services.transcode(astatement, statement_size,
341: astatement, statement_size,
1.19 paf 342: services.request_charset(),
1.33 misha 343: connection.client_charset);
1.19 paf 344: }
345:
1.1 parser 346: const char *statement;
1.38 misha 347: if(offset || limit!=SQL_NO_LIMIT) {
348: if(!statement_size)
349: statement_size=strlen(astatement);
1.13 paf 350: char *statement_limited=(char *)services.malloc_atomic(
1.33 misha 351: statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1);
1.1 parser 352: char *cur=statement_limited;
353: memcpy(cur, astatement, statement_size); cur+=statement_size;
1.33 misha 354: cur+=sprintf(cur, " LIMIT ");
1.1 parser 355: if(offset)
1.42 moko 356: cur+=snprintf(cur, MAX_NUMBER, "%lu,", offset);
1.33 misha 357: if(limit!=SQL_NO_LIMIT)
1.41 moko 358: cur+=snprintf(cur, MAX_NUMBER, "%lu", limit);
1.1 parser 359: statement=statement_limited;
360: } else
361: statement=astatement;
362:
1.14 paf 363: if(mysql_query(connection.handle, statement))
1.33 misha 364: _throw(connection, mysql_error(connection.handle));
1.38 misha 365: if(!(res=mysql_store_result(connection.handle)) && mysql_field_count(connection.handle))
1.33 misha 366: _throw(connection, mysql_error(connection.handle));
1.1 parser 367: if(!res) // empty result: insert|delete|update|...
368: return;
1.38 misha 369:
370: size_t column_count=mysql_num_fields(res);
1.1 parser 371: if(!column_count) // old client
1.14 paf 372: column_count=mysql_field_count(connection.handle);
1.1 parser 373:
1.33 misha 374: if(!column_count){
1.1 parser 375: mysql_free_result(res);
376: services._throw("result contains no columns");
377: }
1.38 misha 378:
1.9 paf 379: bool failed=false;
380: SQL_Error sql_error;
1.38 misha 381:
382: #define CHECK(afailed) \
383: if(afailed) { \
384: failed=true; \
1.9 paf 385: goto cleanup; \
386: }
387:
1.44 moko 388: #define DO_FETCH_FIELDS(transcode_column_name) { \
389: for(size_t i=0; i<column_count; i++) { \
390: if(MYSQL_FIELD *field = mysql_fetch_field(res)){ \
391: size_t length=field->name_length; \
392: const char* str=strdup(services, field->name, length); \
393: transcode_column_name \
394: CHECK(handlers.add_column(sql_error, str, length)); \
395: } else { \
396: /* seen broken client, that reported "44" column count for "select 2+2" */ \
397: column_count=i; \
398: break; \
399: } \
400: } \
401: }
402:
403: #define DO_FETCH_ROWS(transcode_cell_value) { \
404: while(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { \
405: CHECK(handlers.add_row(sql_error)); \
406: unsigned long *lengths=mysql_fetch_lengths(res); \
407: for(size_t i=0; i<column_count; i++) { \
408: const char* str=0; \
409: size_t length=lengths[i]; \
410: if(length) { \
411: str=strdup(services, mysql_row[i], length); \
412: transcode_cell_value \
413: } \
414: CHECK(handlers.add_row_cell(sql_error, str, length)); \
415: } \
416: } \
1.38 misha 417: }
418:
419: bool* transcode_column=0;
420: if(transcode_needed) {
421: transcode_column = new bool[column_count];
422: DO_FETCH_FIELDS(
1.44 moko 423: transcode_column[i] = is_column_transcode_required(field->type);
1.38 misha 424: // transcode column's name from ?ClientCharset to $request:charset
425: services.transcode(str, length,
426: str, length,
427: connection.client_charset,
428: services.request_charset());
429: )
430: CHECK(handlers.before_rows(sql_error));
431: DO_FETCH_ROWS(
432: if(transcode_column[i])
433: // transcode cell's value from ?ClientCharset to $request:charset
1.19 paf 434: services.transcode(str, length,
435: str, length,
1.33 misha 436: connection.client_charset,
1.19 paf 437: services.request_charset());
1.38 misha 438: )
439: } else {
440: // without transcoding
441: DO_FETCH_FIELDS()
442: CHECK(handlers.before_rows(sql_error));
443: DO_FETCH_ROWS()
1.32 misha 444: }
1.9 paf 445: cleanup:
1.38 misha 446: if(transcode_column)
447: delete transcode_column;
1.1 parser 448: mysql_free_result(res);
1.9 paf 449: if(failed)
450: services._throw(sql_error);
1.1 parser 451: }
452:
1.33 misha 453: private:
454: void _exec(Connection& connection, const char* statement) {
455: if(mysql_query(connection.handle, statement))
456: _throw(connection, mysql_error(connection.handle));
457: (*mysql_store_result)(connection.handle); // throw out the result [don't need but must call]
458: }
459:
460: void _throw(Connection& connection, const char* aerr_msg) {
461: size_t length=strlen(aerr_msg);
462: if(length && _transcode_required(connection)) {
463: connection.services->transcode(aerr_msg, length,
464: aerr_msg, length,
465: connection.client_charset,
466: connection.services->request_charset());
467: }
468: connection.services->_throw(aerr_msg);
469: }
470:
471: bool _transcode_required(Connection& connection){
472: return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0);
473: }
474:
1.1 parser 475: private: // mysql client library funcs
476:
1.52 moko 477: typedef MYSQL* (STDCALL *t_mysql_init)(MYSQL *); t_mysql_init mysql_init;
478:
479: typedef void (STDCALL *t_mysql_server_end)(); t_mysql_server_end mysql_server_end;
480:
481: typedef int (STDCALL *t_mysql_options)(MYSQL *mysql, enum mysql_option option, const char *arg); t_mysql_options mysql_options;
1.29 misha 482:
1.1 parser 483: typedef MYSQL_RES* (STDCALL *t_mysql_store_result)(MYSQL *); t_mysql_store_result mysql_store_result;
1.52 moko 484:
485: typedef int (STDCALL *t_mysql_query)(MYSQL *, const char *q); t_mysql_query mysql_query;
486:
487: typedef char* (STDCALL *t_mysql_error)(MYSQL *); t_mysql_error mysql_error;
1.1 parser 488: static char* STDCALL subst_mysql_error(MYSQL *mysql) { return (mysql)->net.last_error; }
489:
1.52 moko 490: typedef MYSQL* (STDCALL *t_mysql_real_connect)(MYSQL *, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned int clientflag); t_mysql_real_connect mysql_real_connect;
491:
492: typedef void (STDCALL *t_mysql_close)(MYSQL *); t_mysql_close mysql_close;
493:
494: typedef int (STDCALL *t_mysql_ping)(MYSQL *); t_mysql_ping mysql_ping;
495:
496: typedef unsigned long (STDCALL *t_mysql_escape_string)(char *to,const char *from, unsigned long from_length); t_mysql_escape_string mysql_escape_string;
497:
498: typedef void (STDCALL *t_mysql_free_result)(MYSQL_RES *result); t_mysql_free_result mysql_free_result;
499:
1.1 parser 500: typedef unsigned long* (STDCALL *t_mysql_fetch_lengths)(MYSQL_RES *result); t_mysql_fetch_lengths mysql_fetch_lengths;
501:
1.52 moko 502: typedef MYSQL_ROW (STDCALL *t_mysql_fetch_row)(MYSQL_RES *result); t_mysql_fetch_row mysql_fetch_row;
503: typedef MYSQL_FIELD* (STDCALL *t_mysql_fetch_field)(MYSQL_RES *result); t_mysql_fetch_field mysql_fetch_field;
504:
505: typedef unsigned int (STDCALL *t_mysql_num_fields)(MYSQL_RES *); t_mysql_num_fields mysql_num_fields;
506: typedef unsigned int (STDCALL *t_mysql_field_count)(MYSQL *); t_mysql_field_count mysql_field_count;
507:
508: static unsigned int STDCALL subst_mysql_num_fields(MYSQL_RES *res) { return res->field_count; }
509: static unsigned int STDCALL subst_mysql_field_count(MYSQL *mysql) { return mysql->field_count; }
1.1 parser 510:
511: private: // mysql client library funcs linking
512:
513: const char *dlink(const char *dlopen_file_spec) {
1.43 moko 514: if(lt_dlinit()){
515: if(const char* result=lt_dlerror())
516: return result;
517: return "can not prepare to dynamic loading";
518: }
1.25 paf 519:
1.33 misha 520: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
521:
522: if(!handle){
523: if(const char* result=lt_dlerror())
524: return result;
1.1 parser 525: return "can not open the dynamic link module";
1.25 paf 526: }
1.1 parser 527:
1.29 misha 528: #define GLINK(name) \
529: name=(t_##name)lt_dlsym(handle, #name);
530:
1.1 parser 531: #define DSLINK(name, action) \
1.29 misha 532: GLINK(name) \
1.1 parser 533: if(!name) \
534: action;
535:
536: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
537: #define SLINK(name) DSLINK(name, name=subst_##name)
538:
539: DLINK(mysql_init);
1.29 misha 540: GLINK(mysql_server_end);
1.2 parser 541: DLINK(mysql_options);
1.1 parser 542: DLINK(mysql_store_result);
543: DLINK(mysql_query);
544: SLINK(mysql_error);
545: DLINK(mysql_real_connect);
546: DLINK(mysql_close);
547: DLINK(mysql_ping);
548: DLINK(mysql_escape_string);
549: DLINK(mysql_free_result);
550: DLINK(mysql_fetch_lengths);
551: DLINK(mysql_fetch_row);
1.44 moko 552: DLINK(mysql_fetch_field);
1.1 parser 553: SLINK(mysql_num_fields);
554: SLINK(mysql_field_count);
555: return 0;
556: }
557:
558: };
559:
560: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
1.29 misha 561: static MySQL_Driver Driver;
562: return &Driver;
1.1 parser 563: }
E-mail: