Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1121 lines (1061 sloc) 30.332 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
<?php
/* SVN FILE: $Id$ */
/**
* Oracle layer for DBO.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model.datasources.dbo
* @since CakePHP v 1.2.0.4041
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Short description for class.
*
* Long description for class
*
* @package cake
* @subpackage cake.cake.libs.model.datasources.dbo
*/
class DboOracle extends DboSource {
/**
* Enter description here...
*
* @var unknown_type
* @access public
*/
var $config = array();
/**
* Enter description here...
*
* @var unknown_type
*/
var $alias = '';
/**
* Sequence names as introspected from the database
*/
var $_sequences = array();
/**
* Transaction in progress flag
*
* @var boolean
*/
var $__transactionStarted = false;
/**
* Enter description here...
*
* @var unknown_type
* @access public
*/
var $columns = array(
'primary_key' => array('name' => ''),
'string' => array('name' => 'varchar2', 'limit' => '255'),
'text' => array('name' => 'varchar2'),
'integer' => array('name' => 'number'),
'float' => array('name' => 'float'),
'datetime' => array('name' => 'date', 'format' => 'Y-m-d H:i:s'),
'timestamp' => array('name' => 'date', 'format' => 'Y-m-d H:i:s'),
'time' => array('name' => 'date', 'format' => 'Y-m-d H:i:s'),
'date' => array('name' => 'date', 'format' => 'Y-m-d H:i:s'),
'binary' => array('name' => 'bytea'),
'boolean' => array('name' => 'boolean'),
'number' => array('name' => 'number'),
'inet' => array('name' => 'inet'));
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $connection;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_limit = -1;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_offset = 0;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_map;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_currentRow;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_numRows;
/**
* Enter description here...
*
* @var unknown_type
* @access protected
*/
var $_results;
/**
* Last error issued by oci extension
*
* @var unknown_type
*/
var $_error;
/**
* Base configuration settings for MySQL driver
*
* @var array
*/
var $_baseConfig = array(
'persistent' => true,
'host' => 'localhost',
'login' => 'system',
'password' => '',
'database' => 'cake',
'nls_sort' => '',
'nls_sort' => ''
);
/**
* Table-sequence map
*
* @var unknown_type
*/
var $_sequenceMap = array();
/**
* Connects to the database using options in the given configuration array.
*
* @return boolean True if the database could be connected, else false
* @access public
*/
function connect() {
$config = $this->config;
$this->connected = false;
$config['charset'] = !empty($config['charset']) ? $config['charset'] : null;

if ($this->config['persistent']) {
$connect = 'ociplogon';
} else {
$connect = 'ocilogon';
}
$this->connection = @$connect($config['login'], $config['password'], $config['database'], $config['charset']);

if ($this->connection) {
$this->connected = true;
if (!empty($config['nls_sort'])) {
$this->execute('ALTER SESSION SET NLS_SORT='.$config['nls_sort']);
}

if (!empty($config['nls_comp'])) {
$this->execute('ALTER SESSION SET NLS_COMP='.$config['nls_comp']);
}
$this->execute("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
} else {
$this->connected = false;
$this->_setError();
return false;
}
return $this->connected;
}
/**
* Keeps track of the most recent Oracle error
*
*/
function _setError($source = null, $clear = false) {
if ($source) {
$e = ocierror($source);
} else {
$e = ocierror();
}
$this->_error = $e['message'];
if ($clear) {
$this->_error = null;
}
}
/**
* Sets the encoding language of the session
*
* @param string $lang language constant
* @return bool
*/
function setEncoding($lang) {
if (!$this->execute('ALTER SESSION SET NLS_LANGUAGE='.$lang)) {
return false;
}
return true;
}
/**
* Gets the current encoding language
*
* @return string language constant
*/
function getEncoding() {
$sql = 'SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER=\'NLS_LANGUAGE\'';
if (!$this->execute($sql)) {
return false;
}

if (!$row = $this->fetchRow()) {
return false;
}
return $row[0]['VALUE'];
}
/**
* Disconnects from database.
*
* @return boolean True if the database could be disconnected, else false
* @access public
*/
function disconnect() {
if ($this->connection) {
$this->connected = !ocilogoff($this->connection);
return !$this->connected;
}
}
/**
* Scrape the incoming SQL to create the association map. This is an extremely
* experimental method that creates the association maps since Oracle will not tell us.
*
* @param string $sql
* @return false if sql is nor a SELECT
* @access protected
*/
function _scrapeSQL($sql) {
$sql = str_replace("\"", '', $sql);
$preFrom = preg_split('/\bFROM\b/', $sql);
$preFrom = $preFrom[0];
$find = array('SELECT');
$replace = array('');
$fieldList = trim(str_replace($find, $replace, $preFrom));
$fields = preg_split('/,\s+/', $fieldList);//explode(', ', $fieldList);
$lastTableName = '';

foreach($fields as $key => $value) {
if ($value != 'COUNT(*) AS count') {
if (preg_match('/\s+(\w+(\.\w+)*)$/', $value, $matches)) {
$fields[$key] = $matches[1];

if (preg_match('/^(\w+\.)/', $value, $matches)) {
$fields[$key] = $matches[1] . $fields[$key];
$lastTableName = $matches[1];
}
}
/*
if (preg_match('/(([[:alnum:]_]+)\.[[:alnum:]_]+)(\s+AS\s+(\w+))?$/i', $value, $matches)) {
$fields[$key] = isset($matches[4]) ? $matches[2] . '.' . $matches[4] : $matches[1];
}
*/
}
}
$this->_map = array();

foreach($fields as $f) {
$e = explode('.', $f);
if (count($e) > 1) {
$table = $e[0];
$field = strtolower($e[1]);
} else {
$table = 0;
$field = $e[0];
}
$this->_map[] = array($table, $field);
}
}
/**
* Modify a SQL query to limit (and offset) the result set
*
* @param integer $limit Maximum number of rows to return
* @param integer $offset Row to begin returning
* @return modified SQL Query
* @access public
*/
function limit($limit = -1, $offset = 0) {
$this->_limit = (int) $limit;
$this->_offset = (int) $offset;
}
/**
* Returns number of rows in previous resultset. If no previous resultset exists,
* this returns false.
*
* @return integer Number of rows in resultset
* @access public
*/
function lastNumRows() {
return $this->_numRows;
}
/**
* Executes given SQL statement. This is an overloaded method.
*
* @param string $sql SQL statement
* @return resource Result resource identifier or null
* @access protected
*/
function _execute($sql) {
$this->_statementId = @ociparse($this->connection, $sql);
if (!$this->_statementId) {
$this->_setError($this->connection);
return false;
}

if ($this->__transactionStarted) {
$mode = OCI_DEFAULT;
} else {
$mode = OCI_COMMIT_ON_SUCCESS;
}

if (!@ociexecute($this->_statementId, $mode)) {
$this->_setError($this->_statementId);
return false;
}

$this->_setError(null, true);

switch(ocistatementtype($this->_statementId)) {
case 'DESCRIBE':
case 'SELECT':
$this->_scrapeSQL($sql);
break;
default:
return $this->_statementId;
break;
}

if ($this->_limit >= 1) {
ocisetprefetch($this->_statementId, $this->_limit);
} else {
ocisetprefetch($this->_statementId, 3000);
}
$this->_numRows = ocifetchstatement($this->_statementId, $this->_results, $this->_offset, $this->_limit, OCI_NUM | OCI_FETCHSTATEMENT_BY_ROW);
$this->_currentRow = 0;
$this->limit();
return $this->_statementId;
}
/**
* Enter description here...
*
* @return unknown
* @access public
*/
function fetchRow() {
if ($this->_currentRow >= $this->_numRows) {
ocifreestatement($this->_statementId);
$this->_map = null;
$this->_results = null;
$this->_currentRow = null;
$this->_numRows = null;
return false;
}
$resultRow = array();

foreach($this->_results[$this->_currentRow] as $index => $field) {
list($table, $column) = $this->_map[$index];

if (strpos($column, ' count')) {
$resultRow[0]['count'] = $field;
} else {
$resultRow[$table][$column] = $this->_results[$this->_currentRow][$index];
}
}
$this->_currentRow++;
return $resultRow;
}
/**
* Fetches the next row from the current result set
*
* @return unknown
*/
function fetchResult() {
return $this->fetchRow();
}
/**
* Checks to see if a named sequence exists
*
* @param string $sequence
* @return bool
* @access public
*/
function sequenceExists($sequence) {
$sql = "SELECT SEQUENCE_NAME FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '$sequence'";
if (!$this->execute($sql)) {
return false;
}
return $this->fetchRow();
}
/**
* Creates a database sequence
*
* @param string $sequence
* @return bool
* @access public
*/
function createSequence($sequence) {
$sql = "CREATE SEQUENCE $sequence";
return $this->execute($sql);
}
/**
* Enter description here...
*
* @param unknown_type $table
* @return unknown
* @access public
*/
function createTrigger($table) {
$sql = "CREATE OR REPLACE TRIGGER pk_$table" . "_trigger BEFORE INSERT ON $table FOR EACH ROW BEGIN SELECT pk_$table.NEXTVAL INTO :NEW.ID FROM DUAL; END;";
return $this->execute($sql);
}
/**
* Returns an array of tables in the database. If there are no tables, an error is
* raised and the application exits.
*
* @return array tablenames in the database
* @access public
*/
function listSources() {
$cache = parent::listSources();
if ($cache != null) {
return $cache;
}
$sql = 'SELECT view_name AS name FROM all_views UNION SELECT table_name AS name FROM all_tables';

if (!$this->execute($sql)) {
return false;
}
$sources = array();

while($r = $this->fetchRow()) {
$sources[] = strtolower($r[0]['name']);
}
parent::listSources($sources);
return $sources;
}
/**
* Returns an array of the fields in given table name.
*
* @param object instance of a model to inspect
* @return array Fields in table. Keys are name and type
* @access public
*/
function describe(&$model) {
$table = $model->fullTableName($model, false);

if (!empty($model->sequence)) {
$this->_sequenceMap[$table] = $model->sequence;
} elseif (!empty($model->table)) {
$this->_sequenceMap[$table] = $model->table . '_seq';
}

$cache = parent::describe($model);

if ($cache != null) {
return $cache;
}

$sql = 'SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH FROM all_tab_columns WHERE table_name = \'';
$sql .= strtoupper($this->fullTableName($model)) . '\'';

if (!$this->execute($sql)) {
return false;
}

$fields = array();

for ($i = 0; $row = $this->fetchRow(); $i++) {
$fields[strtolower($row[0]['COLUMN_NAME'])] = array(
'type'=> $this->column($row[0]['DATA_TYPE']),
'length'=> $row[0]['DATA_LENGTH']
);
}
$this->__cacheDescription($this->fullTableName($model, false), $fields);

return $fields;
}
/**
* Deletes all the records in a table and drops all associated auto-increment sequences.
* Using DELETE instead of TRUNCATE because it causes locking problems.
*
* @param mixed $table A string or model class representing the table to be truncated
* @param integer $reset If -1, sequences are dropped, if 0 (default), sequences are reset,
* and if 1, sequences are not modified
* @return boolean SQL TRUNCATE TABLE statement, false if not applicable.
* @access public
*
*/
function truncate($table, $reset = 0) {

if (empty($this->_sequences)) {
$sql = "SELECT sequence_name FROM all_sequences";
$this->execute($sql);
while ($row = $this->fetchRow()) {
$this->_sequences[] = strtolower($row[0]['sequence_name']);
}
}

$this->execute('DELETE FROM ' . $this->fullTableName($table));
if (!isset($this->_sequenceMap[$table]) || !in_array($this->_sequenceMap[$table], $this->_sequences)) {
return true;
}
if ($reset === 0) {
$this->execute("SELECT {$this->_sequenceMap[$table]}.nextval FROM dual");
$row = $this->fetchRow();
$currval = $row[$this->_sequenceMap[$table]]['nextval'];

$this->execute("SELECT min_value FROM all_sequences WHERE sequence_name = '{$this->_sequenceMap[$table]}'");
$row = $this->fetchRow();
$min_value = $row[0]['min_value'];

if ($min_value == 1) $min_value = 0;
$offset = -($currval - $min_value);

$this->execute("ALTER SEQUENCE {$this->_sequenceMap[$table]} INCREMENT BY $offset MINVALUE $min_value");
$this->execute("SELECT {$this->_sequenceMap[$table]}.nextval FROM dual");
$this->execute("ALTER SEQUENCE {$this->_sequenceMap[$table]} INCREMENT BY 1");
} else {
//$this->execute("DROP SEQUENCE {$this->_sequenceMap[$table]}");
}
return true;
}
/**
* Enables, disables, and lists table constraints
*
* Note: This method could have been written using a subselect for each table,
* however the effort Oracle expends to run the constraint introspection is very high.
* Therefore, this method caches the result once and loops through the arrays to find
* what it needs. It reduced my query time by 50%. YMMV.
*
* @param string $action
* @param string $table
* @return mixed boolean true or array of constraints
*/
function constraint($action, $table) {
if (empty($table)) {
trigger_error(__('Must specify table to operate on constraints'));
}

$table = strtoupper($table);

if (empty($this->_keyConstraints)) {
$sql = "SELECT
table_name,
c.constraint_name
FROM all_cons_columns cc
LEFT JOIN all_indexes i ON (cc.constraint_name = i.index_name)
LEFT JOIN all_constraints c ON(c.constraint_name = cc.constraint_name)";
$this->execute($sql);
while ($row = $this->fetchRow()) {
$this->_keyConstraints[] = array($row[0]['table_name'], $row['c']['constraint_name']);
}
}

$relatedKeys = array();
foreach ($this->_keyConstraints as $c) {
if ($c[0] == $table) {
$relatedKeys[] = $c[1];
}
}

if (empty($this->_constraints)) {
$sql = "SELECT
table_name,
constraint_name,
r_constraint_name
FROM
all_constraints";
$this->execute($sql);
while ($row = $this->fetchRow()) {
$this->_constraints[] = $row[0];
}
}

$constraints = array();
foreach ($this->_constraints as $c) {
if (in_array($c['r_constraint_name'], $relatedKeys)) {
$constraints[] = array($c['table_name'], $c['constraint_name']);
}
}

foreach ($constraints as $c) {
list($table, $constraint) = $c;
switch ($action) {
case 'enable':
$this->execute("ALTER TABLE $table ENABLE CONSTRAINT $constraint");
break;
case 'disable':
$this->execute("ALTER TABLE $table DISABLE CONSTRAINT $constraint");
break;
case 'list':
return $constraints;
break;
default:
trigger_error(__('DboOracle::constraint() accepts only enable, disable, or list'));
}
}
return true;
}
/**
* Returns an array of the indexes in given table name.
*
* @param string $model Name of model to inspect
* @return array Fields in table. Keys are column and unique
*/
function index($model) {
$index = array();
$table = $this->fullTableName($model, false);
if ($table) {
$indexes = $this->query('SELECT
cc.table_name,
cc.column_name,
cc.constraint_name,
c.constraint_type,
i.index_name,
i.uniqueness
FROM all_cons_columns cc
LEFT JOIN all_indexes i ON(cc.constraint_name = i.index_name)
LEFT JOIN all_constraints c ON(c.constraint_name = cc.constraint_name)
WHERE cc.table_name = \'' . strtoupper($table) .'\'');
foreach ($indexes as $i => $idx) {
if ($idx['c']['constraint_type'] == 'P') {
$key = 'PRIMARY';
} else {
continue;
}
if (!isset($index[$key])) {
$index[$key]['column'] = strtolower($idx['cc']['column_name']);
$index[$key]['unique'] = intval($idx['i']['uniqueness'] == 'UNIQUE');
} else {
if (!is_array($index[$key]['column'])) {
$col[] = $index[$key]['column'];
}
$col[] = strtolower($idx['cc']['column_name']);
$index[$key]['column'] = $col;
}
}
}
return $index;
}
/**
* Generate a Oracle Alter Table syntax for the given Schema comparison
*
* @param unknown_type $schema
* @return unknown
*/
function alterSchema($compare, $table = null) {
if (!is_array($compare)) {
return false;
}
$out = '';
$colList = array();
foreach($compare as $curTable => $types) {
if (!$table || $table == $curTable) {
$out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n";
foreach($types as $type => $column) {
switch($type) {
case 'add':
foreach($column as $field => $col) {
$col['name'] = $field;
$alter = 'ADD '.$this->buildColumn($col);
if (isset($col['after'])) {
$alter .= ' AFTER '. $this->name($col['after']);
}
$colList[] = $alter;
}
break;
case 'drop':
foreach($column as $field => $col) {
$col['name'] = $field;
$colList[] = 'DROP '.$this->name($field);
}
break;
case 'change':
foreach($column as $field => $col) {
if (!isset($col['name'])) {
$col['name'] = $field;
}
$colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col);
}
break;
}
}
$out .= "\t" . join(",\n\t", $colList) . ";\n\n";
}
}
return $out;
}
/**
* This method should quote Oracle identifiers. Well it doesn't.
* It would break all scaffolding and all of Cake's default assumptions.
*
* @param unknown_type $var
* @return unknown
* @access public
*/
function name($name) {
if (strpos($name, '.') !== false && strpos($name, '"') === false) {
list($model, $field) = explode('.', $name);
if ($field[0] == "_") {
$name = "$model.\"$field\"";
}
} else {
if ($name[0] == "_") {
$name = "\"$name\"";
}
}
return $name;
}
/**
* Begin a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions).
*/
function begin() {
$this->__transactionStarted = true;
return true;
}
/**
* Rollback a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function rollback() {
return ocirollback($this->connection);
}
/**
* Commit a transaction
*
* @param unknown_type $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
function commit() {
$this->__transactionStarted = false;
return ocicommit($this->connection);
}
/**
* Converts database-layer column types to basic types
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return string Abstract column type (i.e. "string")
* @access public
*/
function column($real) {
if (is_array($real)) {
$col = $real['name'];

if (isset($real['limit'])) {
$col .= '('.$real['limit'].')';
}
return $col;
} else {
$real = strtolower($real);
}
$col = str_replace(')', '', $real);
$limit = null;
if (strpos($col, '(') !== false) {
list($col, $limit) = explode('(', $col);
}

if (in_array($col, array('date', 'timestamp'))) {
return $col;
}
if (strpos($col, 'number') !== false) {
return 'integer';
}
if (strpos($col, 'integer') !== false) {
return 'integer';
}
if (strpos($col, 'char') !== false) {
return 'string';
}
if (strpos($col, 'text') !== false) {
return 'text';
}
if (strpos($col, 'blob') !== false) {
return 'binary';
}
if (in_array($col, array('float', 'double', 'decimal'))) {
return 'float';
}
if ($col == 'boolean') {
return $col;
}
return 'text';
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* @param string $data String to be prepared for use in an SQL statement
* @return string Quoted and escaped
* @access public
*/
function value($data, $column = null, $safe = false) {
$parent = parent::value($data, $column, $safe);

if ($parent != null) {
return $parent;
}

if ($data === null) {
return 'NULL';
}

if ($data === '') {
return "''";
}

switch($column) {
case 'date':
$data = date('Y-m-d H:i:s', strtotime($data));
$data = "TO_DATE('$data', 'YYYY-MM-DD HH24:MI:SS')";
break;
case 'integer' :
case 'float' :
case null :
if (is_numeric($data)) {
break;
}
default:
$data = str_replace("'", "''", $data);
$data = "'$data'";
break;
}
return $data;
}
/**
* Returns the ID generated from the previous INSERT operation.
*
* @param string
* @return integer
* @access public
*/
function lastInsertId($source) {
$sequence = $this->_sequenceMap[$source];
$sql = "SELECT $sequence.currval FROM dual";

if (!$this->execute($sql)) {
return false;
}

while($row = $this->fetchRow()) {
return $row[$sequence]['currval'];
}
return false;
}
/**
* Returns a formatted error message from previous database operation.
*
* @return string Error message with error number
* @access public
*/
function lastError() {
return $this->_error;
}
/**
* Returns number of affected rows in previous database operation. If no previous operation exists, this returns false.
*
* @return int Number of affected rows
* @access public
*/
function lastAffected() {
return $this->_statementId ? ocirowcount($this->_statementId): false;
}
/**
* Renders a final SQL statement by putting together the component parts in the correct order
*
* @param string $type
* @param array $data
* @return string
*/
function renderStatement($type, $data) {
extract($data);
$aliases = null;

switch (strtolower($type)) {
case 'select':
return "SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order} {$limit}";
break;
case 'create':
return "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
break;
case 'update':
if (!empty($alias)) {
$aliases = "{$this->alias}{$alias} ";
}
return "UPDATE {$table} {$aliases}SET {$fields} {$conditions}";
break;
case 'delete':
if (!empty($alias)) {
$aliases = "{$this->alias}{$alias} ";
}
return "DELETE FROM {$table} {$aliases}{$conditions}";
break;
case 'schema':
foreach (array('columns', 'indexes') as $var) {
if (is_array(${$var})) {
${$var} = "\t" . join(",\n\t", array_filter(${$var}));
}
}
if (trim($indexes) != '') {
$columns .= ',';
}
return "CREATE TABLE {$table} (\n{$columns}{$indexes})";
break;
case 'alter':
break;
}
}
/**
* Enter description here...
*
* @param Model $model
* @param unknown_type $linkModel
* @param string $type Association type
* @param unknown_type $association
* @param unknown_type $assocData
* @param unknown_type $queryData
* @param unknown_type $external
* @param unknown_type $resultSet
* @param integer $recursive Number of levels of association
* @param array $stack
*/
function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) {
if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) {
if (!isset($resultSet) || !is_array($resultSet)) {
if (Configure::read() > 0) {
echo '<div style = "font: Verdana bold 12px; color: #FF0000">' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' ';
if (isset($this->error) && $this->error != null) {
echo $this->error;
}
echo '</div>';
}
return null;
}
$count = count($resultSet);

if ($type === 'hasMany' && (!isset($assocData['limit']) || empty($assocData['limit']))) {
$ins = $fetch = array();
for ($i = 0; $i < $count; $i++) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
$ins[] = $in;
}
}

if (!empty($ins)) {
$fetch = array();
$ins = array_chunk($ins, 1000);
foreach ($ins as $i) {
$q = str_replace('{$__cakeID__$}', join(', ', $i), $query);
$q = str_replace('= (', 'IN (', $q);
$res = $this->fetchAll($q, $model->cacheQueries, $model->alias);
$fetch = array_merge($fetch, $res);
}
}

if (!empty($fetch) && is_array($fetch)) {
if ($recursive > 0) {

foreach ($linkModel->__associations as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
$deepModel =& $linkModel->{$assoc1};
$tmpStack = $stack;
$tmpStack[] = $assoc1;

if ($linkModel->useDbConfig === $deepModel->useDbConfig) {
$db =& $this;
} else {
$db =& ConnectionManager::getDataSource($deepModel->useDbConfig);
}
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
}
}
}
}
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
for ($i = 0; $i < $count; $i++) {
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
$ins[] = $in;
}
}

$foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey'];
$joinKeys = array($foreignKey, $model->hasAndBelongsToMany[$association]['associationForeignKey']);
list($with, $habtmFields) = $model->joinModel($model->hasAndBelongsToMany[$association]['with'], $joinKeys);
$habtmFieldsCount = count($habtmFields);

if (!empty($ins)) {
$fetch = array();
$ins = array_chunk($ins, 1000);
foreach ($ins as $i) {
$q = str_replace('{$__cakeID__$}', '(' .join(', ', $i) .')', $query);
$q = str_replace('= (', 'IN (', $q);
$q = str_replace(' WHERE 1 = 1', '', $q);


$q = $this->insertQueryData($q, null, $association, $assocData, $model, $linkModel, $stack);
if ($q != false) {
$res = $this->fetchAll($q, $model->cacheQueries, $model->alias);
$fetch = array_merge($fetch, $res);
}
}
}
}

for ($i = 0; $i < $count; $i++) {
$row =& $resultSet[$i];

if ($type !== 'hasAndBelongsToMany') {
$q = $this->insertQueryData($query, $resultSet[$i], $association, $assocData, $model, $linkModel, $stack);
if ($q != false) {
$fetch = $this->fetchAll($q, $model->cacheQueries, $model->alias);
} else {
$fetch = null;
}
}

if (!empty($fetch) && is_array($fetch)) {
if ($recursive > 0) {

foreach ($linkModel->__associations as $type1) {
foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {

$deepModel =& $linkModel->{$assoc1};
if (($type1 === 'belongsTo') || ($deepModel->alias === $model->alias && $type === 'belongsTo') || ($deepModel->alias != $model->alias)) {
$tmpStack = $stack;
$tmpStack[] = $assoc1;
if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
$db =& $this;
} else {
$db =& ConnectionManager::getDataSource($deepModel->useDbConfig);
}
$db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
}
}
}
}
if ($type == 'hasAndBelongsToMany') {
$merge = array();
foreach($fetch as $j => $data) {
if (isset($data[$with]) && $data[$with][$foreignKey] === $row[$model->alias][$model->primaryKey]) {
if ($habtmFieldsCount > 2) {
$merge[] = $data;
} else {
$merge[] = Set::diff($data, array($with => $data[$with]));
}
}
}
if (empty($merge) && !isset($row[$association])) {
$row[$association] = $merge;
} else {
$this->__mergeAssociation($resultSet[$i], $merge, $association, $type);
}
} else {
$this->__mergeAssociation($resultSet[$i], $fetch, $association, $type);
}
$resultSet[$i][$association] = $linkModel->afterfind($resultSet[$i][$association]);

} else {
$tempArray[0][$association] = false;
$this->__mergeAssociation($resultSet[$i], $tempArray, $association, $type);
}
}
}
}
/**
* Generate a "drop table" statement for the given Schema object
*
* @param object $schema An instance of a subclass of CakeSchema
* @param string $table Optional. If specified only the table name given will be generated.
* Otherwise, all tables defined in the schema are generated.
* @return string
*/
function dropSchema($schema, $table = null) {
if (!is_a($schema, 'CakeSchema')) {
trigger_error(__('Invalid schema object', true), E_USER_WARNING);
return null;
}
$out = '';

foreach ($schema->tables as $curTable => $columns) {
if (!$table || $table == $curTable) {
$out .= 'DROP TABLE ' . $this->fullTableName($curTable) . "\n";
}
}
return $out;
}
}
?>
Something went wrong with that request. Please try again.