--- sql/mysql/parser3mysql.C 2004/03/04 10:46:52 1.19 +++ sql/mysql/parser3mysql.C 2008/05/04 08:29:46 1.31 @@ -10,7 +10,7 @@ 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.19 2004/03/04 10:46:52 paf Exp $"; +static const char *RCSId="$Id: parser3mysql.C,v 1.31 2008/05/04 08:29:46 misha Exp $"; #include "config_includes.h" @@ -46,7 +46,18 @@ static char *lsplit(char **string_ref, c return result; } -static void toupper(char *out, const char *in, size_t size) { +static char* rsplit(char* string, char delim) { + if(string) { + char* v=strrchr(string, delim); + if(v) { + *v=0; + return v+1; + } + } + return NULL; +} + +static void toupper_str(char *out, const char *in, size_t size) { while(size--) *out++=(char)toupper(*in++); } @@ -57,15 +68,21 @@ struct Connection { MYSQL* handle; const char* cstrClientCharset; bool autocommit; + bool multi_statements; }; + /** MySQL server driver */ class MySQL_Driver : public SQL_Driver { public: - MySQL_Driver() : SQL_Driver() { + MySQL_Driver() : SQL_Driver() {} + + ~MySQL_Driver() { + if(mysql_server_end) + mysql_server_end(); } /// get api version @@ -76,23 +93,25 @@ public: dlink(dlopen_file_spec):"client library column is empty"; } /** connect - @param used_only_in_connect_url + @param url format: @b user:pass@host[:port]|[/unix/socket]/database? charset=cp1251_koi8& timeout=3& compress=1& named_pipe=1 3.23.22b - Currently the only option for @b character_set_name is cp1251_koi8. - WARNING: must be used only to connect, for buffer doesn't live long + Currently the only option for @b character_set_name is cp1251_koi8. + WARNING: must be used only to connect, for buffer doesn't live long + 4.1+ accept not only 'cp1251_koi8' but 'cp1251', 'utf8' and much more + it can be usable for transcoding using sql server */ void connect( - char *used_only_in_connect_url, + char *url, SQL_Driver_services& services, void **connection_ref ///< output: Connection* ) { - char *user=used_only_in_connect_url; - char *s=lsplit(user, '@'); + char *user=url; + char *s=rsplit(user, '@'); char *host=0; char *unix_socket=0; if(s && s[0]=='[') { // unix socket @@ -115,16 +134,17 @@ public: connection.handle=mysql_init(NULL); connection.cstrClientCharset=0; connection.autocommit=true; + connection.multi_statements=false; *connection_ref=&connection; while(options) { if(char *key=lsplit(&options, '&')) { if(*key) { if(char *value=lsplit(key, '=')) { - if(strcmp(key, "ClientCharset" ) == 0) { - toupper(value, value, strlen(value)); + if(strcmp(key, "ClientCharset" ) == 0) { // transcoding with parser + toupper_str(value, value, strlen(value)); connection.cstrClientCharset=value; - } else if(strcasecmp(key, "charset")==0) { // left for backward compatibility, consider using ClientCharset + } else if(strcasecmp(key, "charset")==0) { // transcoding with mysql server cstrBackwardCompAskServerToTranscode=value; } else if(strcasecmp(key, "timeout")==0) { unsigned int timeout=(unsigned int)atoi(value); @@ -138,10 +158,12 @@ 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, "multi_statements")==0) { + if(atoi(value)!=0) + connection.multi_statements=true; } else if(strcasecmp(key, "autocommit")==0) { - if(atoi(value)==0) { + if(atoi(value)==0) connection.autocommit=false; - } } else services._throw("unknown connect option" /*key*/); } else @@ -150,17 +172,17 @@ public: } } - if(connection.cstrClientCharset && cstrBackwardCompAskServerToTranscode) - services._throw("use 'ClientCharset' option only, " - "'charset' option is obsolete and should not be used with new 'ClientCharset' option"); - + // if(connection.cstrClientCharset && cstrBackwardCompAskServerToTranscode) + // services._throw("use 'ClientCharset' option only, " + // "'charset' option is obsolete and should not be used with new 'ClientCharset' option"); + if(!mysql_real_connect(connection.handle, - host, user, pwd, db, port?port:MYSQL_PORT, unix_socket, 0)) + host, user, pwd, db, port?port:MYSQL_PORT, unix_socket, (connection.multi_statements) ? CLIENT_MULTI_STATEMENTS : CLIENT_MULTI_RESULTS)) services._throw(mysql_error(connection.handle)); if(cstrBackwardCompAskServerToTranscode) { // set charset - char statement[MAX_STRING]="set CHARACTER SET "; // cp1251_koi8 + char statement[MAX_STRING]="set CHARACTER SET "; strncat(statement, cstrBackwardCompAskServerToTranscode, MAX_STRING); exec(connection, statement); @@ -183,6 +205,7 @@ public: connection.handle=0; } void commit(void *aconnection) { + //_asm int 3; Connection& connection=*static_cast(aconnection); if(!connection.autocommit) @@ -213,16 +236,21 @@ public: mysql_escape_string(result, from, length); return result; } - void query( - void *aconnection, - const char *astatement, unsigned long offset, unsigned long limit, + void query(void *aconnection, + const char *astatement, + size_t placeholders_count, Placeholder* placeholders, + unsigned long offset, unsigned long limit, SQL_Driver_query_event_handlers& handlers) { Connection& connection=*static_cast(aconnection); SQL_Driver_services& services=*connection.services; + const char* cstrClientCharset=connection.cstrClientCharset; MYSQL_RES *res=NULL; + if(placeholders_count>0) + services._throw("bind variables not supported (yet)"); + // transcode from $request:charset to connect-string?client_charset - if(const char* cstrClientCharset=connection.cstrClientCharset) { + if(cstrClientCharset) { size_t transcoded_statement_size; services.transcode(astatement, strlen(astatement), astatement, transcoded_statement_size, @@ -272,12 +300,13 @@ public: for(int i=0; iname); - char* str=(char*)services.malloc_atomic(length+1); - memcpy(str, field->name, length+1); + size_t length=strlen(field->name); + char* strm=(char*)services.malloc_atomic(length+1); + memcpy(strm, field->name, length+1); + const char* str=strm; // transcode to $request:charset from connect-string?client_charset - if(const char* cstrClientCharset=connection.cstrClientCharset) { + if(cstrClientCharset) { services.transcode(str, length, str, length, cstrClientCharset, @@ -300,14 +329,15 @@ public: unsigned long *lengths=mysql_fetch_lengths(res); for(int i=0; i