File: Oracle.xs

package info (click to toggle)
libdbd-oracle-perl 1.21-2
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 1,808 kB
  • ctags: 576
  • sloc: ansic: 6,172; perl: 4,753; makefile: 14
file content (478 lines) | stat: -rwxr-xr-x 13,313 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#include "Oracle.h"

#define BIND_PARAM_INOUT_ALLOW_ARRAY

DBISTATE_DECLARE;

MODULE = DBD::Oracle    PACKAGE = DBD::Oracle

I32
constant(name=Nullch)
    char *name
    ALIAS:
    ORA_VARCHAR2 =   1
    ORA_NUMBER	 =   2
    ORA_STRING	 =   5
    ORA_LONG	 =   8
    ORA_ROWID	 =  11
    ORA_DATE	 =  12
    ORA_RAW	 	 =  23
    ORA_LONGRAW	 =  24
    ORA_CHAR	 =  96
    ORA_CHARZ	 =  97
    ORA_MLSLABEL = 105
    ORA_XMLTYPE	 = 108
    ORA_CLOB	 = 112
    ORA_BLOB	 = 113
    ORA_RSET	 = 116
    ORA_VARCHAR2_TABLE = ORA_VARCHAR2_TABLE
    ORA_NUMBER_TABLE   = ORA_NUMBER_TABLE
    ORA_SYSDBA	 		  = 0x0002
    ORA_SYSOPER	 		  = 0x0004
    SQLCS_IMPLICIT 		  = SQLCS_IMPLICIT
	SQLCS_NCHAR    		  = SQLCS_NCHAR
	SQLT_INT     		  = SQLT_INT
	SQLT_FLT     		  = SQLT_FLT
	OCI_BATCH_MODE        = 0x01
	OCI_EXACT_FETCH       = 0x02
	OCI_KEEP_FETCH_STATE  = 0x04
	OCI_DESCRIBE_ONLY     = 0x10
	OCI_COMMIT_ON_SUCCESS = 0x20
	OCI_NON_BLOCKING      = 0x40
	OCI_BATCH_ERRORS      = 0x80
	OCI_PARSE_ONLY        = 0x100
	OCI_SHOW_DML_WARNINGS = 0x400
	OCI_STMT_SCROLLABLE_READONLY = 0x08
  	OCI_FETCH_CURRENT 	= OCI_FETCH_CURRENT
	OCI_FETCH_NEXT 		= OCI_FETCH_NEXT
	OCI_FETCH_FIRST		= OCI_FETCH_FIRST
	OCI_FETCH_LAST 		= OCI_FETCH_LAST
	OCI_FETCH_PRIOR 	= OCI_FETCH_PRIOR
	OCI_FETCH_ABSOLUTE 	= OCI_FETCH_ABSOLUTE
	OCI_FETCH_RELATIVE	= OCI_FETCH_RELATIVE
	SQLT_CHR    = SQLT_CHR
	SQLT_BIN	= SQLT_BIN
    CODE:
    if (!ix) {
	if (!name) name = GvNAME(CvGV(cv));
	croak("Unknown DBD::Oracle constant '%s'", name);
    }
    else RETVAL = ix;
    OUTPUT:
    RETVAL

void
ORA_OCI()
    CODE:
    SV *sv = sv_newmortal();
    sv_setnv(sv, atof(ORA_OCI_VERSION));	/* 9.1! see docs */
    sv_setpv(sv, ORA_OCI_VERSION);		/* 9.10.11.12    */
    SvNOK_on(sv); /* dualvar hack */
    ST(0) = sv;

void
ora_env_var(name)
    char *name
    CODE:
    char buf[1024];
    char *p = ora_env_var(name, buf, sizeof(buf)-1);
    SV *sv = sv_newmortal();
    if (p)
        sv_setpv(sv, p);
    ST(0) = sv;

#ifdef __CYGWIN32__
void
ora_cygwin_set_env(name, value)
    char * name
    char * value
    CODE:
    ora_cygwin_set_env(name, value);

#endif /* __CYGWIN32__ */


INCLUDE: Oracle.xsi

MODULE = DBD::Oracle    PACKAGE = DBD::Oracle::st


void
ora_scroll_position(sth)
    SV *	sth
    PREINIT:
    D_imp_sth(sth);
   CODE:
    {
   	XSRETURN_IV( imp_sth->fetch_position);
}

