00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class DboSybase extends DboSource {
00036
00037
00038
00039
00040
00041 var $description = "Sybase DBO Driver";
00042
00043
00044
00045
00046
00047 var $startQuote = "";
00048
00049
00050
00051
00052
00053 var $endQuote = "";
00054
00055
00056
00057
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
00069
00070
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
00087
00088
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
00107
00108
00109
00110 function disconnect() {
00111 $this->connected = !@sybase_close($this->connection);
00112 return !$this->connected;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 function _execute($sql) {
00122 return sybase_query($sql, $this->connection);
00123 }
00124
00125
00126
00127
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
00151
00152
00153
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
00183
00184
00185
00186
00187
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
00217
00218
00219
00220
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
00233
00234
00235
00236
00237
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
00248
00249
00250
00251
00252
00253
00254 function rollback(&$model) {
00255 if (parent::rollback($model)) {
00256 return $this->execute('ROLLBACK TRAN');
00257 }
00258 return false;
00259 }
00260
00261
00262
00263
00264
00265
00266 function lastError() {
00267 return null;
00268 }
00269
00270
00271
00272
00273
00274
00275 function lastAffected() {
00276 if ($this->_result) {
00277 return sybase_affected_rows($this->connection);
00278 }
00279 return null;
00280 }
00281
00282
00283
00284
00285
00286
00287 function lastNumRows() {
00288 if ($this->hasResult()) {
00289 return @sybase_num_rows($this->_result);
00290 }
00291 return null;
00292 }
00293
00294
00295
00296
00297
00298
00299 function lastInsertId($source = null) {
00300 $result=$this->fetchRow('SELECT @@IDENTITY');
00301 return $result[0];
00302 }
00303
00304
00305
00306
00307
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
00343
00344
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
00366
00367
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 ?>