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