--- sql/mysql/parser3mysql.C 2009/04/08 11:13:00 1.35 +++ sql/mysql/parser3mysql.C 2016/10/11 13:38:10 1.49 @@ -1,21 +1,22 @@ /** @file Parser MySQL driver. - Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - 2001.07.30 using MySQL 3.23.22b + 2001-07-30 using MySQL 3.23.22b - 2001.11.06 numrows on "HP-UX istok1 B.11.00 A 9000/869 448594332 two-user license" + 2001-11-06 numrows on "HP-UX istok1 B.11.00 A 9000/869 448594332 two-user license" 3.23.42 & 4.0.0.alfa never worked, both subst & .sl version returned 0 */ -static const char *RCSId="$Id: parser3mysql.C,v 1.35 2009/04/08 11:13:00 misha Exp $"; #include "config_includes.h" #include "pa_sql_driver.h" +volatile const char * IDENT_PARSER3MYSQL_C="$Id: parser3mysql.C,v 1.49 2016/10/11 13:38:10 moko Exp $" IDENT_PA_SQL_DRIVER_H; + #define NO_CLIENT_LONG_LONG #include "mysql.h" #include "ltdl.h" @@ -29,14 +30,16 @@ static const char *RCSId="$Id: parser3my #endif // for mysql < 4.1 +#if !defined(CLIENT_MULTI_RESULTS) || !defined(CLIENT_MULTI_STATEMENTS) +# define OLD_MYSQL_CLIENT 1 +#endif + #ifndef CLIENT_MULTI_RESULTS -#define CLIENT_MULTI_RESULTS (1UL << 17) -#define OLD_MYSQL_CLIENT 1 +# define CLIENT_MULTI_RESULTS (1UL << 17) #endif + #ifndef CLIENT_MULTI_STATEMENTS -#define CLIENT_MULTI_STATEMENTS (1UL << 16) -#undef OLD_MYSQL_CLIENT -#define OLD_MYSQL_CLIENT 1 +# define CLIENT_MULTI_STATEMENTS (1UL << 16) #endif static char *lsplit(char *string, char delim){ @@ -71,6 +74,51 @@ static void toupper_str(char *out, const *out++=(char)toupper(*in++); } +inline static bool is_column_transcode_required(enum_field_types type) { + switch(type) { + case MYSQL_TYPE_NULL: + +#ifdef FIELD_TYPE_NEWDECIMAL + case MYSQL_TYPE_NEWDECIMAL: +#endif + case MYSQL_TYPE_DECIMAL: + case MYSQL_TYPE_FLOAT: + case MYSQL_TYPE_DOUBLE: + + case MYSQL_TYPE_TINY: + case MYSQL_TYPE_SHORT: + case MYSQL_TYPE_LONG: + case MYSQL_TYPE_LONGLONG: + case MYSQL_TYPE_INT24: +#ifdef FIELD_TYPE_BIT + case MYSQL_TYPE_BIT: +#endif + + case MYSQL_TYPE_DATE: + case MYSQL_TYPE_NEWDATE: + case MYSQL_TYPE_TIME: + case MYSQL_TYPE_DATETIME: + case MYSQL_TYPE_YEAR: + case MYSQL_TYPE_TIMESTAMP: + + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + return false; + break; + default: + return true; + } +} + +inline static const char* strdup(SQL_Driver_services& services, char* str, size_t length) { + char *strm=(char*)services.malloc_atomic(length+1); + memcpy(strm, str, length); + strm[length]=0; + return (const char*)strm; +} + struct Connection { SQL_Driver_services* services; @@ -88,13 +136,6 @@ public: MySQL_Driver() : SQL_Driver() {} -#ifndef FREEBSD4 - ~MySQL_Driver() { - if(mysql_server_end) - mysql_server_end(); - } -#endif - /// get api version int api_version() { return SQL_DRIVER_API_VERSION; } @@ -107,7 +148,7 @@ public: /** connect @param url format: @b user:pass@host[:port]|[/unix/socket]/database? - charset=value& // transcode by server with command 'SET CHARACTER SET value' + charset=value& // transcode by server with command 'SET NAMES value' ClientCharset=charset& // transcode by parser timeout=3& compress=0& @@ -178,6 +219,10 @@ public: if(atoi(value)) if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE, 0)!=0) services._throw(mysql_error(connection.handle)); + } else if(strcasecmp(key, "local_infile")==0){ + if(atoi(value)) + if(mysql_options(connection.handle, MYSQL_OPT_LOCAL_INFILE, 0)!=0) + services._throw(mysql_error(connection.handle)); } else if(strcasecmp(key, "autocommit")==0){ if(atoi(value)==0) connection.autocommit=false; @@ -219,7 +264,7 @@ public: } if(charset){ - char statement[MAX_STRING]="SET CHARACTER SET "; + char statement[MAX_STRING+1]="SET NAMES "; strncat(statement, charset, MAX_STRING); _exec(connection, statement); } @@ -251,16 +296,63 @@ public: return mysql_ping(connection.handle)==0; } - const char* quote(void *aconnection, const char *from, unsigned int length) { + // charset here is services.request_charset(), not connection.client_charset + // thus we can't use the sql server quoting support + const char* quote(void *aconnection, const char *str, unsigned int length) { + const char* from; + const char* from_end=str+length; + + size_t quoted=0; + + for(from=str; from(aconnection); - /* - 3.23.22b - You must allocate the to buffer to be at least length*2+1 bytes long. - (In the worse case, each character may need to be encoded as using two bytes, - and you need room for the terminating null byte.) - */ - char *result=(char*)connection.services->malloc_atomic(length*2+1); - mysql_escape_string(result, from, length); + char *result=(char*)connection.services->malloc_atomic(length + quoted + 1); + char *to = result; + + for(from=str; fromname); - char* strm=(char*)services.malloc_atomic(length+1); - memcpy(strm, field->name, length+1); - const char* str=strm; - - // transcode column name from ?ClientCharset to $request:charset - if(transcode_needed){ +#define DO_FETCH_FIELDS(transcode_column_name) { \ + for(size_t i=0; iname_length; \ + const char* str=strdup(services, field->name, length); \ + transcode_column_name \ + CHECK(handlers.add_column(sql_error, str, length)); \ + } else { \ + /* seen broken client, that reported "44" column count for "select 2+2" */ \ + column_count=i; \ + break; \ + } \ + } \ + } + +#define DO_FETCH_ROWS(transcode_cell_value) { \ + while(MYSQL_ROW mysql_row=mysql_fetch_row(res)) { \ + CHECK(handlers.add_row(sql_error)); \ + unsigned long *lengths=mysql_fetch_lengths(res); \ + for(size_t i=0; itype); + // transcode column's name from ?ClientCharset to $request:charset + services.transcode(str, length, + str, length, + connection.client_charset, + services.request_charset()); + ) + CHECK(handlers.before_rows(sql_error)); + DO_FETCH_ROWS( + if(transcode_column[i]) + // transcode cell's value from ?ClientCharset to $request:charset services.transcode(str, length, str, length, connection.client_charset, services.request_charset()); - } - - CHECK(handlers.add_column(sql_error, str, length)); - } else { - // seen some broken client, - // which reported "44" for column count of response to "select 2+2" - column_count=i; - break; - } - } - - CHECK(handlers.before_rows(sql_error)); - - while(MYSQL_ROW mysql_row=mysql_fetch_row(res)){ - CHECK(handlers.add_row(sql_error)); - unsigned long *lengths=mysql_fetch_lengths(res); - for(int i=0; i