Oracle.php 22 KB

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