|
|
1.1 parser 1: /** @file
2: Parser Oracle driver.
3:
1.29 paf 4: Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.1 parser 5:
1.19 paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 7:
8: 2001.07.30 using Oracle 8.1.6 [@test tested with Oracle 7.x.x]
9: */
1.63 ! paf 10: /*
! 11: prob:
! 12: kgepop: no error frame to pop to for error 21500
! 13:
! 14:
! 15: > I happy to tell you that I have solved the problem.
! 16:
! 17: Excellent news.
! 18:
! 19: > By allocating a separate
! 20: > environment handle for every database handle I share
! 21: */
! 22:
! 23: static const char *RCSId="$Id: parser3oracle.C,v 1.62 2004/07/28 14:23:32 paf Exp $";
1.1 parser 24:
25: #include "config_includes.h"
26:
27: #include "pa_sql_driver.h"
28:
29: #include <oci.h>
30:
31: #define MAX_COLS 500
32: #define MAX_IN_LOBS 5
33: #define MAX_LOB_NAME_LENGTH 100
34: #define MAX_OUT_STRING_LENGTH 4000
1.61 paf 35: #define MAX_BINDS 100
1.1 parser 36:
37: #define EMPTY_CLOB_FUNC_CALL "empty_clob()"
38:
39: #include "ltdl.h"
40:
41: #define MAX_STRING 0x400
42: #define MAX_NUMBER 20
43:
44: #if _MSC_VER
45: # define snprintf _snprintf
46: # define strcasecmp _stricmp
47: # define strncasecmp _strnicmp
48: #endif
49:
50: #ifndef max
51: inline int max(int a, int b) { return a>b?a:b; }
52: inline int min(int a, int b){ return a<b?a:b; }
53: #endif
54:
1.45 paf 55: #if _MSC_VER
56: // interaction between '_setjmp' and C++ object destruction is non-portable
57: // but we forced to do that under HPUX
58: #pragma warning(disable:4611)
59: #endif
60:
1.61 paf 61: const sb2 MAGIC_INDICATOR_VALUE_MEANING_NOT_NULL_AND_UNCHANGED=99;
62:
1.33 paf 63: /// @todo small memory leaks here
1.10 paf 64: static int pa_setenv(const char *name, const char *value, bool do_append) {
65: const char *prev_value=0;
66: if(do_append)
67: prev_value=getenv(name);
1.4 paf 68: #ifdef HAVE_PUTENV
69: // MEM_LEAK_HERE. refer to EOF man putenv
1.10 paf 70: char *buf=(char *)::malloc(strlen(name)
71: +1
72: +(prev_value?strlen(prev_value):0)
73: +strlen(value)
74: +1);
1.4 paf 75: strcpy(buf, name);
76: strcat(buf, "=");
1.10 paf 77: if(prev_value)
78: strcat(buf, prev_value);
1.4 paf 79: strcat(buf, value);
1.5 paf 80: /*
81: if(FILE *f=fopen("f", "at")) {
82: fprintf(f, "****************************%s\n", buf);
83: // for (char **env = environ; env != NULL && *env != NULL; env++)
84: // fputs(*env, f);
85:
86: fclose(f);
87: }
88: */
1.4 paf 89: return putenv(buf);
90: #else
91: //#ifdef HAVE_SETENV
1.10 paf 92: if(value) {
93: if(prev_value) {
94: // MEM_LEAK_HERE
1.33 paf 95: char *buf=(char *)::malloc(strlen(prev_value)
1.10 paf 96: +strlen(value)
97: +1);
98: strcpy(buf, prev_value);
99: strcat(buf, value);
1.24 paf 100: value=buf;
101: }
102: return setenv(name, value, 1/*overwrite*/);
1.10 paf 103: } else {
1.4 paf 104: unsetenv(name);
105: return 0;
106: }
107: #endif
108: }
109:
1.1 parser 110: static char *lsplit(char *string, char delim) {
111: if(string) {
112: char *v=strchr(string, delim);
113: if(v) {
114: *v=0;
115: return v+1;
116: }
117: }
118: return 0;
119: }
120:
1.4 paf 121: static char *lsplit(char **string_ref, char delim) {
122: char *result=*string_ref;
123: char *next=lsplit(*string_ref, delim);
124: *string_ref=next;
125: return result;
126: }
127:
1.1 parser 128: #ifndef DOXYGEN
1.49 paf 129: struct Connection {
1.42 paf 130: SQL_Driver_services *services;
131:
1.1 parser 132: jmp_buf mark; char error[MAX_STRING];
1.27 paf 133: SQL_Error sql_error;
1.1 parser 134: OCIEnv *envhp;
135: OCIServer *srvhp;
136: OCIError *errhp;
137: OCISvcCtx *svchp;
138: OCISession *usrhp;
1.39 paf 139:
1.46 paf 140: char* fetch_buffers[MAX_COLS];
1.61 paf 141: char* bind_buffers[MAX_BINDS];
1.46 paf 142:
1.42 paf 143: struct Options {
144: bool bLowerCaseColumnNames;
145: const char* cstrClientCharset;
146: } options;
1.1 parser 147: };
148:
1.60 paf 149: struct Query_lobs {
1.1 parser 150: struct return_rows {
151: struct return_row {
152: OCILobLocator *locator; ub4 len;
153: int ind;
154: ub2 rcode;
155: } *row;
156: int count;
157: };
158: struct Item {
159: const char *name_ptr; size_t name_size;
160: char *data_ptr; size_t data_size;
161: OCILobLocator *locator;
162: OCIBind *bind;
163: return_rows rows;
1.57 paf 164: Connection *connection;
1.1 parser 165: } items[MAX_IN_LOBS];
166: int count;
167: };
1.60 paf 168:
1.1 parser 169: #endif
170:
171: // forwards
1.60 paf 172: static void faile(Connection& connection, const char *msg);
173: static void check(Connection& connection, const char *step, sword status);
174: static void check(Connection& connection, bool error);
1.1 parser 175: static sb4 cbf_no_data(
176: dvoid *ctxp,
177: OCIBind *bindp,
178: ub4 iter, ub4 index,
179: dvoid **bufpp,
180: ub4 *alenpp,
181: ub1 *piecep,
182: dvoid **indpp);
183: static sb4 cbf_get_data(dvoid *ctxp,
184: OCIBind *bindp,
185: ub4 iter, ub4 index,
186: dvoid **bufpp,
187: ub4 **alenp,
188: ub1 *piecep,
189: dvoid **indpp,
190: ub2 **rcodepp);
1.55 paf 191: static void tolower_str(char *out, const char *in, size_t size);
192: static void toupper_str(char *out, const char *in, size_t size);
1.1 parser 193:
1.49 paf 194: static const char *options2env(char *s, Connection::Options* options) {
1.42 paf 195: while(s) {
196: if(char *key=lsplit(&s, '&')) {
197: if(*key) {
198: if(char *value=lsplit(key, '=')) {
199: if( strcmp( key, "ClientCharset" ) == 0 ) {
1.44 paf 200: if(options) {
1.55 paf 201: toupper_str(value, value, strlen(value));
1.42 paf 202: options->cstrClientCharset = value;
1.44 paf 203: }
1.42 paf 204: continue;
205: }
206:
207: if( strcmp( key, "LowerCaseColumnNames" ) == 0 ) {
208: if(options)
209: options->bLowerCaseColumnNames = atoi(value)!=0;
210: continue;
211: }
212:
213: bool do_append=key[strlen(key)-1]=='+'; // PATH+=
214: if(do_append)
215: key[strlen(key)-1]=0; // remove trailing +
216: if(strncmp(key, "ORACLE_", 7)==0 // ORACLE_HOME & co
217: || strncmp(key, "ORA_", 4)==0 // ORA_ENCRYPT_LOGIN & co
218: || strncmp(key, "NLS_", 4)==0 // NLS_LANG & co
219: || do_append
220: ) {
221: if(pa_setenv(key, value, do_append)!=0)
222: return "problem changing process environment" /*key*/;
223: } else
224: return "unknown option" /*key*/;
225: } else
226: return "option without =value" /*key*/;
227: }
228: }
229: }
230: return 0;
231: }
232:
1.1 parser 233: /**
234: OracleSQL server driver
235: */
236: class OracleSQL_Driver : public SQL_Driver {
237: public:
238:
239: OracleSQL_Driver() : SQL_Driver() {
240: }
241:
242: /// get api version
243: int api_version() { return SQL_DRIVER_API_VERSION; }
1.6 paf 244: /** initialize driver by loading sql dynamic link library
245: @todo ?objects=1 which would turn on OCI_OBJECT init flag
246: */
1.5 paf 247: const char *initialize(char *dlopen_file_spec) {
248: char *options=lsplit(dlopen_file_spec, '?');
1.1 parser 249:
250: const char *error=dlopen_file_spec?
251: dlink(dlopen_file_spec):"client library column is empty";
252: if(!error) {
1.39 paf 253: error=options2env(options, 0);
1.1 parser 254:
1.5 paf 255: if(!error)
256: OCIInitialize((ub4)OCI_THREADED/*| OCI_OBJECT*/, (dvoid *)0,
257: (dvoid * (*)(void *, unsigned int))0,
258: (dvoid * (*)(void*, void*, unsigned int))0,
259: (void (*)(void*, void*))0
260: );
1.1 parser 261: }
262:
263: return error;
264: }
265:
266: /** connect
1.58 paf 267: @param url
1.4 paf 268: format: @b user:pass@service?
269: ORACLE_HOME=/u01/app/oracle/product/8.1.5&
1.5 paf 270: ORA_NLS33=/u01/app/oracle/product/8.1.5/ocommon/nls/admin/data&
1.4 paf 271: NLS_LANG=RUSSIAN_AMERICA.CL8MSWIN1251&
272: ORA_ENCRYPT_LOGIN=TRUE
273:
274: @todo environment manupulation doesnt look thread safe
1.58 paf 275: @todo allocate 'aused_only_in_connect_url' on gc heap, so it can be manipulated directly
1.1 parser 276: */
277: void connect(
1.58 paf 278: char *url,
1.1 parser 279: SQL_Driver_services& services,
1.50 paf 280: void **connection_ref ///< output: Connection *
1.56 paf 281: )
282: {
283: // connections are cross-request, do not use services._alloc [linked with request]
1.58 paf 284: Connection& connection=*(Connection *)services.malloc(sizeof(Connection));
1.49 paf 285: connection.services=&services;
286: connection.options.bLowerCaseColumnNames = true;
1.50 paf 287: *connection_ref=&connection;
1.1 parser 288:
1.58 paf 289: char *user=url;
1.1 parser 290: char *service=lsplit(user, '@');
291: char *pwd=lsplit(user, ':');
1.4 paf 292: char *options=lsplit(service, '?');
1.1 parser 293:
294: if(!(user && pwd && service))
295: services._throw("mailformed connect part, must be 'user:pass@service'");
296:
1.49 paf 297: if(const char *error=options2env(options, &connection.options))
1.5 paf 298: services._throw(error);
1.4 paf 299:
1.49 paf 300: if(setjmp(connection.mark))
301: services._throw(connection.error);
1.1 parser 302:
303: // Allocate and initialize OCIError handle, attempt #1
304: /*
305: grabbed from sample
306: /server.804/a58234/oci_func.htm#446192
307: but doc
308: /server.804/a58234/oci_func.htm#446100
309: doesnt have this param listed as allowed
310: 8.1.6 client library barks as OCI_INVALID_HANDLE
311: and debugging revealed that OCI_HTYPE_ENV param value is invalid
312: later in doc
313: /server.804/a58234/oci_func.htm#446192
314: on OCIEnvInit thay say
315: "No changes are done to an already initialized handle"
316: think, this is some sort of backward compatibility wonder.
317: leaving as it is, and without check()
318: */
1.49 paf 319: OCIHandleAlloc((dvoid *)NULL, (dvoid **) &connection.envhp, (ub4)OCI_HTYPE_ENV, 0, 0);
1.1 parser 320: // Initialize an environment handle, attempt #2
1.49 paf 321: check(connection, "EnvInit", OCIEnvInit(
322: &connection.envhp, (ub4)OCI_DEFAULT, 0, 0));
1.1 parser 323: // Allocate and initialize OCIError handle
1.49 paf 324: check(connection, "HandleAlloc errhp", OCIHandleAlloc(
325: (dvoid *)connection.envhp, (dvoid **) &connection.errhp, (ub4)OCI_HTYPE_ERROR, 0, 0));
1.1 parser 326: // Allocate and initialize OCIServer handle
1.49 paf 327: check(connection, "HandleAlloc srvhp", OCIHandleAlloc(
328: (dvoid *)connection.envhp, (dvoid **) &connection.srvhp, (ub4)OCI_HTYPE_SERVER, 0, 0));
1.1 parser 329: // Attach to a 'service'; initialize server context handle
1.49 paf 330: check(connection, "ServerAttach", OCIServerAttach(
331: connection.srvhp, connection.errhp, (text *)service, (sb4)strlen(service), (ub4)OCI_DEFAULT));
1.1 parser 332: // Allocate and initialize OCISvcCtx handle
1.49 paf 333: check(connection, "HandleAlloc svchp", OCIHandleAlloc(
334: (dvoid *)connection.envhp, (dvoid **) &connection.svchp, (ub4)OCI_HTYPE_SVCCTX, 0, 0));
1.1 parser 335: // set attribute server context in the service context
1.49 paf 336: check(connection, "AttrSet server-service", OCIAttrSet(
337: (dvoid *)connection.svchp, (ub4)OCI_HTYPE_SVCCTX,
338: (dvoid *)connection.srvhp, (ub4)0,
339: (ub4)OCI_ATTR_SERVER, (OCIError *)connection.errhp));
1.1 parser 340: // allocate a user context handle
1.49 paf 341: check(connection, "HandleAlloc usrhp", OCIHandleAlloc(
342: (dvoid *)connection.envhp, (dvoid **)&connection.usrhp, (ub4)OCI_HTYPE_SESSION, 0, 0));
1.1 parser 343: // set 'user' name
1.49 paf 344: check(connection, "AttrSet user-session", OCIAttrSet(
345: (dvoid *)connection.usrhp, (ub4)OCI_HTYPE_SESSION,
1.1 parser 346: (dvoid *)user, (ub4)strlen(user),
1.49 paf 347: OCI_ATTR_USERNAME, connection.errhp));
1.1 parser 348: // set 'pwd' password
1.49 paf 349: check(connection, "AttrSet pwd-session", OCIAttrSet(
350: (dvoid *)connection.usrhp, (ub4)OCI_HTYPE_SESSION,
1.1 parser 351: (dvoid *)pwd, (ub4)strlen(pwd),
1.49 paf 352: OCI_ATTR_PASSWORD, connection.errhp));
1.1 parser 353: // Authenticate a user
1.49 paf 354: check(connection, "SessionBegin", OCISessionBegin(
355: connection.svchp, connection.errhp, connection.usrhp,
1.1 parser 356: OCI_CRED_RDBMS, OCI_DEFAULT));
357: // remember connection in session
1.49 paf 358: check(connection, "AttrSet service-session", OCIAttrSet(
359: (dvoid *)connection.svchp, (ub4)OCI_HTYPE_SVCCTX,
360: (dvoid *)connection.usrhp, (ub4)0,
361: OCI_ATTR_SESSION, connection.errhp));
1.1 parser 362: }
1.49 paf 363: void disconnect(void *aconnection) {
364: Connection& connection=*static_cast<Connection *>(aconnection);
1.47 paf 365:
1.58 paf 366: // free fetch buffers. leave that to GC [no such services func. yet?]
367: /*
1.47 paf 368: for(int i=0; i<MAX_COLS; i++) {
1.49 paf 369: if(void* fetch_buffer=connection.fetch_buffers[i])
1.58 paf 370: connection.services->free(fetch_buffer);
1.47 paf 371: else
372: break;
1.58 paf 373: }
374: */
1.47 paf 375:
1.1 parser 376: // Terminate a user session
377: OCISessionEnd(
1.49 paf 378: connection.svchp, connection.errhp, connection.usrhp, (ub4)OCI_DEFAULT);
1.1 parser 379: // Detach from a server; uninitialize server context handle
380: OCIServerDetach(
1.49 paf 381: connection.srvhp, connection.errhp, (ub4)OCI_DEFAULT);
1.1 parser 382: // Free a previously allocated handles
383: /*
384: oci will free them up as belonging to env
385: OCIHandleFree(
1.49 paf 386: (dvoid *)connection.srvhp, (ub4)OCI_HTYPE_SERVER);
1.1 parser 387: OCIHandleFree(
1.49 paf 388: (dvoid *)connection.svchp, (ub4)OCI_HTYPE_SVCCTX);
1.1 parser 389: OCIHandleFree(
1.49 paf 390: (dvoid *)connection.errhp, (ub4)OCI_HTYPE_ERROR);
1.1 parser 391: */
392: OCIHandleFree(
1.49 paf 393: (dvoid *)connection.envhp, (ub4)OCI_HTYPE_ENV);
1.1 parser 394:
1.58 paf 395: // free connection. leave that to GC [no such services func. yet?]
396: // connection.services->free(&connection);
1.1 parser 397: }
1.49 paf 398: void commit(void *aconnection) {
399: Connection& connection=*static_cast<Connection *>(aconnection);
400: if(setjmp(connection.mark))
401: connection.services->_throw(connection.error);
1.1 parser 402:
1.49 paf 403: check(connection, "commit", OCITransCommit(connection.svchp, connection.errhp, 0));
1.1 parser 404: }
1.49 paf 405: void rollback(void *aconnection) {
406: Connection& connection=*static_cast<Connection *>(aconnection);
407: if(setjmp(connection.mark))
408: connection.services->_throw(connection.error);
1.1 parser 409:
1.42 paf 410: // sometimes rollback is done in context when this yields error which masks previous error
411: // consider consequent errors not very important to report, reporting first one
1.49 paf 412: /*check(connection, "rollback", */OCITransRollback(connection.svchp, connection.errhp, 0)/*)*/;
1.1 parser 413: }
414:
1.45 paf 415: bool ping(void* /*connection*/) {
1.1 parser 416: // maybe OCIServerVersion?
1.4 paf 417: // select 0 from dual
1.1 parser 418: return true;
419: }
420:
1.49 paf 421: const char* quote(void *aconnection,
1.42 paf 422: const char *from, unsigned int length)
423: {
1.49 paf 424: Connection& connection=*static_cast<Connection *>(aconnection);
425: char *result=(char*)connection.services->malloc_atomic(length*2+1);
1.32 paf 426: char *to=result;
427: while(length--) {
428: switch(*from) {
429: case '\'': // "'" -> "''"
1.35 paf 430: *to++='\'';
1.32 paf 431: break;
1.1 parser 432: }
1.32 paf 433: *to++=*from++;
434: }
435: *to=0;
436: return result;
1.1 parser 437: }
1.60 paf 438: void query(void* aconnection,
439: const char* astatement,
440: size_t placeholders_count, Placeholder* placeholders,
441: unsigned long offset, unsigned long limit,
1.42 paf 442: SQL_Driver_query_event_handlers& handlers)
443: {
1.61 paf 444:
1.49 paf 445: Connection& connection=*static_cast<Connection *>(aconnection);
1.54 paf 446: const char* cstrClientCharset=connection.options.cstrClientCharset;
1.60 paf 447: Query_lobs lobs={{0}, 0};
1.1 parser 448: OCIStmt *stmthp=0;
449:
1.49 paf 450: SQL_Driver_services& services=*connection.services;
1.42 paf 451:
452: // transcode from $request:charset to connect-string?client_charset
1.54 paf 453: if(cstrClientCharset) {
1.60 paf 454: size_t transcoded_xxx_size;
1.42 paf 455: services.transcode(astatement, strlen(astatement),
1.60 paf 456: astatement, transcoded_xxx_size,
1.42 paf 457: services.request_charset(),
458: cstrClientCharset);
1.53 paf 459: }
1.42 paf 460:
1.1 parser 461: bool failed=false;
1.49 paf 462: if(setjmp(connection.mark)) {
1.1 parser 463: failed=true;
464: goto cleanup;
465: } else {
1.61 paf 466: if(placeholders_count>MAX_BINDS)
467: fail(connection, "too many bind variables");
468:
1.49 paf 469: const char *statement=preprocess_statement(connection, astatement, lobs);
1.1 parser 470:
1.49 paf 471: check(connection, "HandleAlloc STMT", OCIHandleAlloc(
472: (dvoid *)connection.envhp, (dvoid **) &stmthp, (ub4)OCI_HTYPE_STMT, 0, 0));
473: check(connection, "syntax",
474: OCIStmtPrepare(stmthp, connection.errhp, (unsigned char *)statement,
1.1 parser 475: (ub4)strlen((char *)statement),
476: (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
1.60 paf 477:
478: struct Bind_info {
479: OCIBind *bind;
480: sb2 indicator;
481: };
482:
483: int binds_size=sizeof(Bind_info) * placeholders_count;
484: Bind_info* binds=static_cast<Bind_info*>(services.malloc_atomic(binds_size));
1.1 parser 485: {
1.60 paf 486: for(size_t i=0; i<placeholders_count; i++) {
487: Placeholder& ph=placeholders[i];
488: Bind_info& bi=binds[i];
489: bi.bind=0;
1.61 paf 490: // http://i/docs/oracle/server.804/a58234/basics.htm#422173
491: bi.indicator=ph.is_null? -1: MAGIC_INDICATOR_VALUE_MEANING_NOT_NULL_AND_UNCHANGED;
1.60 paf 492:
493: size_t value_length;
494:
495: if(cstrClientCharset) {
496: size_t name_length;
497: services.transcode(ph.name, strlen(ph.name),
498: ph.name, name_length,
499: services.request_charset(),
500: cstrClientCharset);
501:
502: if(ph.value)
503: services.transcode(ph.value, strlen(ph.value),
504: ph.value, value_length,
505: services.request_charset(),
506: cstrClientCharset);
507: } else {
508: value_length=ph.value? strlen(ph.value): 0;
509: }
510:
1.63 ! paf 511: {
! 512: // clone value for possible output binds
! 513: // note: even empty input can be replaced by huge output
1.61 paf 514: char*& buf=connection.bind_buffers[i]; // get cached buffer
515: if(!buf) // allocate if needed, caching it
516: buf=(char *)services.malloc_atomic(MAX_OUT_STRING_LENGTH+1/*terminator*/);
1.63 ! paf 517: if(value_length)
! 518: memcpy(buf, ph.value, value_length+1);
1.61 paf 519: ph.value=buf;
520: }
521:
1.60 paf 522: char placeholder_buf[MAX_STRING];
523: sb4 placeh_len=snprintf(placeholder_buf, sizeof(placeholder_buf), ":%s", ph.name);
1.61 paf 524: char check_step_buf[MAX_STRING];
525: snprintf(check_step_buf, sizeof(check_step_buf), "bind by name :%s", ph.name);
526: check(connection, check_step_buf, OCIBindByName(stmthp,
1.60 paf 527: &bi.bind, connection.errhp,
528: (text*)placeholder_buf, placeh_len,
1.61 paf 529: (dvoid *)ph.value, (sword)(MAX_OUT_STRING_LENGTH+1), SQLT_STR, (dvoid *)&bi.indicator,
1.60 paf 530: (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT));
531: }
532:
1.1 parser 533: for(int i=0; i<lobs.count; i++) {
1.60 paf 534: Query_lobs::Item &item=lobs.items[i];
1.49 paf 535: check(connection, "alloc output var desc", OCIDescriptorAlloc(
1.57 paf 536: (dvoid *)connection.envhp, (dvoid **)&item.locator, (ub4)OCI_DTYPE_LOB, 0, 0));
1.1 parser 537:
1.59 paf 538: char placeholder_buf[MAX_STRING];
539: sb4 placeh_len=snprintf(placeholder_buf, sizeof(placeholder_buf),
540: ":%.*s", item.name_size, item.name_ptr);
541: check(connection, "bind lob", OCIBindByName(stmthp,
1.57 paf 542: &item.bind, connection.errhp,
1.59 paf 543: (text*)placeholder_buf, placeh_len,
1.57 paf 544: (dvoid *)&item.locator,
545: (sword)sizeof (item.locator), SQLT_CLOB, (dvoid *)0,
1.1 parser 546: (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DATA_AT_EXEC));
547:
1.57 paf 548: item.rows.count=0;
549: item.connection=&connection;
1.49 paf 550: check(connection, "bind dynamic", OCIBindDynamic(
1.57 paf 551: item.bind, connection.errhp,
552: (dvoid *) &item, cbf_no_data,
553: (dvoid *) &item, cbf_get_data));
1.1 parser 554: }
555: }
556:
1.49 paf 557: execute_prepared(connection,
1.26 paf 558: statement, stmthp, lobs,
559: offset, limit, handlers);
1.60 paf 560:
561: {
562: for(size_t i=0; i<placeholders_count; i++) {
563: Placeholder& ph=placeholders[i];
564: Bind_info& bi=binds[i];
565:
1.61 paf 566: if(bi.indicator==MAGIC_INDICATOR_VALUE_MEANING_NOT_NULL_AND_UNCHANGED/*unchanged*/)
1.60 paf 567: continue;
568:
569: if(bi.indicator==-1)
570: ph.is_null=true;
571: else
572: if(bi.indicator==0)
573: ph.is_null=false;
574: else
575: fail(connection, bi.indicator<0?
1.61 paf 576: "output bind buffer overflow, additionally size too big to be returned in 'indicator'"
577: : "output bind buffer overflow");
1.60 paf 578:
579: ph.were_updated=true;
580:
581: if(cstrClientCharset) {
582: if(ph.value) {
583: size_t value_length;
584: services.transcode(ph.value, strlen(ph.value),
585: ph.value, value_length,
586: cstrClientCharset,
587: services.request_charset());
588: }
589: }
590: }
591: }
1.1 parser 592: }
593: cleanup: // no check call after this point!
594: {
595: for(int i=0; i<lobs.count; i++) {
596: /* free var locator */
597: if(OCILobLocator *locator=lobs.items[i].locator)
598: OCIDescriptorFree((dvoid *)locator, (ub4)OCI_DTYPE_LOB);
599:
600: /* free rows descriptors */
1.60 paf 601: Query_lobs::return_rows &rows=lobs.items[i].rows;
1.1 parser 602: for(int r=0; r<rows.count; r++)
603: OCIDescriptorFree((dvoid *)rows.row[r].locator, (ub4)OCI_DTYPE_LOB);
604: }
605: }
606: if(stmthp)
607: OCIHandleFree((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT);
608:
1.26 paf 609: if(failed) {
1.49 paf 610: if(connection.sql_error.defined())
611: services._throw(connection.sql_error);
612: services._throw(connection.error);
1.26 paf 613: }
1.1 parser 614: }
615:
616: private: // private funcs
617:
1.49 paf 618: const char *preprocess_statement(Connection& connection,
1.60 paf 619: const char *astatement, Query_lobs &lobs) {
1.1 parser 620: size_t statement_size=strlen(astatement);
1.49 paf 621: SQL_Driver_services& services=*connection.services;
1.1 parser 622:
1.32 paf 623: char *result=(char *)services.malloc_atomic(statement_size
1.1 parser 624: +MAX_STRING // in case of short 'strings'
625: +11/* returning */+6/* into */+(MAX_LOB_NAME_LENGTH+2/*:, */)*2/*ret into*/*MAX_IN_LOBS
626: +1);
627: const char *o=astatement;
628:
629: // /**xxx**/'literal' -> EMPTY_CLOB_FUNC_CALL
630: char *n=result;
631: while(*o) {
632: if(
633: o[0]=='/' &&
634: o[1]=='*' &&
635: o[2]=='*') { // name start
1.34 paf 636: const char* saved_o=o;
1.1 parser 637: o+=3;
638: const char *name_begin=o;
639: while(*o)
640: if(
641: o[0]=='*' &&
642: o[1]=='*' &&
643: o[2]=='/' &&
644: o[3]=='\'') { // name end
1.34 paf 645: saved_o=0; // found, marking that
1.1 parser 646: const char *name_end=o;
647: o+=4;
1.60 paf 648: Query_lobs::Item &item=lobs.items[lobs.count++];
1.1 parser 649: item.name_ptr=name_begin; item.name_size=name_end-name_begin;
1.32 paf 650: item.data_ptr=(char *)services.malloc_atomic(statement_size/*max*/); item.data_size=0;
1.1 parser 651:
652: const char *start=o;
653: bool escaped=false;
654: while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
1.14 paf 655: escaped=o[0]=='\'' && o[1]=='\'';
1.1 parser 656: if(escaped) {
657: // write pending, skip "\" or "'"
658: if(size_t size=o-start) {
659: memcpy(item.data_ptr+item.data_size, start, size);
660: item.data_size+=size;
661: }
662: start=++o;
663: } else
664: o++;
665: }
666: if(size_t size=o-start) {
667: memcpy(item.data_ptr+item.data_size, start, size);
668: item.data_size+=size;
669: }
670: if(*o)
671: o++; // skip "'"
672:
673: n+=sprintf(n, EMPTY_CLOB_FUNC_CALL);
674: break;
675: } else
676: o++; // /**skip**/'xxx'
1.34 paf 677: if(saved_o) {
678: o=saved_o;
679: *n++=*o++;
680: }
1.1 parser 681: } else
682: *n++=*o++;
683: }
684: *n=0;
685:
686: if(lobs.count) {
687: int i;
688: n+=sprintf(n, " returning ");
689: for(i=0; i<lobs.count; i++) {
690: if(i)
691: *n++=',';
692: n+=sprintf(n, "%.*s", lobs.items[i].name_size, lobs.items[i].name_ptr);
693: }
694: n+=sprintf(n, " into ");
695: for(i=0; i<lobs.count; i++) {
696: if(i)
1.41 paf 697: *n++=',';
1.1 parser 698: n+=sprintf(n, ":%.*s", lobs.items[i].name_size, lobs.items[i].name_ptr);
699: }
700: }
701:
702: return result;
703: }
704:
705: void execute_prepared(
1.49 paf 706: Connection& connection,
1.60 paf 707: const char *statement, OCIStmt *stmthp, Query_lobs &lobs,
1.1 parser 708: unsigned long offset, unsigned long limit,
709: SQL_Driver_query_event_handlers& handlers) {
710:
711: ub2 stmt_type=0; // UNKNOWN
712: /*
713: //gpfs on sun. paf 000818
714: //Zanyway, this is needed before.
1.49 paf 715: check(connection, "get stmt type", OCIAttrGet(
1.1 parser 716: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT, (ub1 *)&stmt_type,
1.49 paf 717: (ub4 *)0, OCI_ATTR_STMT_TYPE, connection.errhp));
1.1 parser 718: */
1.16 paf 719:
1.62 paf 720: while(isspace((unsigned char)*statement))
1.16 paf 721: statement++;
1.1 parser 722: if(strncasecmp(statement, "select", 6)==0)
723: stmt_type=OCI_STMT_SELECT;
724: else if(strncasecmp(statement, "insert", 6)==0)
725: stmt_type=OCI_STMT_INSERT;
726: else if(strncasecmp(statement, "update", 6)==0)
727: stmt_type=OCI_STMT_UPDATE;
728:
1.49 paf 729: sword status=OCIStmtExecute(connection.svchp, stmthp, connection.errhp,
1.1 parser 730: (ub4)stmt_type==OCI_STMT_SELECT?0:1, (ub4)0,
731: (OCISnapshot *)NULL,
732: (OCISnapshot *)NULL, (ub4)OCI_DEFAULT);
733:
734: if(status!=OCI_NO_DATA)
1.49 paf 735: check(connection, "execute", status);
1.1 parser 736:
737: {
738: for(int i=0; i<lobs.count; i++)
739: if(ub4 bytes_to_write=lobs.items[i].data_size) {
1.60 paf 740: Query_lobs::return_rows *rows=&lobs.items[i].rows;
1.1 parser 741: for(int r=0; r<rows->count; r++) {
742: OCILobLocator *locator=rows->row[r].locator;
1.49 paf 743: check(connection, "lobwrite", OCILobWrite (
744: connection.svchp, connection.errhp,
1.1 parser 745: locator, &bytes_to_write, 1,
746: (dvoid *)lobs.items[i].data_ptr, (ub4)bytes_to_write, OCI_ONE_PIECE,
747: (dvoid *)0, 0, (ub2)0,
748: (ub1) SQLCS_IMPLICIT));
749: }
750: }
751: }
752:
753: switch(stmt_type) {
754: case OCI_STMT_SELECT:
1.49 paf 755: fetch_table(connection,
1.1 parser 756: stmthp, offset, limit,
757: handlers);
758: break;
759: default:
760: /*
761: case OCI_STMT_INSERT:
762: case OCI_STMT_UPDATE:
763: */
764: break;
765: }
766: }
767:
1.49 paf 768: void fetch_table(Connection& connection,
1.1 parser 769: OCIStmt *stmthp, unsigned long offset, unsigned long limit,
1.42 paf 770: SQL_Driver_query_event_handlers& handlers)
771: {
1.54 paf 772: const char* cstrClientCharset=connection.options.cstrClientCharset;
1.49 paf 773: SQL_Driver_services& services=*connection.services;
1.12 paf 774:
1.10 paf 775: ub4 prefetch_rows=100;
1.49 paf 776: check(connection, "AttrSet prefetch-rows", OCIAttrSet(
1.9 paf 777: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
778: (dvoid *)&prefetch_rows, (ub4)0,
1.49 paf 779: (ub4)OCI_ATTR_PREFETCH_ROWS, (OCIError *)connection.errhp));
1.9 paf 780:
1.20 paf 781: ub4 prefetch_mem_size=100*0x400;
1.49 paf 782: check(connection, "AttrSet prefetch-memory", OCIAttrSet(
1.9 paf 783: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
784: (dvoid *)&prefetch_mem_size, (ub4)0,
1.49 paf 785: (ub4)OCI_ATTR_PREFETCH_MEMORY, (OCIError *)connection.errhp));
1.1 parser 786:
787: OCIParam *mypard;
788: ub2 dtype;
1.51 paf 789: const char* col_name;
1.1 parser 790:
1.40 paf 791: struct Col {
1.1 parser 792: ub2 type;
793: char *str;
794: OCILobLocator *var;
795: OCIDefine *def;
796: sb2 indicator;
797: } cols[MAX_COLS]={0};
798: int column_count=0;
799:
800: bool failed=false;
1.49 paf 801: jmp_buf saved_mark; memcpy(saved_mark, connection.mark, sizeof(jmp_buf));
802: if(setjmp(connection.mark)) {
1.1 parser 803: failed=true;
804: goto cleanup;
805: } else {
1.27 paf 806: // idea of preincrementing is that at error time all handles would free up
807: while(++column_count<=MAX_COLS) {
808: /* get next descriptor, if there is one */
1.49 paf 809: if(OCIParamGet(stmthp, OCI_HTYPE_STMT, connection.errhp, (void **)&mypard,
1.27 paf 810: (ub4) column_count)!=OCI_SUCCESS) {
811: --column_count;
812: break;
813: }
814:
815: /* Retrieve the data type attribute */
1.49 paf 816: check(connection, "get type", OCIAttrGet(
1.27 paf 817: (dvoid*) mypard, (ub4)OCI_DTYPE_PARAM,
818: (dvoid*) &dtype, (ub4 *)0, (ub4)OCI_ATTR_DATA_TYPE,
1.49 paf 819: (OCIError *)connection.errhp));
1.27 paf 820:
821: /* Retrieve the column name attribute */
822: ub4 col_name_len;
1.49 paf 823: check(connection, "get name", OCIAttrGet(
1.27 paf 824: (dvoid*) mypard, (ub4)OCI_DTYPE_PARAM,
825: (dvoid**) &col_name, (ub4 *) &col_name_len, (ub4)OCI_ATTR_NAME,
1.49 paf 826: (OCIError *)connection.errhp));
1.51 paf 827: // transcode to $request:charset from connect-string?client_charset
1.54 paf 828: if(cstrClientCharset) {
1.51 paf 829: services.transcode(col_name, col_name_len,
830: col_name, col_name_len,
831: cstrClientCharset,
832: services.request_charset());
833: }
834:
1.40 paf 835: Col& col=cols[column_count-1];
1.27 paf 836: {
1.38 paf 837: size_t length=(size_t)col_name_len;
838: char *ptr=(char *)services.malloc_atomic(length+1);
1.49 paf 839: if( connection.options.bLowerCaseColumnNames )
1.55 paf 840: tolower_str(ptr, col_name, length);
1.39 paf 841: else
842: memcpy(ptr, col_name, length);
1.38 paf 843: ptr[length]=0;
1.49 paf 844: check(connection, handlers.add_column(connection.sql_error, ptr, length));
1.27 paf 845: }
846:
847: ub2 coerce_type=dtype;
848: sb4 size=0;
849: void *ptr;
850:
851: switch(dtype) {
852: case SQLT_CLOB:
1.1 parser 853: {
1.49 paf 854: check(connection, "alloc output var desc", OCIDescriptorAlloc(
855: (dvoid *)connection.envhp, (dvoid **)(ptr=&col.var),
1.27 paf 856: (ub4)OCI_DTYPE_LOB,
857: 0, (dvoid **)0));
858:
859: size=0;
1.1 parser 860: break;
861: }
1.27 paf 862: default:
863: coerce_type=SQLT_STR;
1.49 paf 864: char*& buf=connection.fetch_buffers[column_count-1];
1.46 paf 865: ptr=buf; // get cached buffer
866: if(!ptr) // allocate if needed, caching it
1.58 paf 867: ptr=buf=(char *)services.malloc_atomic(MAX_OUT_STRING_LENGTH+1/*terminator*/);
1.46 paf 868: col.str=(char*)ptr;
1.27 paf 869: size=MAX_OUT_STRING_LENGTH;
870: break;
1.1 parser 871: }
872:
1.40 paf 873: col.type=coerce_type;
1.1 parser 874:
1.48 paf 875: // http://i/docs/oracle/server.804/a58234/oci_func.htm#449680
876: // this call implicitly allocates the define handle
877: // http://sunsite.eunnet.net/documentation/oracle.8.0.4/server.804/a58234/basics.htm
878: // when a statement handle is freed, any bind and define handles associated with it
879: // are also freed
1.49 paf 880: col.def=0; check(connection, "DefineByPos", OCIDefineByPos(
881: stmthp, &col.def, connection.errhp,
1.27 paf 882: column_count, (ub1 *) ptr, size,
1.40 paf 883: coerce_type, (dvoid *) &col.indicator,
1.27 paf 884: (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
885: }
886:
1.49 paf 887: check(connection, handlers.before_rows(connection.sql_error));
1.27 paf 888:
889: for(unsigned long row=0; !limit||row<offset+limit; row++) {
1.49 paf 890: sword status=OCIStmtFetch(stmthp, connection.errhp, (ub4)1, (ub4)OCI_FETCH_NEXT,
1.27 paf 891: (ub4)OCI_DEFAULT);
892: if(status==OCI_NO_DATA)
893: break;
1.49 paf 894: check(connection, "fetch", status);
1.3 paf 895:
1.27 paf 896: if(row>=offset) {
1.49 paf 897: check(connection, handlers.add_row(connection.sql_error));
1.27 paf 898: for(int i=0; i<column_count; i++) {
1.37 paf 899: size_t length=0;
1.42 paf 900: char* strm=0;
1.60 paf 901:
902: sb2 indicator=cols[i].indicator;
903: if(indicator!=-1) { // not NULL
904: if(indicator!=0)
905: fail(connection, indicator<0?
906: "column return buffer overflow, additionally size too big to be returned in 'indicator'"
907: : "column return buffer overflow");
908:
1.27 paf 909: switch(cols[i].type) {
910: case SQLT_CLOB:
911: {
912: ub4 offset=1;
913: OCILobLocator *var=(OCILobLocator *)cols[i].var;
1.42 paf 914: size_t read_size=0;
1.45 paf 915: strm=(char*)services.malloc_atomic(1/*for terminator*/); // set type of memory block
1.42 paf 916: do {
917: char buf[MAX_STRING*10];
1.45 paf 918: ub4 amtp=0/*to be read in stream mode*/;
919: // http://i/docs/oracle/server.804/a58234/oci_func.htm#427818
1.49 paf 920: status=OCILobRead(connection.svchp, connection.errhp,
1.42 paf 921: var, &amtp, offset, (dvoid *)buf,
922: sizeof(buf),
923: (dvoid *)0, 0,
924: (ub2)0, (ub1)SQLCS_IMPLICIT);
925: if(status!=OCI_SUCCESS && status!=OCI_NEED_DATA)
1.49 paf 926: check(connection, "lobread", status);
1.42 paf 927:
1.45 paf 928: strm=(char*)services.realloc(strm, read_size+amtp+1/*for termintator*/);
1.42 paf 929: memcpy(strm+read_size, buf, amtp);
930: read_size+=amtp;
931: offset+=amtp;
932: } while(status==OCI_NEED_DATA);
933:
934: length=(size_t)read_size;
935: strm[length]=0;
1.1 parser 936: break;
937: }
1.27 paf 938: default:
1.32 paf 939: if(const char *value=cols[i].str) {
1.37 paf 940: length=strlen(value);
1.42 paf 941: strm=(char*)services.malloc_atomic(length+1);
942: memcpy(strm, value, length+1);
1.27 paf 943: } else {
1.37 paf 944: length=0;
1.42 paf 945: strm=0;
1.27 paf 946: }
947: break;
948: }
1.60 paf 949: }
1.42 paf 950:
951: const char* str=strm;
952: if(str && length)
953: {
954: // transcode to $request:charset from connect-string?client_charset
1.54 paf 955: if(cstrClientCharset)
1.42 paf 956: services.transcode(str, length,
1.53 paf 957: str, length,
1.42 paf 958: cstrClientCharset,
959: services.request_charset());
960: }
961:
1.49 paf 962: check(connection, handlers.add_row_cell(connection.sql_error, str, length));
1.1 parser 963: }
964: }
965: }
966: }
967:
968: cleanup: // no check call after this point!
969: for(int i=0; i<column_count; i++) {
970: switch(cols[i].type) {
971: case SQLT_CLOB:
972: /* free var locator */
973: OCIDescriptorFree((dvoid *) cols[i].var, (ub4)OCI_DTYPE_LOB);
974: break;
975: default:
976: break;
977: }
978: }
979:
980: if(failed) // need rethrow?
981: longjmp(saved_mark, 1);
982: }
983:
984: private: // conn client library funcs
985:
1.60 paf 986: friend void fail(Connection& connection, const char *msg);
1.49 paf 987: friend void check(Connection& connection, const char *step, sword status);
1.1 parser 988: friend sb4 cbf_get_data(dvoid *ctxp,
989: OCIBind *bindp,
990: ub4 iter, ub4 index,
991: dvoid **bufpp,
992: ub4 **alenp,
993: ub1 *piecep,
994: dvoid **indpp,
995: ub2 **rcodepp);
996:
997:
998: #define OCI_DECL(name, params) \
999: typedef sword (*t_OCI##name)params; t_OCI##name OCI##name
1000:
1001: OCI_DECL(Initialize, (ub4 mode, dvoid *ctxp,
1002: dvoid * (*malocfp)(dvoid *ctxp, size_t size),
1003: dvoid * (*ralocfp)(dvoid *ctxp, dvoid *memptr, size_t newsize),
1004: void (*mfreefp)(dvoid *ctxp, dvoid *memptr) ));
1005:
1006: OCI_DECL(EnvInit, (OCIEnv **envp, ub4 mode,
1007: size_t xtramem_sz, dvoid **usrmempp));
1008:
1009: OCI_DECL(AttrGet, (CONST dvoid *trgthndlp, ub4 trghndltyp,
1010: dvoid *attributep, ub4 *sizep, ub4 attrtype,
1011: OCIError *errhp));
1012:
1013: OCI_DECL(AttrSet, (dvoid *trgthndlp, ub4 trghndltyp, dvoid *attributep,
1014: ub4 size, ub4 attrtype, OCIError *errhp));
1015:
1016: OCI_DECL(BindByPos, (OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
1017: ub4 position, dvoid *valuep, sb4 value_sz,
1018: ub2 dty, dvoid *indp, ub2 *alenp, ub2 *rcodep,
1019: ub4 maxarr_len, ub4 *curelep, ub4 mode));
1020:
1.59 paf 1021: OCI_DECL(BindByName, (OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
1022: text* placeholder, sb4 placeh_len, dvoid *valuep, sb4 value_sz,
1023: ub2 dty, dvoid *indp, ub2 *alenp, ub2 *rcodep,
1024: ub4 maxarr_len, ub4 *curelep, ub4 mode));
1025:
1.1 parser 1026: OCI_DECL(BindDynamic, (OCIBind *bindp, OCIError *errhp, dvoid *ictxp,
1027: OCICallbackInBind icbfp, dvoid *octxp,
1028: OCICallbackOutBind ocbfp));
1029:
1030: OCI_DECL(DefineByPos, (OCIStmt *stmtp, OCIDefine **defnp, OCIError *errhp,
1031: ub4 position, dvoid *valuep, sb4 value_sz, ub2 dty,
1032: dvoid *indp, ub2 *rlenp, ub2 *rcodep, ub4 mode));
1033:
1034: OCI_DECL(DescriptorAlloc, (CONST dvoid *parenth, dvoid **descpp,
1035: CONST ub4 type, CONST size_t xtramem_sz,
1036: dvoid **usrmempp));
1037:
1038: OCI_DECL(DescriptorFree, (dvoid *descp, CONST ub4 type));
1039:
1040:
1041: OCI_DECL(ErrorGet, (dvoid *hndlp, ub4 recordno, OraText *sqlstate,
1042: sb4 *errcodep, OraText *bufp, ub4 bufsiz, ub4 type));
1043:
1044: OCI_DECL(HandleAlloc, (CONST dvoid *parenth, dvoid **hndlpp, CONST ub4 type,
1045: CONST size_t xtramem_sz, dvoid **usrmempp));
1046:
1047: OCI_DECL(HandleFree, (dvoid *hndlp, CONST ub4 type));
1048:
1049: OCI_DECL(LobGetLength, (OCISvcCtx *svchp, OCIError *errhp,
1050: OCILobLocator *locp,
1051: ub4 *lenp));
1052:
1053: OCI_DECL(LobRead, (OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *locp,
1054: ub4 *amtp, ub4 offset, dvoid *bufp, ub4 bufl,
1055: dvoid *ctxp, sb4 (*cbfp)(dvoid *ctxp,
1056: CONST dvoid *bufp,
1057: ub4 len,
1058: ub1 piece),
1059: ub2 csid, ub1 csfrm));
1060:
1061: OCI_DECL(LobWrite, (OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *locp,
1062: ub4 *amtp, ub4 offset, dvoid *bufp, ub4 buflen,
1063: ub1 piece, dvoid *ctxp,
1064: sb4 (*cbfp)(dvoid *ctxp,
1065: dvoid *bufp,
1066: ub4 *len,
1067: ub1 *piece),
1068: ub2 csid, ub1 csfrm));
1069:
1070: OCI_DECL(ParamGet, (CONST dvoid *hndlp, ub4 htype, OCIError *errhp,
1071: dvoid **parmdpp, ub4 pos));
1072:
1073: OCI_DECL(ServerAttach, (OCIServer *srvhp, OCIError *errhp,
1074: CONST OraText *dblink, sb4 dblink_len, ub4 mode));
1075:
1076: OCI_DECL(ServerDetach, (OCIServer *srvhp, OCIError *errhp, ub4 mode));
1077:
1078: OCI_DECL(SessionBegin, (OCISvcCtx *svchp, OCIError *errhp, OCISession *usrhp,
1079: ub4 credt, ub4 mode));
1080:
1081: OCI_DECL(SessionEnd, (OCISvcCtx *svchp, OCIError *errhp, OCISession *usrhp,
1082: ub4 mode));
1083:
1084: OCI_DECL(StmtExecute, (OCISvcCtx *svchp, OCIStmt *stmtp, OCIError *errhp,
1085: ub4 iters, ub4 rowoff, CONST OCISnapshot *snap_in,
1086: OCISnapshot *snap_out, ub4 mode));
1087:
1088: OCI_DECL(StmtFetch, (OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
1089: ub2 orientation, ub4 mode));
1090:
1091: OCI_DECL(StmtPrepare, (OCIStmt *stmtp, OCIError *errhp, CONST OraText *stmt,
1092: ub4 stmt_len, ub4 language, ub4 mode));
1093:
1094: OCI_DECL(TransCommit, (OCISvcCtx *svchp, OCIError *errhp, ub4 flags));
1095:
1096: OCI_DECL(TransRollback, (OCISvcCtx *svchp, OCIError *errhp, ub4 flags));
1097:
1098: private: // conn client library funcs linking
1099:
1100: const char *dlink(const char *dlopen_file_spec) {
1101: if(lt_dlinit())
1102: return lt_dlerror();
1103: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
1104: if(!handle)
1105: return lt_dlerror(); //"can not open the dynamic link module";
1106:
1107: #define DSLINK(name, action) \
1108: name=(t_##name)lt_dlsym(handle, #name); \
1109: if(!name) \
1110: action;
1111:
1112: #define OCI_LINK(name) DSLINK(OCI##name, return "function OCI" #name " was not found")
1113:
1114: OCI_LINK(Initialize);
1115: OCI_LINK(EnvInit);
1116: OCI_LINK(AttrGet); OCI_LINK(AttrSet);
1.59 paf 1117: OCI_LINK(BindByPos); OCI_LINK(BindByName); OCI_LINK(BindDynamic);
1.1 parser 1118: OCI_LINK(DefineByPos);
1119: OCI_LINK(DescriptorAlloc); OCI_LINK(DescriptorFree);
1120: OCI_LINK(ErrorGet);
1121: OCI_LINK(HandleAlloc); OCI_LINK(HandleFree);
1122: OCI_LINK(LobGetLength);
1123: OCI_LINK(LobRead); OCI_LINK(LobWrite);
1124: OCI_LINK(ParamGet);
1125: OCI_LINK(ServerAttach); OCI_LINK(ServerDetach);
1126: OCI_LINK(SessionBegin); OCI_LINK(SessionEnd);
1127: OCI_LINK(StmtExecute); OCI_LINK(StmtFetch); OCI_LINK(StmtPrepare);
1128: OCI_LINK(TransCommit); OCI_LINK(TransRollback);
1129:
1130: return 0;
1131: }
1132:
1133: } *OracleSQL_driver;
1134:
1.49 paf 1135: void check(Connection& connection, const char *step, sword status) {
1.1 parser 1136:
1137: const char *msg;
1138: char reason[MAX_STRING/2];
1139:
1140: switch (status) {
1.22 paf 1141: case OCI_SUCCESS: // hurrah
1142: case OCI_SUCCESS_WITH_INFO: // ignoring. example: count(column) when column contains NULLs,
1143: // count() not counting them and gives that status
1144: return;
1.1 parser 1145: case OCI_ERROR:
1146: {
1.45 paf 1147: sb4 errcode;
1.49 paf 1148: if(OracleSQL_driver->OCIErrorGet((dvoid *)connection.errhp, (ub4)1, (text *)NULL, &errcode,
1.45 paf 1149: (text *)reason, (ub4)sizeof(reason), OCI_HTYPE_ERROR)==OCI_SUCCESS) {
1150: msg=reason;
1151:
1152: // transcode to $request:charset from connect-string?client_charset
1.49 paf 1153: if(const char* cstrClientCharset=connection.options.cstrClientCharset) {
1.45 paf 1154: if(msg) {
1155: if(size_t msg_length=strlen(msg)) {
1.49 paf 1156: connection.services->transcode(msg, msg_length,
1.45 paf 1157: msg, msg_length,
1158: cstrClientCharset,
1.49 paf 1159: connection.services->request_charset());
1.45 paf 1160: }
1.42 paf 1161: }
1162: }
1.45 paf 1163: } else
1164: msg="[can not get error description]";
1165: break;
1.1 parser 1166: }
1167: case OCI_NEED_DATA:
1168: msg="NEED_DATA"; break;
1169: case OCI_NO_DATA:
1170: msg="NODATA"; break;
1171: case OCI_INVALID_HANDLE:
1172: msg="INVALID_HANDLE"; break;
1173: case OCI_STILL_EXECUTING:
1174: msg="STILL_EXECUTE"; break;
1175: case OCI_CONTINUE:
1176: msg="CONTINUE"; break;
1177: default:
1178: msg="unknown"; break;
1179: }
1180:
1.49 paf 1181: snprintf(connection.error, sizeof(connection.error), "%s (%s, %d)",
1.22 paf 1182: msg, step, (int)status);
1.49 paf 1183: longjmp(connection.mark, 1);
1.1 parser 1184: }
1185:
1.60 paf 1186: void fail(Connection& connection, const char* msg) {
1187: snprintf(connection.error, sizeof(connection.error), "%s", msg);
1188: longjmp(connection.mark, 1);
1189: }
1190:
1.49 paf 1191: void check(Connection& connection, bool error) {
1.27 paf 1192: if(error)
1.49 paf 1193: longjmp(connection.mark, 1);
1.27 paf 1194: }
1.1 parser 1195:
1196: /* ----------------------------------------------------------------- */
1197: /* Intbind callback that does not do any data input. */
1198: /* ----------------------------------------------------------------- */
1.60 paf 1199: sb4 cbf_no_data(
1.45 paf 1200: dvoid* /*ctxp*/,
1201: OCIBind* /*bindp*/,
1202: ub4 /*iter*/, ub4 /*index*/,
1.1 parser 1203: dvoid **bufpp,
1204: ub4 *alenpp,
1205: ub1 *piecep,
1206: dvoid **indpp) {
1207: *bufpp=(dvoid *)0;
1208: *alenpp=0;
1209: static sb2 null_ind=-1;
1210: *indpp=(dvoid *) &null_ind;
1211: *piecep=OCI_ONE_PIECE;
1212:
1213: return OCI_CONTINUE;
1214: }
1215:
1216: /* ----------------------------------------------------------------- */
1217: /* Outbind callback for returning data. */
1218: /* ----------------------------------------------------------------- */
1219: static sb4 cbf_get_data(dvoid *ctxp,
1220: OCIBind *bindp,
1.45 paf 1221: ub4 /*iter*/, ub4 index,
1.1 parser 1222: dvoid **bufpp,
1223: ub4 **alenp,
1224: ub1 *piecep,
1225: dvoid **indpp,
1226: ub2 **rcodepp) {
1.60 paf 1227: Query_lobs::Item& context=*static_cast<Query_lobs::Item*>(ctxp);
1.1 parser 1228:
1229: if(index==0) {
1230: static ub4 rows;
1.49 paf 1231: check(*context.connection, "AttrGet cbf_get_data ROWS_RETURNED",
1.1 parser 1232: OracleSQL_driver->OCIAttrGet(
1233: (CONST dvoid *) bindp, OCI_HTYPE_BIND, (dvoid *)&rows,
1.49 paf 1234: (ub4 *)sizeof(ub2), OCI_ATTR_ROWS_RETURNED, context.connection->errhp)) ;
1.57 paf 1235: context.rows.count=(ub2)rows;
1.60 paf 1236: context.rows.row=(Query_lobs::return_rows::return_row *)
1237: context.connection->services->malloc_atomic(sizeof(Query_lobs::return_rows::return_row)*rows);
1.1 parser 1238: }
1239:
1.60 paf 1240: Query_lobs::return_rows::return_row &var=context.rows.row[index];
1.1 parser 1241:
1.49 paf 1242: check(*context.connection, "alloc output var desc dynamic", OracleSQL_driver->OCIDescriptorAlloc(
1243: (dvoid *) context.connection->envhp, (dvoid **)&var.locator,
1.1 parser 1244: (ub4)OCI_DTYPE_LOB,
1245: 0, (dvoid **)0));
1246:
1247: *bufpp=var.locator;
1248: *alenp=&var.len;
1249: *indpp=(dvoid *) &var.ind;
1250: *piecep=OCI_ONE_PIECE;
1251: *rcodepp=&var.rcode;
1252:
1253: return OCI_CONTINUE;
1254: }
1255:
1.55 paf 1256: static void tolower_str(char *out, const char *in, size_t size) {
1.38 paf 1257: while(size--)
1.45 paf 1258: *out++=(char)tolower(*in++);
1.1 parser 1259: }
1.55 paf 1260: static void toupper_str(char *out, const char *in, size_t size) {
1.44 paf 1261: while(size--)
1.45 paf 1262: *out++=(char)toupper(*in++);
1.44 paf 1263: }
1264:
1.1 parser 1265:
1266: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
1267: return OracleSQL_driver=new OracleSQL_Driver();
1268: }