3 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Portions Copyright (c) 2007-2009, iAnywhere Solutions, Inc.
5 All rights reserved. All unpublished rights reserved.
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
11 Set tabs to 4 for best viewing.
14 NOTE: This driver requires the Advantage PHP client libraries, which
15 can be downloaded for free via:
16 http://devzone.advantagedatabase.com/dz/content.aspx?key=20
19 The following steps can be taken to utilize this driver from the
20 CodeGear Delphi for PHP product:
21 1 - See note above, download and install the Advantage PHP client.
22 2 - Copy the following files to the Delphi for PHP\X.X\php\ext directory:
26 php_advantage.dll (rename the existing php_advantage.dll.5.x.x file)
27 3 - Add the following line to the Delphi for PHP\X.X\php\php.ini.template file:
28 extension=php_advantage.dll
29 4 - To use: enter "ads" as the DriverName on a connection component, and set
30 a Host property similar to "DataDirectory=c:\". See the Advantage PHP
31 help file topic for ads_connect for details on connection path options
33 5 - (optional) - Modify the Delphi for PHP\X.X\vcl\packages\database.packages.php
34 file and add ads to the list of strings returned when registering the
35 Database object's DriverName property.
38 // security - hide paths
39 if (!defined('ADODB_DIR')) die();
41 define("_ADODB_ADS_LAYER", 2 );
43 /*--------------------------------------------------------------------------------------
44 --------------------------------------------------------------------------------------*/
47 class ADODB_ads
extends ADOConnection
{
48 var $databaseType = "ads";
50 var $fmtTimeStamp = "'Y-m-d H:i:s'";
51 var $concat_operator = '';
52 var $replaceQuote = "''"; // string to use to replace quotes
53 var $dataProvider = "ads";
54 var $hasAffectedRows = true;
55 var $binmode = ODBC_BINMODE_RETURN
;
56 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
57 // breaking backward-compat
58 //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
59 var $_bindInputArray = false;
60 var $curmode = SQL_CUR_USE_DRIVER
; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
61 var $_genSeqSQL = "create table %s (id integer)";
62 var $_autocommit = true;
63 var $_haserrorfunctions = true;
64 var $_has_stupid_odbc_fetch_api_change = true;
65 var $_lastAffectedRows = 0;
66 var $uCaseTables = true; // for meta* functions, uppercase table names
71 $this->_haserrorfunctions
= ADODB_PHPVER
>= 0x4050;
72 $this->_has_stupid_odbc_fetch_api_change
= ADODB_PHPVER
>= 0x4200;
75 // returns true or false
76 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
80 if (!function_exists('ads_connect')) return null;
82 if ($this->debug
&& $argDatabasename && $this->databaseType
!= 'vfp') {
83 ADOConnection
::outp("For Advantage Connect(), $argDatabasename is not used. Place dsn in 1st parameter.");
85 if (isset($php_errormsg)) $php_errormsg = '';
86 if ($this->curmode
=== false) $this->_connectionID
= ads_connect($argDSN,$argUsername,$argPassword);
87 else $this->_connectionID
= ads_connect($argDSN,$argUsername,$argPassword,$this->curmode
);
88 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
89 if (isset($this->connectStmt
)) $this->Execute($this->connectStmt
);
91 return $this->_connectionID
!= false;
94 // returns true or false
95 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
99 if (!function_exists('ads_connect')) return null;
101 if (isset($php_errormsg)) $php_errormsg = '';
102 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
103 if ($this->debug
&& $argDatabasename) {
104 ADOConnection
::outp("For PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
106 // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
107 if ($this->curmode
=== false) $this->_connectionID
= ads_connect($argDSN,$argUsername,$argPassword);
108 else $this->_connectionID
= ads_pconnect($argDSN,$argUsername,$argPassword,$this->curmode
);
110 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
111 if ($this->_connectionID
&& $this->autoRollback
) @ads_rollback
($this->_connectionID
);
112 if (isset($this->connectStmt
)) $this->Execute($this->connectStmt
);
114 return $this->_connectionID
!= false;
117 // returns the Server version and Description
118 function ServerInfo()
121 if (!empty($this->host
) && ADODB_PHPVER
>= 0x4300) {
122 $stmt = $this->Prepare('EXECUTE PROCEDURE sp_mgGetInstallInfo()');
123 $res = $this->Execute($stmt);
125 print $this->ErrorMsg();
127 $ret["version"]= $res->fields
[3];
128 $ret["description"]="Advantage Database Server";
133 return ADOConnection
::ServerInfo();
138 // returns true or false
139 function CreateSequence( $seqname,$start=1)
141 $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
143 print $this->ErrorMsg();
151 // returns true or false
152 function DropSequence($seqname)
154 $res = $this->Execute("DROP TABLE $seqname");
156 print $this->ErrorMsg();
164 // returns the generated ID or false
165 // checks if the table already exists, else creates the table and inserts a record into the table
166 // and gets the ID number of the last inserted record.
167 function GenID($seqname,$start=1)
169 $go = $this->Execute("select * from $seqname");
171 $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
173 print $this->ErrorMsg();
177 $res = $this->Execute("INSERT INTO $seqname VALUES( DEFAULT )");
179 print $this->ErrorMsg();
183 $gen = $this->Execute("SELECT LastAutoInc( STATEMENT ) FROM system.iota");
184 $ret = $gen->fields
[0];
195 if ($this->_haserrorfunctions
) {
196 if ($this->_errorMsg
!== false) return $this->_errorMsg
;
197 if (empty($this->_connectionID
)) return @ads_errormsg
();
198 return @ads_errormsg
($this->_connectionID
);
199 } else return ADOConnection
::ErrorMsg();
206 if ($this->_haserrorfunctions
) {
207 if ($this->_errorCode
!== false) {
208 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
209 return (strlen($this->_errorCode
)<=2) ?
0 : $this->_errorCode
;
212 if (empty($this->_connectionID
)) $e = @ads_error
();
213 else $e = @ads_error
($this->_connectionID
);
215 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
216 // so we check and patch
217 if (strlen($e)<=2) return 0;
219 } else return ADOConnection
::ErrorNo();
224 function BeginTrans()
226 if (!$this->hasTransactions
) return false;
227 if ($this->transOff
) return true;
228 $this->transCnt +
= 1;
229 $this->_autocommit
= false;
230 return ads_autocommit($this->_connectionID
,false);
233 function CommitTrans($ok=true)
235 if ($this->transOff
) return true;
236 if (!$ok) return $this->RollbackTrans();
237 if ($this->transCnt
) $this->transCnt
-= 1;
238 $this->_autocommit
= true;
239 $ret = ads_commit($this->_connectionID
);
240 ads_autocommit($this->_connectionID
,true);
244 function RollbackTrans()
246 if ($this->transOff
) return true;
247 if ($this->transCnt
) $this->transCnt
-= 1;
248 $this->_autocommit
= true;
249 $ret = ads_rollback($this->_connectionID
);
250 ads_autocommit($this->_connectionID
,true);
255 // Returns tables,Views or both on succesfull execution. Returns
256 // tables by default on succesfull execustion.
257 function &MetaTables($ttype)
259 $recordSet1 = $this->Execute("select * from system.tables");
261 print $this->ErrorMsg();
264 $recordSet2 = $this->Execute("select * from system.views");
266 print $this->ErrorMsg();
270 while (!$recordSet1->EOF
){
271 $arr["$i"] = $recordSet1->fields
[0];
272 $recordSet1->MoveNext();
276 while (!$recordSet2->EOF
){
277 $arr["$i"] = $recordSet2->fields
[0];
278 $recordSet2->MoveNext();
283 elseif($ttype=='VIEWS'){
284 while (!$recordSet2->EOF
){
285 $arrV["$i"] = $recordSet2->fields
[0];
286 $recordSet2->MoveNext();
297 function &MetaPrimaryKeys($table)
299 $recordSet = $this->Execute("select table_primary_key from system.tables where name='$table'");
301 print $this->ErrorMsg();
305 while (!$recordSet->EOF
){
306 $arr["$i"] = $recordSet->fields
[0];
307 $recordSet->MoveNext();
314 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
315 / SQL data type codes /
316 #define SQL_UNKNOWN_TYPE 0
318 #define SQL_NUMERIC 2
319 #define SQL_DECIMAL 3
320 #define SQL_INTEGER 4
321 #define SQL_SMALLINT 5
325 #if (ODBCVER >= 0x0300)
326 #define SQL_DATETIME 9
328 #define SQL_VARCHAR 12
331 / One-parameter shortcuts for date/time data types /
332 #if (ODBCVER >= 0x0300)
333 #define SQL_TYPE_DATE 91
334 #define SQL_TYPE_TIME 92
335 #define SQL_TYPE_TIMESTAMP 93
337 #define SQL_UNICODE (-95)
338 #define SQL_UNICODE_VARCHAR (-96)
339 #define SQL_UNICODE_LONGVARCHAR (-97)
341 function ODBCTypes($t)
343 switch ((integer)$t) {
371 case -11: // uniqidentifier
381 function &MetaColumns($table)
383 global $ADODB_FETCH_MODE;
386 if ($this->uCaseTables
) $table = strtoupper($table);
388 $this->_findschema($table,$schema);
390 $savem = $ADODB_FETCH_MODE;
391 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
393 /*if (false) { // after testing, confirmed that the following does not work becoz of a bug
394 $qid2 = ads_tables($this->_connectionID);
395 $rs = new ADORecordSet_ads($qid2);
396 $ADODB_FETCH_MODE = $savem;
397 if (!$rs) return false;
398 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
402 if ($table == strtoupper($rs->fields[2])) {
411 $qid = ads_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
414 switch ($this->databaseType
) {
417 $qid = ads_columns($this->_connectionID
);#,'%','',strtoupper($table),'%');
423 $qid = ads_columns($this->_connectionID
, "", $schema, $table, $colname);
427 $qid = @ads_columns
($this->_connectionID
,'%','%',strtoupper($table),'%');
428 if (empty($qid)) $qid = ads_columns($this->_connectionID
);
431 if (empty($qid)) return $false;
433 $rs = new ADORecordSet_ads($qid);
434 $ADODB_FETCH_MODE = $savem;
436 if (!$rs) return $false;
437 $rs->_has_stupid_odbc_fetch_api_change
= $this->_has_stupid_odbc_fetch_api_change
;
458 // adodb_pr($rs->fields);
459 if (strtoupper(trim($rs->fields
[2])) == $table && (!$schema ||
strtoupper($rs->fields
[1]) == $schema)) {
460 $fld = new ADOFieldObject();
461 $fld->name
= $rs->fields
[3];
462 $fld->type
= $this->ODBCTypes($rs->fields
[4]);
464 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
465 // access uses precision to store length for char/varchar
466 if ($fld->type
== 'C' or $fld->type
== 'X') {
467 if ($this->databaseType
== 'access')
468 $fld->max_length
= $rs->fields
[6];
469 else if ($rs->fields
[4] <= -95) // UNICODE
470 $fld->max_length
= $rs->fields
[7]/2;
472 $fld->max_length
= $rs->fields
[7];
474 $fld->max_length
= $rs->fields
[7];
475 $fld->not_null
= !empty($rs->fields
[10]);
476 $fld->scale
= $rs->fields
[8];
477 $retarr[strtoupper($fld->name
)] = $fld;
478 } else if (sizeof($retarr)>0)
482 $rs->Close(); //-- crashes 4.03pl1 -- why?
484 if (empty($retarr)) $retarr = false;
488 // Returns an array of columns names for a given table
489 function &MetaColumnNames($table)
491 $recordSet = $this->Execute("select name from system.columns where parent='$table'");
493 print $this->ErrorMsg();
498 while (!$recordSet->EOF
){
499 $arr["FIELD$i"] = $recordSet->fields
[0];
500 $recordSet->MoveNext();
508 function Prepare($sql)
510 if (! $this->_bindInputArray
) return $sql; // no binding
511 $stmt = ads_prepare($this->_connectionID
,$sql);
513 // we don't know whether odbc driver is parsing prepared stmts, so just return sql
516 return array($sql,$stmt,false);
519 /* returns queryID or false */
520 function _query($sql,$inputarr=false)
522 GLOBAL $php_errormsg;
523 if (isset($php_errormsg)) $php_errormsg = '';
527 if (is_array($sql)) {
530 $stmtid = ads_prepare($this->_connectionID
,$sql);
532 if ($stmtid == false) {
533 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
538 if (! ads_execute($stmtid,$inputarr)) {
539 //@ads_free_result($stmtid);
540 if ($this->_haserrorfunctions
) {
541 $this->_errorMsg
= ads_errormsg();
542 $this->_errorCode
= ads_error();
547 } else if (is_array($sql)) {
549 if (!ads_execute($stmtid)) {
550 //@ads_free_result($stmtid);
551 if ($this->_haserrorfunctions
) {
552 $this->_errorMsg
= ads_errormsg();
553 $this->_errorCode
= ads_error();
560 $stmtid = ads_exec($this->_connectionID
,$sql);
564 $this->_lastAffectedRows
= 0;
569 if (@ads_num_fields
($stmtid) == 0) {
570 $this->_lastAffectedRows
= ads_num_rows($stmtid);
575 $this->_lastAffectedRows
= 0;
576 ads_binmode($stmtid,$this->binmode
);
577 ads_longreadlen($stmtid,$this->maxblobsize
);
581 if ($this->_haserrorfunctions
)
584 $this->_errorMsg
= '';
585 $this->_errorCode
= 0;
588 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
592 if ($this->_haserrorfunctions
) {
593 $this->_errorMsg
= ads_errormsg();
594 $this->_errorCode
= ads_error();
596 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
604 Insert a null into the blob field of the table first.
605 Then use UpdateBlob to store the blob.
609 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
610 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
612 function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
614 $sql = "UPDATE $table SET $column=? WHERE $where";
615 $stmtid = ads_prepare($this->_connectionID
,$sql);
616 if ($stmtid == false){
617 $this->_errorMsg
= isset($php_errormsg) ?
$php_errormsg : '';
620 if (! ads_execute($stmtid,array($val),array(SQL_BINARY
) )){
621 if ($this->_haserrorfunctions
){
622 $this->_errorMsg
= ads_errormsg();
623 $this->_errorCode
= ads_error();
630 // returns true or false
633 $ret = @ads_close
($this->_connectionID
);
634 $this->_connectionID
= false;
638 function _affectedrows()
640 return $this->_lastAffectedRows
;
645 /*--------------------------------------------------------------------------------------
646 Class Name: Recordset
647 --------------------------------------------------------------------------------------*/
649 class ADORecordSet_ads
extends ADORecordSet
{
652 var $databaseType = "ads";
653 var $dataProvider = "ads";
655 var $_has_stupid_odbc_fetch_api_change;
657 function ADORecordSet_ads($id,$mode=false)
659 if ($mode === false) {
660 global $ADODB_FETCH_MODE;
661 $mode = $ADODB_FETCH_MODE;
663 $this->fetchMode
= $mode;
665 $this->_queryID
= $id;
667 // the following is required for mysql odbc driver in 4.3.1 -- why?
669 $this->_currentRow
= -1;
670 //$this->ADORecordSet($id);
674 // returns the field object
675 function &FetchField($fieldOffset = -1)
678 $off=$fieldOffset+
1; // offsets begin at 1
680 $o= new ADOFieldObject();
681 $o->name
= @ads_field_name
($this->_queryID
,$off);
682 $o->type
= @ads_field_type
($this->_queryID
,$off);
683 $o->max_length
= @ads_field_len
($this->_queryID
,$off);
684 if (ADODB_ASSOC_CASE
== 0) $o->name
= strtolower($o->name
);
685 else if (ADODB_ASSOC_CASE
== 1) $o->name
= strtoupper($o->name
);
689 /* Use associative array to get fields array */
690 function Fields($colname)
692 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) return $this->fields
[$colname];
694 $this->bind
= array();
695 for ($i=0; $i < $this->_numOfFields
; $i++
) {
696 $o = $this->FetchField($i);
697 $this->bind
[strtoupper($o->name
)] = $i;
701 return $this->fields
[$this->bind
[strtoupper($colname)]];
707 global $ADODB_COUNTRECS;
708 $this->_numOfRows
= ($ADODB_COUNTRECS) ? @ads_num_rows
($this->_queryID
) : -1;
709 $this->_numOfFields
= @ads_num_fields
($this->_queryID
);
710 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
711 if ($this->_numOfRows
== 0) $this->_numOfRows
= -1;
712 //$this->useFetchArray = $this->connection->useFetchArray;
713 $this->_has_stupid_odbc_fetch_api_change
= ADODB_PHPVER
>= 0x4200;
721 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
722 function &GetArrayLimit($nrows,$offset=-1)
725 $rs =& $this->GetArray($nrows);
728 $savem = $this->fetchMode
;
729 $this->fetchMode
= ADODB_FETCH_NUM
;
730 $this->Move($offset);
731 $this->fetchMode
= $savem;
733 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
734 $this->fields
=& $this->GetRowAssoc(ADODB_ASSOC_CASE
);
739 while (!$this->EOF
&& $nrows != $cnt) {
740 $results[$cnt++
] = $this->fields
;
750 if ($this->_numOfRows
!= 0 && !$this->EOF
) {
751 $this->_currentRow++
;
753 if ($this->_has_stupid_odbc_fetch_api_change
)
754 $rez = @ads_fetch_into
($this->_queryID
,$this->fields
);
757 $rez = @ads_fetch_into
($this->_queryID
,$row,$this->fields
);
760 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
761 $this->fields
=& $this->GetRowAssoc(ADODB_ASSOC_CASE
);
766 $this->fields
= false;
774 if ($this->_has_stupid_odbc_fetch_api_change
)
775 $rez = @ads_fetch_into
($this->_queryID
,$this->fields
);
778 $rez = @ads_fetch_into
($this->_queryID
,$row,$this->fields
);
781 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
782 $this->fields
=& $this->GetRowAssoc(ADODB_ASSOC_CASE
);
786 $this->fields
= false;
792 return @ads_free_result
($this->_queryID
);