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