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
00036
00037
00038
00039
00040
00041 class DboDb2 extends DboSource {
00042
00043
00044
00045
00046
00047 var $description = 'IBM DB2 DBO Driver';
00048
00049
00050
00051
00052
00053 var $startQuote = '';
00054
00055
00056
00057
00058
00059 var $endQuote = '';
00060
00061
00062
00063
00064
00065
00066 var $_baseConfig = array(
00067 'persistent' => true,
00068 'login' => 'db2inst1',
00069 'password' => '',
00070 'database' => 'cake',
00071 'schema' => '',
00072 'hostname' => '127.0.0.1',
00073 'port' => '50001',
00074 'encoding' => 'UTF-8',
00075 'cataloged' => true,
00076 'autocommit' => true
00077 );
00078
00079
00080
00081
00082
00083
00084
00085
00086 var $columns = array(
00087 'primary_key' => array('name' => 'not null generated by default as identity (start with 1, increment by 1)'),
00088 'string' => array('name' => 'varchar', 'limit' => '255'),
00089 'text' => array('name' => 'clob'),
00090 'integer' => array('name' => 'integer', 'limit' => '10', 'formatter' => 'intval'),
00091 'float' => array('name' => 'double', 'formatter' => 'floatval'),
00092 'datetime' => array('name' => 'timestamp', 'format' => 'Y-m-d-H.i.s', 'formatter' => 'date'),
00093 'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d-H.i.s', 'formatter' => 'date'),
00094 'time' => array('name' => 'time', 'format' => 'H.i.s', 'formatter' => 'date'),
00095 'date' => array('name' => 'date', 'format' => 'Y-m-d', 'formatter' => 'date'),
00096 'binary' => array('name' => 'blob'),
00097 'boolean' => array('name' => 'smallint', 'limit' => '1')
00098 );
00099
00100
00101
00102
00103
00104 var $_resultMap = array();
00105
00106
00107
00108
00109
00110 function connect() {
00111 $config = $this->config;
00112 $connect = 'db2_connect';
00113 if ($config['persistent']) {
00114 $connect = 'db2_pconnect';
00115 }
00116 $this->connected = false;
00117
00118 if ($config['cataloged']) {
00119 $this->connection = $connect($config['database'], $config['login'], $config['password']);
00120 } else {
00121 $conn_string = sprintf(
00122 "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s;PWD=%s;",
00123 $config['database'],
00124 $config['hostname'],
00125 $config['port'],
00126 $config['login'],
00127 $config['password']
00128 );
00129 $this->connection = db2_connect($conn_string, '', '');
00130 }
00131
00132 if ($this->connection) {
00133 $this->connected = true;
00134 }
00135
00136 if ($config['schema'] !== '') {
00137 $this->_execute('SET CURRENT SCHEMA = ' . $config['schema']);
00138 }
00139 return $this->connected;
00140 }
00141
00142
00143
00144
00145
00146 function disconnect() {
00147 @db2_free_result($this->results);
00148 $this->connected = !@db2_close($this->connection);
00149 return !$this->connected;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 function _execute($sql) {
00161
00162 $result = db2_exec($this->connection, $sql);
00163
00164 if(!is_bool($result)){
00165
00166 $map = array();
00167 $num_fields = db2_num_fields($result);
00168 $index = 0;
00169 $j = 0;
00170 $offset = 0;
00171
00172 while ($j < $num_fields) {
00173 $columnName = strtolower(db2_field_name($result, $j));
00174 $tmp = strpos($sql, '.' . $columnName, $offset);
00175 $tableName = substr($sql, $offset, ($tmp-$offset));
00176 $tableName = substr($tableName, strrpos($tableName, ' ') + 1);
00177 $map[$index++] = array($tableName, $columnName);
00178 $j++;
00179 $offset = strpos($sql, ' ', $tmp);
00180 }
00181
00182 $this->_resultMap[$result] = $map;
00183 }
00184
00185 return $result;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 function listSources() {
00196 $cache = parent::listSources();
00197
00198 if ($cache != null) {
00199 return $cache;
00200 }
00201 $result = db2_tables($this->connection);
00202 $tables = array();
00203
00204 while (db2_fetch_row($result)) {
00205 $tables[] = strtolower(db2_result($result, 'TABLE_NAME'));
00206 }
00207 parent::listSources($tables);
00208 return $tables;
00209 }
00210
00211
00212
00213
00214
00215
00216 function &describe(&$model) {
00217 $cache = parent::describe($model);
00218
00219 if ($cache != null) {
00220 return $cache;
00221 }
00222 $fields = array();
00223 $result = db2_columns($this->connection, '', '', strtoupper($this->fullTableName($model)));
00224
00225 while (db2_fetch_row($result)) {
00226 $fields[strtolower(db2_result($result, 'COLUMN_NAME'))] = array(
00227 'type' => $this->column(strtolower(db2_result($result, 'TYPE_NAME'))),
00228 'null' => db2_result($result, 'NULLABLE'),
00229 'default' => db2_result($result, 'COLUMN_DEF'),
00230 'length' => db2_result($result, 'COLUMN_SIZE')
00231 );
00232 }
00233 $this->__cacheDescription($model->tablePrefix . $model->table, $fields);
00234 return $fields;
00235 }
00236
00237
00238
00239
00240
00241
00242 function name($data) {
00243 return $data;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 function value($data, $column = null, $safe = false) {
00255 $parent = parent::value($data, $column, $safe);
00256
00257 if ($parent != null) {
00258 return $parent;
00259 }
00260
00261 if ($data === null) {
00262 return 'NULL';
00263 }
00264
00265 if ($data === '') {
00266 return "''";
00267 }
00268
00269 switch ($column) {
00270 case 'boolean':
00271 $data = $this->boolean((bool)$data);
00272 break;
00273 case 'integer':
00274 $data = intval($data);
00275 break;
00276 default:
00277 $data = str_replace("'", "''", $data);
00278 break;
00279 }
00280
00281
00282
00283 if ($column == 'integer' || is_numeric($data)) {
00284 return $data;
00285 } else {
00286 return "'" . $data . "'";
00287 }
00288 }
00289
00290
00291
00292
00293
00294
00295
00296 function boolean($data) {
00297 if ($data === true || $data === false) {
00298 if ($data === true) {
00299 return 1;
00300 }
00301 return 0;
00302 } else {
00303 if (intval($data !== 0)) {
00304 return true;
00305 }
00306 return false;
00307 }
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317 function begin(&$model) {
00318 if (parent::begin($model)) {
00319 if (db2_autocommit($this->connection, DB2_AUTOCOMMIT_OFF)) {
00320 $this->_transactionStarted = true;
00321 return true;
00322 }
00323 }
00324 return false;
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334 function commit(&$model) {
00335 if (parent::commit($model)) {
00336 if (db2_commit($this->connection)) {
00337 $this->_transactionStarted = false;
00338 db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON);
00339 return true;
00340 }
00341 }
00342 return false;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352 function rollback(&$model) {
00353 if (parent::rollback($model)) {
00354 $this->_transactionStarted = false;
00355 db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON);
00356 return db2_rollback($this->connection);
00357 }
00358 return false;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368 function update(&$model, $fields = array(), $values = array()) {
00369 foreach ($fields as $i => $field) {
00370 if ($field == $model->primaryKey) {
00371 unset ($fields[$i]);
00372 unset ($values[$i]);
00373 break;
00374 }
00375 }
00376 return parent::update($model, $fields, $values);
00377 }
00378
00379
00380
00381
00382
00383
00384
00385 function lastError() {
00386 if (db2_stmt_error()) {
00387 return db2_stmt_error() . ': ' . db2_stmt_errormsg();
00388 } elseif (db2_conn_error()) {
00389 return db2_conn_error() . ': ' . db2_conn_errormsg();
00390 }
00391 return null;
00392 }
00393
00394
00395
00396
00397
00398
00399 function lastAffected() {
00400 if ($this->_result) {
00401 return db2_num_rows($this->_result);
00402 }
00403 return null;
00404 }
00405
00406
00407
00408
00409
00410
00411 function lastNumRows() {
00412 if ($this->_result) {
00413 return db2_num_rows($this->_result);
00414 }
00415 return null;
00416 }
00417
00418
00419
00420
00421
00422
00423 function lastInsertId($source = null) {
00424 $data = $this->fetchRow(sprintf('SELECT SYSIBM.IDENTITY_VAL_LOCAL() AS ID FROM %s FETCH FIRST ROW ONLY', $source));
00425
00426 if ($data && isset($data[0]['id'])) {
00427 return $data[0]['id'];
00428 }
00429 return null;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438 function limit($limit, $offset = null) {
00439 if ($limit) {
00440 $rt = '';
00441
00442
00443 if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) {
00444 $rt = sprintf('FETCH FIRST %d ROWS ONLY', $limit);
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 return $rt;
00467 }
00468 return null;
00469 }
00470
00471
00472
00473
00474
00475
00476 function column($real) {
00477 if (is_array($real)) {
00478 $col = $real['name'];
00479
00480 if (isset($real['limit'])) {
00481 $col .= '(' . $real['limit'] . ')';
00482 }
00483 return $col;
00484 }
00485 $col = str_replace(')', '', $real);
00486 $limit = null;
00487 if (strpos($col, '(') !== false) {
00488 list($col, $limit) = explode('(', $col);
00489 }
00490
00491 if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
00492 return $col;
00493 }
00494
00495 if ($col == 'smallint') {
00496 return 'boolean';
00497 }
00498
00499 if (strpos($col, 'char') !== false) {
00500 return 'string';
00501 }
00502
00503 if (strpos($col, 'clob') !== false) {
00504 return 'text';
00505 }
00506
00507 if (strpos($col, 'blob') !== false || $col == 'image') {
00508 return 'binary';
00509 }
00510
00511 if (in_array($col, array('double', 'real', 'decimal'))) {
00512 return 'float';
00513 }
00514 return 'text';
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 function resultSet(&$results, $sql = null) {
00542 $this->results =& $results;
00543 $this->map = $this->_resultMap[$this->results];
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 function fetchResult() {
00555 if ($row = db2_fetch_array($this->results)) {
00556 $resultRow = array();
00557 $i = 0;
00558
00559 foreach ($row as $index => $field) {
00560 $table = $this->map[$index][0];
00561 $column = strtolower($this->map[$index][1]);
00562 $resultRow[$table][$column] = $row[$index];
00563 $i++;
00564 }
00565 return $resultRow;
00566 } else {
00567 return false;
00568 }
00569 }
00570 }
00571 ?>