|
|
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: MultiRowFetch=1& // 0 -- disable (slower)
! 100: autocommit=1& // 0 -- disable auto commit
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.28 ! misha 113: connection.fast_offset_search=true;
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){
! 130: if(atoi(value)==0)
! 131: connection.fast_offset_search=false;
! 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.27 misha 223: bool transcode_needed=_transcode_required(connection);
224:
225: // transcode query from $request:charset to ?ClientCharset
226: if(transcode_needed){
227: size_t length=strlen(statement);
228: services.transcode(statement, length,
229: statement, length,
1.20 paf 230: services.request_charset(),
1.27 misha 231: connection.client_charset);
1.20 paf 232: }
233:
1.24 paf 234: while(isspace((unsigned char)*statement))
1.1 parser 235: statement++;
1.27 misha 236:
1.1 parser 237: TRY {
1.8 paf 238: // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\adosql.chm::/adoprg02_4g33.htm
1.27 misha 239: // or http://msdn.microsoft.com/en-us/library/aa905899(SQL.80).aspx
1.8 paf 240: // Server cursors are created only for statements that begin with:
241: // SELECT
242: // EXEC[ute] procedure_name
243: // call procedure_name
244: // mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\odbcsql.chm::/od_6_035_5dnp.htm
245: // The ODBC CALL escape sequence for calling a procedure is:
246: // {[?=]call procedure_name[([parameter][,[parameter]]...)]}
1.27 misha 247: if(strncasecmp(statement, "SELECT", 6)==0
1.8 paf 248: || strncasecmp(statement, "EXEC", 4)==0
249: || strncasecmp(statement, "call", 4)==0
1.27 misha 250: || strncasecmp(statement, "{", 1)==0
251: ){
252: CRecordset rs(db);
253: DWORD options=CRecordset::executeDirect|CRecordset::readOnly;
254: //CRecordset::skipDeletedRecords
255: //CRecordset::useMultiRowFetch
256: //CRecordset::userAllocMultiRowBuffers
257: //CRecordset::useExtendedFetch
1.28 ! misha 258: /*
! 259: if(connection.fast_offset_search){
1.27 misha 260: options+=CRecordset::useMultiRowFetch+CRecordset::userAllocMultiRowBuffers;
261: } else {
262: options+=CRecordset::skipDeletedRecords;
263: }
1.28 ! misha 264: */
1.9 paf 265: TRY {
266: rs.Open(
1.28 ! misha 267: (connection.fast_offset_search)?CRecordset::dynamic:CRecordset::forwardOnly,
1.9 paf 268: statement,
1.27 misha 269: options
270: );
1.9 paf 271: } CATCH_ALL (e) {
272: // could not fetch a table
273: TRY {
274: // then try resultless query
275: db->ExecuteSQL(statement);
276: // OK then
277: return;
278: } CATCH_ALL (e2) {
279: // still nothing good
280: _throw(services, e); // throw ORIGINAL exception
281: } END_CATCH_ALL
282: } END_CATCH_ALL
1.1 parser 283:
284: int column_count=rs.GetODBCFieldCount();
285: if(!column_count)
286: services._throw("result contains no columns");
287:
1.10 paf 288: if(column_count>MAX_COLS)
289: column_count=MAX_COLS;
290:
1.27 misha 291: SWORD column_types[MAX_COLS];
292:
1.12 paf 293: SQL_Error sql_error;
294: #define CHECK(afailed) if(afailed) services._throw(sql_error)
295:
1.1 parser 296: for(int i=0; i<column_count; i++){
297: CString string;
298: CODBCFieldInfo fieldinfo;
299: rs.GetODBCFieldInfo(i, fieldinfo);
1.10 paf 300: column_types[i]=fieldinfo.m_nSQLType;
1.20 paf 301: size_t length=fieldinfo.m_strName.GetLength();
1.14 paf 302: char *str=0;
1.27 misha 303: if(length){
1.20 paf 304: str=(char*)services.malloc_atomic(length+1);
1.27 misha 305: memcpy(str, (char*)LPCTSTR(fieldinfo.m_strName), length+1);
1.20 paf 306:
1.27 misha 307: // transcode column name from ?ClientCharset to $request:charset
308: if(transcode_needed){
1.20 paf 309: services.transcode(str, length,
310: str, length,
1.27 misha 311: connection.client_charset,
1.20 paf 312: services.request_charset());
313: }
1.1 parser 314: }
1.20 paf 315: CHECK(handlers.add_column(sql_error, str, length));
1.1 parser 316: }
317:
1.12 paf 318: CHECK(handlers.before_rows(sql_error));
1.27 misha 319:
320: // skip offset rows
321: if(offset){
1.28 ! misha 322: if(connection.fast_offset_search){
1.27 misha 323: rs.Move(offset);
324: } else {
325: unsigned long row=offset;
326: while(!rs.IsEOF() && row>0){
327: rs.MoveNext();
328: row--;
329: }
330: }
331: }
1.1 parser 332:
333: unsigned long row=0;
1.10 paf 334: CDBVariant v;
335: CString s;
1.27 misha 336: while(!rs.IsEOF() && (limit==SQL_NO_LIMIT || row<limit)){
337: CHECK(handlers.add_row(sql_error));
338: for(int i=0; i<column_count; i++){
339: size_t length;
340: char* str;
341: switch(column_types[i]){
1.7 paf 342: //case xBOOL:
1.27 misha 343: //case SQL_INTEGER: // serg@design.ru did that in parser2. test first!
1.14 paf 344: //case SQL_DATETIME: << default: handles that more properly (?)
345: case SQL_BINARY:
1.11 paf 346: case SQL_VARBINARY:
347: case SQL_LONGVARBINARY:
1.7 paf 348: case SQL_SMALLDATETIME:
1.27 misha 349: //case SQL_NVARCHAR: // mfc 7.1 has errors with nvarchar(length): SQLGetData in dbcore.cpp truncates last byte for unknown reason.
350: // could be fixed by uncommenting this and handing DBVT_WSTRING inside, but it's UNICODE
1.10 paf 351: rs.GetFieldValue(i, v);
1.20 paf 352: getFromDBVariant(services, v, str, length);
1.10 paf 353: break;
1.7 paf 354: default:
1.10 paf 355: rs.GetFieldValue(i, s);
1.20 paf 356: getFromString(services, s, str, length);
1.10 paf 357: break;
1.27 misha 358: }
1.20 paf 359:
1.27 misha 360: // transcode cell value from ?ClientCharset to $request:charset
361: if(transcode_needed && length)
362: services.transcode(str, length,
363: str, length,
364: connection.client_charset,
365: services.request_charset());
1.20 paf 366:
1.27 misha 367: CHECK(handlers.add_row_cell(sql_error, str, length));
1.1 parser 368: }
1.27 misha 369: rs.MoveNext();
370: row++;
1.1 parser 371: }
372:
373: rs.Close();
374: } else {
375: db->ExecuteSQL(statement);
376: }
1.9 paf 377: } CATCH_ALL (e) {
1.1 parser 378: _throw(services, e);
1.9 paf 379: } END_CATCH_ALL
1.7 paf 380: }
381:
1.27 misha 382: private:
383: void getFromDBVariant(SQL_Driver_services& services, CDBVariant& v, char*& str, size_t& length){
384: switch(v.m_dwType){
1.14 paf 385: case DBVT_BINARY: /* << would cause problems with current String implementation
386: now falling into NULL case, effectively ignoring such columns [not failing]
387: {
1.17 paf 388: if(length=v.m_pbinary->m_dwDataLength) {
389: str=services.malloc_atomic(length+1);
390: memcpy(ptr, ::GlobalLock(v.m_pbinary->m_hData), length);
1.14 paf 391: ::GlobalUnlock(v.m_pbinary->m_hData);
392: } else
393: str=0;
394: break;
395: }*/
1.7 paf 396: case DBVT_NULL: // No union member is valid for access.
1.14 paf 397: str=0;
1.17 paf 398: length=0;
1.7 paf 399: break;
400: /* case DBVT_BOOL:
401: ptr=v.m_boolVal?"1":"0";
1.17 paf 402: length=1;
1.7 paf 403: break;*/
404: /* case DBVT_UCHAR:
1.17 paf 405: length=strlen(ptr=v.m_chVal);
1.7 paf 406: break;
407: case DBVT_SHORT:
408: char buf[MAX_NUMBER];
1.17 paf 409: length=snprintf(HEAPIZE buf, "%d", v.m_iVal);
1.27 misha 410: break;
411: */
1.7 paf 412: /* case DBVT_LONG:
413: {
414: char local_buf[MAX_NUMBER];
1.17 paf 415: length=snprintf(local_buf, MAX_NUMBER, "%ld", v.m_lVal);
416: ptr=services.malloc_atomic(length);
417: memcpy(ptr, local_buf, length);
1.7 paf 418: break;
1.27 misha 419: }
420: */
421: /*
422: case DBVT_SINGLE:
1.7 paf 423: m_fltVal
424: break;
1.27 misha 425: case DBVT_DOUBLE m_dblVal
426: case DBVT_STRING m_pstring
427: */
1.7 paf 428: case DBVT_DATE:
429: {
430: char local_buf[MAX_STRING];
1.17 paf 431: length=snprintf(local_buf, MAX_STRING,
1.7 paf 432: "%04d-%02d-%02d %02d:%02d:%02d.%03d",
433: v.m_pdate->year,
434: v.m_pdate->month,
435: v.m_pdate->day,
436: v.m_pdate->hour,
437: v.m_pdate->minute,
438: v.m_pdate->second,
1.25 paf 439: v.m_pdate->fraction/1000000); // lexical parser of INCOMING literal choked on times like hh:mm:ss.123000000
1.17 paf 440: str=(char*)services.malloc_atomic(length+1);
441: memcpy(str, local_buf, length+1);
1.7 paf 442: break;
443: }
444: default:
445: char msg[MAX_STRING];
446: snprintf(msg, MAX_STRING, "unknown column return variant type (%d)",
447: v.m_dwType);
448: services._throw(msg);
449: }
450: }
451:
1.27 misha 452: void getFromString(SQL_Driver_services& services, CString& s, char*& astr, size_t& length){
453: if(s.IsEmpty()){
1.14 paf 454: astr=0;
1.17 paf 455: length=0;
1.7 paf 456: } else {
457: const char *cstr=LPCTSTR(s);
1.17 paf 458: length=strlen(cstr); //string.GetLength() works wrong with non-string types:
459: astr=(char*)services.malloc_atomic(length+1);
460: memcpy(astr, cstr, length+1);
1.7 paf 461: }
1.1 parser 462: }
463:
1.27 misha 464: void _throw(SQL_Driver_services& services, CException *e){
465: char szCause[MAX_STRING];
466: szCause[0]=0;
1.1 parser 467: e->GetErrorMessage(szCause, MAX_STRING);
468: char msg[MAX_STRING];
469: snprintf(msg, MAX_STRING, "%s: %s",
470: e->GetRuntimeClass()->m_lpszClassName,
471: *szCause?szCause:"unknown");
472: services._throw(msg);
473: }
474:
1.27 misha 475: void _throw(Connection& connection, long value){
476: char msg[MAX_STRING];
477: snprintf(msg, MAX_STRING, "%u", value);
478: connection.services->_throw(msg);
479: }
480:
481: bool _transcode_required(Connection& connection){
482: return (connection.client_charset && strcmp(connection.client_charset, connection.services->request_charset())!=0);
483: }
484:
1.1 parser 485: };
486:
487: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
488: return new ODBC_Driver();
1.26 paf 489: }