Ibm
2 years ago
Abstract.php
2 years ago
Ibm.php
2 years ago
Mssql.php
2 years ago
Mysql.php
1 year ago
Oci.php
2 years ago
Pgsql.php
2 years ago
Sqlite.php
2 years ago
Oci.php
322 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Adapter |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Oci.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Adapter_Pdo_Abstract |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Adapter/Pdo/Abstract.php'; |
| 28 | /** |
| 29 | * Class for connecting to Oracle databases and performing common operations. |
| 30 | * |
| 31 | * @category Zend |
| 32 | * @package Zend_Db |
| 33 | * @subpackage Adapter |
| 34 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 35 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 36 | */ |
| 37 | class Zend_Db_Adapter_Pdo_Oci extends \Zend_Db_Adapter_Pdo_Abstract |
| 38 | { |
| 39 | /** |
| 40 | * PDO type. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $_pdoType = 'oci'; |
| 45 | /** |
| 46 | * Default class name for a DB statement. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo_Oci'; |
| 51 | /** |
| 52 | * Keys are UPPERCASE SQL datatypes or the constants |
| 53 | * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE. |
| 54 | * |
| 55 | * Values are: |
| 56 | * 0 = 32-bit integer |
| 57 | * 1 = 64-bit integer |
| 58 | * 2 = float or decimal |
| 59 | * |
| 60 | * @var array Associative array of datatypes to values 0, 1, or 2. |
| 61 | */ |
| 62 | protected $_numericDataTypes = array(\Zend_Db::INT_TYPE => \Zend_Db::INT_TYPE, \Zend_Db::BIGINT_TYPE => \Zend_Db::BIGINT_TYPE, \Zend_Db::FLOAT_TYPE => \Zend_Db::FLOAT_TYPE, 'BINARY_DOUBLE' => \Zend_Db::FLOAT_TYPE, 'BINARY_FLOAT' => \Zend_Db::FLOAT_TYPE, 'NUMBER' => \Zend_Db::FLOAT_TYPE); |
| 63 | /** |
| 64 | * Creates a PDO DSN for the adapter from $this->_config settings. |
| 65 | * |
| 66 | * @return string |
| 67 | */ |
| 68 | protected function _dsn() |
| 69 | { |
| 70 | // baseline of DSN parts |
| 71 | $dsn = $this->_config; |
| 72 | if (isset($dsn['host'])) { |
| 73 | $tns = 'dbname=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)' . '(HOST=' . $dsn['host'] . ')'; |
| 74 | if (isset($dsn['port'])) { |
| 75 | $tns .= '(PORT=' . $dsn['port'] . ')'; |
| 76 | } else { |
| 77 | $tns .= '(PORT=1521)'; |
| 78 | } |
| 79 | $tns .= '))(CONNECT_DATA=(SID=' . $dsn['dbname'] . ')))'; |
| 80 | } else { |
| 81 | $tns = 'dbname=' . $dsn['dbname']; |
| 82 | } |
| 83 | if (isset($dsn['charset'])) { |
| 84 | $tns .= ';charset=' . $dsn['charset']; |
| 85 | } |
| 86 | return $this->_pdoType . ':' . $tns; |
| 87 | } |
| 88 | /** |
| 89 | * Quote a raw string. |
| 90 | * Most PDO drivers have an implementation for the quote() method, |
| 91 | * but the Oracle OCI driver must use the same implementation as the |
| 92 | * Zend_Db_Adapter_Abstract class. |
| 93 | * |
| 94 | * @param string $value Raw string |
| 95 | * @return string Quoted string |
| 96 | */ |
| 97 | protected function _quote($value) |
| 98 | { |
| 99 | if (\is_int($value) || \is_float($value)) { |
| 100 | return $value; |
| 101 | } |
| 102 | $value = \str_replace("'", "''", $value); |
| 103 | return "'" . \addcslashes($value, "\x00\n\r\\\x1a") . "'"; |
| 104 | } |
| 105 | /** |
| 106 | * Quote a table identifier and alias. |
| 107 | * |
| 108 | * @param string|array|Zend_Db_Expr $ident The identifier or expression. |
| 109 | * @param string $alias An alias for the table. |
| 110 | * @return string The quoted identifier and alias. |
| 111 | */ |
| 112 | public function quoteTableAs($ident, $alias = null, $auto = \false) |
| 113 | { |
| 114 | // Oracle doesn't allow the 'AS' keyword between the table identifier/expression and alias. |
| 115 | return $this->_quoteIdentifierAs($ident, $alias, $auto, ' '); |
| 116 | } |
| 117 | /** |
| 118 | * Returns a list of the tables in the database. |
| 119 | * |
| 120 | * @return array |
| 121 | */ |
| 122 | public function listTables() |
| 123 | { |
| 124 | $data = $this->fetchCol('SELECT table_name FROM all_tables'); |
| 125 | return $data; |
| 126 | } |
| 127 | /** |
| 128 | * Returns the column descriptions for a table. |
| 129 | * |
| 130 | * The return value is an associative array keyed by the column name, |
| 131 | * as returned by the RDBMS. |
| 132 | * |
| 133 | * The value of each array element is an associative array |
| 134 | * with the following keys: |
| 135 | * |
| 136 | * SCHEMA_NAME => string; name of schema |
| 137 | * TABLE_NAME => string; |
| 138 | * COLUMN_NAME => string; column name |
| 139 | * COLUMN_POSITION => number; ordinal position of column in table |
| 140 | * DATA_TYPE => string; SQL datatype name of column |
| 141 | * DEFAULT => string; default expression of column, null if none |
| 142 | * NULLABLE => boolean; true if column can have nulls |
| 143 | * LENGTH => number; length of CHAR/VARCHAR |
| 144 | * SCALE => number; scale of NUMERIC/DECIMAL |
| 145 | * PRECISION => number; precision of NUMERIC/DECIMAL |
| 146 | * UNSIGNED => boolean; unsigned property of an integer type |
| 147 | * PRIMARY => boolean; true if column is part of the primary key |
| 148 | * PRIMARY_POSITION => integer; position of column in primary key |
| 149 | * IDENTITY => integer; true if column is auto-generated with unique values |
| 150 | * |
| 151 | * @todo Discover integer unsigned property. |
| 152 | * |
| 153 | * @param string $tableName |
| 154 | * @param string $schemaName OPTIONAL |
| 155 | * @return array |
| 156 | */ |
| 157 | public function describeTable($tableName, $schemaName = null) |
| 158 | { |
| 159 | $version = $this->getServerVersion(); |
| 160 | if ($version === null || \version_compare($version, '9.0.0', '>=')) { |
| 161 | $sql = "SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,\n TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,\n TC.DATA_SCALE, TC.DATA_PRECISION, C.CONSTRAINT_TYPE, CC.POSITION\n FROM ALL_TAB_COLUMNS TC\n LEFT JOIN (ALL_CONS_COLUMNS CC JOIN ALL_CONSTRAINTS C\n ON (CC.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND CC.TABLE_NAME = C.TABLE_NAME AND CC.OWNER = C.OWNER AND C.CONSTRAINT_TYPE = 'P'))\n ON TC.TABLE_NAME = CC.TABLE_NAME AND TC.COLUMN_NAME = CC.COLUMN_NAME\n WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)"; |
| 162 | $bind[':TBNAME'] = $tableName; |
| 163 | if ($schemaName) { |
| 164 | $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)'; |
| 165 | $bind[':SCNAME'] = $schemaName; |
| 166 | } |
| 167 | $sql .= ' ORDER BY TC.COLUMN_ID'; |
| 168 | } else { |
| 169 | $subSql = "SELECT AC.OWNER, AC.TABLE_NAME, ACC.COLUMN_NAME, AC.CONSTRAINT_TYPE, ACC.POSITION\n from ALL_CONSTRAINTS AC, ALL_CONS_COLUMNS ACC\n WHERE ACC.CONSTRAINT_NAME = AC.CONSTRAINT_NAME\n AND ACC.TABLE_NAME = AC.TABLE_NAME\n AND ACC.OWNER = AC.OWNER\n AND AC.CONSTRAINT_TYPE = 'P'\n AND UPPER(AC.TABLE_NAME) = UPPER(:TBNAME)"; |
| 170 | $bind[':TBNAME'] = $tableName; |
| 171 | if ($schemaName) { |
| 172 | $subSql .= ' AND UPPER(ACC.OWNER) = UPPER(:SCNAME)'; |
| 173 | $bind[':SCNAME'] = $schemaName; |
| 174 | } |
| 175 | $sql = "SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,\n TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,\n TC.DATA_SCALE, TC.DATA_PRECISION, CC.CONSTRAINT_TYPE, CC.POSITION\n FROM ALL_TAB_COLUMNS TC, ({$subSql}) CC\n WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)\n AND TC.OWNER = CC.OWNER(+) AND TC.TABLE_NAME = CC.TABLE_NAME(+) AND TC.COLUMN_NAME = CC.COLUMN_NAME(+)"; |
| 176 | if ($schemaName) { |
| 177 | $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)'; |
| 178 | } |
| 179 | $sql .= ' ORDER BY TC.COLUMN_ID'; |
| 180 | } |
| 181 | $stmt = $this->query($sql, $bind); |
| 182 | /** |
| 183 | * Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection |
| 184 | */ |
| 185 | $result = $stmt->fetchAll(\Zend_Db::FETCH_NUM); |
| 186 | $table_name = 0; |
| 187 | $owner = 1; |
| 188 | $column_name = 2; |
| 189 | $data_type = 3; |
| 190 | $data_default = 4; |
| 191 | $nullable = 5; |
| 192 | $column_id = 6; |
| 193 | $data_length = 7; |
| 194 | $data_scale = 8; |
| 195 | $data_precision = 9; |
| 196 | $constraint_type = 10; |
| 197 | $position = 11; |
| 198 | $desc = array(); |
| 199 | foreach ($result as $key => $row) { |
| 200 | list($primary, $primaryPosition, $identity) = array(\false, null, \false); |
| 201 | if ($row[$constraint_type] == 'P') { |
| 202 | $primary = \true; |
| 203 | $primaryPosition = $row[$position]; |
| 204 | /** |
| 205 | * Oracle does not support auto-increment keys. |
| 206 | */ |
| 207 | $identity = \false; |
| 208 | } |
| 209 | $desc[$this->foldCase($row[$column_name])] = array( |
| 210 | 'SCHEMA_NAME' => $this->foldCase($row[$owner]), |
| 211 | 'TABLE_NAME' => $this->foldCase($row[$table_name]), |
| 212 | 'COLUMN_NAME' => $this->foldCase($row[$column_name]), |
| 213 | 'COLUMN_POSITION' => $row[$column_id], |
| 214 | 'DATA_TYPE' => $row[$data_type], |
| 215 | 'DEFAULT' => $row[$data_default], |
| 216 | 'NULLABLE' => (bool) ($row[$nullable] == 'Y'), |
| 217 | 'LENGTH' => $row[$data_length], |
| 218 | 'SCALE' => $row[$data_scale], |
| 219 | 'PRECISION' => $row[$data_precision], |
| 220 | 'UNSIGNED' => null, |
| 221 | // @todo |
| 222 | 'PRIMARY' => $primary, |
| 223 | 'PRIMARY_POSITION' => $primaryPosition, |
| 224 | 'IDENTITY' => $identity, |
| 225 | ); |
| 226 | } |
| 227 | return $desc; |
| 228 | } |
| 229 | /** |
| 230 | * Return the most recent value from the specified sequence in the database. |
| 231 | * This is supported only on RDBMS brands that support sequences |
| 232 | * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null. |
| 233 | * |
| 234 | * @param string $sequenceName |
| 235 | * @return integer |
| 236 | */ |
| 237 | public function lastSequenceId($sequenceName) |
| 238 | { |
| 239 | $this->_connect(); |
| 240 | $value = $this->fetchOne('SELECT ' . $this->quoteIdentifier($sequenceName, \true) . '.CURRVAL FROM dual'); |
| 241 | return $value; |
| 242 | } |
| 243 | /** |
| 244 | * Generate a new value from the specified sequence in the database, and return it. |
| 245 | * This is supported only on RDBMS brands that support sequences |
| 246 | * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null. |
| 247 | * |
| 248 | * @param string $sequenceName |
| 249 | * @return integer |
| 250 | */ |
| 251 | public function nextSequenceId($sequenceName) |
| 252 | { |
| 253 | $this->_connect(); |
| 254 | $value = $this->fetchOne('SELECT ' . $this->quoteIdentifier($sequenceName, \true) . '.NEXTVAL FROM dual'); |
| 255 | return $value; |
| 256 | } |
| 257 | /** |
| 258 | * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column. |
| 259 | * |
| 260 | * As a convention, on RDBMS brands that support sequences |
| 261 | * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence |
| 262 | * from the arguments and returns the last id generated by that sequence. |
| 263 | * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method |
| 264 | * returns the last value generated for such a column, and the table name |
| 265 | * argument is disregarded. |
| 266 | * |
| 267 | * Oracle does not support IDENTITY columns, so if the sequence is not |
| 268 | * specified, this method returns null. |
| 269 | * |
| 270 | * @param string $tableName OPTIONAL Name of table. |
| 271 | * @param string $primaryKey OPTIONAL Name of primary key column. |
| 272 | * @return string |
| 273 | * @throws Zend_Db_Adapter_Oracle_Exception |
| 274 | */ |
| 275 | public function lastInsertId($tableName = null, $primaryKey = null) |
| 276 | { |
| 277 | if ($tableName !== null) { |
| 278 | $sequenceName = $tableName; |
| 279 | if ($primaryKey) { |
| 280 | $sequenceName .= $this->foldCase("_{$primaryKey}"); |
| 281 | } |
| 282 | $sequenceName .= $this->foldCase('_seq'); |
| 283 | return $this->lastSequenceId($sequenceName); |
| 284 | } |
| 285 | // No support for IDENTITY columns; return null |
| 286 | return null; |
| 287 | } |
| 288 | /** |
| 289 | * Adds an adapter-specific LIMIT clause to the SELECT statement. |
| 290 | * |
| 291 | * @param string $sql |
| 292 | * @param integer $count |
| 293 | * @param integer $offset |
| 294 | * @throws Zend_Db_Adapter_Exception |
| 295 | * @return string |
| 296 | */ |
| 297 | public function limit($sql, $count, $offset = 0) |
| 298 | { |
| 299 | $count = \intval($count); |
| 300 | if ($count <= 0) { |
| 301 | /** @see Zend_Db_Adapter_Exception */ |
| 302 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 303 | throw new \Zend_Db_Adapter_Exception("LIMIT argument count={$count} is not valid"); |
| 304 | } |
| 305 | $offset = \intval($offset); |
| 306 | if ($offset < 0) { |
| 307 | /** @see Zend_Db_Adapter_Exception */ |
| 308 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 309 | throw new \Zend_Db_Adapter_Exception("LIMIT argument offset={$offset} is not valid"); |
| 310 | } |
| 311 | /** |
| 312 | * Oracle does not implement the LIMIT clause as some RDBMS do. |
| 313 | * We have to simulate it with subqueries and ROWNUM. |
| 314 | * Unfortunately because we use the column wildcard "*", |
| 315 | * this puts an extra column into the query result set. |
| 316 | */ |
| 317 | $limit_sql = "SELECT z2.*\n FROM (\n SELECT z1.*, ROWNUM AS \"zend_db_rownum\"\n FROM (\n " . $sql . "\n ) z1\n ) z2\n WHERE z2.\"zend_db_rownum\" BETWEEN " . ($offset + 1) . " AND " . ($offset + $count); |
| 318 | return $limit_sql; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 |