void
ora_fetch_scroll(sth,fetch_orient,fetch_offset)
    SV *	sth
    IV  fetch_orient
    IV 	fetch_offset
    PREINIT:
    D_imp_sth(sth);
    CODE:
    {
    AV *av;
 	imp_sth->fetch_orient=fetch_orient;
    imp_sth->fetch_offset=fetch_offset;
    av = dbd_st_fetch(sth,imp_sth);
    ST(0) = (av) ? sv_2mortal(newRV((SV *)av)) : &PL_sv_undef;
}

void
ora_bind_param_inout_array(sth, param, av_ref, maxlen, attribs)
    SV *	sth
    SV *	param
    SV *	av_ref
    IV 		maxlen
    SV *	attribs
    CODE:
    {
    IV sql_type = 0;
    D_imp_sth(sth);
    SV *av_value;
    if (!SvROK(av_ref) || SvTYPE(SvRV(av_ref)) != SVt_PVAV)
	croak("bind_param_inout_array needs a reference to a array value");
    av_value = av_ref;
    if (SvREADONLY(av_value))
	croak("Modification of a read-only value attempted");
    if (attribs) {
    	if (SvNIOK(attribs)) {
	    sql_type = SvIV(attribs);
	    attribs = Nullsv;
	}
	else {
	    SV **svp;
	    DBD_ATTRIBS_CHECK("bind_param", sth, attribs);
	    DBD_ATTRIB_GET_IV(attribs, "ora_type",4, svp, sql_type);
	}
    }
    ST(0) = dbd_bind_ph(sth, imp_sth, param,av_value, sql_type, attribs, TRUE, maxlen)
		? &sv_yes : &sv_no;
}

void
ora_fetch(sth)
    SV *	sth
    PPCODE:
    /* fetchrow: but with scalar fetch returning NUM_FIELDS for Oraperl	*/
    /* This code is called _directly_ by Oraperl.pm bypassing the DBI.	*/
    /* as a result we have to do some things ourselves (like calling	*/
    /* CLEAR_ERROR) and we loose the tracing that the DBI offers :-(	*/
    D_imp_sth(sth);
    AV *av;
    int debug = DBIc_DEBUGIV(imp_sth);
    if (DBIS->debug > debug)
	debug = DBIS->debug;
    DBIh_CLEAR_ERROR(imp_sth);
    if (GIMME == G_SCALAR) {	/* XXX Oraperl	*/
	/* This non-standard behaviour added only to increase the	*/
	/* performance of the oraperl emulation layer (Oraperl.pm)	*/
	if (!imp_sth->done_desc && !dbd_describe(sth, imp_sth))
		XSRETURN_UNDEF;
	XSRETURN_IV(DBIc_NUM_FIELDS(imp_sth));
    }
    if (debug >= 2)
	PerlIO_printf(DBILOGFP, "    -> ora_fetch\n");
    av = dbd_st_fetch(sth, imp_sth);
    if (av) {
	int num_fields = AvFILL(av)+1;
	int i;
	EXTEND(sp, num_fields);
	for(i=0; i < num_fields; ++i) {
	    PUSHs(AvARRAY(av)[i]);
	}
	if (debug >= 2)
	    PerlIO_printf(DBILOGFP, "    <- (...) [%d items]\n", num_fields);
    }
    else {
	if (debug >= 2)
	    PerlIO_printf(DBILOGFP, "    <- () [0 items]\n");
    }
    if (debug >= 2 && SvTRUE(DBIc_ERR(imp_sth)))
	PerlIO_printf(DBILOGFP, "    !! ERROR: %s %s",
	    neatsvpv(DBIc_ERR(imp_sth),0), neatsvpv(DBIc_ERRSTR(imp_sth),0));

void
ora_execute_array(sth, tuples, exe_count, tuples_status, cols=&sv_undef)
    SV *        sth
    SV *        tuples
    IV         exe_count
    SV *        tuples_status
    SV *        cols
    PREINIT:
    D_imp_sth(sth);
    int retval;
    CODE:
    /* XXX Need default bindings if any phs are so far unbound(?) */
    /* XXX this code is duplicated in selectrow_arrayref above  */
    if (DBIc_ROW_COUNT(imp_sth) > 0) /* reset for re-execute */
        DBIc_ROW_COUNT(imp_sth) = 0;
    retval = ora_st_execute_array(sth, imp_sth, tuples, tuples_status,
                                  cols, (ub4)exe_count);
    /* XXX Handle return value ... like DBI::execute_array(). */
    /* remember that dbd_st_execute must return <= -2 for error */
    if (retval == 0)            /* ok with no rows affected     */
        XST_mPV(0, "0E0");      /* (true but zero)              */
    else if (retval < -1)       /* -1 == unknown number of rows */
        XST_mUNDEF(0);          /* <= -2 means error            */
    else
        XST_mIV(0, retval);     /* typically 1, rowcount or -1  */


