📄 dbo_oracle.php
字号:
<?php/* SVN FILE: $Id: dbo_oracle.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * 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. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @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: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @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;/** * 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' => 'number NOT NULL'), 'string' => array('name' => 'varchar2', 'limit' => '255'), 'text' => array('name' => 'varchar2'), 'integer' => array('name' => 'numeric'), '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' => 'numeric'), '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) { if ($source) { $e = ocierror($source); } else { $e = ocierror(); } $this->_error = $e['message']; }/** * 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; } 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; }/** * 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 user_views UNION SELECT table_name AS name FROM user_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) { if (!empty($model->sequence)) { $this->_sequenceMap[$model->table] = $model->sequence; } elseif (!empty($model->table)) { $this->_sequenceMap[$model->table] = $model->table . '_seq'; } else { trigger_error(__('Missing table name')); } $cache = parent::describe($model); if ($cache != null) { return $cache; } $sql = 'SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH FROM user_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 user_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 user_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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -