| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: dbo_sybase.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Sybase layer for DBO
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
21 |
* @since CakePHP(tm) v 1.2.0.3097
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Short description for class.
|
|
|
29 |
*
|
|
|
30 |
* Long description for class
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.libs.model.datasources.dbo
|
|
|
34 |
*/
|
|
|
35 |
class DboSybase extends DboSource {
|
|
|
36 |
/**
|
|
|
37 |
* Driver description
|
|
|
38 |
*
|
|
|
39 |
* @var string
|
|
|
40 |
*/
|
|
|
41 |
var $description = "Sybase DBO Driver";
|
|
|
42 |
/**
|
|
|
43 |
* Start quote for quoted identifiers
|
|
|
44 |
*
|
|
|
45 |
* @var string
|
|
|
46 |
*/
|
|
|
47 |
var $startQuote = "";
|
|
|
48 |
/**
|
|
|
49 |
* End quote for quoted identifiers
|
|
|
50 |
*
|
|
|
51 |
* @var string
|
|
|
52 |
*/
|
|
|
53 |
var $endQuote = "";
|
|
|
54 |
/**
|
|
|
55 |
* Base configuration settings for Sybase driver
|
|
|
56 |
*
|
|
|
57 |
* @var array
|
|
|
58 |
*/
|
|
|
59 |
var $_baseConfig = array(
|
|
|
60 |
'persistent' => true,
|
|
|
61 |
'host' => 'localhost',
|
|
|
62 |
'login' => 'sa',
|
|
|
63 |
'password' => '',
|
|
|
64 |
'database' => 'cake',
|
|
|
65 |
'port' => '4100'
|
|
|
66 |
);
|
|
|
67 |
/**
|
|
|
68 |
* Sybase column definition
|
|
|
69 |
*
|
|
|
70 |
* @var array
|
|
|
71 |
*/
|
|
|
72 |
var $columns = array(
|
|
|
73 |
'primary_key' => array('name' => 'numeric(9,0) IDENTITY PRIMARY KEY'),
|
|
|
74 |
'string' => array('name' => 'varchar', 'limit' => '255'),
|
|
|
75 |
'text' => array('name' => 'text'),
|
|
|
76 |
'integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'),
|
|
|
77 |
'float' => array('name' => 'float', 'formatter' => 'floatval'),
|
|
|
78 |
'datetime' => array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
79 |
'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),
|
|
|
80 |
'time' => array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),
|
|
|
81 |
'date' => array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),
|
|
|
82 |
'binary' => array('name' => 'image'),
|
|
|
83 |
'boolean' => array('name' => 'bit')
|
|
|
84 |
);
|
|
|
85 |
/**
|
|
|
86 |
* Connects to the database using options in the given configuration array.
|
|
|
87 |
*
|
|
|
88 |
* @return boolean True if the database could be connected, else false
|
|
|
89 |
*/
|
|
|
90 |
function connect() {
|
|
|
91 |
$config = $this->config;
|
|
|
92 |
$this->connected = false;
|
|
|
93 |
|
|
|
94 |
if (!$config['persistent']) {
|
|
|
95 |
$this->connection = sybase_connect($config['host'], $config['login'], $config['password'], true);
|
|
|
96 |
} else {
|
|
|
97 |
$this->connection = sybase_pconnect($config['host'], $config['login'], $config['password']);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
if (sybase_select_db($config['database'], $this->connection)) {
|
|
|
101 |
$this->connected = true;
|
|
|
102 |
}
|
|
|
103 |
return $this->connected;
|
|
|
104 |
}
|
|
|
105 |
/**
|
|
|
106 |
* Disconnects from database.
|
|
|
107 |
*
|
|
|
108 |
* @return boolean True if the database could be disconnected, else false
|
|
|
109 |
*/
|
|
|
110 |
function disconnect() {
|
|
|
111 |
$this->connected = !@sybase_close($this->connection);
|
|
|
112 |
return !$this->connected;
|
|
|
113 |
}
|
|
|
114 |
/**
|
|
|
115 |
* Executes given SQL statement.
|
|
|
116 |
*
|
|
|
117 |
* @param string $sql SQL statement
|
|
|
118 |
* @return resource Result resource identifier
|
|
|
119 |
* @access protected
|
|
|
120 |
*/
|
|
|
121 |
function _execute($sql) {
|
|
|
122 |
return sybase_query($sql, $this->connection);
|
|
|
123 |
}
|
|
|
124 |
/**
|
|
|
125 |
* Returns an array of sources (tables) in the database.
|
|
|
126 |
*
|
|
|
127 |
* @return array Array of tablenames in the database
|
|
|
128 |
*/
|
|
|
129 |
function listSources() {
|
|
|
130 |
$cache = parent::listSources();
|
|
|
131 |
if ($cache != null) {
|
|
|
132 |
return $cache;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$result = $this->_execute("select name from sysobjects where type='U'");
|
|
|
136 |
if (!$result) {
|
|
|
137 |
return array();
|
|
|
138 |
} else {
|
|
|
139 |
|
|
|
140 |
$tables = array();
|
|
|
141 |
while ($line = sybase_fetch_array($result)) {
|
|
|
142 |
$tables[] = $line[0];
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
parent::listSources($tables);
|
|
|
146 |
return $tables;
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
/**
|
|
|
150 |
* Returns an array of the fields in given table name.
|
|
|
151 |
*
|
|
|
152 |
* @param string $tableName Name of database table to inspect
|
|
|
153 |
* @return array Fields in table. Keys are name and type
|
|
|
154 |
*/
|
|
|
155 |
function describe(&$model) {
|
|
|
156 |
|
|
|
157 |
$cache = parent::describe($model);
|
|
|
158 |
if ($cache != null) {
|
|
|
159 |
return $cache;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$fields = false;
|
|
|
163 |
$cols = $this->query('DESC ' . $this->fullTableName($model));
|
|
|
164 |
|
|
|
165 |
foreach ($cols as $column) {
|
|
|
166 |
$colKey = array_keys($column);
|
|
|
167 |
if (isset($column[$colKey[0]]) && !isset($column[0])) {
|
|
|
168 |
$column[0] = $column[$colKey[0]];
|
|
|
169 |
}
|
|
|
170 |
if (isset($column[0])) {
|
|
|
171 |
$fields[$column[0]['Field']] = array('type' => $this->column($column[0]['Type']),
|
|
|
172 |
'null' => $column[0]['Null'],
|
|
|
173 |
'length' => $this->length($column[0]['Type']),
|
|
|
174 |
);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
$this->__cacheDescription($model->tablePrefix.$model->table, $fields);
|
|
|
179 |
return $fields;
|
|
|
180 |
}
|
|
|
181 |
/**
|
|
|
182 |
* Returns a quoted and escaped string of $data for use in an SQL statement.
|
|
|
183 |
*
|
|
|
184 |
* @param string $data String to be prepared for use in an SQL statement
|
|
|
185 |
* @param string $column The column into which this data will be inserted
|
|
|
186 |
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
|
|
|
187 |
* @return string Quoted and escaped data
|
|
|
188 |
*/
|
|
|
189 |
function value($data, $column = null, $safe = false) {
|
|
|
190 |
$parent = parent::value($data, $column, $safe);
|
|
|
191 |
|
|
|
192 |
if ($parent != null) {
|
|
|
193 |
return $parent;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if ($data === null) {
|
|
|
197 |
return 'NULL';
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if ($data === '') {
|
|
|
201 |
return "''";
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
switch ($column) {
|
|
|
205 |
case 'boolean':
|
|
|
206 |
$data = $this->boolean((bool)$data);
|
|
|
207 |
break;
|
|
|
208 |
default:
|
|
|
209 |
$data = str_replace("'", "''", $data);
|
|
|
210 |
break;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
return "'" . $data . "'";
|
|
|
214 |
}
|
|
|
215 |
/**
|
|
|
216 |
* Begin a transaction
|
|
|
217 |
*
|
|
|
218 |
* @param unknown_type $model
|
|
|
219 |
* @return boolean True on success, false on fail
|
|
|
220 |
* (i.e. if the database/model does not support transactions).
|
|
|
221 |
*/
|
|
|
222 |
function begin(&$model) {
|
|
|
223 |
if (parent::begin($model)) {
|
|
|
224 |
if ($this->execute('BEGIN TRAN')) {
|
|
|
225 |
$this->_transactionStarted = true;
|
|
|
226 |
return true;
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
return false;
|
|
|
230 |
}
|
|
|
231 |
/**
|
|
|
232 |
* Commit a transaction
|
|
|
233 |
*
|
|
|
234 |
* @param unknown_type $model
|
|
|
235 |
* @return boolean True on success, false on fail
|
|
|
236 |
* (i.e. if the database/model does not support transactions,
|
|
|
237 |
* or a transaction has not started).
|
|
|
238 |
*/
|
|
|
239 |
function commit(&$model) {
|
|
|
240 |
if (parent::commit($model)) {
|
|
|
241 |
$this->_transactionStarted = false;
|
|
|
242 |
return $this->execute('COMMIT TRAN');
|
|
|
243 |
}
|
|
|
244 |
return false;
|
|
|
245 |
}
|
|
|
246 |
/**
|
|
|
247 |
* Rollback a transaction
|
|
|
248 |
*
|
|
|
249 |
* @param unknown_type $model
|
|
|
250 |
* @return boolean True on success, false on fail
|
|
|
251 |
* (i.e. if the database/model does not support transactions,
|
|
|
252 |
* or a transaction has not started).
|
|
|
253 |
*/
|
|
|
254 |
function rollback(&$model) {
|
|
|
255 |
if (parent::rollback($model)) {
|
|
|
256 |
return $this->execute('ROLLBACK TRAN');
|
|
|
257 |
}
|
|
|
258 |
return false;
|
|
|
259 |
}
|
|
|
260 |
/**
|
|
|
261 |
* Returns a formatted error message from previous database operation.
|
|
|
262 |
*
|
|
|
263 |
* @todo not implemented
|
|
|
264 |
* @return string Error message with error number
|
|
|
265 |
*/
|
|
|
266 |
function lastError() {
|
|
|
267 |
return null;
|
|
|
268 |
}
|
|
|
269 |
/**
|
|
|
270 |
* Returns number of affected rows in previous database operation. If no previous operation exists,
|
|
|
271 |
* this returns false.
|
|
|
272 |
*
|
|
|
273 |
* @return integer Number of affected rows
|
|
|
274 |
*/
|
|
|
275 |
function lastAffected() {
|
|
|
276 |
if ($this->_result) {
|
|
|
277 |
return sybase_affected_rows($this->connection);
|
|
|
278 |
}
|
|
|
279 |
return null;
|
|
|
280 |
}
|
|
|
281 |
/**
|
|
|
282 |
* Returns number of rows in previous resultset. If no previous resultset exists,
|
|
|
283 |
* this returns false.
|
|
|
284 |
*
|
|
|
285 |
* @return integer Number of rows in resultset
|
|
|
286 |
*/
|
|
|
287 |
function lastNumRows() {
|
|
|
288 |
if ($this->hasResult()) {
|
|
|
289 |
return @sybase_num_rows($this->_result);
|
|
|
290 |
}
|
|
|
291 |
return null;
|
|
|
292 |
}
|
|
|
293 |
/**
|
|
|
294 |
* Returns the ID generated from the previous INSERT operation.
|
|
|
295 |
*
|
|
|
296 |
* @param unknown_type $source
|
|
|
297 |
* @return in
|
|
|
298 |
*/
|
|
|
299 |
function lastInsertId($source = null) {
|
|
|
300 |
$result=$this->fetchRow('SELECT @@IDENTITY');
|
|
|
301 |
return $result[0];
|
|
|
302 |
}
|
|
|
303 |
/**
|
|
|
304 |
* Converts database-layer column types to basic types
|
|
|
305 |
*
|
|
|
306 |
* @param string $real Real database-layer column type (i.e. "varchar(255)")
|
|
|
307 |
* @return string Abstract column type (i.e. "string")
|
|
|
308 |
*/
|
|
|
309 |
function column($real) {
|
|
|
310 |
if (is_array($real)) {
|
|
|
311 |
$col = $real['name'];
|
|
|
312 |
if (isset($real['limit']))
|
|
|
313 |
{
|
|
|
314 |
$col .= '('.$real['limit'].')';
|
|
|
315 |
}
|
|
|
316 |
return $col;
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
$col = str_replace(')', '', $real);
|
|
|
320 |
$limit = null;
|
|
|
321 |
if (strpos($col, '(') !== false) {
|
|
|
322 |
list($col, $limit) = explode('(', $col);
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
if (in_array($col, array('datetime', 'smalldatetime'))) {
|
|
|
326 |
return 'datetime';
|
|
|
327 |
} elseif (in_array($col, array('int', 'bigint', 'smallint', 'tinyint'))) {
|
|
|
328 |
return 'integer';
|
|
|
329 |
} elseif (in_array($col, array('float', 'double', 'real', 'decimal', 'money', 'numeric', 'smallmoney'))) {
|
|
|
330 |
return 'float';
|
|
|
331 |
} elseif (strpos($col, 'text') !== false) {
|
|
|
332 |
return 'text';
|
|
|
333 |
} elseif (in_array($col, array('char', 'nchar', 'nvarchar', 'string', 'varchar'))) {
|
|
|
334 |
return 'binary';
|
|
|
335 |
} elseif (in_array($col, array('binary', 'image', 'varbinary'))) {
|
|
|
336 |
return 'binary';
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
return 'text';
|
|
|
340 |
}
|
|
|
341 |
/**
|
|
|
342 |
* Enter description here...
|
|
|
343 |
*
|
|
|
344 |
* @param unknown_type $results
|
|
|
345 |
*/
|
|
|
346 |
function resultSet(&$results) {
|
|
|
347 |
$this->results =& $results;
|
|
|
348 |
$this->map = array();
|
|
|
349 |
$num_fields = sybase_num_fields($results);
|
|
|
350 |
$index = 0;
|
|
|
351 |
$j = 0;
|
|
|
352 |
|
|
|
353 |
while ($j < $num_fields) {
|
|
|
354 |
|
|
|
355 |
$column = sybase_fetch_field($results,$j);
|
|
|
356 |
if (!empty($column->table)) {
|
|
|
357 |
$this->map[$index++] = array($column->table, $column->name);
|
|
|
358 |
} else {
|
|
|
359 |
$this->map[$index++] = array(0, $column->name);
|
|
|
360 |
}
|
|
|
361 |
$j++;
|
|
|
362 |
}
|
|
|
363 |
}
|
|
|
364 |
/**
|
|
|
365 |
* Fetches the next row from the current result set
|
|
|
366 |
*
|
|
|
367 |
* @return unknown
|
|
|
368 |
*/
|
|
|
369 |
function fetchResult() {
|
|
|
370 |
if ($row = sybase_fetch_row($this->results)) {
|
|
|
371 |
$resultRow = array();
|
|
|
372 |
$i = 0;
|
|
|
373 |
foreach ($row as $index => $field) {
|
|
|
374 |
list($table, $column) = $this->map[$index];
|
|
|
375 |
$resultRow[$table][$column] = $row[$index];
|
|
|
376 |
$i++;
|
|
|
377 |
}
|
|
|
378 |
return $resultRow;
|
|
|
379 |
} else {
|
|
|
380 |
return false;
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
}
|
|
|
384 |
?>
|