dbo_sybase.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: dbo_sybase.php 7847 2008-11-08 02:54:07Z renan.saddam $ */
00003 /**
00004  * Sybase layer for DBO
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.libs.model.datasources.dbo
00021  * @since         CakePHP(tm) v 1.2.0.3097
00022  * @version       $Revision: 7847 $
00023  * @modifiedby    $LastChangedBy: renan.saddam $
00024  * @lastmodified  $Date: 2008-11-07 21:54:07 -0500 (Fri, 07 Nov 2008) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Short description for class.
00029  *
00030  * Long description for class
00031  *
00032  * @package       cake
00033  * @subpackage    cake.cake.libs.model.datasources.dbo
00034  */
00035 class DboSybase extends DboSource {
00036 /**
00037  * Driver description
00038  *
00039  * @var string
00040  */
00041     var $description = "Sybase DBO Driver";
00042 /**
00043  * Start quote for quoted identifiers
00044  *
00045  * @var string
00046  */
00047     var $startQuote = "";
00048 /**
00049  * End quote for quoted identifiers
00050  *
00051  * @var string
00052  */
00053     var $endQuote = "";
00054 /**
00055  * Base configuration settings for Sybase driver
00056  *
00057  * @var array
00058  */
00059     var $_baseConfig = array(
00060         'persistent' => true,
00061         'host' => 'localhost',
00062         'login' => 'sa',
00063         'password' => '',
00064         'database' => 'cake',
00065         'port' => '4100'
00066     );
00067 /**
00068  * Sybase column definition
00069  *
00070  * @var array
00071  */
00072     var $columns = array(
00073         'primary_key' => array('name' => 'numeric(9,0) IDENTITY PRIMARY KEY'),
00074         'string' => array('name' => 'varchar', 'limit' => '255'),
00075         'text' => array('name' => 'text'),
00076         'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
00077         'float' => array('name' => 'float', 'formatter' => 'floatval'),
00078         'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
00079         'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
00080         'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
00081         'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
00082         'binary' => array('name' => 'image'),
00083         'boolean' => array('name' => 'bit')
00084     );
00085 /**
00086  * Connects to the database using options in the given configuration array.
00087  *
00088  * @return boolean True if the database could be connected, else false
00089  */
00090     function connect() {
00091         $config = $this->config;
00092         $this->connected = false;
00093 
00094         if (!$config['persistent']) {
00095             $this->connection = sybase_connect($config['host'], $config['login'], $config['password'], true);
00096         } else {
00097             $this->connection = sybase_pconnect($config['host'], $config['login'], $config['password']);
00098         }
00099 
00100         if (sybase_select_db($config['database'], $this->connection)) {
00101             $this->connected = true;
00102         }
00103         return $this->connected;
00104     }
00105 /**
00106  * Disconnects from database.
00107  *
00108  * @return boolean True if the database could be disconnected, else false
00109  */
00110     function disconnect() {
00111         $this->connected = !@sybase_close($this->connection);
00112         return !$this->connected;
00113     }
00114 /**
00115  * Executes given SQL statement.
00116  *
00117  * @param string $sql SQL statement
00118  * @return resource Result resource identifier
00119  * @access protected
00120  */
00121     function _execute($sql) {
00122         return sybase_query($sql, $this->connection);
00123     }
00124 /**
00125  * Returns an array of sources (tables) in the database.
00126  *
00127  * @return array Array of tablenames in the database
00128  */
00129     function listSources() {
00130         $cache = parent::listSources();
00131         if ($cache != null) {
00132             return $cache;
00133         }
00134 
00135         $result = $this->_execute("select name from sysobjects where type='U'");
00136         if (!$result) {
00137             return array();
00138         } else {
00139 
00140             $tables = array();
00141             while ($line = sybase_fetch_array($result)) {
00142                 $tables[] = $line[0];
00143             }
00144 
00145             parent::listSources($tables);
00146             return $tables;
00147         }
00148     }
00149 /**
00150  * Returns an array of the fields in given table name.
00151  *
00152  * @param string $tableName Name of database table to inspect
00153  * @return array Fields in table. Keys are name and type
00154  */
00155     function describe(&$model) {
00156 
00157         $cache = parent::describe($model);
00158         if ($cache != null) {
00159             return $cache;
00160         }
00161 
00162         $fields = false;
00163         $cols = $this->query('DESC ' . $this->fullTableName($model));
00164 
00165         foreach ($cols as $column) {
00166             $colKey = array_keys($column);
00167             if (isset($column[$colKey[0]]) && !isset($column[0])) {
00168                 $column[0] = $column[$colKey[0]];
00169             }
00170             if (isset($column[0])) {
00171                 $fields[$column[0]['Field']] = array('type' => $this->column($column[0]['Type']),
00172                                                     'null' => $column[0]['Null'],
00173                                                     'length' => $this->length($column[0]['Type']),
00174                                                     );
00175             }
00176         }
00177 
00178         $this->__cacheDescription($model->tablePrefix.$model->table, $fields);
00179         return $fields;
00180     }
00181 /**
00182  * Returns a quoted and escaped string of $data for use in an SQL statement.
00183  *
00184  * @param string $data String to be prepared for use in an SQL statement
00185  * @param string $column The column into which this data will be inserted
00186  * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
00187  * @return string Quoted and escaped data
00188  */
00189     function value($data, $column = null, $safe = false) {
00190         $parent = parent::value($data, $column, $safe);
00191 
00192         if ($parent != null) {
00193             return $parent;
00194         }
00195 
00196         if ($data === null) {
00197             return 'NULL';
00198         }
00199 
00200         if ($data === '') {
00201             return  "''";
00202         }
00203 
00204         switch ($column) {
00205             case 'boolean':
00206                 $data = $this->boolean((bool)$data);
00207             break;
00208             default:
00209                 $data = str_replace("'", "''", $data);
00210             break;
00211         }
00212 
00213         return "'" . $data . "'";
00214     }
00215 /**
00216  * Begin a transaction
00217  *
00218  * @param unknown_type $model
00219  * @return boolean True on success, false on fail
00220  * (i.e. if the database/model does not support transactions).
00221  */
00222     function begin(&$model) {
00223         if (parent::begin($model)) {
00224             if ($this->execute('BEGIN TRAN')) {
00225                 $this->_transactionStarted = true;
00226                 return true;
00227             }
00228         }
00229         return false;
00230     }
00231 /**
00232  * Commit a transaction
00233  *
00234  * @param unknown_type $model
00235  * @return boolean True on success, false on fail
00236  * (i.e. if the database/model does not support transactions,
00237  * or a transaction has not started).
00238  */
00239     function commit(&$model) {
00240         if (parent::commit($model)) {
00241             $this->_transactionStarted = false;
00242             return $this->execute('COMMIT TRAN');
00243         }
00244         return false;
00245     }
00246 /**
00247  * Rollback a transaction
00248  *
00249  * @param unknown_type $model
00250  * @return boolean True on success, false on fail
00251  * (i.e. if the database/model does not support transactions,
00252  * or a transaction has not started).
00253  */
00254     function rollback(&$model) {
00255         if (parent::rollback($model)) {
00256             return $this->execute('ROLLBACK TRAN');
00257         }
00258         return false;
00259     }
00260 /**
00261  * Returns a formatted error message from previous database operation.
00262  *
00263  * @todo not implemented
00264  * @return string Error message with error number
00265  */
00266     function lastError() {
00267         return null;
00268     }
00269 /**
00270  * Returns number of affected rows in previous database operation. If no previous operation exists,
00271  * this returns false.
00272  *
00273  * @return integer Number of affected rows
00274  */
00275     function lastAffected() {
00276         if ($this->_result) {
00277             return sybase_affected_rows($this->connection);
00278         }
00279         return null;
00280     }
00281 /**
00282  * Returns number of rows in previous resultset. If no previous resultset exists,
00283  * this returns false.
00284  *
00285  * @return integer Number of rows in resultset
00286  */
00287     function lastNumRows() {
00288         if ($this->hasResult()) {
00289             return @sybase_num_rows($this->_result);
00290         }
00291         return null;
00292     }
00293 /**
00294  * Returns the ID generated from the previous INSERT operation.
00295  *
00296  * @param unknown_type $source
00297  * @return in
00298  */
00299     function lastInsertId($source = null) {
00300         $result=$this->fetchRow('SELECT @@IDENTITY');
00301         return $result[0];
00302     }
00303 /**
00304  * Converts database-layer column types to basic types
00305  *
00306  * @param string $real Real database-layer column type (i.e. "varchar(255)")
00307  * @return string Abstract column type (i.e. "string")
00308  */
00309     function column($real) {
00310         if (is_array($real)) {
00311             $col = $real['name'];
00312             if (isset($real['limit']))
00313             {
00314                 $col .= '('.$real['limit'].')';
00315             }
00316             return $col;
00317         }
00318 
00319         $col = str_replace(')', '', $real);
00320         $limit = null;
00321         if (strpos($col, '(') !== false) {
00322             list($col, $limit) = explode('(', $col);
00323         }
00324 
00325         if (in_array($col, array('datetime', 'smalldatetime'))) {
00326             return 'datetime';
00327         } elseif (in_array($col, array('int', 'bigint', 'smallint', 'tinyint'))) {
00328             return 'integer';
00329         } elseif (in_array($col, array('float', 'double', 'real', 'decimal', 'money', 'numeric', 'smallmoney'))) {
00330             return 'float';
00331         } elseif (strpos($col, 'text') !== false) {
00332             return 'text';
00333         } elseif (in_array($col, array('char', 'nchar', 'nvarchar', 'string', 'varchar'))) {
00334             return 'binary';
00335         } elseif (in_array($col, array('binary', 'image', 'varbinary'))) {
00336             return 'binary';
00337         }
00338 
00339         return 'text';
00340     }
00341 /**
00342  * Enter description here...
00343  *
00344  * @param unknown_type $results
00345  */
00346     function resultSet(&$results) {
00347         $this->results =& $results;
00348         $this->map = array();
00349         $num_fields = sybase_num_fields($results);
00350         $index = 0;
00351         $j = 0;
00352 
00353         while ($j < $num_fields) {
00354 
00355             $column = sybase_fetch_field($results,$j);
00356             if (!empty($column->table)) {
00357                 $this->map[$index++] = array($column->table, $column->name);
00358             } else {
00359                 $this->map[$index++] = array(0, $column->name);
00360             }
00361             $j++;
00362         }
00363     }
00364 /**
00365  * Fetches the next row from the current result set
00366  *
00367  * @return unknown
00368  */
00369     function fetchResult() {
00370         if ($row = sybase_fetch_row($this->results)) {
00371             $resultRow = array();
00372             $i = 0;
00373             foreach ($row as $index => $field) {
00374                 list($table, $column) = $this->map[$index];
00375                 $resultRow[$table][$column] = $row[$index];
00376                 $i++;
00377             }
00378             return $resultRow;
00379         } else {
00380             return false;
00381         }
00382     }
00383 }
00384 ?>

Generated on Sun Nov 22 00:30:53 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7