--- sql/mysql/parser3mysql.C 2001/09/21 15:40:55 1.1 +++ sql/mysql/parser3mysql.C 2004/01/26 15:09:02 1.16 @@ -1,13 +1,16 @@ /** @file Parser MySQL driver. - Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) + Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Author: Alexandr Petrosian (http://paf.design.ru) 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" + 3.23.42 & 4.0.0.alfa never worked, both subst & .sl version returned 0 */ -static const char *RCSId="$Id: parser3mysql.C,v 1.1 2001/09/21 15:40:55 parser Exp $"; +static const char *RCSId="$Id: parser3mysql.C,v 1.16 2004/01/26 15:09:02 paf Exp $"; #include "config_includes.h" @@ -22,6 +25,7 @@ static const char *RCSId="$Id: parser3my #if _MSC_VER # define snprintf _snprintf +# define strcasecmp _stricmp #endif static char *lsplit(char *string, char delim) { @@ -35,6 +39,20 @@ static char *lsplit(char *string, char d return 0; } +static char *lsplit(char **string_ref, char delim) { + char *result=*string_ref; + char *next=lsplit(*string_ref, delim); + *string_ref=next; + return result; +} + +struct Connection { + SQL_Driver_services* services; + + MYSQL* handle; + bool autocommit; +}; + /** MySQL server driver */ @@ -47,13 +65,17 @@ public: /// get api version int api_version() { return SQL_DRIVER_API_VERSION; } /// initialize driver by loading sql dynamic link library - const char *initialize(const char *dlopen_file_spec) { + const char *initialize(char *dlopen_file_spec) { return dlopen_file_spec? dlink(dlopen_file_spec):"client library column is empty"; } /** connect @param used_only_in_connect_url - format: @b user:pass@host[:port]|[/unix/socket]/database/charset + 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 @@ -61,7 +83,7 @@ public: void connect( char *used_only_in_connect_url, SQL_Driver_services& services, - void **connection ///< output: MYSQL * + void **connection_ref ///< output: Connection* ) { char *user=used_only_in_connect_url; char *s=lsplit(user, '@'); @@ -78,60 +100,116 @@ public: char *error_pos=0; char *port_cstr=lsplit(host, ':'); int port=port_cstr?strtol(port_cstr, &error_pos, 0):0; - char *charset=lsplit(db, '/'); + char *options=lsplit(db, '?'); + + char *charset=0; + + Connection& connection=*(Connection *)::calloc(sizeof(Connection), 1); + *connection_ref=&connection; + connection.services=&services; + connection.handle=mysql_init(NULL); + connection.autocommit=true; + + while(options) { + if(char *key=lsplit(&options, '&')) { + if(*key) { + if(char *value=lsplit(key, '=')) { + if(strcasecmp(key, "charset")==0) { + charset=value; + } else if(strcasecmp(key, "timeout")==0) { + unsigned int timeout=(unsigned int)atoi(value); + if(mysql_options(connection.handle, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout)!=0) + services._throw(mysql_error(connection.handle)); + } else if(strcasecmp(key, "compress")==0) { + if(atoi(value)) + if(mysql_options(connection.handle, MYSQL_OPT_COMPRESS, 0)!=0) + services._throw(mysql_error(connection.handle)); + } else if(strcasecmp(key, "named_pipe")==0) { + if(atoi(value)) + if(mysql_options(connection.handle, MYSQL_OPT_NAMED_PIPE , 0)!=0) + services._throw(mysql_error(connection.handle)); + } else if(strcasecmp(key, "autocommit")==0) { + if(atoi(value)==0) { + connection.autocommit=false; + } + } else + services._throw("unknown connect option" /*key*/); + } else + services._throw("connect option without =value" /*key*/); + } + } + } - MYSQL *mysql=mysql_init(NULL); - if(!mysql_real_connect(mysql, + if(!mysql_real_connect(connection.handle, host, user, pwd, db, port?port:MYSQL_PORT, unix_socket, 0)) - services._throw(mysql_error(mysql)); + services._throw(mysql_error(connection.handle)); - if(charset) { + if(charset) { // set charset char statement[MAX_STRING]="set CHARACTER SET "; // cp1251_koi8 strncat(statement, charset, MAX_STRING); - if(mysql_query(mysql, statement)) - services._throw(mysql_error(mysql)); - (*mysql_store_result)(mysql); // throw out the result [don't need but must call] + exec(connection, statement); } - *(MYSQL **)connection=mysql; + if(!connection.autocommit) + exec(connection, "set autocommit=0"); + } + + void exec(Connection& connection, const char* statement) { + if(mysql_query(connection.handle, statement)) + connection.services->_throw(mysql_error(connection.handle)); + (*mysql_store_result)(connection.handle); // throw out the result [don't need but must call] + } + + void disconnect(void *aconnection) { + Connection& connection=*static_cast(aconnection); + + mysql_close(connection.handle); } - void disconnect(void *connection) { - mysql_close((MYSQL *)connection); + void commit(void *aconnection) { + Connection& connection=*static_cast(aconnection); + + if(!connection.autocommit) + exec(connection, "commit"); } - void commit(SQL_Driver_services&, void *) {} - void rollback(SQL_Driver_services&, void *) {} + void rollback(void *aconnection) { + Connection& connection=*static_cast(aconnection); - bool ping(SQL_Driver_services&, void *connection) { - return mysql_ping((MYSQL *)connection)==0; + if(!connection.autocommit) + exec(connection, "rollback"); } - unsigned int quote( - SQL_Driver_services&, void *connection, - char *to, const char *from, unsigned int length) { + bool ping(void *aconnection) { + Connection& connection=*static_cast(aconnection); + + return mysql_ping(connection.handle)==0; + } + + const char* quote(void *aconnection, const char *from, unsigned int length) { + Connection& connection=*static_cast(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.) - - it's already UNTAINT_TIMES_BIGGER */ - return (*mysql_escape_string)(to, from, length); + char *result=(char*)connection.services->malloc_atomic(length*2+1); + mysql_escape_string(result, from, length); + return result; } void query( - SQL_Driver_services& services, void *connection, + void *aconnection, const char *astatement, unsigned long offset, unsigned long limit, SQL_Driver_query_event_handlers& handlers) { - - MYSQL *mysql=(MYSQL *)connection; + Connection& connection=*static_cast(aconnection); + SQL_Driver_services& services=*connection.services; MYSQL_RES *res=NULL; const char *statement; if(offset || limit) { size_t statement_size=strlen(astatement); - char *statement_limited=(char *)services.malloc( + char *statement_limited=(char *)services.malloc_atomic( statement_size+MAX_NUMBER*2+8/* limit #,#*/+1); char *cur=statement_limited; memcpy(cur, astatement, statement_size); cur+=statement_size; @@ -144,56 +222,73 @@ public: } else statement=astatement; - if(mysql_query(mysql, statement)) - services._throw(mysql_error(mysql)); - if(!(res=mysql_store_result(mysql)) && mysql_field_count(mysql)) - services._throw(mysql_error(mysql)); + if(mysql_query(connection.handle, statement)) + services._throw(mysql_error(connection.handle)); + if(!(res=mysql_store_result(connection.handle)) && mysql_field_count(connection.handle)) + services._throw(mysql_error(connection.handle)); if(!res) // empty result: insert|delete|update|... return; int column_count=mysql_num_fields(res); if(!column_count) // old client - column_count=mysql_field_count(mysql); + column_count=mysql_field_count(connection.handle); if(!column_count) { mysql_free_result(res); services._throw("result contains no columns"); } + + bool failed=false; + SQL_Error sql_error; +#define CHECK(afailed) \ + if(afailed) { \ + failed=true; \ + goto cleanup; \ + } for(int i=0; iname); - void *ptr=services.malloc(size); - memcpy(ptr, field->name, size); - handlers.add_column(ptr, size); + if(MYSQL_FIELD *field=mysql_fetch_field(res)) { + size_t length=strlen(field->name); + char* str=(char*)services.malloc_atomic(length+1); + memcpy(str, field->name, length+1); + 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; + } } - handlers.before_rows(); + CHECK(handlers.before_rows(sql_error)); - if(unsigned long row_count=(unsigned long)mysql_num_rows(res)) - for(unsigned long r=0; rrow_count; } - typedef unsigned int (STDCALL *t_mysql_num_fields)(MYSQL_RES *); t_mysql_num_fields mysql_num_fields; static unsigned int STDCALL subst_mysql_num_fields(MYSQL_RES *res) { return res->field_count; } @@ -236,6 +328,8 @@ private: // mysql client library funcs private: // mysql client library funcs linking const char *dlink(const char *dlopen_file_spec) { + if(lt_dlinit()) + return lt_dlerror(); lt_dlhandle handle=lt_dlopen(dlopen_file_spec); if (!handle) return "can not open the dynamic link module"; @@ -249,6 +343,7 @@ private: // mysql client library funcs l #define SLINK(name) DSLINK(name, name=subst_##name) DLINK(mysql_init); + DLINK(mysql_options); DLINK(mysql_store_result); DLINK(mysql_query); SLINK(mysql_error); @@ -260,7 +355,6 @@ private: // mysql client library funcs l DLINK(mysql_fetch_lengths); DLINK(mysql_fetch_row); DLINK(mysql_fetch_field); - SLINK(mysql_num_rows); SLINK(mysql_num_fields); SLINK(mysql_field_count); return 0;