void
cancel(sth)
    SV *        sth
    CODE:
    D_imp_sth(sth);
    ST(0) = dbd_st_cancel(sth, imp_sth) ? &sv_yes : &sv_no;


MODULE = DBD::Oracle    PACKAGE = DBD::Oracle::db

void
reauthenticate(dbh, uid, pwd)
    SV *	dbh
    char *	uid
    char *	pwd
    CODE:
    D_imp_dbh(dbh);
    ST(0) = ora_db_reauthenticate(dbh, imp_dbh, uid, pwd) ? &sv_yes : &sv_no;

void
ora_lob_write(dbh, locator, offset, data)
    SV *dbh
    OCILobLocator   *locator
    UV	offset
    SV	*data
    PREINIT:
    D_imp_dbh(dbh);
    ub4 amtp;
    STRLEN data_len; /* bytes not chars */
    dvoid *bufp;
    sword status;
    ub2 csid;
    ub1 csform;
    CODE:
    csid = 0;
    csform = SQLCS_IMPLICIT;
    bufp = SvPV(data, data_len);
    amtp = data_len;
    /* if locator is CLOB and data is UTF8 and not in bytes pragma */
    /* if (0 && SvUTF8(data) && !IN_BYTES) { amtp = sv_len_utf8(data); }  */
    /* added by lab: */
    /* LAB do something about length here? see above comment */
     OCILobCharSetForm_log_stat( imp_dbh->envhp, imp_dbh->errhp, locator, &csform, status );
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobCharSetForm");
	ST(0) = &sv_undef;
        return;
    }
#ifdef OCI_ATTR_CHARSET_ID
    /* Effectively only used so AL32UTF8 works properly */
    OCILobCharSetId_log_stat( imp_dbh->envhp, imp_dbh->errhp, locator, &csid, status );
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobCharSetId");
	ST(0) = &sv_undef;
        return;
    }
#endif /* OCI_ATTR_CHARSET_ID */
    /* if data is utf8 but charset isn't then switch to utf8 csid */
    csid = (SvUTF8(data) && !CS_IS_UTF8(csid)) ? utf8_csid : CSFORM_IMPLIED_CSID(csform);

    OCILobWrite_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator,
	    &amtp, (ub4)offset,
	    bufp, (ub4)data_len, OCI_ONE_PIECE,
	    NULL, NULL,
	    (ub2)0, csform , status);
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobWrite");
	ST(0) = &sv_undef;
    }
    else {
	ST(0) = &sv_yes;
    }

void
ora_lob_append(dbh, locator, data)
    SV *dbh
    OCILobLocator   *locator
    SV	*data
    PREINIT:
    D_imp_dbh(dbh);
    ub4 amtp;
    STRLEN data_len; /* bytes not chars */
    dvoid *bufp;
    sword status;
#if defined(ORA_OCI_8) || !defined(OCI_HTYPE_DIRPATH_FN_CTX) /* Oracle is < 9.0 */
    ub4 startp;
#endif
    ub1 csform;
    ub2 csid;
    CODE:
    csid = 0;
    csform = SQLCS_IMPLICIT;
    bufp = SvPV(data, data_len);
    amtp = data_len;
    /* if locator is CLOB and data is UTF8 and not in bytes pragma */
    /* if (1 && SvUTF8(data) && !IN_BYTES) */
    /* added by lab: */
    /* LAB do something about length here? see above comment */
    OCILobCharSetForm_log_stat( imp_dbh->envhp, imp_dbh->errhp, locator, &csform, status );
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobCharSetForm");
	ST(0) = &sv_undef;
        return;
    }
#ifdef OCI_ATTR_CHARSET_ID
    /* Effectively only used so AL32UTF8 works properly */
    OCILobCharSetId_log_stat( imp_dbh->envhp, imp_dbh->errhp, locator, &csid, status );
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobCharSetId");
	ST(0) = &sv_undef;
        return;
    }
#endif /* OCI_ATTR_CHARSET_ID */
    /* if data is utf8 but charset isn't then switch to utf8 csid */
    csid = (SvUTF8(data) && !CS_IS_UTF8(csid)) ? utf8_csid : CSFORM_IMPLIED_CSID(csform);
