Oracle.php 22 KB

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