/scalr-2/tags/scalr-2.0.0/app/src/Lib/Data/DB/adodb_lite/adodbSQL_drivers/sybase/sybase_driver.inc
PHP | 662 lines | 587 code | 23 blank | 52 comment | 10 complexity | 32c1d639e3f2a9a231cf7b990b8299cc MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
- <?php
-
- /**
- * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with
- * a subset of the ADODB Command Syntax.
- * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,
- * PostgresSQL64, PostgresSQL7, SqLite and Sybase.
- *
- */
-
- class sybase_driver_ADOConnection extends ADOConnection
- {
- var $sysDate = 'GetDate()';
-
- function sybase_driver_ADOConnection()
- {
- $this->dbtype = 'sybase';
- $this->dataProvider = 'sybase';
- }
-
- /**
- * Connection to database server and selected database
- *
- * @access private
- */
-
- function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew)
- {
- if (!function_exists('sybase_connect')) return false;
-
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->persistent = $persistent;
- $this->forcenewconnection = $forcenew;
-
- if($this->persistent == 1)
- {
- $this->connectionId = @sybase_pconnect( $this->host, $this->username, $this->password );
- }
- else
- {
- $this->connectionId = @sybase_connect( $this->host, $this->username, $this->password );
- }
-
- if ($this->connectionId === false)
- {
- if ($fn = $this->raiseErrorFn)
- $fn($this->dbtype, 'CONNECT', $this->ErrorNo(), $this->ErrorMsg(), $this->host, $this->database, $this);
- return false;
- }
-
- if (!empty($this->database))
- {
- if($this->SelectDB( $this->database ) == false)
- {
- $this->connectionId = false;
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Choose a database to connect.
- *
- * @param dbname is the name of the database to select
- * @return true or false
- * @access public
- */
-
- function SelectDB($dbname)
- {
- $this->database = $dbname;
-
- if ($this->connectionId === false)
- {
- $this->connectionId = false;
- return false;
- }
- else
- {
- @sybase_select_db( $this->database, $this->connectionId );
- return true;
- }
- }
-
- /**
- * Return database error message
- * Usage: $errormessage =& $db->ErrorMsg();
- *
- * @access public
- */
-
- function ErrorMsg()
- {
- return @sybase_get_last_message($this->connectionId);
- }
-
- /**
- * Return database error number
- * Usage: $errorbo =& $db->ErrorNo();
- *
- * @access public
- */
-
- function ErrorNo()
- {
- $error = @sybase_get_last_message($this->connectionId);
- $errorno = strlen($error) ? $error : 0;
- return $errorno;
- }
-
- /**
- * Returns # of affected rows from insert/delete/update query
- *
- * @access public
- * @return integer Affected rows
- */
-
- function Affected_Rows()
- {
- return @sybase_affected_rows($this->connectionId);
- }
-
- /**
- * Returns the last record id of an inserted item
- * Usage: $db->Insert_ID();
- *
- * @access public
- */
-
- function Insert_ID()
- {
- return false;
- }
-
- /**
- * Correctly quotes a string so that all strings are escape coded.
- * An example is $db->qstr("Haven't a clue.");
- *
- * @param string the string to quote
- * @param [magic_quotes] if $s is GET/POST var, set to get_magic_quotes_gpc().
- *
- * @return single-quoted string IE: 'Haven\'t a clue.'
- */
-
- function qstr($string, $magic_quotes=false)
- {
- if (!$magic_quotes) {
- return "'".str_replace("'", "''", $string)."'";
- }
- $string = str_replace("\\'", "''", str_replace('\\\\', '\\', str_replace('\\"', '"', $string)));
- return "'" . $string . "'";
- }
-
- function QMagic($string)
- {
- return $this->qstr($string, get_magic_quotes_gpc());
- }
-
- /**
- * Returns concatenated string
- * Usage: $db->Concat($str1,$str2);
- *
- * @return concatenated string
- */
- function Concat()
- {
- $arr = func_get_args();
- return implode("+", $arr);
- }
-
- function IfNull( $field, $ifNull )
- {
- return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
- }
-
- /**
- * Closes database connection
- * Usage: $db->close();
- *
- * @access public
- */
-
- function Close()
- {
- @sybase_close( $this->connectionId );
- $this->connectionId = false;
- }
-
- /**
- * Returns All Records in an array
- *
- * Usage: $db->GetAll($sql);
- * @access public
- */
-
- function &GetAll($sql, $inputarr = false)
- {
- $data =& $this->GetArray($sql, $inputarr);
- return $data;
- }
-
- /**
- * Returns All Records in an array
- *
- * Usage: $db->GetArray($sql);
- * @access public
- */
-
- function &GetArray($sql, $inputarr = false)
- {
- $data = false;
- $result =& $this->Execute($sql, $inputarr);
- if ($result)
- {
- $data =& $result->GetArray();
- $result->Close();
- }
- return $data;
- }
-
- /**
- * Executes SQL query and instantiates resultset methods
- *
- * @access private
- * @return mixed Resultset methods
- */
-
- function &do_query( $sql, $offset, $nrows, $inputarr=false )
- {
- global $ADODB_FETCH_MODE;
-
- $false = false;
- $nrows = (integer) $nrows;
- $offset = (integer) $offset;
-
- $cnt = ($nrows >= 0) ? $nrows : 999999999;
- if ($offset > 0 && $cnt) $cnt += $offset;
-
- // @sybase_query("set rowcount $cnt", $this->connectionId);
- // $resultId =& @sybase_query($this->sql, $this->connectionId);
- // @sybase_query("set rowcount 0", $this->connectionId);
-
- if ($inputarr && is_array($inputarr)) {
- $sqlarr = explode('?', $sql);
- if (!is_array(reset($inputarr))) $inputarr = array($inputarr);
- foreach($inputarr as $arr) {
- $sql = ''; $i = 0;
- foreach($arr as $v) {
- $sql .= $sqlarr[$i];
- switch(gettype($v)){
- case 'string':
- $sql .= $this->qstr($v);
- break;
- case 'double':
- $sql .= str_replace(',', '.', $v);
- break;
- case 'boolean':
- $sql .= $v ? 1 : 0;
- break;
- default:
- if ($v === null)
- $sql .= 'NULL';
- else $sql .= $v;
- }
- $i += 1;
- }
- $sql .= $sqlarr[$i];
- if ($i+1 != sizeof($sqlarr))
- return $false;
- $this->sql = $sql;
- $time_start = array_sum(explode(' ', microtime()));
- $this->query_count++;
- $resultId = @sybase_query( $this->sql, $this->connectionId );
- $time_total = (array_sum(explode(' ', microtime())) - $time_start);
- $this->query_time_total += $time_total;
- if($this->debug_console)
- {
- $this->query_list[] = $this->sql;
- $this->query_list_time[] = $time_total;
- $this->query_list_errors[] = $this->ErrorMsg();
- }
- if($this->debug)
- {
- $this->outp($sql);
- }
- if ($resultId === false) { // error handling if query fails
- if ($fn = $this->raiseErrorFn)
- $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
- return $false;
- }
- }
- }
- else
- {
- $this->sql = $sql;
- $time_start = array_sum(explode(' ', microtime()));
- $this->query_count++;
- $resultId = @sybase_query( $this->sql, $this->connectionId );
- $time_total = (array_sum(explode(' ', microtime())) - $time_start);
- $this->query_time_total += $time_total;
- if($this->debug_console)
- {
- $this->query_list[] = $this->sql;
- $this->query_list_time[] = $time_total;
- $this->query_list_errors[] = $this->ErrorMsg();
- }
- if($this->debug)
- {
- $this->outp($sql);
- }
- }
-
- if ($resultId === false) { // error handling if query fails
- if ($fn = $this->raiseErrorFn)
- $fn($this->dbtype, 'EXECUTE', $this->ErrorNo(), $this->ErrorMsg(), $this->sql, $inputarr, $this);
- return $false;
- }
-
- if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
- $recordset = new ADORecordSet_empty();
- return $recordset;
- }
-
- $resultset_name = $this->last_module_name . "_ResultSet";
- $recordset = new $resultset_name( $resultId, $this->connectionId );
-
- $recordset->_currentRow = 0;
-
- switch ($ADODB_FETCH_MODE)
- {
- case ADODB_FETCH_NUM: $recordset->fetchMode = 0; break;
- case ADODB_FETCH_ASSOC:$recordset->fetchMode = 1; break;
- default:
- case ADODB_FETCH_DEFAULT:
- case ADODB_FETCH_BOTH:$recordset->fetchMode = 1; break;
- }
-
- $recordset->_numOfRows = @sybase_num_rows( $resultId );
- if( $recordset->_numOfRows == 0)
- {
- $recordset->EOF = true;
- }
- $recordset->_numOfFields = @sybase_num_fields( $resultId );
-
- if ($offset != -1 || $nrows != -1)
- {
- if($offset == -1 || ($offset == 0 && $nrows != -1))
- {
- $recordset->_numOfRows = ($nrows < $recordset->_numOfRows) ? $nrows : $recordset->_numOfRows;
- $recordset->_fetch();
- }
- else
- {
- if($offset > $recordset->_numOfRows)
- {
- $rs =& new ADORecordSet_empty();
- return $rs;
- }
-
- $recordset->_fetch();
- for($i = 0; $i < $offset; $i++)
- {
- $recordset->MoveNext();
- }
-
- $recordset->_currentRow = 0;
- if($nrows != -1)
- {
- $recordset->_numOfRows = ($nrows < ($recordset->_numOfRows - $offset)) ? $nrows : $recordset->_numOfRows - $offset;
- }
- else
- {
- $recordset->_numOfRows -= $offset;
- }
- }
- }
- else
- {
- $recordset->_fetch();
- }
-
- return $recordset;
- }
- }
-
- class sybase_driver_ResultSet
- {
- var $connectionId;
- var $fields;
- var $resultId;
- var $_currentRow = 0;
- var $_numOfRows = -1;
- var $_numOfFields = -1;
- var $fetchMode;
- var $EOF;
-
- /**
- * sybaseResultSet Constructor
- *
- * @access private
- * @param string $record
- * @param string $resultId
- */
-
- function sybase_driver_ResultSet( $resultId, $connectionId )
- {
- $this->fields = array();
- $this->connectionId = $connectionId;
- $this->record = array();
- $this->resultId = $resultId;
- $this->EOF = false;
- }
-
- /**
- * Frees resultset
- *
- * @access public
- */
-
- function close()
- {
- @sybase_free_result( $this->resultId );
- $this->fields = array();
- $this->resultId = false;
- }
-
- /**
- * Returns field name from select query
- *
- * @access public
- * @param string $field
- * @return string Field name
- */
-
- function fields( $field )
- {
- if(empty($field))
- {
- return $this->fields;
- }
- else
- {
- return $this->fields[$field];
- }
- }
-
- /**
- * Returns numrows from select query
- *
- * @access public
- * @return integer Numrows
- */
-
- function RecordCount()
- {
- return $this->_numOfRows;
- }
-
- /**
- * Returns num of fields from select query
- *
- * @access public
- * @return integer numfields
- */
-
- function FieldCount()
- {
- return $this->_numOfFields;
- }
-
- /**
- * Returns next record
- *
- * @access public
- */
-
- function MoveNext()
- {
- if($this->fetchMode == 0)
- {
- if (@$this->fields =& sybase_fetch_row($this->resultId)) {
- $this->_currentRow += 1;
- return true;
- }
- }
- else
- if($this->fetchMode == 1)
- {
- if (@$this->fields =& sybase_fetch_assoc($this->resultId)) {
- $this->_currentRow += 1;
- return true;
- }
- }
-
- if (!$this->EOF) {
- $this->_currentRow += 1;
- $this->EOF = true;
- }
- return false;
- }
-
- /**
- * Move to the first row in the recordset. Many databases do NOT support this.
- *
- * @return true or false
- */
-
- function MoveFirst()
- {
- if ($this->_currentRow == 0) return true;
- return $this->Move(0);
- }
-
- /**
- * Returns the Last Record
- *
- * @access public
- */
-
- function MoveLast()
- {
- if ($this->EOF) return false;
- return $this->Move($this->_numOfRows - 1);
- }
-
- /**
- * Random access to a specific row in the recordset. Some databases do not support
- * access to previous rows in the databases (no scrolling backwards).
- *
- * @param rowNumber is the row to move to (0-based)
- *
- * @return true if there still rows available, or false if there are no more rows (EOF).
- */
-
- function Move($rowNumber = 0)
- {
- if ($rowNumber == $this->_currentRow) return true;
- $this->EOF = false;
- if ($this->_numOfRows > 0){
- if ($rowNumber >= $this->_numOfRows - 1){
- $rowNumber = $this->_numOfRows - 1;
- }
- }
-
- if ($this->_seek($rowNumber)) {
- $this->_currentRow = $rowNumber;
- if ($this->_fetch()) {
- return true;
- }
- $this->fields = false;
- }
- $this->EOF = true;
- return false;
- }
-
- /**
- * Perform Seek to specific row
- *
- * @access private
- */
-
- function _seek($row)
- {
- if ($this->_numOfRows == 0) return false;
- return @sybase_data_seek($this->resultId,$row);
- }
-
- /**
- * Fills field array with first database element when query initially executed
- *
- * @access private
- */
-
- function _fetch()
- {
- if($this->fetchMode == 0)
- {
- $this->fields = sybase_fetch_row($this->resultId);
- }
- else
- if($this->fetchMode == 1)
- {
- $this->fields = sybase_fetch_assoc($this->resultId);
- }
-
- return is_array($this->fields);
- }
-
- /**
- * Check to see if last record reached
- *
- * @access public
- */
-
- function EOF()
- {
- if( $this->_currentRow < $this->_numOfRows)
- {
- return false;
- }
- else
- {
- $this->EOF = true;
- return true;
- }
- }
-
- /**
- * Returns All Records in an array
- *
- * @access public
- * @param [nRows] is the number of rows to return. -1 means every row.
- */
-
- function &GetArray($nRows = -1)
- {
- $results = array();
- $cnt = 0;
- while (!$this->EOF && $nRows != $cnt) {
- $results[] = $this->fields;
- $this->MoveNext();
- $cnt++;
- }
- return $results;
- }
-
- function &GetRows($nRows = -1)
- {
- $arr =& $this->GetArray($nRows);
- return $arr;
- }
-
- function &GetAll($nRows = -1)
- {
- $arr =& $this->GetArray($nRows);
- return $arr;
- }
-
- /**
- * Fetch field information for a table.
- *
- * @return object containing the name, type and max_length
- */
- function FetchField($fieldOffset = -1)
- {
- if ($fieldOffset != -1) {
- $fieldObject = @sybase_fetch_field($this->resultId, $fieldOffset);
- }
- else
- {
- $fieldObject = @sybase_fetch_field($this->resultId);
- }
- if ($fieldObject && !isset($fieldObject->type)) $fieldObject->type = ($fieldObject->numeric) ? 'float' : 'varchar';
- return $fieldObject;
- }
- }
- ?>