#if !defined(ORA_OCI_8) && defined(OCI_HTYPE_DIRPATH_FN_CTX) /* Oracle is >= 9.0 */
    OCILobWriteAppend_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator,
			       &amtp, bufp, (ub4)data_len, OCI_ONE_PIECE,
			       NULL, NULL,
			       csid, csform, status);
    if (status != OCI_SUCCESS) {
       oci_error(dbh, imp_dbh->errhp, status, "OCILobWriteAppend");
       ST(0) = &sv_undef;
    }
    else {
       ST(0) = &sv_yes;
    }
#else
    OCILobGetLength_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator, &startp, status);
    if (status != OCI_SUCCESS) {
       oci_error(dbh, imp_dbh->errhp, status, "OCILobGetLength");
       ST(0) = &sv_undef;
    } else {
       /* start one after the end -- the first position in the LOB is 1 */
       startp++;
       if (DBIS->debug >= 2 )
            PerlIO_printf(DBILOGFP, "    Calling OCILobWrite with csid=%d csform=%d\n",csid, csform );
       OCILobWrite_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator,
			    &amtp, startp,
			    bufp, (ub4)data_len, OCI_ONE_PIECE,
			    NULL, NULL,
			    csid, csform , status);
       if (status != OCI_SUCCESS) {
	  oci_error(dbh, imp_dbh->errhp, status, "OCILobWrite");
	  ST(0) = &sv_undef;
       }
       else {
	  ST(0) = &sv_yes;
       }
    }
#endif


void
ora_lob_read(dbh, locator, offset, length)
    SV *dbh
    OCILobLocator   *locator
    UV	offset
    UV	length
    PREINIT:
    D_imp_dbh(dbh);
    ub4 amtp;
    STRLEN bufp_len;
    SV *dest_sv;
    dvoid *bufp;
    sword status;
    ub1 csform;
    CODE:
    csform = SQLCS_IMPLICIT;
    dest_sv = sv_2mortal(newSV(length*4)); /*LAB: crude hack that works... tim did it else where XXX */
    SvPOK_on(dest_sv);
    bufp_len = SvLEN(dest_sv);	/* XXX bytes not chars? (lab: yes) */
    bufp = SvPVX(dest_sv);
    amtp = length;	/* if utf8 and clob/nclob: in: chars, out: bytes */
    /* http://www.lc.leidenuniv.nl/awcourse/oracle/appdev.920/a96584/oci16m40.htm#427818 */
    /* if locator is CLOB and data is UTF8 and not in bytes pragma */
    /* if (0 && SvUTF8(dest_sv) && !IN_BYTES) { amtp = sv_len_utf8(dest_sv); }  */
    /* added by lab: */
    OCILobCharSetForm_log_stat( imp_dbh->envhp, imp_dbh->errhp, locator, &csform, status );
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobCharSetForm");
	dest_sv = &sv_undef;
        return;
    }
    OCILobRead_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator,
	    &amtp, (ub4)offset, /* offset starts at 1 */
	    bufp, (ub4)bufp_len,
	    0, 0, (ub2)0, csform, status);
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobRead");
        dest_sv = &sv_undef;
    }
    else {
        SvCUR(dest_sv) = amtp; /* always bytes here */
        *SvEND(dest_sv) = '\0';
	if (CSFORM_IMPLIES_UTF8(csform))
	    SvUTF8_on(dest_sv);

    }
    ST(0) = dest_sv;

void
ora_lob_trim(dbh, locator, length)
    SV *dbh
    OCILobLocator   *locator
    UV	length
    PREINIT:
    D_imp_dbh(dbh);
    sword status;
    CODE:
    OCILobTrim_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator, length, status);
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobTrim");
	ST(0) = &sv_undef;
    }
    else {
	ST(0) = &sv_yes;
    }

void
ora_lob_length(dbh, locator)
    SV *dbh
    OCILobLocator   *locator
    PREINIT:
    D_imp_dbh(dbh);
    sword status;
    ub4 len = 0;
    CODE:
    OCILobGetLength_log_stat(imp_dbh->svchp, imp_dbh->errhp, locator, &len, status);
    if (status != OCI_SUCCESS) {
        oci_error(dbh, imp_dbh->errhp, status, "OCILobTrim");
	ST(0) = &sv_undef;
    }
    else {
	ST(0) = sv_2mortal(newSVuv(len));
    }



MODULE = DBD::Oracle    PACKAGE = DBD::Oracle::dr

void
init_oci(drh)
    SV *	drh
    CODE:
    D_imp_drh(drh);
	dbd_init_oci(DBIS) ;
	dbd_init_oci_drh(imp_drh) ;