diff options
| author | Damien Regad <dregad@mantisbt.org> | 2015-04-20 15:51:22 +0200 |
|---|---|---|
| committer | Damien Regad <dregad@mantisbt.org> | 2015-04-20 15:51:22 +0200 |
| commit | 6efaea3b84221742cd41207677cae0bec40cec79 (patch) | |
| tree | 40ba08c3231ed989968e01ba1e68bd6d50d8e586 | |
| parent | 1ba29887672d41b6b017cd13efd474342aa234f3 (diff) | |
| parent | d347922492694003c40c239a0462d4c8fbc18932 (diff) | |
| download | adodb-6efaea3b84221742cd41207677cae0bec40cec79.tar.gz adodb-6efaea3b84221742cd41207677cae0bec40cec79.tar.bz2 adodb-6efaea3b84221742cd41207677cae0bec40cec79.zip | |
New PDO sqlsrv driver
Pull request https://github.com/ADOdb/ADOdb/pull/81
| -rw-r--r-- | docs/docs-adodb.htm | 1 | ||||
| -rw-r--r-- | drivers/adodb-pdo.inc.php | 323 | ||||
| -rw-r--r-- | drivers/adodb-pdo_sqlsrv.inc.php | 49 |
3 files changed, 276 insertions, 97 deletions
diff --git a/docs/docs-adodb.htm b/docs/docs-adodb.htm index d418d9dd..ca3508ae 100644 --- a/docs/docs-adodb.htm +++ b/docs/docs-adodb.htm @@ -6194,6 +6194,7 @@ PHP</a>. </p> <p>oci8: Fix broken quoting of table name in AddColumnSQL and AlterColumnSQL, thanks to Andreas Fernandez. see Github #67 <p>pdo: Added missing property. see Github #56 <p>pdo: Align method signatures with parent class, thanks to Andy Theuninck. see Github #62 +<p>pdo: new sqlsrv driver, thanks to MarcelTO. See Github #81 <p>pdo/mysql: New methods to make the driver behave more like mysql/mysqli, thanks to Andy Theuninck. see Github #40 <p>postgres: Stop using legacy function aliases <p>postgres: Fix AlterColumnSQL when updating multiple columns, thanks to Jouni Ahto. See Github #72 diff --git a/drivers/adodb-pdo.inc.php b/drivers/adodb-pdo.inc.php index 00029121..919cc3a7 100644 --- a/drivers/adodb-pdo.inc.php +++ b/drivers/adodb-pdo.inc.php @@ -1,19 +1,22 @@ <?php -/* -V5.20dev ??-???-2014 (c) 2000-2014 John Lim (jlim#natsoft.com). All rights reserved. - Released under both BSD license and Lesser GPL library license. - Whenever there is any discrepancy between the two licenses, - the BSD license will take precedence. -Set tabs to 4 for best viewing. +/** + @version V5.20dev ??-???-2014 (c) 2000-2014 John Lim (jlim#natsoft.com). All rights reserved. + + Released under both BSD license and Lesser GPL library license. + Whenever there is any discrepancy between the two licenses, + the BSD license will take precedence. - Latest version is available at http://adodb.sourceforge.net + Set tabs to 4 for best viewing. - Requires ODBC. Works on Windows and Unix. + Latest version is available at http://adodb.sourceforge.net + + Requires ODBC. Works on Windows and Unix. Problems: Where is float/decimal type in pdo_param_type LOB handling for CLOB/BLOB differs significantly */ + // security - hide paths if (!defined('ADODB_DIR')) die(); @@ -57,14 +60,7 @@ function adodb_pdo_type($t) } } -/*-------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------*/ - -//////////////////////////////////////////////// - - - - +/*----------------------------------------------------------------------------*/ class ADODB_pdo extends ADOConnection { @@ -113,11 +109,17 @@ class ADODB_pdo extends ADOConnection { function Time() { - if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual"; - else $sql = "select $this->sysTimeStamp"; + if (!empty($this->_driver->_hasdual)) { + $sql = "select $this->sysTimeStamp from dual"; + } + else { + $sql = "select $this->sysTimeStamp"; + } $rs = $this->_Execute($sql); - if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields)); + if ($rs && !$rs->EOF) { + return $this->UnixTimeStamp(reset($rs->fields)); + } return false; } @@ -129,7 +131,18 @@ class ADODB_pdo extends ADOConnection { $this->dsnType = substr($argDSN,0,$at); if ($argDatabasename) { - $argDSN .= ';dbname='.$argDatabasename; + switch($this->dsnType){ + case 'sqlsrv': + $argDSN .= ';database='.$argDatabasename; + break; + case 'mssql': + case 'mysql': + case 'oci': + case 'pgsql': + case 'sqlite': + default: + $argDSN .= ';dbname='.$argDatabasename; + } } try { $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword); @@ -143,10 +156,16 @@ class ADODB_pdo extends ADOConnection { if ($this->_connectionID) { switch(ADODB_ASSOC_CASE){ - case 0: $m = PDO::CASE_LOWER; break; - case 1: $m = PDO::CASE_UPPER; break; - default: - case 2: $m = PDO::CASE_NATURAL; break; + case ADODB_ASSOC_CASE_LOWER: + $m = PDO::CASE_LOWER; + break; + case ADODB_ASSOC_CASE_UPPER: + $m = PDO::CASE_UPPER; + break; + default: + case ADODB_ASSOC_CASE_NATIVE: + $m = PDO::CASE_NATURAL; + break; } //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT ); @@ -155,18 +174,21 @@ class ADODB_pdo extends ADOConnection { $class = 'ADODB_pdo_'.$this->dsnType; //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true); switch($this->dsnType) { - case 'oci': - case 'mysql': - case 'pgsql': - case 'mssql': - case 'sqlite': - include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php'); - break; + case 'mssql': + case 'mysql': + case 'oci': + case 'pgsql': + case 'sqlite': + case 'sqlsrv': + include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php'); + break; } - if (class_exists($class)) + if (class_exists($class)) { $this->_driver = new $class(); - else + } + else { $this->_driver = new ADODB_pdo_base(); + } $this->_driver->_connectionID = $this->_connectionID; $this->_UpdatePDO(); @@ -179,10 +201,13 @@ class ADODB_pdo extends ADOConnection { function Concat() { $args = func_get_args(); - if(method_exists($this->_driver, 'Concat')) + if(method_exists($this->_driver, 'Concat')) { return call_user_func_array(array($this->_driver, 'Concat'), $args); + } - if (PHP_VERSION >= 5.3) return call_user_func_array('parent::Concat', $args); + if (PHP_VERSION >= 5.3) { + return call_user_func_array('parent::Concat', $args); + } return call_user_func_array(array($this,'parent::Concat'), $args); } @@ -199,7 +224,7 @@ class ADODB_pdo extends ADOConnection { { $save = $this->_driver->fetchMode; $this->_driver->fetchMode = $this->fetchMode; - $this->_driver->debug = $this->debug; + $this->_driver->debug = $this->debug; $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); $this->_driver->fetchMode = $save; return $ret; @@ -224,76 +249,123 @@ class ADODB_pdo extends ADOConnection { function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false) { $obj = $stmt[1]; - if ($type) $obj->bindParam($name,$var,$type,$maxLen); - else $obj->bindParam($name, $var); + if ($type) { + $obj->bindParam($name, $var, $type, $maxLen); + } + else { + $obj->bindParam($name, $var); + } } function OffsetDate($dayFraction,$date=false) - { - return $this->_driver->OffsetDate($dayFraction,$date); - } + { + return $this->_driver->OffsetDate($dayFraction,$date); + } function ErrorMsg() { - if ($this->_errormsg !== false) return $this->_errormsg; - if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo(); - else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo(); - else return 'No Connection Established'; - + if ($this->_errormsg !== false) { + return $this->_errormsg; + } + if (!empty($this->_stmt)) { + $arr = $this->_stmt->errorInfo(); + } + else if (!empty($this->_connectionID)) { + $arr = $this->_connectionID->errorInfo(); + } + else { + return 'No Connection Established'; + } if ($arr) { - if (sizeof($arr)<2) return ''; - if ((integer)$arr[0]) return $arr[2]; - else return ''; - } else return '-1'; + if (sizeof($arr)<2) { + return ''; + } + if ((integer)$arr[0]) { + return $arr[2]; + } + else { + return ''; + } + } + else { + return '-1'; + } } function ErrorNo() { - if ($this->_errorno !== false) return $this->_errorno; - if (!empty($this->_stmt)) $err = $this->_stmt->errorCode(); + if ($this->_errorno !== false) { + return $this->_errorno; + } + if (!empty($this->_stmt)) { + $err = $this->_stmt->errorCode(); + } else if (!empty($this->_connectionID)) { $arr = $this->_connectionID->errorInfo(); - if (isset($arr[0])) $err = $arr[0]; - else $err = -1; - } else + if (isset($arr[0])) { + $err = $arr[0]; + } + else { + $err = -1; + } + } else { return 0; + } - if ($err == '00000') return 0; // allows empty check + if ($err == '00000') { + return 0; // allows empty check + } return $err; } function SetTransactionMode($transaction_mode) { - if(method_exists($this->_driver, 'SetTransactionMode')) + if(method_exists($this->_driver, 'SetTransactionMode')) { return $this->_driver->SetTransactionMode($transaction_mode); + } return parent::SetTransactionMode($seqname); } function BeginTrans() { - if(method_exists($this->_driver, 'BeginTrans')) + if(method_exists($this->_driver, 'BeginTrans')) { return $this->_driver->BeginTrans(); + } - if (!$this->hasTransactions) return false; - if ($this->transOff) return true; + if (!$this->hasTransactions) { + return false; + } + if ($this->transOff) { + return true; + } $this->transCnt += 1; $this->_autocommit = false; $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false); + return $this->_connectionID->beginTransaction(); } function CommitTrans($ok=true) { - if(method_exists($this->_driver, 'CommitTrans')) + if(method_exists($this->_driver, 'CommitTrans')) { return $this->_driver->CommitTrans($ok); + } - if (!$this->hasTransactions) return false; - if ($this->transOff) return true; - if (!$ok) return $this->RollbackTrans(); - if ($this->transCnt) $this->transCnt -= 1; + if (!$this->hasTransactions) { + return false; + } + if ($this->transOff) { + return true; + } + if (!$ok) { + return $this->RollbackTrans(); + } + if ($this->transCnt) { + $this->transCnt -= 1; + } $this->_autocommit = true; $ret = $this->_connectionID->commit(); @@ -303,12 +375,19 @@ class ADODB_pdo extends ADOConnection { function RollbackTrans() { - if(method_exists($this->_driver, 'RollbackTrans')) + if(method_exists($this->_driver, 'RollbackTrans')) { return $this->_driver->RollbackTrans(); + } - if (!$this->hasTransactions) return false; - if ($this->transOff) return true; - if ($this->transCnt) $this->transCnt -= 1; + if (!$this->hasTransactions) { + return false; + } + if ($this->transOff) { + return true; + } + if ($this->transCnt) { + $this->transCnt -= 1; + } $this->_autocommit = true; $ret = $this->_connectionID->rollback(); @@ -319,7 +398,9 @@ class ADODB_pdo extends ADOConnection { function Prepare($sql) { $this->_stmt = $this->_connectionID->prepare($sql); - if ($this->_stmt) return array($sql,$this->_stmt); + if ($this->_stmt) { + return array($sql,$this->_stmt); + } return false; } @@ -327,31 +408,36 @@ class ADODB_pdo extends ADOConnection { function PrepareStmt($sql) { $stmt = $this->_connectionID->prepare($sql); - if (!$stmt) return false; + if (!$stmt) { + return false; + } $obj = new ADOPDOStatement($stmt,$this); return $obj; } function CreateSequence($seqname='adodbseq',$startID=1) { - if(method_exists($this->_driver, 'CreateSequence')) + if(method_exists($this->_driver, 'CreateSequence')) { return $this->_driver->CreateSequence($seqname, $startID); + } return parent::CreateSequence($seqname, $startID); } function DropSequence($seqname='adodbseq') { - if(method_exists($this->_driver, 'DropSequence')) + if(method_exists($this->_driver, 'DropSequence')) { return $this->_driver->DropSequence($seqname); + } return parent::DropSequence($seqname); } function GenID($seqname='adodbseq',$startID=1) { - if(method_exists($this->_driver, 'GenID')) + if(method_exists($this->_driver, 'GenID')) { return $this->_driver->GenID($seqname, $startID); + } return parent::GenID($seqname, $startID); } @@ -369,8 +455,12 @@ class ADODB_pdo extends ADOConnection { #var_dump($this->_bindInputArray); if ($stmt) { $this->_driver->debug = $this->debug; - if ($inputarr) $ok = $stmt->execute($inputarr); - else $ok = $stmt->execute(); + if ($inputarr) { + $ok = $stmt->execute($inputarr); + } + else { + $ok = $stmt->execute(); + } } @@ -473,8 +563,12 @@ class ADOPDOStatement { function InParameter(&$var,$name,$maxLen=4000,$type=false) { - if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen); - else $this->_stmt->bindParam($name, $var); + if ($type) { + $this->_stmt->bindParam($name,$var,$type,$maxLen); + } + else { + $this->_stmt->bindParam($name, $var); + } } function Affected_Rows() @@ -484,13 +578,23 @@ class ADOPDOStatement { function ErrorMsg() { - if ($this->_stmt) $arr = $this->_stmt->errorInfo(); - else $arr = $this->_connectionID->errorInfo(); + if ($this->_stmt) { + $arr = $this->_stmt->errorInfo(); + } + else { + $arr = $this->_connectionID->errorInfo(); + } if (is_array($arr)) { - if ((integer) $arr[0] && isset($arr[2])) return $arr[2]; - else return ''; - } else return '-1'; + if ((integer) $arr[0] && isset($arr[2])) { + return $arr[2]; + } + else { + return ''; + } + } else { + return '-1'; + } } function NumCols() @@ -500,13 +604,17 @@ class ADOPDOStatement { function ErrorNo() { - if ($this->_stmt) return $this->_stmt->errorCode(); - else return $this->_connectionID->errorInfo(); + if ($this->_stmt) { + return $this->_stmt->errorCode(); + } + else { + return $this->_connectionID->errorInfo(); + } } } /*-------------------------------------------------------------------------------------- - Class Name: Recordset + Class Name: Recordset --------------------------------------------------------------------------------------*/ class ADORecordSet_pdo extends ADORecordSet { @@ -538,9 +646,13 @@ class ADORecordSet_pdo extends ADORecordSet { function Init() { - if ($this->_inited) return; + if ($this->_inited) { + return; + } $this->_inited = true; - if ($this->_queryID) @$this->_initrs(); + if ($this->_queryID) { + @$this->_initrs(); + } else { $this->_numOfRows = 0; $this->_numOfFields = 0; @@ -560,7 +672,9 @@ class ADORecordSet_pdo extends ADORecordSet { global $ADODB_COUNTRECS; $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1; - if (!$this->_numOfRows) $this->_numOfRows = -1; + if (!$this->_numOfRows) { + $this->_numOfRows = -1; + } $this->_numOfFields = $this->_queryID->columnCount(); } @@ -581,14 +695,25 @@ class ADORecordSet_pdo extends ADORecordSet { } //adodb_pr($arr); $o->name = $arr['name']; - if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type']; - else $o->type = adodb_pdo_type($arr['pdo_type']); + if (isset($arr['native_type']) && $arr['native_type'] <> "null") { + $o->type = $arr['native_type']; + } + else { + $o->type = adodb_pdo_type($arr['pdo_type']); + } $o->max_length = $arr['len']; $o->precision = $arr['precision']; - if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); - else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); - return $o; + switch(ADODB_ASSOC_CASE == 0) { + case ADODB_ASSOC_CASE_LOWER: + $o->name = strtolower($o->name); + break; + case ADODB_ASSOC_CASE_UPPER: + $o->name = strtoupper($o->name); + break; + default: + return $o; + } } function _seek($row) @@ -598,7 +723,9 @@ class ADORecordSet_pdo extends ADORecordSet { function _fetch() { - if (!$this->_queryID) return false; + if (!$this->_queryID) { + return false; + } $this->fields = $this->_queryID->fetch($this->fetchMode); return !empty($this->fields); @@ -611,7 +738,9 @@ class ADORecordSet_pdo extends ADORecordSet { function Fields($colname) { - if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname]; + if ($this->adodbFetchMode != ADODB_FETCH_NUM) { + return @$this->fields[$colname]; + } if (!$this->bind) { $this->bind = array(); @@ -620,7 +749,7 @@ class ADORecordSet_pdo extends ADORecordSet { $this->bind[strtoupper($o->name)] = $i; } } - return $this->fields[$this->bind[strtoupper($colname)]]; + return $this->fields[$this->bind[strtoupper($colname)]]; } } diff --git a/drivers/adodb-pdo_sqlsrv.inc.php b/drivers/adodb-pdo_sqlsrv.inc.php new file mode 100644 index 00000000..869e8e18 --- /dev/null +++ b/drivers/adodb-pdo_sqlsrv.inc.php @@ -0,0 +1,49 @@ +<?php + +/** + * Provided by Ned Andre to support sqlsrv library + */ +class ADODB_pdo_sqlsrv extends ADODB_pdo +{ + + var $hasTop = 'top'; + var $sysDate = 'convert(datetime,convert(char,GetDate(),102),102)'; + var $sysTimeStamp = 'GetDate()'; + + function _init(ADODB_pdo $parentDriver) + { + $parentDriver->hasTransactions = true; + $parentDriver->_bindInputArray = true; + $parentDriver->hasInsertID = true; + $parentDriver->fmtTimeStamp = "'Y-m-d H:i:s'"; + $parentDriver->fmtDate = "'Y-m-d'"; + } + + function BeginTrans() + { + $returnval = parent::BeginTrans(); + return $returnval; + } + + function MetaColumns($table, $normalize = true) + { + return false; + } + + function MetaTables($ttype = false, $showSchema = false, $mask = false) + { + return false; + } + + function SelectLimit($sql, $nrows = -1, $offset = -1, $inputarr = false, $secs2cache = 0) + { + $ret = ADOConnection::SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); + return $ret; + } + + function ServerInfo() + { + return ADOConnection::ServerInfo(); + } + +} |
