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