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