--- sql/mysql/parser3mysql.C 2004/06/23 07:32:06 1.24 +++ 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.24 2004/06/23 07:32:06 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,6 +46,17 @@ static char *lsplit(char **string_ref, c return result; } +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 @@ -83,8 +100,10 @@ public: 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 *url, @@ -92,7 +111,7 @@ public: void **connection_ref ///< output: Connection* ) { char *user=url; - char *s=lsplit(user, '@'); + 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) { + 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); @@ -335,6 +357,8 @@ private: // mysql client library funcs typedef MYSQL* (STDCALL *t_mysql_init)(MYSQL *); t_mysql_init mysql_init; + typedef void (STDCALL *t_mysql_server_end)(); t_mysql_server_end mysql_server_end; + typedef int (STDCALL *t_mysql_options)(MYSQL *mysql, enum mysql_option option, const char *arg); t_mysql_options mysql_options; typedef MYSQL_RES* (STDCALL *t_mysql_store_result)(MYSQL *); t_mysql_store_result mysql_store_result; @@ -379,11 +403,18 @@ private: // mysql client library funcs l if(lt_dlinit()) return lt_dlerror(); lt_dlhandle handle=lt_dlopen(dlopen_file_spec); - if (!handle) + if (!handle) { + if(const char* result=lt_dlerror()) + return result; + return "can not open the dynamic link module"; + } + + #define GLINK(name) \ + name=(t_##name)lt_dlsym(handle, #name); #define DSLINK(name, action) \ - name=(t_##name)lt_dlsym(handle, #name); \ + GLINK(name) \ if(!name) \ action; @@ -391,6 +422,7 @@ private: // mysql client library funcs l #define SLINK(name) DSLINK(name, name=subst_##name) DLINK(mysql_init); + GLINK(mysql_server_end); DLINK(mysql_options); DLINK(mysql_store_result); DLINK(mysql_query); @@ -411,5 +443,6 @@ private: // mysql client library funcs l }; extern "C" SQL_Driver *SQL_DRIVER_CREATE() { - return new MySQL_Driver(); + static MySQL_Driver Driver; + return &Driver; }