|
|
| version 1.22, 2004/05/25 07:07:28 | version 1.28, 2008/06/27 13:38:36 |
|---|---|
| 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){ |
| char *v=strchr(string, delim); | if(char* v=strchr(string, delim)){ |
| if(v) { | |
| *v=0; | *v=0; |
| return v+1; | return v+1; |
| } | } |
| } | } |
| 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 65 struct Connection { | Line 74 struct Connection { |
| SQL_Driver_services* services; | SQL_Driver_services* services; |
| CDatabase* db; | CDatabase* db; |
| const char* cstrClientCharset; | const char* client_charset; |
| bool autocommit; | |
| bool fast_offset_search; | |
| }; | }; |
| /** | /** |
| Line 79 public: | Line 90 public: |
| /// get api version | /// get api version |
| int api_version() { return SQL_DRIVER_API_VERSION; } | int api_version() { return SQL_DRIVER_API_VERSION; } |
| 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 | |
| MultiRowFetch=1& // 0 -- disable (slower) | |
| autocommit=1& // 0 -- disable auto commit | |
| 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 |
| */ | */ |
| void connect( | void connect( |
| char *url, | char *url, |
| SQL_Driver_services& services, | SQL_Driver_services& services, |
| void **connection_ref ///< output: Connection* | void **connection_ref ///< output: Connection* |
| ) { | ){ |
| Connection& connection=*(Connection *)services.malloc(sizeof(Connection)); | Connection& connection=*(Connection *)services.malloc(sizeof(Connection)); |
| *connection_ref=&connection; | *connection_ref=&connection; |
| connection.services=&services; | connection.services=&services; |
| connection.cstrClientCharset=0; | connection.client_charset=0; |
| connection.fast_offset_search=true; | |
| if(const char* key_start=strstr(url, "ClientCharset=")) { | connection.autocommit=true; |
| const char* value_start=key_start+14/*strlen("ClientCharset=")*/; | |
| const char* value_end=strchr(value_start, ';'); | size_t url_length=strlen(url); |
| if(!value_end) | char *options=lsplit(url, '?'); |
| value_end=url+strlen(url); | |
| while(options){ | |
| if(size_t ClientCharsetLength=value_end-value_start) { | if(char *key=lsplit(&options, '&')){ |
| char* cstrClientCharset=(char*)services.malloc_atomic(ClientCharsetLength+1); | if(*key){ |
| toupper_str(cstrClientCharset, value_start, ClientCharsetLength); | if(char *value=lsplit(key, '=')){ |
| cstrClientCharset[ClientCharsetLength]=0; | if(strcmp(key, "ClientCharset")==0){ |
| toupper_str(value, value, strlen(value)); | |
| connection.cstrClientCharset=cstrClientCharset; | connection.client_charset=value; |
| } else if(strcasecmp(key, "autocommit")==0){ | |
| if(atoi(value)==0) | |
| connection.autocommit=false; | |
| } else if(strcmp(key, "FastOffsetSearch")==0){ | |
| if(atoi(value)==0) | |
| connection.fast_offset_search=false; | |
| } else | |
| services._throw("unknown connect option" /*key*/); | |
| } else | |
| services._throw("connect option without =value" /*key*/); | |
| } | |
| } | } |
| } | } |
| 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); |
| } | } |
| END_CATCH_ALL | END_CATCH_ALL |
| } | } |
| void disconnect(void *aconnection) { | |
| void disconnect(void *aconnection){ | |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| TRY | TRY |
| delete connection.db; | delete connection.db; |
| Line 130 public: | Line 160 public: |
| } | } |
| END_CATCH_ALL | END_CATCH_ALL |
| } | } |
| void commit(void *aconnection) { | |
| void commit(void *aconnection){ | |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| TRY | if(!connection.autocommit){ |
| connection.db->CommitTrans(); | TRY |
| connection.db->BeginTrans(); | connection.db->CommitTrans(); |
| CATCH_ALL (e) { | connection.db->BeginTrans(); |
| _throw(*connection.services, e); | CATCH_ALL (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); |
| TRY | if(!connection.autocommit){ |
| connection.db->Rollback(); | TRY |
| connection.db->BeginTrans(); | connection.db->Rollback(); |
| CATCH_ALL (e) { | connection.db->BeginTrans(); |
| _throw(*connection.services, e); | CATCH_ALL (e) { |
| _throw(*connection.services, e); | |
| } | |
| END_CATCH_ALL | |
| } | } |
| END_CATCH_ALL | |
| } | } |
| bool ping(void *connection) { | bool ping(void *connection){ |
| return true; | return true; |
| } | } |
| const char* quote(void *aconnection, const char *from, unsigned int length) { | const char* quote(void *aconnection, const char *from, unsigned int length){ |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| char *result=(char*)connection.services->malloc_atomic(length*2+1); | char *result=(char*)connection.services->malloc_atomic(length*2+1); |
| char *to=result; | char *to=result; |
| while(length--) { | while(length--){ |
| if(*from=='\'') { // ' -> '' | if(*from=='\'') { // ' -> '' |
| *to++='\''; | *to++='\''; |
| } | } |
| Line 168 public: | Line 204 public: |
| *to=0; | *to=0; |
| return result; | return result; |
| } | } |
| void query(void *aconnection, | |
| const char *statement, unsigned long offset, unsigned long limit, | |
| SQL_Driver_query_event_handlers& handlers) { | |
| void query(void *aconnection, | |
| const char *statement, | |
| size_t placeholders_count, | |
| Placeholder* placeholders, | |
| unsigned long offset, | |
| unsigned long limit, | |
| SQL_Driver_query_event_handlers& handlers | |
| ){ | |
| Connection& connection=*static_cast<Connection*>(aconnection); | Connection& connection=*static_cast<Connection*>(aconnection); |
| CDatabase *db=connection.db; | CDatabase *db=connection.db; |
| SQL_Driver_services& services=*connection.services; | SQL_Driver_services& services=*connection.services; |
| // transcode from $request:charset to connect-string?client_charset | if(placeholders_count>0) |
| if(const char* cstrClientCharset=connection.cstrClientCharset) { | services._throw("bind variables not supported (yet)"); |
| size_t transcoded_statement_size; | |
| services.transcode(statement, strlen(statement), | bool transcode_needed=_transcode_required(connection); |
| statement, transcoded_statement_size, | |
| // transcode query from $request:charset to ?ClientCharset | |
| if(transcode_needed){ | |
| size_t length=strlen(statement); | |
| services.transcode(statement, length, | |
| statement, length, | |
| services.request_charset(), | services.request_charset(), |
| cstrClientCharset); | connection.client_charset); |
| } | } |
| while(isspace(*statement)) | while(isspace((unsigned char)*statement)) |
| statement++; | statement++; |
| TRY { | TRY { |
| // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm | // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm |
| // or http://msdn.microsoft.com/en-us/library/aa905899(SQL.80).aspx | |
| // Server cursors are created only for statements that begin with: | // Server cursors are created only for statements that begin with: |
| // SELECT | // SELECT |
| // EXEC[ute] procedure_name | // EXEC[ute] procedure_name |
| Line 197 public: | Line 244 public: |
| // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm | // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm |
| // The ODBC CALL escape sequence for calling a procedure is: | // The ODBC CALL escape sequence for calling a procedure is: |
| // {[?=]call procedure_name[([parameter][,[parameter]]...)]} | // {[?=]call procedure_name[([parameter][,[parameter]]...)]} |
| if(strncasecmp(statement, "select", 6)==0 | if(strncasecmp(statement, "SELECT", 6)==0 |
| || strncasecmp(statement, "EXEC", 4)==0 | || strncasecmp(statement, "EXEC", 4)==0 |
| || strncasecmp(statement, "call", 4)==0 | || strncasecmp(statement, "call", 4)==0 |
| || strncasecmp(statement, "{", 1)==0) { | || strncasecmp(statement, "{", 1)==0 |
| CRecordset rs(db); | ){ |
| CRecordset rs(db); | |
| DWORD options=CRecordset::executeDirect|CRecordset::readOnly; | |
| //CRecordset::skipDeletedRecords | |
| //CRecordset::useMultiRowFetch | |
| //CRecordset::userAllocMultiRowBuffers | |
| //CRecordset::useExtendedFetch | |
| /* | |
| if(connection.fast_offset_search){ | |
| options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers; | |
| } else { | |
| options+=CRecordset::skipDeletedRecords; | |
| } | |
| */ | |
| TRY { | TRY { |
| rs.Open( | rs.Open( |
| CRecordset::forwardOnly, | (connection.fast_offset_search)?CRecordset::dynamic:CRecordset::forwardOnly, |
| statement, | statement, |
| CRecordset::executeDirect | options |
| ); | ); |
| } CATCH_ALL (e) { | } CATCH_ALL (e) { |
| // could not fetch a table | // could not fetch a table |
| TRY { | TRY { |
| Line 225 public: | Line 285 public: |
| if(!column_count) | if(!column_count) |
| services._throw("result contains no columns"); | services._throw("result contains no columns"); |
| SWORD column_types[MAX_COLS]; | |
| if(column_count>MAX_COLS) | if(column_count>MAX_COLS) |
| column_count=MAX_COLS; | column_count=MAX_COLS; |
| SWORD column_types[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 239 public: | Line 300 public: |
| column_types[i]=fieldinfo.m_nSQLType; | column_types[i]=fieldinfo.m_nSQLType; |
| size_t length=fieldinfo.m_strName.GetLength(); | size_t length=fieldinfo.m_strName.GetLength(); |
| char *str=0; | char *str=0; |
| if(length) { | if(length){ |
| str=(char*)services.malloc_atomic(length+1); | str=(char*)services.malloc_atomic(length+1); |
| memcpy(str, (char *)LPCTSTR(fieldinfo.m_strName), length+1); | memcpy(str, (char*)LPCTSTR(fieldinfo.m_strName), length+1); |
| // transcode to $request:charset from connect-string?client_charset | // transcode column name from ?ClientCharset to $request:charset |
| if(const char* cstrClientCharset=connection.cstrClientCharset) { | if(transcode_needed){ |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| cstrClientCharset, | connection.client_charset, |
| services.request_charset()); | services.request_charset()); |
| } | } |
| } | } |
| Line 255 public: | Line 316 public: |
| } | } |
| CHECK(handlers.before_rows(sql_error)); | CHECK(handlers.before_rows(sql_error)); |
| // skip offset rows | |
| if(offset){ | |
| if(connection.fast_offset_search){ | |
| rs.Move(offset); | |
| } else { | |
| unsigned long row=offset; | |
| while(!rs.IsEOF() && row>0){ | |
| rs.MoveNext(); | |
| row--; | |
| } | |
| } | |
| } | |
| unsigned long row=0; | unsigned long row=0; |
| CDBVariant v; | CDBVariant v; |
| CString s; | CString s; |
| while(!rs.IsEOF() && (!limit||(row<offset+limit))) { | while(!rs.IsEOF() && (limit==SQL_NO_LIMIT || row<limit)){ |
| if(row>=offset) { | CHECK(handlers.add_row(sql_error)); |
| CHECK(handlers.add_row(sql_error)); | for(int i=0; i<column_count; i++){ |
| for(int i=0; i<column_count; i++) { | size_t length; |
| size_t length; | 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_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: |
| case SQL_LONGVARBINARY: | case SQL_LONGVARBINARY: |
| case SQL_SMALLDATETIME: | case SQL_SMALLDATETIME: |
| //case SQL_NVARCHAR: // mfc 7.1 has errors with nvarchar(length): SQLGetData in dbcore.cpp truncates last byte for unknown reason. could be fixed by uncommenting this and handing DBVT_WSTRING inside, but it's UNICODE | //case SQL_NVARCHAR: // mfc 7.1 has errors with nvarchar(length): SQLGetData in dbcore.cpp truncates last byte for unknown reason. |
| // could be fixed by uncommenting this and handing DBVT_WSTRING inside, but it's UNICODE | |
| rs.GetFieldValue(i, v); | rs.GetFieldValue(i, v); |
| getFromDBVariant(services, v, str, length); | getFromDBVariant(services, v, str, length); |
| break; | break; |
| Line 281 public: | Line 355 public: |
| rs.GetFieldValue(i, s); | rs.GetFieldValue(i, s); |
| getFromString(services, s, str, length); | getFromString(services, s, str, length); |
| break; | break; |
| } | } |
| // transcode to $request:charset from connect-string?client_charset | // transcode cell value from ?ClientCharset to $request:charset |
| if(const char* cstrClientCharset=connection.cstrClientCharset) | if(transcode_needed && length) |
| services.transcode(str, length, | services.transcode(str, length, |
| str, length, | str, length, |
| cstrClientCharset, | connection.client_charset, |
| services.request_charset()); | services.request_charset()); |
| CHECK(handlers.add_row_cell(sql_error, str, length)); | CHECK(handlers.add_row_cell(sql_error, str, length)); |
| } | |
| } | } |
| rs.MoveNext(); row++; | rs.MoveNext(); |
| row++; | |
| } | } |
| rs.Close(); | rs.Close(); |
| Line 305 public: | Line 379 public: |
| } END_CATCH_ALL | } END_CATCH_ALL |
| } | } |
| void getFromDBVariant(SQL_Driver_services& services, CDBVariant& v, char*& str, size_t& length) { | private: |
| switch(v.m_dwType) { | void getFromDBVariant(SQL_Driver_services& services, CDBVariant& v, char*& str, size_t& length){ |
| switch(v.m_dwType){ | |
| case DBVT_BINARY: /* << would cause problems with current String implementation | case DBVT_BINARY: /* << would cause problems with current String implementation |
| now falling into NULL case, effectively ignoring such columns [not failing] | now falling into NULL case, effectively ignoring such columns [not failing] |
| { | { |
| Line 332 public: | Line 407 public: |
| case DBVT_SHORT: | case DBVT_SHORT: |
| char buf[MAX_NUMBER]; | char buf[MAX_NUMBER]; |
| length=snprintf(HEAPIZE buf, "%d", v.m_iVal); | length=snprintf(HEAPIZE buf, "%d", v.m_iVal); |
| break;*/ | break; |
| */ | |
| /* case DBVT_LONG: | /* case DBVT_LONG: |
| { | { |
| char local_buf[MAX_NUMBER]; | char local_buf[MAX_NUMBER]; |
| Line 340 public: | Line 416 public: |
| ptr=services.malloc_atomic(length); | ptr=services.malloc_atomic(length); |
| memcpy(ptr, local_buf, length); | memcpy(ptr, local_buf, length); |
| break; | break; |
| }*/ | } |
| /*case DBVT_SINGLE: | */ |
| /* | |
| case DBVT_SINGLE: | |
| m_fltVal | m_fltVal |
| break; | break; |
| case DBVT_DOUBLE m_dblVal | case DBVT_DOUBLE m_dblVal |
| case DBVT_STRING m_pstring */ | case DBVT_STRING m_pstring |
| */ | |
| case DBVT_DATE: | case DBVT_DATE: |
| { | { |
| char local_buf[MAX_STRING]; | char local_buf[MAX_STRING]; |
| Line 357 public: | Line 436 public: |
| v.m_pdate->hour, | v.m_pdate->hour, |
| v.m_pdate->minute, | v.m_pdate->minute, |
| v.m_pdate->second, | v.m_pdate->second, |
| v.m_pdate->fraction); | v.m_pdate->fraction/1000000); // lexical parser of INCOMING literal choked on times like hh:mm:ss.123000000 |
| str=(char*)services.malloc_atomic(length+1); | str=(char*)services.malloc_atomic(length+1); |
| memcpy(str, local_buf, length+1); | memcpy(str, local_buf, length+1); |
| break; | break; |
| Line 370 public: | Line 449 public: |
| } | } |
| } | } |
| void getFromString(SQL_Driver_services& services, CString& s, char*& astr, size_t& length) { | void getFromString(SQL_Driver_services& services, CString& s, char*& astr, size_t& length){ |
| if(s.IsEmpty()) { | if(s.IsEmpty()){ |
| astr=0; | astr=0; |
| length=0; | length=0; |
| } else { | } else { |
| Line 382 public: | Line 461 public: |
| } | } |
| } | } |
| void _throw(SQL_Driver_services& services, CException *e) { | void _throw(SQL_Driver_services& services, CException *e){ |
| char szCause[MAX_STRING]; szCause[0]=0; | char szCause[MAX_STRING]; |
| szCause[0]=0; | |
| e->GetErrorMessage(szCause, MAX_STRING); | e->GetErrorMessage(szCause, MAX_STRING); |
| char msg[MAX_STRING]; | char msg[MAX_STRING]; |
| snprintf(msg, MAX_STRING, "%s: %s", | snprintf(msg, MAX_STRING, "%s: %s", |
| Line 392 public: | Line 472 public: |
| services._throw(msg); | services._throw(msg); |
| } | } |
| void _throw(Connection& connection, long value){ | |
| char msg[MAX_STRING]; | |
| snprintf(msg, MAX_STRING, "%u", value); | |
| 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() { |
| return new ODBC_Driver(); | return new ODBC_Driver(); |
| } | |
| } |