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