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