2
0

Oracle.php 22 KB

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