|
|
| version 1.27, 2008/06/27 12:54:44 | version 1.32, 2008/07/01 10:21:03 |
|---|---|
| Line 44 static const char *RCSId="$Id$"; | Line 44 static const char *RCSId="$Id$"; |
| #ifndef strncasecmp | #ifndef strncasecmp |
| # define strncasecmp _strnicmp | # define strncasecmp _strnicmp |
| #endif | #endif |
| #ifndef strcasecmp | |
| # define strcasecmp _stricmp | |
| #endif | |
| static char *lsplit(char *string, char delim){ | static char *lsplit(char *string, char delim){ |
| if(string){ | if(string){ |
| Line 55 static char *lsplit(char *string, char d | Line 58 static char *lsplit(char *string, char d |
| return 0; | 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; | |
| } | |
| static void toupper_str(char *out, const char *in, size_t size){ | static void toupper_str(char *out, const char *in, size_t size){ |
| while(size--) | while(size--) |
| *out++=(char)toupper(*in++); | *out++=(char)toupper(*in++); |
| Line 66 struct Connection { | Line 76 struct Connection { |
| CDatabase* db; | CDatabase* db; |
| const char* client_charset; | const char* client_charset; |
| bool autocommit; | bool autocommit; |
| bool use_multi_row_fetch; | bool fast_offset_search; |
| }; | }; |
| /** | /** |
| Line 84 public: | Line 94 public: |
| const char *initialize(char *dlopen_file_spec) { return 0; } | const char *initialize(char *dlopen_file_spec) { return 0; } |
| /** connect | /** connect |
| @param url | @param url |
| format: @b DSN=dsn;UID=user;PWD=password (ODBC connect string) | format: @b DSN=dsn;UID=user;PWD=password? (ODBC connect string) |
| ;ClientCharset=charset // transcode with parser | ClientCharset=charset& // transcode with parser |
| ;MultiRowFetch=1 // 0 -- disable (slower) | autocommit=1& // 0 -- disable auto commit |
| ;autocommit=1 // 0 -- disable auto commit | FastOffsetSearch=0 |
| WARNING: must be used only to connect, for buffer doesn't live long | WARNING: must be used only to connect, for buffer doesn't live long |
| */ | */ |
| Line 100 public: | Line 110 public: |
| *connection_ref=&connection; | *connection_ref=&connection; |
| connection.services=&services; | connection.services=&services; |
| connection.client_charset=0; | connection.client_charset=0; |
| connection.use_multi_row_fetch=true; | connection.fast_offset_search=false; |
| connection.autocommit=true; | connection.autocommit=true; |
| size_t url_length=strlen(url); | size_t url_length=strlen(url); |
| char *options=lsplit(url, '?'); | |
| if(const char* key_start=strstr(url, "ClientCharset=")){ | // todo: analize connect string and if 'SQL Server' found, modify query and add TOP into SELECTs |
| const char* value_start=key_start+14/*strlen("ClientCharset=")*/; | |
| const char* value_end=strchr(value_start, ';'); | |
| if(!value_end) | |
| value_end=url+url_length; | |
| if(size_t value_length=value_end-value_start){ | |
| char* client_charset=(char*)services.malloc_atomic(value_length+1); | |
| toupper_str(client_charset, value_start, value_length); | |
| client_charset[value_length]=0; | |
| connection.client_charset=client_charset; | while(options){ |
| } | if(char *key=lsplit(&options, '&')){ |
| } | if(*key){ |
| if(const char* key_start=strstr(url, "MultiRowFetch=")){ | if(char *value=lsplit(key, '=')){ |
| const char* value_start=key_start+14/*strlen("MultiRowFetch=")*/; | if(strcmp(key, "ClientCharset")==0){ |
| const char* value_end=strchr(value_start, ';'); | toupper_str(value, value, strlen(value)); |
| if(!value_end) | connection.client_charset=value; |
| value_end=url+url_length; | } else if(strcasecmp(key, "autocommit")==0){ |
| if(atoi(value)==0) | |
| if(size_t value_length=value_end-value_start){ | connection.autocommit=false; |
| char* value=(char*)services.malloc_atomic(value_length+1); | } else if(strcmp(key, "FastOffsetSearch")==0){ |
| memcpy(value, value_start, value_length); | if(atoi(value)==1) |
| value[value_length]=0; | connection.fast_offset_search=true; |
| if(atoi(value)==0) | } else |
| connection.use_multi_row_fetch=false; | services._throw("unknown connect option" /*key*/); |
| } | } else |
| } | services._throw("connect option without =value" /*key*/); |
| if(const char* key_start=strstr(url, "autocommit=")){ | } |
| const char* value_start=key_start+11/*strlen("autocommit=")*/; | |
| const char* value_end=strchr(value_start, ';'); | |
| if(!value_end) | |
| value_end=url+url_length; | |
| if(size_t value_length=value_end-value_start){ | |
| char* value=(char*)services.malloc_atomic(value_length+1); | |
| memcpy(value, value_start, value_length); | |
| value[value_length]=0; | |
| if(atoi(value)==0) | |
| connection.autocommit=false; | |
| } | } |
| } | } |
| TRY { | TRY { |
| connection.db=new CDatabase(); | connection.db=new CDatabase(); |
| connection.db->OpenEx(url, CDatabase::noOdbcDialog); | connection.db->OpenEx(url, CDatabase::noOdbcDialog); |
| connection.db->BeginTrans(); | |
| if(!connection.autocommit) | |
| connection.db->BeginTrans(); | |
| } | } |
| CATCH_ALL (e) { | CATCH_ALL (e) { |
| _throw(services, e); | _throw(services, e); |
| Line 174 public: | Line 163 public: |
| void commit(void *aconnection){ | void commit(void *aconnection){ |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| if(!connection.autocommit){ | TRY |
| TRY | connection.db->CommitTrans(); |
| connection.db->CommitTrans(); | connection.db->BeginTrans(); |
| connection.db->BeginTrans(); | CATCH_ALL (e) { |
| CATCH_ALL (e) { | _throw(*connection.services, e); |
| _throw(*connection.services, e); | |
| } | |
| END_CATCH_ALL | |
| } | } |
| END_CATCH_ALL | |
| } | } |
| void rollback(void *aconnection){ | void rollback(void *aconnection){ |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| if(!connection.autocommit){ | TRY |
| TRY | connection.db->Rollback(); |
| connection.db->Rollback(); | connection.db->BeginTrans(); |
| connection.db->BeginTrans(); | CATCH_ALL (e) { |
| CATCH_ALL (e) { | _throw(*connection.services, e); |
| _throw(*connection.services, e); | |
| } | |
| END_CATCH_ALL | |
| } | } |
| END_CATCH_ALL | |
| } | } |
| bool ping(void *connection){ | bool ping(void *connection){ |
| Line 231 public: | Line 216 public: |
| if(placeholders_count>0) | if(placeholders_count>0) |
| services._throw("bind variables not supported (yet)"); | services._throw("bind variables not supported (yet)"); |
| bool transcode_needed=_transcode_required(connection); | const char* client_charset=connection.client_charset; |
| const char* request_charset=services.request_charset(); | |
| bool transcode_needed=(client_charset && strcmp(client_charset, request_charset)!=0); | |
| // transcode query from $request:charset to ?ClientCharset | // transcode query from $request:charset to ?ClientCharset |
| if(transcode_needed){ | if(transcode_needed){ |
| size_t length=strlen(statement); | size_t length=strlen(statement); |
| services.transcode(statement, length, | services.transcode(statement, length, |
| statement, length, | statement, length, |
| services.request_charset(), | request_charset, |
| connection.client_charset); | client_charset); |
| } | } |
| while(isspace((unsigned char)*statement)) | while(isspace((unsigned char)*statement)) |
| Line 266 public: | Line 253 public: |
| //CRecordset::useMultiRowFetch | //CRecordset::useMultiRowFetch |
| //CRecordset::userAllocMultiRowBuffers | //CRecordset::userAllocMultiRowBuffers |
| //CRecordset::useExtendedFetch | //CRecordset::useExtendedFetch |
| /* | /* |
| if(connection.use_multi_row_fetch){ | if(connection.fast_offset_search){ |
| options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers; | options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers; |
| } else { | } else { |
| options+=CRecordset::skipDeletedRecords; | options+=CRecordset::skipDeletedRecords; |
| } | } |
| */ | */ |
| TRY { | TRY { |
| rs.Open( | rs.Open( |
| (connection.use_multi_row_fetch)?CRecordset::dynamic:CRecordset::forwardOnly, | (connection.fast_offset_search)?CRecordset::dynamic:CRecordset::forwardOnly, |
| statement, | statement, |
| options | options |
| ); | ); |
| Line 300 public: | Line 287 public: |
| column_count=MAX_COLS; | column_count=MAX_COLS; |
| SWORD column_types[MAX_COLS]; | SWORD column_types[MAX_COLS]; |
| bool transcode_column[MAX_COLS]; | |
| SQL_Error sql_error; | SQL_Error sql_error; |
| #define CHECK(afailed) if(afailed) services._throw(sql_error) | #define CHECK(afailed) if(afailed) services._throw(sql_error) |
| Line 309 public: | Line 297 public: |
| CODBCFieldInfo fieldinfo; | CODBCFieldInfo fieldinfo; |
| rs.GetODBCFieldInfo(i, fieldinfo); | rs.GetODBCFieldInfo(i, fieldinfo); |
| column_types[i]=fieldinfo.m_nSQLType; | column_types[i]=fieldinfo.m_nSQLType; |
| switch(fieldinfo.m_nSQLType){ | |
| case SQL_NUMERIC: | |
| case SQL_DECIMAL: | |
| case SQL_INTEGER: | |
| case SQL_SMALLINT: | |
| case SQL_FLOAT: | |
| case SQL_REAL: | |
| case SQL_DOUBLE: | |
| case SQL_DATETIME: | |
| case SQL_SMALLDATETIME: | |
| case SQL_BIGINT: | |
| case SQL_TINYINT: | |
| transcode_column[i]=false; | |
| break; | |
| default: | |
| transcode_column[i]=transcode_needed; | |
| break; | |
| } | |
| size_t length=fieldinfo.m_strName.GetLength(); | size_t length=fieldinfo.m_strName.GetLength(); |
| char *str=0; | char *str=0; |
| if(length){ | if(length){ |
| Line 319 public: | Line 325 public: |
| if(transcode_needed){ | if(transcode_needed){ |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| connection.client_charset, | client_charset, |
| services.request_charset()); | request_charset); |
| } | } |
| } | } |
| CHECK(handlers.add_column(sql_error, str, length)); | CHECK(handlers.add_column(sql_error, str, length)); |
| Line 330 public: | Line 336 public: |
| // skip offset rows | // skip offset rows |
| if(offset){ | if(offset){ |
| if(connection.use_multi_row_fetch){ | if(connection.fast_offset_search){ |
| rs.Move(offset); | rs.Move(offset); |
| } else { | } else { |
| unsigned long row=offset; | unsigned long row=offset; |
| while(!rs.IsEOF() && row>0){ | while(!rs.IsEOF() && row--) |
| rs.MoveNext(); | rs.MoveNext(); |
| row--; | |
| } | |
| } | } |
| } | } |
| Line 351 public: | Line 355 public: |
| char* str; | char* str; |
| switch(column_types[i]){ | switch(column_types[i]){ |
| //case xBOOL: | //case xBOOL: |
| //case SQL_INTEGER: // serg@design.ru did that in parser2. test first! | |
| //case SQL_DATETIME: << default: handles that more properly (?) | //case SQL_DATETIME: << default: handles that more properly (?) |
| case SQL_BINARY: | case SQL_BINARY: |
| case SQL_VARBINARY: | case SQL_VARBINARY: |
| Line 369 public: | Line 372 public: |
| } | } |
| // transcode cell value from ?ClientCharset to $request:charset | // transcode cell value from ?ClientCharset to $request:charset |
| if(transcode_needed && length) | if(length && transcode_column[i]){ |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| connection.client_charset, | client_charset, |
| services.request_charset()); | request_charset); |
| } | |
| CHECK(handlers.add_row_cell(sql_error, str, length)); | CHECK(handlers.add_row_cell(sql_error, str, length)); |
| } | } |
| Line 388 public: | Line 392 public: |
| } CATCH_ALL (e) { | } CATCH_ALL (e) { |
| _throw(services, e); | _throw(services, e); |
| } END_CATCH_ALL | } END_CATCH_ALL |
| if(connection.autocommit) | |
| commit(aconnection); | |
| } | } |
| private: | private: |
| Line 412 private: | Line 419 private: |
| ptr=v.m_boolVal?"1":"0"; | ptr=v.m_boolVal?"1":"0"; |
| length=1; | length=1; |
| break;*/ | break;*/ |
| /* case DBVT_UCHAR: | /* case DBVT_UCHAR: |
| length=strlen(ptr=v.m_chVal); | length=strlen(ptr=v.m_chVal); |
| break; | break; |
| case DBVT_SHORT: | case DBVT_SHORT: |
| Line 489 private: | Line 496 private: |
| connection.services->_throw(msg); | connection.services->_throw(msg); |
| } | } |
| bool _transcode_required(Connection& connection){ | |
| return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0); | |
| } | |
| }; | }; |
| extern "C" SQL_Driver *SQL_DRIVER_CREATE() { | extern "C" SQL_Driver *SQL_DRIVER_CREATE() { |