|
|
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.65 ! paf 11: static const char *RCSId="$Id: parser3oracle.C,v 1.64 2004/08/03 11:20:13 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.61 paf 509:
1.65 ! paf 510: char name_buf[MAX_STRING];
! 511: sb4 placeh_len=snprintf(name_buf, sizeof(name_buf), ":%s", ph.name);
1.61 paf 512: char check_step_buf[MAX_STRING];
513: snprintf(check_step_buf, sizeof(check_step_buf), "bind by name :%s", ph.name);
514: check(connection, check_step_buf, OCIBindByName(stmthp,
1.60 paf 515: &bi.bind, connection.errhp,
1.65 ! paf 516: (text*)name_buf, placeh_len,
! 517: (dvoid *)value_buf, (sword)(MAX_OUT_STRING_LENGTH+1), SQLT_STR, (dvoid *)&bi.indicator,
1.60 paf 518: (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DEFAULT));
519: }
520:
1.1 parser 521: for(int i=0; i<lobs.count; i++) {
1.60 paf 522: Query_lobs::Item &item=lobs.items[i];
1.49 paf 523: check(connection, "alloc output var desc", OCIDescriptorAlloc(
1.57 paf 524: (dvoid *)connection.envhp, (dvoid **)&item.locator, (ub4)OCI_DTYPE_LOB, 0, 0));
1.1 parser 525:
1.59 paf 526: char placeholder_buf[MAX_STRING];
527: sb4 placeh_len=snprintf(placeholder_buf, sizeof(placeholder_buf),
528: ":%.*s", item.name_size, item.name_ptr);
529: check(connection, "bind lob", OCIBindByName(stmthp,
1.57 paf 530: &item.bind, connection.errhp,
1.59 paf 531: (text*)placeholder_buf, placeh_len,
1.57 paf 532: (dvoid *)&item.locator,
533: (sword)sizeof (item.locator), SQLT_CLOB, (dvoid *)0,
1.1 parser 534: (ub2 *)0, (ub2 *)0, (ub4)0, (ub4 *)0, OCI_DATA_AT_EXEC));
535:
1.57 paf 536: item.rows.count=0;
537: item.connection=&connection;
1.49 paf 538: check(connection, "bind dynamic", OCIBindDynamic(
1.57 paf 539: item.bind, connection.errhp,
540: (dvoid *) &item, cbf_no_data,
541: (dvoid *) &item, cbf_get_data));
1.1 parser 542: }
543: }
544:
1.49 paf 545: execute_prepared(connection,
1.26 paf 546: statement, stmthp, lobs,
547: offset, limit, handlers);
1.60 paf 548:
549: {
550: for(size_t i=0; i<placeholders_count; i++) {
551: Bind_info& bi=binds[i];
1.61 paf 552: if(bi.indicator==MAGIC_INDICATOR_VALUE_MEANING_NOT_NULL_AND_UNCHANGED/*unchanged*/)
1.60 paf 553: continue;
554:
1.65 ! paf 555: Placeholder& ph=placeholders[i];
1.60 paf 556: if(bi.indicator==-1)
557: ph.is_null=true;
558: else
559: if(bi.indicator==0)
560: ph.is_null=false;
561: else
562: fail(connection, bi.indicator<0?
1.61 paf 563: "output bind buffer overflow, additionally size too big to be returned in 'indicator'"
564: : "output bind buffer overflow");
1.60 paf 565:
566: ph.were_updated=true;
1.65 ! paf 567: const char* bind_buffer=connection.bind_buffers[i];
! 568: if( size_t value_length=strlen(bind_buffer) ) {
! 569: char* returned_value=(char*)services.malloc_atomic(value_length+1/*terminator*/);
! 570: memcpy(returned_value, bind_buffer, value_length+1 );
! 571: ph.value=returned_value;
! 572:
! 573: if(cstrClientCharset) {
! 574: services.transcode(ph.value, value_length,
! 575: ph.value, value_length/*<this new value is not used afterwards, actually*/,
1.60 paf 576: cstrClientCharset,
577: services.request_charset());
578: }
1.65 ! paf 579: } else {
! 580: ph.value=0;
1.60 paf 581: }
582: }
583: }
1.1 parser 584: }
585: cleanup: // no check call after this point!
586: {
587: for(int i=0; i<lobs.count; i++) {
588: /* free var locator */
589: if(OCILobLocator *locator=lobs.items[i].locator)
590: OCIDescriptorFree((dvoid *)locator, (ub4)OCI_DTYPE_LOB);
591:
592: /* free rows descriptors */
1.60 paf 593: Query_lobs::return_rows &rows=lobs.items[i].rows;
1.1 parser 594: for(int r=0; r<rows.count; r++)
595: OCIDescriptorFree((dvoid *)rows.row[r].locator, (ub4)OCI_DTYPE_LOB);
596: }
597: }
598: if(stmthp)
599: OCIHandleFree((dvoid *)stmthp, (ub4)OCI_HTYPE_STMT);
600:
1.26 paf 601: if(failed) {
1.49 paf 602: if(connection.sql_error.defined())
603: services._throw(connection.sql_error);
604: services._throw(connection.error);
1.26 paf 605: }
1.1 parser 606: }
607:
608: private: // private funcs
609:
1.49 paf 610: const char *preprocess_statement(Connection& connection,
1.60 paf 611: const char *astatement, Query_lobs &lobs) {
1.1 parser 612: size_t statement_size=strlen(astatement);
1.49 paf 613: SQL_Driver_services& services=*connection.services;
1.1 parser 614:
1.32 paf 615: char *result=(char *)services.malloc_atomic(statement_size
1.1 parser 616: +MAX_STRING // in case of short 'strings'
617: +11/* returning */+6/* into */+(MAX_LOB_NAME_LENGTH+2/*:, */)*2/*ret into*/*MAX_IN_LOBS
618: +1);
619: const char *o=astatement;
620:
621: // /**xxx**/'literal' -> EMPTY_CLOB_FUNC_CALL
622: char *n=result;
623: while(*o) {
624: if(
625: o[0]=='/' &&
626: o[1]=='*' &&
627: o[2]=='*') { // name start
1.34 paf 628: const char* saved_o=o;
1.1 parser 629: o+=3;
630: const char *name_begin=o;
631: while(*o)
632: if(
633: o[0]=='*' &&
634: o[1]=='*' &&
635: o[2]=='/' &&
636: o[3]=='\'') { // name end
1.34 paf 637: saved_o=0; // found, marking that
1.1 parser 638: const char *name_end=o;
639: o+=4;
1.60 paf 640: Query_lobs::Item &item=lobs.items[lobs.count++];
1.1 parser 641: item.name_ptr=name_begin; item.name_size=name_end-name_begin;
1.32 paf 642: item.data_ptr=(char *)services.malloc_atomic(statement_size/*max*/); item.data_size=0;
1.1 parser 643:
644: const char *start=o;
645: bool escaped=false;
646: while(*o && !(o[0]=='\'' && o[1]!='\'' && !escaped)) {
1.14 paf 647: escaped=o[0]=='\'' && o[1]=='\'';
1.1 parser 648: if(escaped) {
649: // write pending, skip "\" or "'"
650: if(size_t size=o-start) {
651: memcpy(item.data_ptr+item.data_size, start, size);
652: item.data_size+=size;
653: }
654: start=++o;
655: } else
656: o++;
657: }
658: if(size_t size=o-start) {
659: memcpy(item.data_ptr+item.data_size, start, size);
660: item.data_size+=size;
661: }
662: if(*o)
663: o++; // skip "'"
664:
665: n+=sprintf(n, EMPTY_CLOB_FUNC_CALL);
666: break;
667: } else
668: o++; // /**skip**/'xxx'
1.34 paf 669: if(saved_o) {
670: o=saved_o;
671: *n++=*o++;
672: }
1.1 parser 673: } else
674: *n++=*o++;
675: }
676: *n=0;
677:
678: if(lobs.count) {
679: int i;
680: n+=sprintf(n, " returning ");
681: for(i=0; i<lobs.count; i++) {
682: if(i)
683: *n++=',';
684: n+=sprintf(n, "%.*s", lobs.items[i].name_size, lobs.items[i].name_ptr);
685: }
686: n+=sprintf(n, " into ");
687: for(i=0; i<lobs.count; i++) {
688: if(i)
1.41 paf 689: *n++=',';
1.1 parser 690: n+=sprintf(n, ":%.*s", lobs.items[i].name_size, lobs.items[i].name_ptr);
691: }
692: }
693:
694: return result;
695: }
696:
697: void execute_prepared(
1.49 paf 698: Connection& connection,
1.60 paf 699: const char *statement, OCIStmt *stmthp, Query_lobs &lobs,
1.1 parser 700: unsigned long offset, unsigned long limit,
701: SQL_Driver_query_event_handlers& handlers) {
702:
703: ub2 stmt_type=0; // UNKNOWN
704: /*
705: //gpfs on sun. paf 000818
706: //Zanyway, this is needed before.
1.49 paf 707: check(connection, "get stmt type", OCIAttrGet(
1.1 parser 708: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT, (ub1 *)&stmt_type,
1.49 paf 709: (ub4 *)0, OCI_ATTR_STMT_TYPE, connection.errhp));
1.1 parser 710: */
1.16 paf 711:
1.62 paf 712: while(isspace((unsigned char)*statement))
1.16 paf 713: statement++;
1.1 parser 714: if(strncasecmp(statement, "select", 6)==0)
715: stmt_type=OCI_STMT_SELECT;
716: else if(strncasecmp(statement, "insert", 6)==0)
717: stmt_type=OCI_STMT_INSERT;
718: else if(strncasecmp(statement, "update", 6)==0)
719: stmt_type=OCI_STMT_UPDATE;
720:
1.49 paf 721: sword status=OCIStmtExecute(connection.svchp, stmthp, connection.errhp,
1.1 parser 722: (ub4)stmt_type==OCI_STMT_SELECT?0:1, (ub4)0,
723: (OCISnapshot *)NULL,
724: (OCISnapshot *)NULL, (ub4)OCI_DEFAULT);
725:
726: if(status!=OCI_NO_DATA)
1.49 paf 727: check(connection, "execute", status);
1.1 parser 728:
729: {
730: for(int i=0; i<lobs.count; i++)
731: if(ub4 bytes_to_write=lobs.items[i].data_size) {
1.60 paf 732: Query_lobs::return_rows *rows=&lobs.items[i].rows;
1.1 parser 733: for(int r=0; r<rows->count; r++) {
734: OCILobLocator *locator=rows->row[r].locator;
1.49 paf 735: check(connection, "lobwrite", OCILobWrite (
736: connection.svchp, connection.errhp,
1.1 parser 737: locator, &bytes_to_write, 1,
738: (dvoid *)lobs.items[i].data_ptr, (ub4)bytes_to_write, OCI_ONE_PIECE,
739: (dvoid *)0, 0, (ub2)0,
740: (ub1) SQLCS_IMPLICIT));
741: }
742: }
743: }
744:
745: switch(stmt_type) {
746: case OCI_STMT_SELECT:
1.49 paf 747: fetch_table(connection,
1.1 parser 748: stmthp, offset, limit,
749: handlers);
750: break;
751: default:
752: /*
753: case OCI_STMT_INSERT:
754: case OCI_STMT_UPDATE:
755: */
756: break;
757: }
758: }
759:
1.49 paf 760: void fetch_table(Connection& connection,
1.1 parser 761: OCIStmt *stmthp, unsigned long offset, unsigned long limit,
1.42 paf 762: SQL_Driver_query_event_handlers& handlers)
763: {
1.54 paf 764: const char* cstrClientCharset=connection.options.cstrClientCharset;
1.49 paf 765: SQL_Driver_services& services=*connection.services;
1.12 paf 766:
1.10 paf 767: ub4 prefetch_rows=100;
1.49 paf 768: check(connection, "AttrSet prefetch-rows", OCIAttrSet(
1.9 paf 769: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
770: (dvoid *)&prefetch_rows, (ub4)0,
1.49 paf 771: (ub4)OCI_ATTR_PREFETCH_ROWS, (OCIError *)connection.errhp));
1.9 paf 772:
1.20 paf 773: ub4 prefetch_mem_size=100*0x400;
1.49 paf 774: check(connection, "AttrSet prefetch-memory", OCIAttrSet(
1.9 paf 775: (dvoid *)stmthp, (ub4)OCI_HTYPE_STMT,
776: (dvoid *)&prefetch_mem_size, (ub4)0,
1.49 paf 777: (ub4)OCI_ATTR_PREFETCH_MEMORY, (OCIError *)connection.errhp));
1.1 parser 778:
779: OCIParam *mypard;
780: ub2 dtype;
1.51 paf 781: const char* col_name;
1.1 parser 782:
1.40 paf 783: struct Col {
1.1 parser 784: ub2 type;
785: char *str;
786: OCILobLocator *var;
787: OCIDefine *def;
788: sb2 indicator;
789: } cols[MAX_COLS]={0};
790: int column_count=0;
791:
792: bool failed=false;
1.49 paf 793: jmp_buf saved_mark; memcpy(saved_mark, connection.mark, sizeof(jmp_buf));
794: if(setjmp(connection.mark)) {
1.1 parser 795: failed=true;
796: goto cleanup;
797: } else {
1.27 paf 798: // idea of preincrementing is that at error time all handles would free up
799: while(++column_count<=MAX_COLS) {
800: /* get next descriptor, if there is one */
1.49 paf 801: if(OCIParamGet(stmthp, OCI_HTYPE_STMT, connection.errhp, (void **)&mypard,
1.27 paf 802: (ub4) column_count)!=OCI_SUCCESS) {
803: --column_count;
804: break;
805: }
806:
807: /* Retrieve the data type attribute */
1.49 paf 808: check(connection, "get type", OCIAttrGet(
1.27 paf 809: (dvoid*) mypard, (ub4)OCI_DTYPE_PARAM,
810: (dvoid*) &dtype, (ub4 *)0, (ub4)OCI_ATTR_DATA_TYPE,
1.49 paf 811: (OCIError *)connection.errhp));
1.27 paf 812:
813: /* Retrieve the column name attribute */
814: ub4 col_name_len;
1.49 paf 815: check(connection, "get name", OCIAttrGet(
1.27 paf 816: (dvoid*) mypard, (ub4)OCI_DTYPE_PARAM,
817: (dvoid**) &col_name, (ub4 *) &col_name_len, (ub4)OCI_ATTR_NAME,
1.49 paf 818: (OCIError *)connection.errhp));
1.51 paf 819: // transcode to $request:charset from connect-string?client_charset
1.54 paf 820: if(cstrClientCharset) {
1.51 paf 821: services.transcode(col_name, col_name_len,
822: col_name, col_name_len,
823: cstrClientCharset,
824: services.request_charset());
825: }
826:
1.40 paf 827: Col& col=cols[column_count-1];
1.27 paf 828: {
1.38 paf 829: size_t length=(size_t)col_name_len;
830: char *ptr=(char *)services.malloc_atomic(length+1);
1.49 paf 831: if( connection.options.bLowerCaseColumnNames )
1.55 paf 832: tolower_str(ptr, col_name, length);
1.39 paf 833: else
834: memcpy(ptr, col_name, length);
1.38 paf 835: ptr[length]=0;
1.49 paf 836: check(connection, handlers.add_column(connection.sql_error, ptr, length));
1.27 paf 837: }
838:
839: ub2 coerce_type=dtype;
840: sb4 size=0;
841: void *ptr;
842:
843: switch(dtype) {
844: case SQLT_CLOB:
1.1 parser 845: {
1.49 paf 846: check(connection, "alloc output var desc", OCIDescriptorAlloc(
847: (dvoid *)connection.envhp, (dvoid **)(ptr=&col.var),
1.27 paf 848: (ub4)OCI_DTYPE_LOB,
849: 0, (dvoid **)0));
850:
851: size=0;
1.1 parser 852: break;
853: }
1.27 paf 854: default:
855: coerce_type=SQLT_STR;
1.49 paf 856: char*& buf=connection.fetch_buffers[column_count-1];
1.46 paf 857: ptr=buf; // get cached buffer
858: if(!ptr) // allocate if needed, caching it
1.58 paf 859: ptr=buf=(char *)services.malloc_atomic(MAX_OUT_STRING_LENGTH+1/*terminator*/);
1.46 paf 860: col.str=(char*)ptr;
1.27 paf 861: size=MAX_OUT_STRING_LENGTH;
862: break;
1.1 parser 863: }
864:
1.40 paf 865: col.type=coerce_type;
1.1 parser 866:
1.48 paf 867: // http://i/docs/oracle/server.804/a58234/oci_func.htm#449680
868: // this call implicitly allocates the define handle
869: // http://sunsite.eunnet.net/documentation/oracle.8.0.4/server.804/a58234/basics.htm
870: // when a statement handle is freed, any bind and define handles associated with it
871: // are also freed
1.49 paf 872: col.def=0; check(connection, "DefineByPos", OCIDefineByPos(
873: stmthp, &col.def, connection.errhp,
1.27 paf 874: column_count, (ub1 *) ptr, size,
1.40 paf 875: coerce_type, (dvoid *) &col.indicator,
1.27 paf 876: (ub2 *)0, (ub2 *)0, OCI_DEFAULT));
877: }
878:
1.49 paf 879: check(connection, handlers.before_rows(connection.sql_error));
1.27 paf 880:
881: for(unsigned long row=0; !limit||row<offset+limit; row++) {
1.49 paf 882: sword status=OCIStmtFetch(stmthp, connection.errhp, (ub4)1, (ub4)OCI_FETCH_NEXT,
1.27 paf 883: (ub4)OCI_DEFAULT);
884: if(status==OCI_NO_DATA)
885: break;
1.49 paf 886: check(connection, "fetch", status);
1.3 paf 887:
1.27 paf 888: if(row>=offset) {
1.49 paf 889: check(connection, handlers.add_row(connection.sql_error));
1.27 paf 890: for(int i=0; i<column_count; i++) {
1.37 paf 891: size_t length=0;
1.42 paf 892: char* strm=0;
1.60 paf 893:
894: sb2 indicator=cols[i].indicator;
895: if(indicator!=-1) { // not NULL
896: if(indicator!=0)
897: fail(connection, indicator<0?
898: "column return buffer overflow, additionally size too big to be returned in 'indicator'"
899: : "column return buffer overflow");
900:
1.27 paf 901: switch(cols[i].type) {
902: case SQLT_CLOB:
903: {
904: ub4 offset=1;
905: OCILobLocator *var=(OCILobLocator *)cols[i].var;
1.42 paf 906: size_t read_size=0;
1.45 paf 907: strm=(char*)services.malloc_atomic(1/*for terminator*/); // set type of memory block
1.42 paf 908: do {
909: char buf[MAX_STRING*10];
1.45 paf 910: ub4 amtp=0/*to be read in stream mode*/;
911: // http://i/docs/oracle/server.804/a58234/oci_func.htm#427818
1.49 paf 912: status=OCILobRead(connection.svchp, connection.errhp,
1.42 paf 913: var, &amtp, offset, (dvoid *)buf,
914: sizeof(buf),
915: (dvoid *)0, 0,
916: (ub2)0, (ub1)SQLCS_IMPLICIT);
917: if(status!=OCI_SUCCESS && status!=OCI_NEED_DATA)
1.49 paf 918: check(connection, "lobread", status);
1.42 paf 919:
1.45 paf 920: strm=(char*)services.realloc(strm, read_size+amtp+1/*for termintator*/);
1.42 paf 921: memcpy(strm+read_size, buf, amtp);
922: read_size+=amtp;
923: offset+=amtp;
924: } while(status==OCI_NEED_DATA);
925:
926: length=(size_t)read_size;
927: strm[length]=0;
1.1 parser 928: break;
929: }
1.27 paf 930: default:
1.32 paf 931: if(const char *value=cols[i].str) {
1.37 paf 932: length=strlen(value);
1.42 paf 933: strm=(char*)services.malloc_atomic(length+1);
934: memcpy(strm, value, length+1);
1.27 paf 935: } else {
1.37 paf 936: length=0;
1.42 paf 937: strm=0;
1.27 paf 938: }
939: break;
940: }
1.60 paf 941: }
1.42 paf 942:
943: const char* str=strm;
944: if(str && length)
945: {
946: // transcode to $request:charset from connect-string?client_charset
1.54 paf 947: if(cstrClientCharset)
1.42 paf 948: services.transcode(str, length,
1.53 paf 949: str, length,
1.42 paf 950: cstrClientCharset,
951: services.request_charset());
952: }
953:
1.49 paf 954: check(connection, handlers.add_row_cell(connection.sql_error, str, length));
1.1 parser 955: }
956: }
957: }
958: }
959:
960: cleanup: // no check call after this point!
961: for(int i=0; i<column_count; i++) {
962: switch(cols[i].type) {
963: case SQLT_CLOB:
964: /* free var locator */
965: OCIDescriptorFree((dvoid *) cols[i].var, (ub4)OCI_DTYPE_LOB);
966: break;
967: default:
968: break;
969: }
970: }
971:
972: if(failed) // need rethrow?
973: longjmp(saved_mark, 1);
974: }
975:
976: private: // conn client library funcs
977:
1.60 paf 978: friend void fail(Connection& connection, const char *msg);
1.49 paf 979: friend void check(Connection& connection, const char *step, sword status);
1.1 parser 980: friend sb4 cbf_get_data(dvoid *ctxp,
981: OCIBind *bindp,
982: ub4 iter, ub4 index,
983: dvoid **bufpp,
984: ub4 **alenp,
985: ub1 *piecep,
986: dvoid **indpp,
987: ub2 **rcodepp);
988:
989:
990: #define OCI_DECL(name, params) \
991: typedef sword (*t_OCI##name)params; t_OCI##name OCI##name
992:
993: OCI_DECL(Initialize, (ub4 mode, dvoid *ctxp,
994: dvoid * (*malocfp)(dvoid *ctxp, size_t size),
995: dvoid * (*ralocfp)(dvoid *ctxp, dvoid *memptr, size_t newsize),
996: void (*mfreefp)(dvoid *ctxp, dvoid *memptr) ));
997:
998: OCI_DECL(EnvInit, (OCIEnv **envp, ub4 mode,
999: size_t xtramem_sz, dvoid **usrmempp));
1000:
1001: OCI_DECL(AttrGet, (CONST dvoid *trgthndlp, ub4 trghndltyp,
1002: dvoid *attributep, ub4 *sizep, ub4 attrtype,
1003: OCIError *errhp));
1004:
1005: OCI_DECL(AttrSet, (dvoid *trgthndlp, ub4 trghndltyp, dvoid *attributep,
1006: ub4 size, ub4 attrtype, OCIError *errhp));
1007:
1008: OCI_DECL(BindByPos, (OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
1009: ub4 position, dvoid *valuep, sb4 value_sz,
1010: ub2 dty, dvoid *indp, ub2 *alenp, ub2 *rcodep,
1011: ub4 maxarr_len, ub4 *curelep, ub4 mode));
1012:
1.59 paf 1013: OCI_DECL(BindByName, (OCIStmt *stmtp, OCIBind **bindp, OCIError *errhp,
1014: text* placeholder, sb4 placeh_len, dvoid *valuep, sb4 value_sz,
1015: ub2 dty, dvoid *indp, ub2 *alenp, ub2 *rcodep,
1016: ub4 maxarr_len, ub4 *curelep, ub4 mode));
1017:
1.1 parser 1018: OCI_DECL(BindDynamic, (OCIBind *bindp, OCIError *errhp, dvoid *ictxp,
1019: OCICallbackInBind icbfp, dvoid *octxp,
1020: OCICallbackOutBind ocbfp));
1021:
1022: OCI_DECL(DefineByPos, (OCIStmt *stmtp, OCIDefine **defnp, OCIError *errhp,
1023: ub4 position, dvoid *valuep, sb4 value_sz, ub2 dty,
1024: dvoid *indp, ub2 *rlenp, ub2 *rcodep, ub4 mode));
1025:
1026: OCI_DECL(DescriptorAlloc, (CONST dvoid *parenth, dvoid **descpp,
1027: CONST ub4 type, CONST size_t xtramem_sz,
1028: dvoid **usrmempp));
1029:
1030: OCI_DECL(DescriptorFree, (dvoid *descp, CONST ub4 type));
1031:
1032:
1033: OCI_DECL(ErrorGet, (dvoid *hndlp, ub4 recordno, OraText *sqlstate,
1034: sb4 *errcodep, OraText *bufp, ub4 bufsiz, ub4 type));
1035:
1036: OCI_DECL(HandleAlloc, (CONST dvoid *parenth, dvoid **hndlpp, CONST ub4 type,
1037: CONST size_t xtramem_sz, dvoid **usrmempp));
1038:
1039: OCI_DECL(HandleFree, (dvoid *hndlp, CONST ub4 type));
1040:
1041: OCI_DECL(LobGetLength, (OCISvcCtx *svchp, OCIError *errhp,
1042: OCILobLocator *locp,
1043: ub4 *lenp));
1044:
1045: OCI_DECL(LobRead, (OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *locp,
1046: ub4 *amtp, ub4 offset, dvoid *bufp, ub4 bufl,
1047: dvoid *ctxp, sb4 (*cbfp)(dvoid *ctxp,
1048: CONST dvoid *bufp,
1049: ub4 len,
1050: ub1 piece),
1051: ub2 csid, ub1 csfrm));
1052:
1053: OCI_DECL(LobWrite, (OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *locp,
1054: ub4 *amtp, ub4 offset, dvoid *bufp, ub4 buflen,
1055: ub1 piece, dvoid *ctxp,
1056: sb4 (*cbfp)(dvoid *ctxp,
1057: dvoid *bufp,
1058: ub4 *len,
1059: ub1 *piece),
1060: ub2 csid, ub1 csfrm));
1061:
1062: OCI_DECL(ParamGet, (CONST dvoid *hndlp, ub4 htype, OCIError *errhp,
1063: dvoid **parmdpp, ub4 pos));
1064:
1065: OCI_DECL(ServerAttach, (OCIServer *srvhp, OCIError *errhp,
1066: CONST OraText *dblink, sb4 dblink_len, ub4 mode));
1067:
1068: OCI_DECL(ServerDetach, (OCIServer *srvhp, OCIError *errhp, ub4 mode));
1069:
1070: OCI_DECL(SessionBegin, (OCISvcCtx *svchp, OCIError *errhp, OCISession *usrhp,
1071: ub4 credt, ub4 mode));
1072:
1073: OCI_DECL(SessionEnd, (OCISvcCtx *svchp, OCIError *errhp, OCISession *usrhp,
1074: ub4 mode));
1075:
1076: OCI_DECL(StmtExecute, (OCISvcCtx *svchp, OCIStmt *stmtp, OCIError *errhp,
1077: ub4 iters, ub4 rowoff, CONST OCISnapshot *snap_in,
1078: OCISnapshot *snap_out, ub4 mode));
1079:
1080: OCI_DECL(StmtFetch, (OCIStmt *stmtp, OCIError *errhp, ub4 nrows,
1081: ub2 orientation, ub4 mode));
1082:
1083: OCI_DECL(StmtPrepare, (OCIStmt *stmtp, OCIError *errhp, CONST OraText *stmt,
1084: ub4 stmt_len, ub4 language, ub4 mode));
1085:
1086: OCI_DECL(TransCommit, (OCISvcCtx *svchp, OCIError *errhp, ub4 flags));
1087:
1088: OCI_DECL(TransRollback, (OCISvcCtx *svchp, OCIError *errhp, ub4 flags));
1089:
1090: private: // conn client library funcs linking
1091:
1092: const char *dlink(const char *dlopen_file_spec) {
1093: if(lt_dlinit())
1094: return lt_dlerror();
1095: lt_dlhandle handle=lt_dlopen(dlopen_file_spec);
1096: if(!handle)
1097: return lt_dlerror(); //"can not open the dynamic link module";
1098:
1099: #define DSLINK(name, action) \
1100: name=(t_##name)lt_dlsym(handle, #name); \
1101: if(!name) \
1102: action;
1103:
1104: #define OCI_LINK(name) DSLINK(OCI##name, return "function OCI" #name " was not found")
1105:
1106: OCI_LINK(Initialize);
1107: OCI_LINK(EnvInit);
1108: OCI_LINK(AttrGet); OCI_LINK(AttrSet);
1.59 paf 1109: OCI_LINK(BindByPos); OCI_LINK(BindByName); OCI_LINK(BindDynamic);
1.1 parser 1110: OCI_LINK(DefineByPos);
1111: OCI_LINK(DescriptorAlloc); OCI_LINK(DescriptorFree);
1112: OCI_LINK(ErrorGet);
1113: OCI_LINK(HandleAlloc); OCI_LINK(HandleFree);
1114: OCI_LINK(LobGetLength);
1115: OCI_LINK(LobRead); OCI_LINK(LobWrite);
1116: OCI_LINK(ParamGet);
1117: OCI_LINK(ServerAttach); OCI_LINK(ServerDetach);
1118: OCI_LINK(SessionBegin); OCI_LINK(SessionEnd);
1119: OCI_LINK(StmtExecute); OCI_LINK(StmtFetch); OCI_LINK(StmtPrepare);
1120: OCI_LINK(TransCommit); OCI_LINK(TransRollback);
1121:
1122: return 0;
1123: }
1124:
1125: } *OracleSQL_driver;
1126:
1.49 paf 1127: void check(Connection& connection, const char *step, sword status) {
1.1 parser 1128:
1129: const char *msg;
1130: char reason[MAX_STRING/2];
1131:
1132: switch (status) {
1.22 paf 1133: case OCI_SUCCESS: // hurrah
1134: case OCI_SUCCESS_WITH_INFO: // ignoring. example: count(column) when column contains NULLs,
1135: // count() not counting them and gives that status
1136: return;
1.1 parser 1137: case OCI_ERROR:
1138: {
1.45 paf 1139: sb4 errcode;
1.49 paf 1140: if(OracleSQL_driver->OCIErrorGet((dvoid *)connection.errhp, (ub4)1, (text *)NULL, &errcode,
1.45 paf 1141: (text *)reason, (ub4)sizeof(reason), OCI_HTYPE_ERROR)==OCI_SUCCESS) {
1142: msg=reason;
1143:
1144: // transcode to $request:charset from connect-string?client_charset
1.49 paf 1145: if(const char* cstrClientCharset=connection.options.cstrClientCharset) {
1.45 paf 1146: if(msg) {
1147: if(size_t msg_length=strlen(msg)) {
1.49 paf 1148: connection.services->transcode(msg, msg_length,
1.45 paf 1149: msg, msg_length,
1150: cstrClientCharset,
1.49 paf 1151: connection.services->request_charset());
1.45 paf 1152: }
1.42 paf 1153: }
1154: }
1.45 paf 1155: } else
1156: msg="[can not get error description]";
1157: break;
1.1 parser 1158: }
1159: case OCI_NEED_DATA:
1160: msg="NEED_DATA"; break;
1161: case OCI_NO_DATA:
1162: msg="NODATA"; break;
1163: case OCI_INVALID_HANDLE:
1164: msg="INVALID_HANDLE"; break;
1165: case OCI_STILL_EXECUTING:
1166: msg="STILL_EXECUTE"; break;
1167: case OCI_CONTINUE:
1168: msg="CONTINUE"; break;
1169: default:
1170: msg="unknown"; break;
1171: }
1172:
1.49 paf 1173: snprintf(connection.error, sizeof(connection.error), "%s (%s, %d)",
1.22 paf 1174: msg, step, (int)status);
1.49 paf 1175: longjmp(connection.mark, 1);
1.1 parser 1176: }
1177:
1.60 paf 1178: void fail(Connection& connection, const char* msg) {
1179: snprintf(connection.error, sizeof(connection.error), "%s", msg);
1180: longjmp(connection.mark, 1);
1181: }
1182:
1.49 paf 1183: void check(Connection& connection, bool error) {
1.27 paf 1184: if(error)
1.49 paf 1185: longjmp(connection.mark, 1);
1.27 paf 1186: }
1.1 parser 1187:
1188: /* ----------------------------------------------------------------- */
1189: /* Intbind callback that does not do any data input. */
1190: /* ----------------------------------------------------------------- */
1.60 paf 1191: sb4 cbf_no_data(
1.45 paf 1192: dvoid* /*ctxp*/,
1193: OCIBind* /*bindp*/,
1194: ub4 /*iter*/, ub4 /*index*/,
1.1 parser 1195: dvoid **bufpp,
1196: ub4 *alenpp,
1197: ub1 *piecep,
1198: dvoid **indpp) {
1199: *bufpp=(dvoid *)0;
1200: *alenpp=0;
1201: static sb2 null_ind=-1;
1202: *indpp=(dvoid *) &null_ind;
1203: *piecep=OCI_ONE_PIECE;
1204:
1205: return OCI_CONTINUE;
1206: }
1207:
1208: /* ----------------------------------------------------------------- */
1209: /* Outbind callback for returning data. */
1210: /* ----------------------------------------------------------------- */
1211: static sb4 cbf_get_data(dvoid *ctxp,
1212: OCIBind *bindp,
1.45 paf 1213: ub4 /*iter*/, ub4 index,
1.1 parser 1214: dvoid **bufpp,
1215: ub4 **alenp,
1216: ub1 *piecep,
1217: dvoid **indpp,
1218: ub2 **rcodepp) {
1.60 paf 1219: Query_lobs::Item& context=*static_cast<Query_lobs::Item*>(ctxp);
1.1 parser 1220:
1221: if(index==0) {
1222: static ub4 rows;
1.49 paf 1223: check(*context.connection, "AttrGet cbf_get_data ROWS_RETURNED",
1.1 parser 1224: OracleSQL_driver->OCIAttrGet(
1225: (CONST dvoid *) bindp, OCI_HTYPE_BIND, (dvoid *)&rows,
1.49 paf 1226: (ub4 *)sizeof(ub2), OCI_ATTR_ROWS_RETURNED, context.connection->errhp)) ;
1.57 paf 1227: context.rows.count=(ub2)rows;
1.60 paf 1228: context.rows.row=(Query_lobs::return_rows::return_row *)
1229: context.connection->services->malloc_atomic(sizeof(Query_lobs::return_rows::return_row)*rows);
1.1 parser 1230: }
1231:
1.60 paf 1232: Query_lobs::return_rows::return_row &var=context.rows.row[index];
1.1 parser 1233:
1.49 paf 1234: check(*context.connection, "alloc output var desc dynamic", OracleSQL_driver->OCIDescriptorAlloc(
1235: (dvoid *) context.connection->envhp, (dvoid **)&var.locator,
1.1 parser 1236: (ub4)OCI_DTYPE_LOB,
1237: 0, (dvoid **)0));
1238:
1239: *bufpp=var.locator;
1240: *alenp=&var.len;
1241: *indpp=(dvoid *) &var.ind;
1242: *piecep=OCI_ONE_PIECE;
1243: *rcodepp=&var.rcode;
1244:
1245: return OCI_CONTINUE;
1246: }
1247:
1.55 paf 1248: static void tolower_str(char *out, const char *in, size_t size) {
1.38 paf 1249: while(size--)
1.45 paf 1250: *out++=(char)tolower(*in++);
1.1 parser 1251: }
1.55 paf 1252: static void toupper_str(char *out, const char *in, size_t size) {
1.44 paf 1253: while(size--)
1.45 paf 1254: *out++=(char)toupper(*in++);
1.44 paf 1255: }
1256:
1.1 parser 1257:
1258: extern "C" SQL_Driver *SQL_DRIVER_CREATE() {
1259: return OracleSQL_driver=new OracleSQL_Driver();
1260: }