Oracle.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Abstract.php';
  26. /**
  27. * @see Zend_Db_Statement_Oracle
  28. */
  29. require_once 'Zend/Db/Statement/Oracle.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Db
  33. * @subpackage Adapter
  34. * @copyright Copyright (c) 2005-2009 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_Oracle extends Zend_Db_Adapter_Abstract
  38. {
  39. /**
  40. * User-provided configuration.
  41. *
  42. * Basic keys are:
  43. *
  44. * username => (string) Connect to the database as this username.
  45. * password => (string) Password associated with the username.
  46. * dbname => Either the name of the local Oracle instance, or the
  47. * name of the entry in tnsnames.ora to which you want to connect.
  48. * persistent => (boolean) Set TRUE to use a persistent connection
  49. * @var array
  50. */
  51. protected $_config = array(
  52. 'dbname' => null,
  53. 'username' => null,
  54. 'password' => null,
  55. 'persistent' => false
  56. );
  57. /**
  58. * Keys are UPPERCASE SQL datatypes or the constants
  59. * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
  60. *
  61. * Values are:
  62. * 0 = 32-bit integer
  63. * 1 = 64-bit integer
  64. * 2 = float or decimal
  65. *
  66. * @var array Associative array of datatypes to values 0, 1, or 2.
  67. */
  68. protected $_numericDataTypes = array(
  69. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  70. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  71. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  72. 'BINARY_DOUBLE' => Zend_Db::FLOAT_TYPE,
  73. 'BINARY_FLOAT' => Zend_Db::FLOAT_TYPE,
  74. 'NUMBER' => Zend_Db::FLOAT_TYPE,
  75. );
  76. /**
  77. * @var integer
  78. */
  79. protected $_execute_mode = OCI_COMMIT_ON_SUCCESS;
  80. /**
  81. * Default class name for a DB statement.
  82. *
  83. * @var string
  84. */
  85. protected $_defaultStmtClass = 'Zend_Db_Statement_Oracle';
  86. /**
  87. * Check if LOB field are returned as string
  88. * instead of OCI-Lob object
  89. *
  90. * @var boolean
  91. */
  92. protected $_lobAsString = null;
  93. /**
  94. * Creates a connection resource.
  95. *
  96. * @return void
  97. * @throws Zend_Db_Adapter_Oracle_Exception
  98. */
  99. protected function _connect()
  100. {
  101. if (is_resource($this->_connection)) {
  102. // connection already exists
  103. return;
  104. }
  105. if (!extension_loaded('oci8')) {
  106. /**
  107. * @see Zend_Db_Adapter_Oracle_Exception
  108. */
  109. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  110. throw new Zend_Db_Adapter_Oracle_Exception('The OCI8 extension is required for this adapter but the extension is not loaded');
  111. }
  112. $connectionFuncName = ($this->_config['persistent'] == true) ? 'oci_pconnect' : 'oci_connect';
  113. $this->_connection = @$connectionFuncName(
  114. $this->_config['username'],
  115. $this->_config['password'],
  116. $this->_config['dbname'],
  117. $this->_config['charset']);
  118. // check the connection
  119. if (!$this->_connection) {
  120. /**
  121. * @see Zend_Db_Adapter_Oracle_Exception
  122. */
  123. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  124. throw new Zend_Db_Adapter_Oracle_Exception(oci_error());
  125. }
  126. }
  127. /**
  128. * Test if a connection is active
  129. *
  130. * @return boolean
  131. */
  132. public function isConnected()
  133. {
  134. return ((bool) (is_resource($this->_connection)
  135. && get_resource_type($this->_connection) == 'oci8 connection'));
  136. }
  137. /**
  138. * Force the connection to close.
  139. *
  140. * @return void
  141. */
  142. public function closeConnection()
  143. {
  144. if ($this->isConnected()) {
  145. oci_close($this->_connection);
  146. }
  147. $this->_connection = null;
  148. }
  149. /**
  150. * Activate/deactivate return of LOB as string
  151. *
  152. * @param string $lob_as_string
  153. * @return Zend_Db_Adapter_Oracle
  154. */
  155. public function setLobAsString($lobAsString)
  156. {
  157. $this->_lobAsString = (bool) $lobAsString;
  158. return $this;
  159. }
  160. /**
  161. * Return whether or not LOB are returned as string
  162. *
  163. * @return boolean
  164. */
  165. public function getLobAsString()
  166. {
  167. if ($this->_lobAsString === null) {
  168. // if never set by user, we use driver option if it exists otherwise false
  169. if (isset($this->_config['driver_options']) &&
  170. isset($this->_config['driver_options']['lob_as_string'])) {
  171. $this->_lobAsString = (bool) $this->_config['driver_options']['lob_as_string'];
  172. } else {
  173. $this->_lobAsString = false;
  174. }
  175. }
  176. return $this->_lobAsString;
  177. }
  178. /**
  179. * Returns an SQL statement for preparation.
  180. *
  181. * @param string $sql The SQL statement with placeholders.
  182. * @return Zend_Db_Statement_Oracle
  183. */
  184. public function prepare($sql)
  185. {
  186. $this->_connect();
  187. $stmtClass = $this->_defaultStmtClass;
  188. if (!class_exists($stmtClass)) {
  189. require_once 'Zend/Loader.php';
  190. Zend_Loader::loadClass($stmtClass);
  191. }
  192. $stmt = new $stmtClass($this, $sql);
  193. if ($stmt instanceof Zend_Db_Statement_Oracle) {
  194. $stmt->setLobAsString($this->getLobAsString());
  195. }
  196. $stmt->setFetchMode($this->_fetchMode);
  197. return $stmt;
  198. }
  199. /**
  200. * Quote a raw string.
  201. *
  202. * @param string $value Raw string
  203. * @return string Quoted string
  204. */
  205. protected function _quote($value)
  206. {
  207. if (is_int($value) || is_float($value)) {
  208. return $value;
  209. }
  210. $value = str_replace("'", "''", $value);
  211. return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
  212. }
  213. /**
  214. * Quote a table identifier and alias.
  215. *
  216. * @param string|array|Zend_Db_Expr $ident The identifier or expression.
  217. * @param string $alias An alias for the table.
  218. * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
  219. * @return string The quoted identifier and alias.
  220. */
  221. public function quoteTableAs($ident, $alias = null, $auto = false)
  222. {
  223. // Oracle doesn't allow the 'AS' keyword between the table identifier/expression and alias.
  224. return $this->_quoteIdentifierAs($ident, $alias, $auto, ' ');
  225. }
  226. /**
  227. * Return the most recent value from the specified sequence in the database.
  228. * This is supported only on RDBMS brands that support sequences
  229. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  230. *
  231. * @param string $sequenceName
  232. * @return string
  233. */
  234. public function lastSequenceId($sequenceName)
  235. {
  236. $this->_connect();
  237. $sql = 'SELECT '.$this->quoteIdentifier($sequenceName, true).'.CURRVAL FROM dual';
  238. $value = $this->fetchOne($sql);
  239. return $value;
  240. }
  241. /**
  242. * Generate a new value from the specified sequence in the database, and return it.
  243. * This is supported only on RDBMS brands that support sequences
  244. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  245. *
  246. * @param string $sequenceName
  247. * @return string
  248. */
  249. public function nextSequenceId($sequenceName)
  250. {
  251. $this->_connect();
  252. $sql = 'SELECT '.$this->quoteIdentifier($sequenceName, true).'.NEXTVAL FROM dual';
  253. $value = $this->fetchOne($sql);
  254. return $value;
  255. }
  256. /**
  257. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  258. *
  259. * As a convention, on RDBMS brands that support sequences
  260. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  261. * from the arguments and returns the last id generated by that sequence.
  262. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  263. * returns the last value generated for such a column, and the table name
  264. * argument is disregarded.
  265. *
  266. * Oracle does not support IDENTITY columns, so if the sequence is not
  267. * specified, this method returns null.
  268. *
  269. * @param string $tableName OPTIONAL Name of table.
  270. * @param string $primaryKey OPTIONAL Name of primary key column.
  271. * @return string
  272. */
  273. public function lastInsertId($tableName = null, $primaryKey = null)
  274. {
  275. if ($tableName !== null) {
  276. $sequenceName = $tableName;
  277. if ($primaryKey) {
  278. $sequenceName .= "_$primaryKey";
  279. }
  280. $sequenceName .= '_seq';
  281. return $this->lastSequenceId($sequenceName);
  282. }
  283. // No support for IDENTITY columns; return null
  284. return null;
  285. }
  286. /**
  287. * Returns a list of the tables in the database.
  288. *
  289. * @return array
  290. */
  291. public function listTables()
  292. {
  293. $this->_connect();
  294. $data = $this->fetchCol('SELECT table_name FROM all_tables');
  295. return $data;
  296. }
  297. /**
  298. * Returns the column descriptions for a table.
  299. *
  300. * The return value is an associative array keyed by the column name,
  301. * as returned by the RDBMS.
  302. *
  303. * The value of each array element is an associative array
  304. * with the following keys:
  305. *
  306. * SCHEMA_NAME => string; name of schema
  307. * TABLE_NAME => string;
  308. * COLUMN_NAME => string; column name
  309. * COLUMN_POSITION => number; ordinal position of column in table
  310. * DATA_TYPE => string; SQL datatype name of column
  311. * DEFAULT => string; default expression of column, null if none
  312. * NULLABLE => boolean; true if column can have nulls
  313. * LENGTH => number; length of CHAR/VARCHAR
  314. * SCALE => number; scale of NUMERIC/DECIMAL
  315. * PRECISION => number; precision of NUMERIC/DECIMAL
  316. * UNSIGNED => boolean; unsigned property of an integer type
  317. * PRIMARY => boolean; true if column is part of the primary key
  318. * PRIMARY_POSITION => integer; position of column in primary key
  319. * IDENTITY => integer; true if column is auto-generated with unique values
  320. *
  321. * @todo Discover integer unsigned property.
  322. *
  323. * @param string $tableName
  324. * @param string $schemaName OPTIONAL
  325. * @return array
  326. */
  327. public function describeTable($tableName, $schemaName = null)
  328. {
  329. $version = $this->getServerVersion();
  330. if (($version === null) || version_compare($version, '9.0.0', '>=')) {
  331. $sql = "SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,
  332. TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,
  333. TC.DATA_SCALE, TC.DATA_PRECISION, C.CONSTRAINT_TYPE, CC.POSITION
  334. FROM ALL_TAB_COLUMNS TC
  335. LEFT JOIN (ALL_CONS_COLUMNS CC JOIN ALL_CONSTRAINTS C
  336. ON (CC.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND CC.TABLE_NAME = C.TABLE_NAME AND C.CONSTRAINT_TYPE = 'P'))
  337. ON TC.TABLE_NAME = CC.TABLE_NAME AND TC.COLUMN_NAME = CC.COLUMN_NAME
  338. WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)";
  339. $bind[':TBNAME'] = $tableName;
  340. if ($schemaName) {
  341. $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)';
  342. $bind[':SCNAME'] = $schemaName;
  343. }
  344. $sql .= ' ORDER BY TC.COLUMN_ID';
  345. } else {
  346. $subSql="SELECT AC.OWNER, AC.TABLE_NAME, ACC.COLUMN_NAME, AC.CONSTRAINT_TYPE, ACC.POSITION
  347. from ALL_CONSTRAINTS AC, ALL_CONS_COLUMNS ACC
  348. WHERE ACC.CONSTRAINT_NAME = AC.CONSTRAINT_NAME
  349. AND ACC.TABLE_NAME = AC.TABLE_NAME
  350. AND ACC.OWNER = AC.OWNER
  351. AND AC.CONSTRAINT_TYPE = 'P'
  352. AND UPPER(AC.TABLE_NAME) = UPPER(:TBNAME)";
  353. $bind[':TBNAME'] = $tableName;
  354. if ($schemaName) {
  355. $subSql .= ' AND UPPER(ACC.OWNER) = UPPER(:SCNAME)';
  356. $bind[':SCNAME'] = $schemaName;
  357. }
  358. $sql="SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,
  359. TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,
  360. TC.DATA_SCALE, TC.DATA_PRECISION, CC.CONSTRAINT_TYPE, CC.POSITION
  361. FROM ALL_TAB_COLUMNS TC, ($subSql) CC
  362. WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)
  363. AND TC.OWNER = CC.OWNER(+) AND TC.TABLE_NAME = CC.TABLE_NAME(+) AND TC.COLUMN_NAME = CC.COLUMN_NAME(+)";
  364. if ($schemaName) {
  365. $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)';
  366. }
  367. $sql .= ' ORDER BY TC.COLUMN_ID';
  368. }
  369. $stmt = $this->query($sql, $bind);
  370. /**
  371. * Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
  372. */
  373. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  374. $table_name = 0;
  375. $owner = 1;
  376. $column_name = 2;
  377. $data_type = 3;
  378. $data_default = 4;
  379. $nullable = 5;
  380. $column_id = 6;
  381. $data_length = 7;
  382. $data_scale = 8;
  383. $data_precision = 9;
  384. $constraint_type = 10;
  385. $position = 11;
  386. $desc = array();
  387. foreach ($result as $key => $row) {
  388. list ($primary, $primaryPosition, $identity) = array(false, null, false);
  389. if ($row[$constraint_type] == 'P') {
  390. $primary = true;
  391. $primaryPosition = $row[$position];
  392. /**
  393. * Oracle does not support auto-increment keys.
  394. */
  395. $identity = false;
  396. }
  397. $desc[$this->foldCase($row[$column_name])] = array(
  398. 'SCHEMA_NAME' => $this->foldCase($row[$owner]),
  399. 'TABLE_NAME' => $this->foldCase($row[$table_name]),
  400. 'COLUMN_NAME' => $this->foldCase($row[$column_name]),
  401. 'COLUMN_POSITION' => $row[$column_id],
  402. 'DATA_TYPE' => $row[$data_type],
  403. 'DEFAULT' => $row[$data_default],
  404. 'NULLABLE' => (bool) ($row[$nullable] == 'Y'),
  405. 'LENGTH' => $row[$data_length],
  406. 'SCALE' => $row[$data_scale],
  407. 'PRECISION' => $row[$data_precision],
  408. 'UNSIGNED' => null, // @todo
  409. 'PRIMARY' => $primary,
  410. 'PRIMARY_POSITION' => $primaryPosition,
  411. 'IDENTITY' => $identity
  412. );
  413. }
  414. return $desc;
  415. }
  416. /**
  417. * Leave autocommit mode and begin a transaction.
  418. *
  419. * @return void
  420. */
  421. protected function _beginTransaction()
  422. {
  423. $this->_setExecuteMode(OCI_DEFAULT);
  424. }
  425. /**
  426. * Commit a transaction and return to autocommit mode.
  427. *
  428. * @return void
  429. * @throws Zend_Db_Adapter_Oracle_Exception
  430. */
  431. protected function _commit()
  432. {
  433. if (!oci_commit($this->_connection)) {
  434. /**
  435. * @see Zend_Db_Adapter_Oracle_Exception
  436. */
  437. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  438. throw new Zend_Db_Adapter_Oracle_Exception(oci_error($this->_connection));
  439. }
  440. $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
  441. }
  442. /**
  443. * Roll back a transaction and return to autocommit mode.
  444. *
  445. * @return void
  446. * @throws Zend_Db_Adapter_Oracle_Exception
  447. */
  448. protected function _rollBack()
  449. {
  450. if (!oci_rollback($this->_connection)) {
  451. /**
  452. * @see Zend_Db_Adapter_Oracle_Exception
  453. */
  454. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  455. throw new Zend_Db_Adapter_Oracle_Exception(oci_error($this->_connection));
  456. }
  457. $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
  458. }
  459. /**
  460. * Set the fetch mode.
  461. *
  462. * @todo Support FETCH_CLASS and FETCH_INTO.
  463. *
  464. * @param integer $mode A fetch mode.
  465. * @return void
  466. * @throws Zend_Db_Adapter_Oracle_Exception
  467. */
  468. public function setFetchMode($mode)
  469. {
  470. switch ($mode) {
  471. case Zend_Db::FETCH_NUM: // seq array
  472. case Zend_Db::FETCH_ASSOC: // assoc array
  473. case Zend_Db::FETCH_BOTH: // seq+assoc array
  474. case Zend_Db::FETCH_OBJ: // object
  475. $this->_fetchMode = $mode;
  476. break;
  477. case Zend_Db::FETCH_BOUND: // bound to PHP variable
  478. /**
  479. * @see Zend_Db_Adapter_Oracle_Exception
  480. */
  481. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  482. throw new Zend_Db_Adapter_Oracle_Exception('FETCH_BOUND is not supported yet');
  483. break;
  484. default:
  485. /**
  486. * @see Zend_Db_Adapter_Oracle_Exception
  487. */
  488. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  489. throw new Zend_Db_Adapter_Oracle_Exception("Invalid fetch mode '$mode' specified");
  490. break;
  491. }
  492. }
  493. /**
  494. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  495. *
  496. * @param string $sql
  497. * @param integer $count
  498. * @param integer $offset OPTIONAL
  499. * @return string
  500. * @throws Zend_Db_Adapter_Oracle_Exception
  501. */
  502. public function limit($sql, $count, $offset = 0)
  503. {
  504. $count = intval($count);
  505. if ($count <= 0) {
  506. /**
  507. * @see Zend_Db_Adapter_Oracle_Exception
  508. */
  509. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  510. throw new Zend_Db_Adapter_Oracle_Exception("LIMIT argument count=$count is not valid");
  511. }
  512. $offset = intval($offset);
  513. if ($offset < 0) {
  514. /**
  515. * @see Zend_Db_Adapter_Oracle_Exception
  516. */
  517. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  518. throw new Zend_Db_Adapter_Oracle_Exception("LIMIT argument offset=$offset is not valid");
  519. }
  520. /**
  521. * Oracle does not implement the LIMIT clause as some RDBMS do.
  522. * We have to simulate it with subqueries and ROWNUM.
  523. * Unfortunately because we use the column wildcard "*",
  524. * this puts an extra column into the query result set.
  525. */
  526. $limit_sql = "SELECT z2.*
  527. FROM (
  528. SELECT z1.*, ROWNUM AS \"zend_db_rownum\"
  529. FROM (
  530. " . $sql . "
  531. ) z1
  532. ) z2
  533. WHERE z2.\"zend_db_rownum\" BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
  534. return $limit_sql;
  535. }
  536. /**
  537. * @param integer $mode
  538. * @throws Zend_Db_Adapter_Oracle_Exception
  539. */
  540. private function _setExecuteMode($mode)
  541. {
  542. switch($mode) {
  543. case OCI_COMMIT_ON_SUCCESS:
  544. case OCI_DEFAULT:
  545. case OCI_DESCRIBE_ONLY:
  546. $this->_execute_mode = $mode;
  547. break;
  548. default:
  549. /**
  550. * @see Zend_Db_Adapter_Oracle_Exception
  551. */
  552. require_once 'Zend/Db/Adapter/Oracle/Exception.php';
  553. throw new Zend_Db_Adapter_Oracle_Exception("Invalid execution mode '$mode' specified");
  554. break;
  555. }
  556. }
  557. /**
  558. * @return int
  559. */
  560. public function _getExecuteMode()
  561. {
  562. return $this->_execute_mode;
  563. }
  564. /**
  565. * Inserts a table row with specified data.
  566. *
  567. * Oracle does not support anonymous ('?') binds.
  568. *
  569. * @param mixed $table The table to insert data into.
  570. * @param array $bind Column-value pairs.
  571. * @return int The number of affected rows.
  572. */
  573. public function insert($table, array $bind)
  574. {
  575. $i = 0;
  576. // extract and quote col names from the array keys
  577. $cols = array();
  578. $vals = array();
  579. foreach ($bind as $col => $val) {
  580. $cols[] = $this->quoteIdentifier($col, true);
  581. if ($val instanceof Zend_Db_Expr) {
  582. $vals[] = $val->__toString();
  583. unset($bind[$col]);
  584. } else {
  585. $vals[] = ':'.$col.$i;
  586. unset($bind[$col]);
  587. $bind[':'.$col.$i] = $val;
  588. }
  589. $i++;
  590. }
  591. // build the statement
  592. $sql = "INSERT INTO "
  593. . $this->quoteIdentifier($table, true)
  594. . ' (' . implode(', ', $cols) . ') '
  595. . 'VALUES (' . implode(', ', $vals) . ')';
  596. // execute the statement and return the number of affected rows
  597. $stmt = $this->query($sql, $bind);
  598. $result = $stmt->rowCount();
  599. return $result;
  600. }
  601. /**
  602. * Check if the adapter supports real SQL parameters.
  603. *
  604. * @param string $type 'positional' or 'named'
  605. * @return bool
  606. */
  607. public function supportsParameters($type)
  608. {
  609. switch ($type) {
  610. case 'named':
  611. return true;
  612. case 'positional':
  613. default:
  614. return false;
  615. }
  616. }
  617. /**
  618. * Retrieve server version in PHP style
  619. *
  620. * @return string
  621. */
  622. public function getServerVersion()
  623. {
  624. $this->_connect();
  625. $version = oci_server_version($this->_connection);
  626. if ($version !== false) {
  627. $matches = null;
  628. if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
  629. return $matches[1];
  630. } else {
  631. return null;
  632. }
  633. } else {
  634. return null;
  635. }
  636. }
  637. }