Annotation of sql/mysql/parser3mysql.C, revision 1.52
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.52 ! moko 18: volatile const char * IDENT_PARSER3MYSQL_C="$Id: parser3mysql.C,v 1.51 2017/06/24 21:09:43 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:
102: inline static const char* strdup(SQL_Driver_services& services, char* str, size_t length) {
103: char *strm=(char*)services.malloc_atomic(length+1);
104: memcpy(strm, str, length);
105: strm[length]=0;
106: return (const char*)strm;
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.52 ! moko 173: char *port_cstr=lsplit(host, ':');
! 174: int port=port_cstr?strtol(port_cstr, &error_pos, 0):0;
! 175:
1.32 misha 176: while(options){
177: if(char *key=lsplit(&options, '&')){
178: if(*key){
179: if(char *value=lsplit(key, '=')){
1.33 misha 180: if(strcmp(key, "ClientCharset")==0){ // transcoding with parser
1.21 paf 181: toupper_str(value, value, strlen(value));
1.33 misha 182: connection.client_charset=value;
1.32 misha 183: } else if(strcasecmp(key, "charset")==0){ // transcoding with server
1.33 misha 184: charset=value;
1.32 misha 185: } else if(strcasecmp(key, "timeout")==0){
1.2 parser 186: unsigned int timeout=(unsigned int)atoi(value);
1.14 paf 187: if(mysql_options(connection.handle, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout)!=0)
188: services._throw(mysql_error(connection.handle));
1.32 misha 189: } else if(strcasecmp(key, "compress")==0){
1.2 parser 190: if(atoi(value))
1.14 paf 191: if(mysql_options(connection.handle, MYSQL_OPT_COMPRESS, 0)!=0)
192: services._throw(mysql_error(connection.handle));
1.32 misha 193: } else if(strcasecmp(key, "named_pipe")==0){
1.2 parser 194: if(atoi(value))
1.33 misha 195: if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE, 0)!=0)
1.14 paf 196: services._throw(mysql_error(connection.handle));
1.39 misha 197: } else if(strcasecmp(key, "local_infile")==0){
198: if(atoi(value))
199: if(mysql_options(connection.handle, MYSQL_OPT_LOCAL_INFILE, 0)!=0)
200: services._throw(mysql_error(connection.handle));
1.32 misha 201: } else if(strcasecmp(key, "autocommit")==0){
1.31 misha 202: if(atoi(value)==0)
1.14 paf 203: connection.autocommit=false;
1.34 misha 204: } else if(strcasecmp(key, "multi_statements")==0){
205: if(atoi(value)!=0)
206: client_flag=CLIENT_MULTI_STATEMENTS;
1.2 parser 207: } else
208: services._throw("unknown connect option" /*key*/);
209: } else
210: services._throw("connect option without =value" /*key*/);
211: }
212: }
213: }
214:
1.52 ! moko 215: if(!mysql_real_connect(connection.handle, host, user, pwd, db, port, unix_socket, CLIENT_MULTI_RESULTS)){
1.14 paf 216: services._throw(mysql_error(connection.handle));
1.33 misha 217: }
1.1 parser 218:
1.33 misha 219: if(charset){
1.47 moko 220: char statement[MAX_STRING+1]="SET NAMES ";
1.33 misha 221: strncat(statement, charset, MAX_STRING);
222: _exec(connection, statement);
1.1 parser 223: }
224:
1.14 paf 225: if(!connection.autocommit)
1.33 misha 226: _exec(connection, "SET AUTOCOMMIT=0");
1.1 parser 227: }
1.14 paf 228:
229: void disconnect(void *aconnection) {
230: Connection& connection=*static_cast<Connection*>(aconnection);
231: mysql_close(connection.handle);
1.17 paf 232: connection.handle=0;
1.1 parser 233: }
1.32 misha 234:
1.15 paf 235: void commit(void *aconnection) {
1.14 paf 236: Connection& connection=*static_cast<Connection*>(aconnection);
237: if(!connection.autocommit)
1.33 misha 238: _exec(connection, "COMMIT");
1.14 paf 239: }
1.32 misha 240:
1.15 paf 241: void rollback(void *aconnection) {
1.14 paf 242: Connection& connection=*static_cast<Connection*>(aconnection);
243: if(!connection.autocommit)
1.33 misha 244: _exec(connection, "ROLLBACK");
1.14 paf 245: }
246:
1.15 paf 247: bool ping(void *aconnection) {
1.14 paf 248: Connection& connection=*static_cast<Connection*>(aconnection);
249: return mysql_ping(connection.handle)==0;
1.1 parser 250: }
251:
1.37 moko 252: // charset here is services.request_charset(), not connection.client_charset
253: // thus we can't use the sql server quoting support
254: const char* quote(void *aconnection, const char *str, unsigned int length) {
255: const char* from;
256: const char* from_end=str+length;
257:
258: size_t quoted=0;
259:
260: for(from=str; from<from_end; from++){
261: switch (*from) {
262: case 0:
263: case '\n':
264: case '\r':
265: case '\032':
266: case '\\':
267: case '\'':
268: case '"':
269: quoted++;
270: }
271: }
272:
273: if(!quoted)
274: return str;
275:
1.15 paf 276: Connection& connection=*static_cast<Connection*>(aconnection);
1.37 moko 277: char *result=(char*)connection.services->malloc_atomic(length + quoted + 1);
278: char *to = result;
279:
280: for(from=str; from<from_end; from++){
281: char escape;
282: switch (*from) {
283: case 0:
284: escape= '0';
285: break;
286: case '\n':
287: escape= 'n';
288: break;
289: case '\r':
290: escape= 'r';
291: break;
292: case '\032':
293: escape= 'Z';
294: break;
295: case '\\':
296: case '\'':
297: case '"':
298: escape= *from;
299: break;
300: default:
301: *to++=*from;
302: continue;
303: }
304: *to++= '\\';
305: *to++= escape;
306: }
307:
308: *to=0;
1.13 paf 309: return result;
1.1 parser 310: }
1.33 misha 311:
1.24 paf 312: void query(void *aconnection,
1.33 misha 313: const char *astatement,
314: size_t placeholders_count, Placeholder* placeholders,
315: unsigned long offset, unsigned long limit,
316: SQL_Driver_query_event_handlers& handlers
317: ){
1.14 paf 318: Connection& connection=*static_cast<Connection*>(aconnection);
1.15 paf 319: SQL_Driver_services& services=*connection.services;
1.1 parser 320: MYSQL_RES *res=NULL;
1.24 paf 321:
322: if(placeholders_count>0)
323: services._throw("bind variables not supported (yet)");
1.1 parser 324:
1.33 misha 325: bool transcode_needed=_transcode_required(connection);
326:
1.38 misha 327: size_t statement_size=0;
328:
329: if(transcode_needed) {
330: statement_size=strlen(astatement);
331: // transcode query from $request:charset to ?ClientCharset
332: services.transcode(astatement, statement_size,
333: astatement, statement_size,
1.19 paf 334: services.request_charset(),
1.33 misha 335: connection.client_charset);
1.19 paf 336: }
337:
1.1 parser 338: const char *statement;
1.38 misha 339: if(offset || limit!=SQL_NO_LIMIT) {
340: if(!statement_size)
341: statement_size=strlen(astatement);
1.13 paf 342: char *statement_limited=(char *)services.malloc_atomic(
1.33 misha 343: statement_size+MAX_NUMBER*2+8/* LIMIT #,#*/+1);
1.1 parser 344: char *cur=statement_limited;
345: memcpy(cur, astatement, statement_size); cur+=statement_size;
1.33 misha 346: cur+=sprintf(cur, " LIMIT ");
1.1 parser 347: if(offset)
1.42 moko 348: cur+=snprintf(cur, MAX_NUMBER, "%lu,", offset);
1.33 misha 349: if(limit!=SQL_NO_LIMIT)
1.41 moko 350: cur+=snprintf(cur, MAX_NUMBER, "%lu", limit);
1.1 parser 351: statement=statement_limited;
352: } else
353: statement=astatement;
354:
1.14 paf 355: if(mysql_query(connection.handle, statement))
1.33 misha 356: _throw(connection, mysql_error(connection.handle));
1.38 misha 357: if(!(res=mysql_store_result(connection.handle)) && mysql_field_count(connection.handle))
1.33 misha 358: _throw(connection, mysql_error(connection.handle));
1.1 parser 359: if(!res) // empty result: insert|delete|update|...
360: return;
1.38 misha 361:
362: size_t column_count=mysql_num_fields(res);
1.1 parser 363: if(!column_count) // old client
1.14 paf 364: column_count=mysql_field_count(connection.handle);
1.1 parser 365:
1.33 misha 366: if(!column_count){
1.1 parser 367: mysql_free_result(res);
368: services._throw("result contains no columns");
369: }
1.38 misha 370:
1.9 paf 371: bool failed=false;
372: SQL_Error sql_error;
1.38 misha 373:
374: #define CHECK(afailed) \
375: if(afailed) { \
376: failed=true; \
1.9 paf 377: goto cleanup; \
378: }
379:
1.44 moko 380: #define DO_FETCH_FIELDS(transcode_column_name) { \
381: for(size_t i=0; i<column_count; i++) { \
382: if(MYSQL_FIELD *field = mysql_fetch_field(res)){ \
383: size_t length=field->name_length; \
384: const char* str=strdup(services, field->name, length); \
385: transcode_column_name \
386: CHECK(handlers.add_column(sql_error, str, length)); \
387: } else { \
388: /* seen broken client, that reported "44" column count for "select 2+2" */ \
389: column_count=i; \
390: break; \
391: } \
392: } \
393: }
394:
395: #define DO_FETCH_ROWS(transcode_cell_value) { \
396: while(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { \
397: CHECK(handlers.add_row(sql_error)); \
398: unsigned long *lengths=mysql_fetch_lengths(res); \
399: for(size_t i=0; i<column_count; i++) { \
400: const char* str=0; \
401: size_t length=lengths[i]; \
402: if(length) { \
403: str=strdup(services, mysql_row[i], length); \
404: transcode_cell_value \
405: } \
406: CHECK(handlers.add_row_cell(sql_error, str, length)); \
407: } \
408: } \
1.38 misha 409: }
410:
411: bool* transcode_column=0;
412: if(transcode_needed) {
413: transcode_column = new bool[column_count];
414: DO_FETCH_FIELDS(
1.44 moko 415: transcode_column[i] = is_column_transcode_required(field->type);
1.38 misha 416: // transcode column's name from ?ClientCharset to $request:charset
417: services.transcode(str, length,
418: str, length,
419: connection.client_charset,
420: services.request_charset());
421: )
422: CHECK(handlers.before_rows(sql_error));
423: DO_FETCH_ROWS(
424: if(transcode_column[i])
425: // transcode cell's value from ?ClientCharset to $request:charset
1.19 paf 426: services.transcode(str, length,
427: str, length,
1.33 misha 428: connection.client_charset,
1.19 paf 429: services.request_charset());
1.38 misha 430: )
431: } else {
432: // without transcoding
433: DO_FETCH_FIELDS()
434: CHECK(handlers.before_rows(sql_error));
435: DO_FETCH_ROWS()
1.32 misha 436: }
1.9 paf 437: cleanup:
1.38 misha 438: if(transcode_column)
439: delete transcode_column;
1.1 parser 440: mysql_free_result(res);
1.9 paf 441: if(failed)
442: services._throw(sql_error);
1.1 parser 443: }
444:
1.33 misha 445: private:
446: void _exec(Connection& connection, const char* statement) {
447: if(mysql_query(connection.handle, statement))
448: _throw(connection, mysql_error(connection.handle));
449: (*mysql_store_result)(connection.handle); // throw out the result [don't need but must call]
450: }
451:
452: void _throw(Connection& connection, const char* aerr_msg) {
453: size_t length=strlen(aerr_msg);
454: if(length && _transcode_required(connection)) {
455: connection.services->transcode(aerr_msg, length,
456: aerr_msg, length,
457: connection.client_charset,
458: connection.services->request_charset());
459: }
460: connection.services->_throw(aerr_msg);
461: }
462:
463: bool _transcode_required(Connection& connection){
464: return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0);
465: }
466:
1.1 parser 467: private: // mysql client library funcs
468:
1.52 ! moko 469: typedef MYSQL* (STDCALL *t_mysql_init)(MYSQL *); t_mysql_init mysql_init;
! 470:
! 471: typedef void (STDCALL *t_mysql_server_end)(); t_mysql_server_end mysql_server_end;
! 472:
! 473: typedef int (STDCALL *t_mysql_options)(MYSQL *mysql, enum mysql_option option, const char *arg); t_mysql_options mysql_options;
1.29 misha 474:
1.1 parser 475: typedef MYSQL_RES* (STDCALL *t_mysql_store_result)(MYSQL *); t_mysql_store_result mysql_store_result;
1.52 ! moko 476:
! 477: typedef int (STDCALL *t_mysql_query)(MYSQL *, const char *q); t_mysql_query mysql_query;
! 478:
! 479: typedef char* (STDCALL *t_mysql_error)(MYSQL *); t_mysql_error mysql_error;
1.1 parser 480: static char* STDCALL subst_mysql_error(MYSQL *mysql) { return (mysql)->net.last_error; }
481:
1.52 ! moko 482: 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;
! 483:
! 484: typedef void (STDCALL *t_mysql_close)(MYSQL *); t_mysql_close mysql_close;
! 485:
! 486: typedef int (STDCALL *t_mysql_ping)(MYSQL *); t_mysql_ping mysql_ping;
! 487:
! 488: typedef unsigned long (STDCALL *t_mysql_escape_string)(char *to,const char *from, unsigned long from_length); t_mysql_escape_string mysql_escape_string;
! 489:
! 490: typedef void (STDCALL *t_mysql_free_result)(MYSQL_RES *result); t_mysql_free_result mysql_free_result;
! 491:
1.1 parser 492: typedef unsigned long* (STDCALL *t_mysql_fetch_lengths)(MYSQL_RES *result); t_mysql_fetch_lengths mysql_fetch_lengths;
493:
1.52 ! moko 494: typedef MYSQL_ROW (STDCALL *t_mysql_fetch_row)(MYSQL_RES *result); t_mysql_fetch_row mysql_fetch_row;
! 495: typedef MYSQL_FIELD* (STDCALL *t_mysql_fetch_field)(MYSQL_RES *result); t_mysql_fetch_field mysql_fetch_field;
! 496:
! 497: typedef unsigned int (STDCALL *t_mysql_num_fields)(MYSQL_RES *); t_mysql_num_fields mysql_num_fields;
! 498: typedef unsigned int (STDCALL *t_mysql_field_count)(MYSQL *); t_mysql_field_count mysql_field_count;
! 499:
! 500: static unsigned int STDCALL subst_mysql_num_fields(MYSQL_RES *res) { return res->field_count; }
! 501: static unsigned int STDCALL subst_mysql_field_count(MYSQL *mysql) { return mysql->field_count; }
1.1 parser 502:
503: private: // mysql client library funcs linking
504:
505: const char *dlink(const char *dlopen_file_spec) {
1.43 moko 506: if(lt_dlinit()){
507: if(const char* result=lt_dlerror())
508: return result;
509: return "can not prepare to dynamic loading";
510: }
1.25 paf 511:
1.33 misha 512: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
513:
514: if(!handle){
515: if(const char* result=lt_dlerror())
516: return result;
1.1 parser 517: return "can not open the dynamic link module";
1.25 paf 518: }
1.1 parser 519:
1.29 misha 520: #define GLINK(name) \
521: name=(t_##name)lt_dlsym(handle, #name);
522:
1.1 parser 523: #define DSLINK(name, action) \
1.29 misha 524: GLINK(name) \
1.1 parser 525: if(!name) \
526: action;
527:
528: #define DLINK(name) DSLINK(name, return "function " #name " was not found")
529: #define SLINK(name) DSLINK(name, name=subst_##name)
530:
531: DLINK(mysql_init);
1.29 misha 532: GLINK(mysql_server_end);
1.2 parser 533: DLINK(mysql_options);
1.1 parser 534: DLINK(mysql_store_result);
535: DLINK(mysql_query);
536: SLINK(mysql_error);
537: DLINK(mysql_real_connect);
538: DLINK(mysql_close);
539: DLINK(mysql_ping);
540: DLINK(mysql_escape_string);
541: DLINK(mysql_free_result);
542: DLINK(mysql_fetch_lengths);
543: DLINK(mysql_fetch_row);
1.44 moko 544: DLINK(mysql_fetch_field);
1.1 parser 545: SLINK(mysql_num_fields);
546: SLINK(mysql_field_count);
547: return 0;
548: }
549:
550: };
551:
552: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
1.29 misha 553: static MySQL_Driver Driver;
554: return &Driver;
1.1 parser 555: }
E-mail: