|
|
1.1 parser 1: /** @file
2: Parser ODBC driver.
3:
1.13 paf 4: Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.1 parser 5:
1.5 paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 7: */
1.27 misha 8: static const char *RCSId="$Id: parser3odbc.C,v 1.26 2004/09/13 14:47:43 paf Exp $";
1.1 parser 9:
10: #ifndef _MSC_VER
11: # error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]
12: #endif
13:
14: #include <string.h>
15: #include <stdio.h>
16: #include <stdlib.h>
1.2 paf 17: #include <setjmp.h>
1.1 parser 18:
19: #include "pa_sql_driver.h"
20:
1.16 paf 21: #define WINVER 0x0400
1.1 parser 22: #include <AFXDB.H>
23:
1.10 paf 24: // defines
25:
26: #define MAX_COLS 500
27:
1.1 parser 28: #define MAX_STRING 0x400
1.7 paf 29: #define MAX_NUMBER 40
30:
1.10 paf 31: // new in MSSQL2000, no MFC constants
1.7 paf 32: #ifndef SQL_NVARCHAR
33: #define SQL_NVARCHAR (-9)
34: #endif
35: #ifndef SQL_NTEXT
36: #define SQL_NTEXT (-10)
37: #endif
38: #ifndef SQL_SMALLDATETIME
39: #define SQL_SMALLDATETIME 11
40: #endif
41: // create table test (id int, a smalldatetime, b ntext, c nvarchar(100))
1.1 parser 42:
43: #define snprintf _snprintf
44: #ifndef strncasecmp
45: # define strncasecmp _strnicmp
46: #endif
1.28 misha 47: #ifndef strcasecmp
48: # define strcasecmp _stricmp
49: #endif
1.1 parser 50:
1.27 misha 51: static char *lsplit(char *string, char delim){
52: if(string){
53: if(char* v=strchr(string, delim)){
1.1 parser 54: *v=0;
55: return v+1;
56: }
1.27 misha 57: }
58: return 0;
1.1 parser 59: }
60:
1.28 misha 61: static char *lsplit(char **string_ref, char delim){
62: char *result=*string_ref;
63: char *next=lsplit(*string_ref, delim);
64: *string_ref=next;
65: return result;
66: }
67:
1.27 misha 68: static void toupper_str(char *out, const char *in, size_t size){
1.20 paf 69: while(size--)
70: *out++=(char)toupper(*in++);
71: }
72:
1.18 paf 73: struct Connection {
74: SQL_Driver_services* services;
75:
76: CDatabase* db;
1.27 misha 77: const char* client_charset;
78: bool autocommit;
1.28 misha 79: bool fast_offset_search;
1.18 paf 80: };
81:
1.1 parser 82: /**
83: ODBC server driver
84: */
85: class ODBC_Driver : public SQL_Driver {
86: public:
87:
88: ODBC_Driver() : SQL_Driver() {
89: }
90:
91: /// get api version
92: int api_version() { return SQL_DRIVER_API_VERSION; }
1.27 misha 93:
1.3 paf 94: const char *initialize(char *dlopen_file_spec) { return 0; }
1.1 parser 95: /** connect
1.22 paf 96: @param url
1.28 misha 97: format: @b DSN=dsn;UID=user;PWD=password? (ODBC connect string)
98: ClientCharset=charset& // transcode with parser
99: autocommit=1& // 0 -- disable auto commit
1.30 ! misha 100: FastOffsetSearch=0
1.1 parser 101: WARNING: must be used only to connect, for buffer doesn't live long
102: */
1.27 misha 103:
1.1 parser 104: void connect(
1.27 misha 105: char *url,
106: SQL_Driver_services& services,
107: void **connection_ref ///< output: Connection*
108: ){
109: Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.18 paf 110: *connection_ref=&connection;
111: connection.services=&services;
1.27 misha 112: connection.client_charset=0;
1.29 misha 113: connection.fast_offset_search=false;
1.27 misha 114: connection.autocommit=true;
115:
116: size_t url_length=strlen(url);
1.28 misha 117: char *options=lsplit(url, '?');
1.20 paf 118:
1.28 misha 119: while(options){
120: if(char *key=lsplit(&options, '&')){
121: if(*key){
122: if(char *value=lsplit(key, '=')){
123: if(strcmp(key, "ClientCharset")==0){
124: toupper_str(value, value, strlen(value));
125: connection.client_charset=value;
126: } else if(strcasecmp(key, "autocommit")==0){
127: if(atoi(value)==0)
128: connection.autocommit=false;
129: } else if(strcmp(key, "FastOffsetSearch")==0){
1.29 misha 130: if(atoi(value)==1)
131: connection.fast_offset_search=true;
1.28 misha 132: } else
133: services._throw("unknown connect option" /*key*/);
134: } else
135: services._throw("connect option without =value" /*key*/);
136: }
1.20 paf 137: }
138: }
1.18 paf 139:
1.1 parser 140: TRY {
1.18 paf 141: connection.db=new CDatabase();
1.22 paf 142: connection.db->OpenEx(url, CDatabase::noOdbcDialog);
1.27 misha 143:
144: if(!connection.autocommit)
145: connection.db->BeginTrans();
1.1 parser 146: }
147: CATCH_ALL (e) {
148: _throw(services, e);
149: }
150: END_CATCH_ALL
151: }
1.27 misha 152:
153: void disconnect(void *aconnection){
1.18 paf 154: Connection& connection=*static_cast<Connection*>(aconnection);
1.1 parser 155: TRY
1.18 paf 156: delete connection.db;
157: connection.db=0;
1.1 parser 158: CATCH_ALL (e) {
159: // nothing
160: }
161: END_CATCH_ALL
162: }
1.27 misha 163:
164: void commit(void *aconnection){
1.18 paf 165: Connection& connection=*static_cast<Connection*>(aconnection);
1.27 misha 166: if(!connection.autocommit){
167: TRY
168: connection.db->CommitTrans();
169: connection.db->BeginTrans();
170: CATCH_ALL (e) {
171: _throw(*connection.services, e);
172: }
173: END_CATCH_ALL
1.1 parser 174: }
175: }
1.27 misha 176:
177: void rollback(void *aconnection){
1.18 paf 178: Connection& connection=*static_cast<Connection*>(aconnection);
1.27 misha 179: if(!connection.autocommit){
180: TRY
181: connection.db->Rollback();
182: connection.db->BeginTrans();
183: CATCH_ALL (e) {
184: _throw(*connection.services, e);
185: }
186: END_CATCH_ALL
1.1 parser 187: }
188: }
189:
1.27 misha 190: bool ping(void *connection){
1.1 parser 191: return true;
192: }
193:
1.27 misha 194: const char* quote(void *aconnection, const char *from, unsigned int length){
1.18 paf 195: Connection& connection=*static_cast<Connection*>(aconnection);
196: char *result=(char*)connection.services->malloc_atomic(length*2+1);
1.14 paf 197: char *to=result;
1.27 misha 198: while(length--){
1.14 paf 199: if(*from=='\'') { // ' -> ''
1.15 paf 200: *to++='\'';
1.3 paf 201: }
1.14 paf 202: *to++=*from++;
203: }
204: *to=0;
205: return result;
1.1 parser 206: }
1.27 misha 207:
1.18 paf 208: void query(void *aconnection,
1.27 misha 209: const char *statement,
210: size_t placeholders_count,
211: Placeholder* placeholders,
212: unsigned long offset,
213: unsigned long limit,
214: SQL_Driver_query_event_handlers& handlers
215: ){
1.18 paf 216: Connection& connection=*static_cast<Connection*>(aconnection);
217: CDatabase *db=connection.db;
218: SQL_Driver_services& services=*connection.services;
1.23 paf 219:
220: if(placeholders_count>0)
221: services._throw("bind variables not supported (yet)");
1.1 parser 222:
1.30 ! misha 223: const char* client_charset=connection.client_charset;
! 224: const char* request_charset=services.request_charset();
! 225: bool transcode_needed=(client_charset && strcmp(client_charset, request_charset)!=0);
1.27 misha 226:
227: // transcode query from $request:charset to ?ClientCharset
228: if(transcode_needed){
229: size_t length=strlen(statement);
230: services.transcode(statement, length,
231: statement, length,
1.30 ! misha 232: request_charset,
! 233: client_charset);
1.20 paf 234: }
235:
1.24 paf 236: while(isspace((unsigned char)*statement))
1.1 parser 237: statement++;
1.27 misha 238:
1.1 parser 239: TRY {
1.8 paf 240: // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm
1.27 misha 241: // or http://msdn.microsoft.com/en-us/library/aa905899(SQL.80).aspx
1.8 paf 242: // Server cursors are created only for statements that begin with:
243: // SELECT
244: // EXEC[ute] procedure_name
245: // call procedure_name
246: // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm
247: // The ODBC CALL escape sequence for calling a procedure is:
248: // {[?=]call procedure_name[([parameter][,[parameter]]...)]}
1.27 misha 249: if(strncasecmp(statement, "SELECT", 6)==0
1.8 paf 250: || strncasecmp(statement, "EXEC", 4)==0
251: || strncasecmp(statement, "call", 4)==0
1.27 misha 252: || strncasecmp(statement, "{", 1)==0
253: ){
254: CRecordset rs(db);
255: DWORD options=CRecordset::executeDirect|CRecordset::readOnly;
256: //CRecordset::skipDeletedRecords
257: //CRecordset::useMultiRowFetch
258: //CRecordset::userAllocMultiRowBuffers
259: //CRecordset::useExtendedFetch
1.28 misha 260: /*
261: if(connection.fast_offset_search){
1.27 misha 262: options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers;
263: } else {
264: options+=CRecordset::skipDeletedRecords;
265: }
1.28 misha 266: */
1.9 paf 267: TRY {
268: rs.Open(
1.28 misha 269: (connection.fast_offset_search)?CRecordset::dynamic:CRecordset::forwardOnly,
1.9 paf 270: statement,
1.27 misha 271: options
272: );
1.9 paf 273: } CATCH_ALL (e) {
274: // could not fetch a table
275: TRY {
276: // then try resultless query
277: db->ExecuteSQL(statement);
278: // OK then
279: return;
280: } CATCH_ALL (e2) {
281: // still nothing good
282: _throw(services, e); // throw ORIGINAL exception
283: } END_CATCH_ALL
284: } END_CATCH_ALL
1.1 parser 285:
286: int column_count=rs.GetODBCFieldCount();
287: if(!column_count)
288: services._throw("result contains no columns");
289:
1.10 paf 290: if(column_count>MAX_COLS)
291: column_count=MAX_COLS;
292:
1.27 misha 293: SWORD column_types[MAX_COLS];
1.30 ! misha 294: bool transcode_column[MAX_COLS];
1.27 misha 295:
1.12 paf 296: SQL_Error sql_error;
297: #define CHECK(afailed) if(afailed) services._throw(sql_error)
298:
1.1 parser 299: for(int i=0; i<column_count; i++){
300: CString string;
301: CODBCFieldInfo fieldinfo;
302: rs.GetODBCFieldInfo(i, fieldinfo);
1.10 paf 303: column_types[i]=fieldinfo.m_nSQLType;
1.30 ! misha 304: switch(fieldinfo.m_nSQLType){
! 305: case SQL_NUMERIC:
! 306: case SQL_DECIMAL:
! 307: case SQL_INTEGER:
! 308: case SQL_SMALLINT:
! 309: case SQL_FLOAT:
! 310: case SQL_REAL:
! 311: case SQL_DOUBLE:
! 312: case SQL_DATETIME:
! 313: case SQL_SMALLDATETIME:
! 314: case SQL_BIGINT:
! 315: case SQL_TINYINT:
! 316: transcode_column[i]=false;
! 317: break;
! 318: default:
! 319: transcode_column[i]=transcode_needed;
! 320:
! 321: }
1.20 paf 322: size_t length=fieldinfo.m_strName.GetLength();
1.14 paf 323: char *str=0;
1.27 misha 324: if(length){
1.20 paf 325: str=(char*)services.malloc_atomic(length+1);
1.27 misha 326: memcpy(str, (char*)LPCTSTR(fieldinfo.m_strName), length+1);
1.20 paf 327:
1.27 misha 328: // transcode column name from ?ClientCharset to $request:charset
329: if(transcode_needed){
1.20 paf 330: services.transcode(str, length,
331: str, length,
1.30 ! misha 332: client_charset,
! 333: request_charset);
1.20 paf 334: }
1.1 parser 335: }
1.20 paf 336: CHECK(handlers.add_column(sql_error, str, length));
1.1 parser 337: }
338:
1.12 paf 339: CHECK(handlers.before_rows(sql_error));
1.27 misha 340:
341: // skip offset rows
342: if(offset){
1.28 misha 343: if(connection.fast_offset_search){
1.27 misha 344: rs.Move(offset);
345: } else {
346: unsigned long row=offset;
347: while(!rs.IsEOF() && row>0){
348: rs.MoveNext();
349: row--;
350: }
351: }
352: }
1.1 parser 353:
354: unsigned long row=0;
1.10 paf 355: CDBVariant v;
356: CString s;
1.27 misha 357: while(!rs.IsEOF() && (limit==SQL_NO_LIMIT || row<limit)){
358: CHECK(handlers.add_row(sql_error));
359: for(int i=0; i<column_count; i++){
360: size_t length;
361: char* str;
362: switch(column_types[i]){
1.7 paf 363: //case xBOOL:
1.14 paf 364: //case SQL_DATETIME: << default: handles that more properly (?)
365: case SQL_BINARY:
1.11 paf 366: case SQL_VARBINARY:
367: case SQL_LONGVARBINARY:
1.7 paf 368: case SQL_SMALLDATETIME:
1.27 misha 369: //case SQL_NVARCHAR: // mfc 7.1 has errors with nvarchar(length): SQLGetData in dbcore.cpp truncates last byte for unknown reason.
370: // could be fixed by uncommenting this and handing DBVT_WSTRING inside, but it's UNICODE
1.10 paf 371: rs.GetFieldValue(i, v);
1.20 paf 372: getFromDBVariant(services, v, str, length);
1.10 paf 373: break;
1.7 paf 374: default:
1.10 paf 375: rs.GetFieldValue(i, s);
1.20 paf 376: getFromString(services, s, str, length);
1.10 paf 377: break;
1.27 misha 378: }
1.20 paf 379:
1.27 misha 380: // transcode cell value from ?ClientCharset to $request:charset
1.30 ! misha 381: if(length && transcode_column[i]){
1.27 misha 382: services.transcode(str, length,
383: str, length,
1.30 ! misha 384: client_charset,
! 385: request_charset);
! 386: }
1.20 paf 387:
1.27 misha 388: CHECK(handlers.add_row_cell(sql_error, str, length));
1.1 parser 389: }
1.27 misha 390: rs.MoveNext();
391: row++;
1.1 parser 392: }
393:
394: rs.Close();
395: } else {
396: db->ExecuteSQL(statement);
397: }
1.9 paf 398: } CATCH_ALL (e) {
1.1 parser 399: _throw(services, e);
1.9 paf 400: } END_CATCH_ALL
1.7 paf 401: }
402:
1.27 misha 403: private:
404: void getFromDBVariant(SQL_Driver_services& services, CDBVariant& v, char*& str, size_t& length){
405: switch(v.m_dwType){
1.14 paf 406: case DBVT_BINARY: /* << would cause problems with current String implementation
407: now falling into NULL case, effectively ignoring such columns [not failing]
408: {
1.17 paf 409: if(length=v.m_pbinary->m_dwDataLength) {
410: str=services.malloc_atomic(length+1);
411: memcpy(ptr, ::GlobalLock(v.m_pbinary->m_hData), length);
1.14 paf 412: ::GlobalUnlock(v.m_pbinary->m_hData);
413: } else
414: str=0;
415: break;
416: }*/
1.7 paf 417: case DBVT_NULL: // No union member is valid for access.
1.14 paf 418: str=0;
1.17 paf 419: length=0;
1.7 paf 420: break;
421: /* case DBVT_BOOL:
422: ptr=v.m_boolVal?"1":"0";
1.17 paf 423: length=1;
1.7 paf 424: break;*/
425: /* case DBVT_UCHAR:
1.17 paf 426: length=strlen(ptr=v.m_chVal);
1.7 paf 427: break;
428: case DBVT_SHORT:
429: char buf[MAX_NUMBER];
1.17 paf 430: length=snprintf(HEAPIZE buf, "%d", v.m_iVal);
1.27 misha 431: break;
432: */
1.7 paf 433: /* case DBVT_LONG:
434: {
435: char local_buf[MAX_NUMBER];
1.17 paf 436: length=snprintf(local_buf, MAX_NUMBER, "%ld", v.m_lVal);
437: ptr=services.malloc_atomic(length);
438: memcpy(ptr, local_buf, length);
1.7 paf 439: break;
1.27 misha 440: }
441: */
442: /*
443: case DBVT_SINGLE:
1.7 paf 444: m_fltVal
445: break;
1.27 misha 446: case DBVT_DOUBLE m_dblVal
447: case DBVT_STRING m_pstring
448: */
1.7 paf 449: case DBVT_DATE:
450: {
451: char local_buf[MAX_STRING];
1.17 paf 452: length=snprintf(local_buf, MAX_STRING,
1.7 paf 453: "%04d-%02d-%02d %02d:%02d:%02d.%03d",
454: v.m_pdate->year,
455: v.m_pdate->month,
456: v.m_pdate->day,
457: v.m_pdate->hour,
458: v.m_pdate->minute,
459: v.m_pdate->second,
1.25 paf 460: v.m_pdate->fraction/1000000); // lexical parser of INCOMING literal choked on times like hh:mm:ss.123000000
1.17 paf 461: str=(char*)services.malloc_atomic(length+1);
462: memcpy(str, local_buf, length+1);
1.7 paf 463: break;
464: }
465: default:
466: char msg[MAX_STRING];
467: snprintf(msg, MAX_STRING, "unknown column return variant type (%d)",
468: v.m_dwType);
469: services._throw(msg);
470: }
471: }
472:
1.27 misha 473: void getFromString(SQL_Driver_services& services, CString& s, char*& astr, size_t& length){
474: if(s.IsEmpty()){
1.14 paf 475: astr=0;
1.17 paf 476: length=0;
1.7 paf 477: } else {
478: const char *cstr=LPCTSTR(s);
1.17 paf 479: length=strlen(cstr); //string.GetLength() works wrong with non-string types:
480: astr=(char*)services.malloc_atomic(length+1);
481: memcpy(astr, cstr, length+1);
1.7 paf 482: }
1.1 parser 483: }
484:
1.27 misha 485: void _throw(SQL_Driver_services& services, CException *e){
486: char szCause[MAX_STRING];
487: szCause[0]=0;
1.1 parser 488: e->GetErrorMessage(szCause, MAX_STRING);
489: char msg[MAX_STRING];
490: snprintf(msg, MAX_STRING, "%s: %s",
491: e->GetRuntimeClass()->m_lpszClassName,
492: *szCause?szCause:"unknown");
493: services._throw(msg);
494: }
495:
1.27 misha 496: void _throw(Connection& connection, long value){
497: char msg[MAX_STRING];
498: snprintf(msg, MAX_STRING, "%u", value);
499: connection.services->_throw(msg);
500: }
501:
1.1 parser 502: };
503:
504: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
505: return new ODBC_Driver();
1.26 paf 506: }