Updated adodb syysext to upstream version 5.0.8a.
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-odbtp.inc.php
1 <?php
2 /*
3 V5.08 6 Apr 2009 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence. See License.txt.
7 Set tabs to 4 for best viewing.
8 Latest version is available at http://adodb.sourceforge.net
9 */
10 // Code contributed by "stefan bogdan" <sbogdan#rsb.ro>
11
12 // security - hide paths
13 if (!defined('ADODB_DIR')) die();
14
15 define("_ADODB_ODBTP_LAYER", 2 );
16
17 class ADODB_odbtp extends ADOConnection{
18 var $databaseType = "odbtp";
19 var $dataProvider = "odbtp";
20 var $fmtDate = "'Y-m-d'";
21 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
22 var $replaceQuote = "''"; // string to use to replace quotes
23 var $odbc_driver = 0;
24 var $hasAffectedRows = true;
25 var $hasInsertID = false;
26 var $hasGenID = true;
27 var $hasMoveFirst = true;
28
29 var $_genSeqSQL = "create table %s (seq_name char(30) not null unique , seq_value integer not null)";
30 var $_dropSeqSQL = "delete from adodb_seq where seq_name = '%s'";
31 var $_bindInputArray = false;
32 var $_useUnicodeSQL = false;
33 var $_canPrepareSP = false;
34 var $_dontPoolDBC = true;
35
36 function ADODB_odbtp()
37 {
38 }
39
40 function ServerInfo()
41 {
42 return array('description' => @odbtp_get_attr( ODB_ATTR_DBMSNAME, $this->_connectionID),
43 'version' => @odbtp_get_attr( ODB_ATTR_DBMSVER, $this->_connectionID));
44 }
45
46 function ErrorMsg()
47 {
48 if ($this->_errorMsg !== false) return $this->_errorMsg;
49 if (empty($this->_connectionID)) return @odbtp_last_error();
50 return @odbtp_last_error($this->_connectionID);
51 }
52
53 function ErrorNo()
54 {
55 if ($this->_errorCode !== false) return $this->_errorCode;
56 if (empty($this->_connectionID)) return @odbtp_last_error_state();
57 return @odbtp_last_error_state($this->_connectionID);
58 }
59 /*
60 function DBDate($d,$isfld=false)
61 {
62 if (empty($d) && $d !== 0) return 'null';
63 if ($isfld) return "convert(date, $d, 120)";
64
65 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
66 $d = adodb_date($this->fmtDate,$d);
67 return "convert(date, $d, 120)";
68 }
69
70 function DBTimeStamp($d,$isfld=false)
71 {
72 if (empty($d) && $d !== 0) return 'null';
73 if ($isfld) return "convert(datetime, $d, 120)";
74
75 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
76 $d = adodb_date($this->fmtDate,$d);
77 return "convert(datetime, $d, 120)";
78 }
79 */
80
81 function _insertid()
82 {
83 // SCOPE_IDENTITY()
84 // Returns the last IDENTITY value inserted into an IDENTITY column in
85 // the same scope. A scope is a module -- a stored procedure, trigger,
86 // function, or batch. Thus, two statements are in the same scope if
87 // they are in the same stored procedure, function, or batch.
88 return $this->GetOne($this->identitySQL);
89 }
90
91 function _affectedrows()
92 {
93 if ($this->_queryID) {
94 return @odbtp_affected_rows ($this->_queryID);
95 } else
96 return 0;
97 }
98
99 function CreateSequence($seqname='adodbseq',$start=1)
100 {
101 //verify existence
102 $num = $this->GetOne("select seq_value from adodb_seq");
103 $seqtab='adodb_seq';
104 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
105 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
106 //if using vfp dbc file
107 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
108 $path = substr($path,0,strrpos($path,'\/'));
109 $seqtab = $path . '/' . $seqtab;
110 }
111 if($num == false) {
112 if (empty($this->_genSeqSQL)) return false;
113 $ok = $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
114 }
115 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seqname'");
116 if ($num) {
117 return false;
118 }
119 $start -= 1;
120 return $this->Execute("insert into adodb_seq values('$seqname',$start)");
121 }
122
123 function DropSequence($seqname)
124 {
125 if (empty($this->_dropSeqSQL)) return false;
126 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
127 }
128
129 function GenID($seq='adodbseq',$start=1)
130 {
131 $seqtab='adodb_seq';
132 if( $this->odbc_driver == ODB_DRIVER_FOXPRO) {
133 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
134 //if using vfp dbc file
135 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
136 $path = substr($path,0,strrpos($path,'\/'));
137 $seqtab = $path . '/' . $seqtab;
138 }
139 $MAXLOOPS = 100;
140 while (--$MAXLOOPS>=0) {
141 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seq'");
142 if ($num === false) {
143 //verify if abodb_seq table exist
144 $ok = $this->GetOne("select seq_value from adodb_seq ");
145 if(!$ok) {
146 //creating the sequence table adodb_seq
147 $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
148 }
149 $start -= 1;
150 $num = '0';
151 $ok = $this->Execute("insert into adodb_seq values('$seq',$start)");
152 if (!$ok) return false;
153 }
154 $ok = $this->Execute("update adodb_seq set seq_value=seq_value+1 where seq_name='$seq'");
155 if($ok) {
156 $num += 1;
157 $this->genID = $num;
158 return $num;
159 }
160 }
161 if ($fn = $this->raiseErrorFn) {
162 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
163 }
164 return false;
165 }
166
167 //example for $UserOrDSN
168 //for visual fox : DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBF;SOURCEDB=c:\YourDbfFileDir;EXCLUSIVE=NO;
169 //for visual fox dbc: DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBC;SOURCEDB=c:\YourDbcFileDir\mydb.dbc;EXCLUSIVE=NO;
170 //for access : DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\path_to_access_db\base_test.mdb;UID=root;PWD=;
171 //for mssql : DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=OdbtpTest;
172 //if uid & pwd can be separate
173 function _connect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
174 {
175 $this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN,$argPassword,$argDatabase);
176 if ($this->_connectionID === false) {
177 $this->_errorMsg = $this->ErrorMsg() ;
178 return false;
179 }
180
181 odbtp_convert_datetime($this->_connectionID,true);
182
183 if ($this->_dontPoolDBC) {
184 if (function_exists('odbtp_dont_pool_dbc'))
185 @odbtp_dont_pool_dbc($this->_connectionID);
186 }
187 else {
188 $this->_dontPoolDBC = true;
189 }
190 $this->odbc_driver = @odbtp_get_attr(ODB_ATTR_DRIVER, $this->_connectionID);
191 $dbms = strtolower(@odbtp_get_attr(ODB_ATTR_DBMSNAME, $this->_connectionID));
192 $this->odbc_name = $dbms;
193
194 // Account for inconsistent DBMS names
195 if( $this->odbc_driver == ODB_DRIVER_ORACLE )
196 $dbms = 'oracle';
197 else if( $this->odbc_driver == ODB_DRIVER_SYBASE )
198 $dbms = 'sybase';
199
200 // Set DBMS specific attributes
201 switch( $dbms ) {
202 case 'microsoft sql server':
203 $this->databaseType = 'odbtp_mssql';
204 $this->fmtDate = "'Y-m-d'";
205 $this->fmtTimeStamp = "'Y-m-d h:i:sA'";
206 $this->sysDate = 'convert(datetime,convert(char,GetDate(),102),102)';
207 $this->sysTimeStamp = 'GetDate()';
208 $this->ansiOuter = true;
209 $this->leftOuter = '*=';
210 $this->rightOuter = '=*';
211 $this->hasTop = 'top';
212 $this->hasInsertID = true;
213 $this->hasTransactions = true;
214 $this->_bindInputArray = true;
215 $this->_canSelectDb = true;
216 $this->substr = "substring";
217 $this->length = 'len';
218 $this->identitySQL = 'select SCOPE_IDENTITY()';
219 $this->metaDatabasesSQL = "select name from master..sysdatabases where name <> 'master'";
220 $this->_canPrepareSP = true;
221 break;
222 case 'access':
223 $this->databaseType = 'odbtp_access';
224 $this->fmtDate = "#Y-m-d#";
225 $this->fmtTimeStamp = "#Y-m-d h:i:sA#";
226 $this->sysDate = "FORMAT(NOW,'yyyy-mm-dd')";
227 $this->sysTimeStamp = 'NOW';
228 $this->hasTop = 'top';
229 $this->hasTransactions = false;
230 $this->_canPrepareSP = true; // For MS Access only.
231 break;
232 case 'visual foxpro':
233 $this->databaseType = 'odbtp_vfp';
234 $this->fmtDate = "{^Y-m-d}";
235 $this->fmtTimeStamp = "{^Y-m-d, h:i:sA}";
236 $this->sysDate = 'date()';
237 $this->sysTimeStamp = 'datetime()';
238 $this->ansiOuter = true;
239 $this->hasTop = 'top';
240 $this->hasTransactions = false;
241 $this->replaceQuote = "'+chr(39)+'";
242 $this->true = '.T.';
243 $this->false = '.F.';
244
245 break;
246 case 'oracle':
247 $this->databaseType = 'odbtp_oci8';
248 $this->fmtDate = "'Y-m-d 00:00:00'";
249 $this->fmtTimeStamp = "'Y-m-d h:i:sA'";
250 $this->sysDate = 'TRUNC(SYSDATE)';
251 $this->sysTimeStamp = 'SYSDATE';
252 $this->hasTransactions = true;
253 $this->_bindInputArray = true;
254 $this->concat_operator = '||';
255 break;
256 case 'sybase':
257 $this->databaseType = 'odbtp_sybase';
258 $this->fmtDate = "'Y-m-d'";
259 $this->fmtTimeStamp = "'Y-m-d H:i:s'";
260 $this->sysDate = 'GetDate()';
261 $this->sysTimeStamp = 'GetDate()';
262 $this->leftOuter = '*=';
263 $this->rightOuter = '=*';
264 $this->hasInsertID = true;
265 $this->hasTransactions = true;
266 $this->identitySQL = 'select SCOPE_IDENTITY()';
267 break;
268 default:
269 $this->databaseType = 'odbtp';
270 if( @odbtp_get_attr(ODB_ATTR_TXNCAPABLE, $this->_connectionID) )
271 $this->hasTransactions = true;
272 else
273 $this->hasTransactions = false;
274 }
275 @odbtp_set_attr(ODB_ATTR_FULLCOLINFO, TRUE, $this->_connectionID );
276
277 if ($this->_useUnicodeSQL )
278 @odbtp_set_attr(ODB_ATTR_UNICODESQL, TRUE, $this->_connectionID);
279
280 return true;
281 }
282
283 function _pconnect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
284 {
285 $this->_dontPoolDBC = false;
286 return $this->_connect($HostOrInterface, $UserOrDSN, $argPassword, $argDatabase);
287 }
288
289 function SelectDB($dbName)
290 {
291 if (!@odbtp_select_db($dbName, $this->_connectionID)) {
292 return false;
293 }
294 $this->database = $dbName;
295 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
296 return true;
297 }
298
299 function MetaTables($ttype='',$showSchema=false,$mask=false)
300 {
301 global $ADODB_FETCH_MODE;
302
303 $savem = $ADODB_FETCH_MODE;
304 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
305 if ($this->fetchMode !== false) $savefm = $this->SetFetchMode(false);
306
307 $arr = $this->GetArray("||SQLTables||||$ttype");
308
309 if (isset($savefm)) $this->SetFetchMode($savefm);
310 $ADODB_FETCH_MODE = $savem;
311
312 $arr2 = array();
313 for ($i=0; $i < sizeof($arr); $i++) {
314 if ($arr[$i][3] == 'SYSTEM TABLE' ) continue;
315 if ($arr[$i][2])
316 $arr2[] = $showSchema && $arr[$i][1]? $arr[$i][1].'.'.$arr[$i][2] : $arr[$i][2];
317 }
318 return $arr2;
319 }
320
321 function MetaColumns($table,$upper=true)
322 {
323 global $ADODB_FETCH_MODE;
324
325 $schema = false;
326 $this->_findschema($table,$schema);
327 if ($upper) $table = strtoupper($table);
328
329 $savem = $ADODB_FETCH_MODE;
330 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
331 if ($this->fetchMode !== false) $savefm = $this->SetFetchMode(false);
332
333 $rs = $this->Execute( "||SQLColumns||$schema|$table" );
334
335 if (isset($savefm)) $this->SetFetchMode($savefm);
336 $ADODB_FETCH_MODE = $savem;
337
338 if (!$rs || $rs->EOF) {
339 $false = false;
340 return $false;
341 }
342 $retarr = array();
343 while (!$rs->EOF) {
344 //print_r($rs->fields);
345 if (strtoupper($rs->fields[2]) == $table) {
346 $fld = new ADOFieldObject();
347 $fld->name = $rs->fields[3];
348 $fld->type = $rs->fields[5];
349 $fld->max_length = $rs->fields[6];
350 $fld->not_null = !empty($rs->fields[9]);
351 $fld->scale = $rs->fields[7];
352 if (isset($rs->fields[12])) // vfp does not have field 12
353 if (!is_null($rs->fields[12])) {
354 $fld->has_default = true;
355 $fld->default_value = $rs->fields[12];
356 }
357 $retarr[strtoupper($fld->name)] = $fld;
358 } else if (!empty($retarr))
359 break;
360 $rs->MoveNext();
361 }
362 $rs->Close();
363
364 return $retarr;
365 }
366
367 function MetaPrimaryKeys($table, $owner='')
368 {
369 global $ADODB_FETCH_MODE;
370
371 $savem = $ADODB_FETCH_MODE;
372 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
373 $arr = $this->GetArray("||SQLPrimaryKeys||$owner|$table");
374 $ADODB_FETCH_MODE = $savem;
375
376 //print_r($arr);
377 $arr2 = array();
378 for ($i=0; $i < sizeof($arr); $i++) {
379 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
380 }
381 return $arr2;
382 }
383
384 function MetaForeignKeys($table, $owner='', $upper=false)
385 {
386 global $ADODB_FETCH_MODE;
387
388 $savem = $ADODB_FETCH_MODE;
389 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
390 $constraints = $this->GetArray("||SQLForeignKeys|||||$owner|$table");
391 $ADODB_FETCH_MODE = $savem;
392
393 $arr = false;
394 foreach($constraints as $constr) {
395 //print_r($constr);
396 $arr[$constr[11]][$constr[2]][] = $constr[7].'='.$constr[3];
397 }
398 if (!$arr) {
399 $false = false;
400 return $false;
401 }
402
403 $arr2 = array();
404
405 foreach($arr as $k => $v) {
406 foreach($v as $a => $b) {
407 if ($upper) $a = strtoupper($a);
408 $arr2[$a] = $b;
409 }
410 }
411 return $arr2;
412 }
413
414 function BeginTrans()
415 {
416 if (!$this->hasTransactions) return false;
417 if ($this->transOff) return true;
418 $this->transCnt += 1;
419 $this->autoCommit = false;
420 if (defined('ODB_TXN_DEFAULT'))
421 $txn = ODB_TXN_DEFAULT;
422 else
423 $txn = ODB_TXN_READUNCOMMITTED;
424 $rs = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,$txn,$this->_connectionID);
425 if(!$rs) return false;
426 return true;
427 }
428
429 function CommitTrans($ok=true)
430 {
431 if ($this->transOff) return true;
432 if (!$ok) return $this->RollbackTrans();
433 if ($this->transCnt) $this->transCnt -= 1;
434 $this->autoCommit = true;
435 if( ($ret = @odbtp_commit($this->_connectionID)) )
436 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
437 return $ret;
438 }
439
440 function RollbackTrans()
441 {
442 if ($this->transOff) return true;
443 if ($this->transCnt) $this->transCnt -= 1;
444 $this->autoCommit = true;
445 if( ($ret = @odbtp_rollback($this->_connectionID)) )
446 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
447 return $ret;
448 }
449
450 function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
451 {
452 // TOP requires ORDER BY for Visual FoxPro
453 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
454 if (!preg_match('/ORDER[ \t\r\n]+BY/is',$sql)) $sql .= ' ORDER BY 1';
455 }
456 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
457 return $ret;
458 }
459
460 function Prepare($sql)
461 {
462 if (! $this->_bindInputArray) return $sql; // no binding
463
464 $this->_errorMsg = false;
465 $this->_errorCode = false;
466
467 $stmt = @odbtp_prepare($sql,$this->_connectionID);
468 if (!$stmt) {
469 // print "Prepare Error for ($sql) ".$this->ErrorMsg()."<br>";
470 return $sql;
471 }
472 return array($sql,$stmt,false);
473 }
474
475 function PrepareSP($sql)
476 {
477 if (!$this->_canPrepareSP) return $sql; // Can't prepare procedures
478
479 $this->_errorMsg = false;
480 $this->_errorCode = false;
481
482 $stmt = @odbtp_prepare_proc($sql,$this->_connectionID);
483 if (!$stmt) return false;
484 return array($sql,$stmt);
485 }
486
487 /*
488 Usage:
489 $stmt = $db->PrepareSP('SP_RUNSOMETHING'); -- takes 2 params, @myid and @group
490
491 # note that the parameter does not have @ in front!
492 $db->Parameter($stmt,$id,'myid');
493 $db->Parameter($stmt,$group,'group',false,64);
494 $db->Parameter($stmt,$group,'photo',false,100000,ODB_BINARY);
495 $db->Execute($stmt);
496
497 @param $stmt Statement returned by Prepare() or PrepareSP().
498 @param $var PHP variable to bind to. Can set to null (for isNull support).
499 @param $name Name of stored procedure variable name to bind to.
500 @param [$isOutput] Indicates direction of parameter 0/false=IN 1=OUT 2= IN/OUT. This is ignored in odbtp.
501 @param [$maxLen] Holds an maximum length of the variable.
502 @param [$type] The data type of $var. Legal values depend on driver.
503
504 See odbtp_attach_param documentation at http://odbtp.sourceforge.net.
505 */
506 function Parameter(&$stmt, &$var, $name, $isOutput=false, $maxLen=0, $type=0)
507 {
508 if ( $this->odbc_driver == ODB_DRIVER_JET ) {
509 $name = '['.$name.']';
510 if( !$type && $this->_useUnicodeSQL
511 && @odbtp_param_bindtype($stmt[1], $name) == ODB_CHAR )
512 {
513 $type = ODB_WCHAR;
514 }
515 }
516 else {
517 $name = '@'.$name;
518 }
519 return @odbtp_attach_param($stmt[1], $name, $var, $type, $maxLen);
520 }
521
522 /*
523 Insert a null into the blob field of the table first.
524 Then use UpdateBlob to store the blob.
525
526 Usage:
527
528 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
529 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
530 */
531
532 function UpdateBlob($table,$column,$val,$where,$blobtype='image')
533 {
534 $sql = "UPDATE $table SET $column = ? WHERE $where";
535 if( !($stmt = @odbtp_prepare($sql, $this->_connectionID)) )
536 return false;
537 if( !@odbtp_input( $stmt, 1, ODB_BINARY, 1000000, $blobtype ) )
538 return false;
539 if( !@odbtp_set( $stmt, 1, $val ) )
540 return false;
541 return @odbtp_execute( $stmt ) != false;
542 }
543
544 function MetaIndexes($table,$primary=false)
545 {
546 switch ( $this->odbc_driver) {
547 case ODB_DRIVER_MSSQL:
548 return $this->MetaIndexes_mssql($table, $primary);
549 default:
550 return array();
551 }
552 }
553
554 function MetaIndexes_mssql($table,$primary=false)
555 {
556 $table = strtolower($this->qstr($table));
557
558 $sql = "SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
559 CASE WHEN I.indid BETWEEN 1 AND 254 AND (I.status & 2048 = 2048 OR I.Status = 16402 AND O.XType = 'V') THEN 1 ELSE 0 END AS IsPK,
560 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
561 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
562 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
563 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
564 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND lower(O.Name) = $table
565 ORDER BY O.name, I.Name, K.keyno";
566
567 global $ADODB_FETCH_MODE;
568 $save = $ADODB_FETCH_MODE;
569 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
570 if ($this->fetchMode !== FALSE) {
571 $savem = $this->SetFetchMode(FALSE);
572 }
573
574 $rs = $this->Execute($sql);
575 if (isset($savem)) {
576 $this->SetFetchMode($savem);
577 }
578 $ADODB_FETCH_MODE = $save;
579
580 if (!is_object($rs)) {
581 return FALSE;
582 }
583
584 $indexes = array();
585 while ($row = $rs->FetchRow()) {
586 if ($primary && !$row[5]) continue;
587
588 $indexes[$row[0]]['unique'] = $row[6];
589 $indexes[$row[0]]['columns'][] = $row[1];
590 }
591 return $indexes;
592 }
593
594 function IfNull( $field, $ifNull )
595 {
596 switch( $this->odbc_driver ) {
597 case ODB_DRIVER_MSSQL:
598 return " ISNULL($field, $ifNull) ";
599 case ODB_DRIVER_JET:
600 return " IIF(IsNull($field), $ifNull, $field) ";
601 }
602 return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
603 }
604
605 function _query($sql,$inputarr=false)
606 {
607 global $php_errormsg;
608
609 $this->_errorMsg = false;
610 $this->_errorCode = false;
611
612 if ($inputarr) {
613 if (is_array($sql)) {
614 $stmtid = $sql[1];
615 } else {
616 $stmtid = @odbtp_prepare($sql,$this->_connectionID);
617 if ($stmtid == false) {
618 $this->_errorMsg = $php_errormsg;
619 return false;
620 }
621 }
622 $num_params = @odbtp_num_params( $stmtid );
623 /*
624 for( $param = 1; $param <= $num_params; $param++ ) {
625 @odbtp_input( $stmtid, $param );
626 @odbtp_set( $stmtid, $param, $inputarr[$param-1] );
627 }*/
628
629 $param = 1;
630 foreach($inputarr as $v) {
631 @odbtp_input( $stmtid, $param );
632 @odbtp_set( $stmtid, $param, $v );
633 $param += 1;
634 if ($param > $num_params) break;
635 }
636
637 if (!@odbtp_execute($stmtid) ) {
638 return false;
639 }
640 } else if (is_array($sql)) {
641 $stmtid = $sql[1];
642 if (!@odbtp_execute($stmtid)) {
643 return false;
644 }
645 } else {
646 $stmtid = odbtp_query($sql,$this->_connectionID);
647 }
648 $this->_lastAffectedRows = 0;
649 if ($stmtid) {
650 $this->_lastAffectedRows = @odbtp_affected_rows($stmtid);
651 }
652 return $stmtid;
653 }
654
655 function _close()
656 {
657 $ret = @odbtp_close($this->_connectionID);
658 $this->_connectionID = false;
659 return $ret;
660 }
661 }
662
663 class ADORecordSet_odbtp extends ADORecordSet {
664
665 var $databaseType = 'odbtp';
666 var $canSeek = true;
667
668 function ADORecordSet_odbtp($queryID,$mode=false)
669 {
670 if ($mode === false) {
671 global $ADODB_FETCH_MODE;
672 $mode = $ADODB_FETCH_MODE;
673 }
674 $this->fetchMode = $mode;
675 $this->ADORecordSet($queryID);
676 }
677
678 function _initrs()
679 {
680 $this->_numOfFields = @odbtp_num_fields($this->_queryID);
681 if (!($this->_numOfRows = @odbtp_num_rows($this->_queryID)))
682 $this->_numOfRows = -1;
683
684 if (!$this->connection->_useUnicodeSQL) return;
685
686 if ($this->connection->odbc_driver == ODB_DRIVER_JET) {
687 if (!@odbtp_get_attr(ODB_ATTR_MAPCHARTOWCHAR,
688 $this->connection->_connectionID))
689 {
690 for ($f = 0; $f < $this->_numOfFields; $f++) {
691 if (@odbtp_field_bindtype($this->_queryID, $f) == ODB_CHAR)
692 @odbtp_bind_field($this->_queryID, $f, ODB_WCHAR);
693 }
694 }
695 }
696 }
697
698 function FetchField($fieldOffset = 0)
699 {
700 $off=$fieldOffset; // offsets begin at 0
701 $o= new ADOFieldObject();
702 $o->name = @odbtp_field_name($this->_queryID,$off);
703 $o->type = @odbtp_field_type($this->_queryID,$off);
704 $o->max_length = @odbtp_field_length($this->_queryID,$off);
705 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
706 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
707 return $o;
708 }
709
710 function _seek($row)
711 {
712 return @odbtp_data_seek($this->_queryID, $row);
713 }
714
715 function fields($colname)
716 {
717 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
718
719 if (!$this->bind) {
720 $this->bind = array();
721 for ($i=0; $i < $this->_numOfFields; $i++) {
722 $name = @odbtp_field_name( $this->_queryID, $i );
723 $this->bind[strtoupper($name)] = $i;
724 }
725 }
726 return $this->fields[$this->bind[strtoupper($colname)]];
727 }
728
729 function _fetch_odbtp($type=0)
730 {
731 switch ($this->fetchMode) {
732 case ADODB_FETCH_NUM:
733 $this->fields = @odbtp_fetch_row($this->_queryID, $type);
734 break;
735 case ADODB_FETCH_ASSOC:
736 $this->fields = @odbtp_fetch_assoc($this->_queryID, $type);
737 break;
738 default:
739 $this->fields = @odbtp_fetch_array($this->_queryID, $type);
740 }
741 if ($this->databaseType = 'odbtp_vfp') {
742 if ($this->fields)
743 foreach($this->fields as $k => $v) {
744 if (strncmp($v,'1899-12-30',10) == 0) $this->fields[$k] = '';
745 }
746 }
747 return is_array($this->fields);
748 }
749
750 function _fetch()
751 {
752 return $this->_fetch_odbtp();
753 }
754
755 function MoveFirst()
756 {
757 if (!$this->_fetch_odbtp(ODB_FETCH_FIRST)) return false;
758 $this->EOF = false;
759 $this->_currentRow = 0;
760 return true;
761 }
762
763 function MoveLast()
764 {
765 if (!$this->_fetch_odbtp(ODB_FETCH_LAST)) return false;
766 $this->EOF = false;
767 $this->_currentRow = $this->_numOfRows - 1;
768 return true;
769 }
770
771 function NextRecordSet()
772 {
773 if (!@odbtp_next_result($this->_queryID)) return false;
774 $this->_inited = false;
775 $this->bind = false;
776 $this->_currentRow = -1;
777 $this->Init();
778 return true;
779 }
780
781 function _close()
782 {
783 return @odbtp_free_query($this->_queryID);
784 }
785 }
786
787 class ADORecordSet_odbtp_mssql extends ADORecordSet_odbtp {
788
789 var $databaseType = 'odbtp_mssql';
790
791 function ADORecordSet_odbtp_mssql($id,$mode=false)
792 {
793 return $this->ADORecordSet_odbtp($id,$mode);
794 }
795 }
796
797 class ADORecordSet_odbtp_access extends ADORecordSet_odbtp {
798
799 var $databaseType = 'odbtp_access';
800
801 function ADORecordSet_odbtp_access($id,$mode=false)
802 {
803 return $this->ADORecordSet_odbtp($id,$mode);
804 }
805 }
806
807 class ADORecordSet_odbtp_vfp extends ADORecordSet_odbtp {
808
809 var $databaseType = 'odbtp_vfp';
810
811 function ADORecordSet_odbtp_vfp($id,$mode=false)
812 {
813 return $this->ADORecordSet_odbtp($id,$mode);
814 }
815 }
816
817 class ADORecordSet_odbtp_oci8 extends ADORecordSet_odbtp {
818
819 var $databaseType = 'odbtp_oci8';
820
821 function ADORecordSet_odbtp_oci8($id,$mode=false)
822 {
823 return $this->ADORecordSet_odbtp($id,$mode);
824 }
825 }
826
827 class ADORecordSet_odbtp_sybase extends ADORecordSet_odbtp {
828
829 var $databaseType = 'odbtp_sybase';
830
831 function ADORecordSet_odbtp_sybase($id,$mode=false)
832 {
833 return $this->ADORecordSet_odbtp($id,$mode);
834 }
835 }
836 ?>