diff --git a/README.md b/README.md index e6a9198..2b24672 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,18 @@ -# CakePHP 3 Driver for Oracle Database +# CakePHP 4 Driver for Oracle Database [![Downloads](https://poser.pugx.org/cakedc/cakephp-oracle-driver/d/total.png)](https://packagist.org/packages/cakedc/cakephp-oracle-driver) [![Latest Version](https://poser.pugx.org/cakedc/cakephp-oracle-driver/v/stable.png)](https://packagist.org/packages/cakedc/cakephp-oracle-driver) +Versions and branches +--------------------- + +| CakePHP | CakeDC Oracle Driver Plugin | Tag | Notes | +| :-------------: | :------------------------: | :--: | :---- | +| 4.0 | [5.x](https://github.com/CakeDC/cakephp-oracle-driver/tree/5.x) | 5.0.0 | stable +| ^3.8 <4.0 | [4.x](https://github.com/CakeDC/cakephp-oracle-driver/tree/4.x) | 4.0.0 | stable | +| ^3.7 | [3.x](https://github.com/CakeDC/cakephp-oracle-driver/tree/3.x) | 3.0.0 | stable | + + ## Installation You can install this plugin into your CakePHP application using @@ -23,7 +33,7 @@ In bootstrap.php load plugin with bootstrap. ## Requirements -- CakePHP 3.2+ +- CakePHP 3.8 - an Oracle PHP extension - OCI8 (PHP extension built with PHP) - PDO_OCI (PHP extension built with PHP) @@ -48,19 +58,25 @@ return [ 'className' => 'CakeDC\OracleDriver\Database\OracleConnection', 'driver' => 'CakeDC\OracleDriver\Database\Driver\OracleOCI', # For OCI8 #'driver' => 'CakeDC\\OracleDriver\\Database\\Driver\\OraclePDO', # For PDO_OCI - 'host' => 'oracle11g', # Database host name or IP address - //'port' => 'nonstandard_port', # Database port number (default: 1521) - 'username' => 'blogs', # Database username - 'password' => 'password', # Database password - 'database' => 'XE', # Database name (maps to Oracle's `SERVICE_NAME`) - 'sid' => '', # Database System ID (maps to Oracle's `SID`) - 'instance' => '', # Database instance name (maps to Oracle's `INSTANCE_NAME`) - 'pooled' => '', # Database pooling (maps to Oracle's `SERVER=POOLED`) + 'persistent' => true, // Database persistent connection between http requests + 'host' => 'oracle11g', // Database host name or IP address + //'port' => 'nonstandard_port', // Database port number (default: 1521) + 'username' => 'blogs', // Database username + 'password' => 'password', // Database password + 'database' => 'XE', // Database name (maps to Oracle's `SERVICE_NAME`) + 'sid' => '', // Database System ID (maps to Oracle's `SID`) + 'instance' => '', // Database instance name (maps to Oracle's `INSTANCE_NAME`) + 'pooled' => '', // Database pooling (maps to Oracle's `SERVER=POOLED`) + 'flags' => [], // Database low level parameters for OCI or PDO connection. Auto-generated by default + 'encoding' => '', // Database charset (default same as database charset) + 'init' => [], // Array of queries executed at connection + 'cacheMetadata' => true, // Enable cakephp schema caching ] ] ]; ``` + As you can see, the `className` and `driver` need switched to Oracle-specific classes. The driver will depend on whether you want to use `PDO` or not. The `database` name "XE" in this case, is defined as the `SERVICE_NAME` in Oracle's diff --git a/src/Database/Driver/OracleBase.php b/src/Database/Driver/OracleBase.php index 2f5100f..a4f84fc 100644 --- a/src/Database/Driver/OracleBase.php +++ b/src/Database/Driver/OracleBase.php @@ -48,10 +48,28 @@ abstract class OracleBase extends Driver 'case' => 'lower', 'timezone' => null, 'init' => [], + 'server_version' => 11, + 'autoincrement' => false, ]; protected $_defaultConfig = []; + + protected $_serverVersion = null; + + /** + * @var bool + */ + protected $_autoincrement; + + /** + * @return bool + */ + public function useAutoincrement() + { + return $this->_autoincrement; + } + /** * Establishes a connection to the database server * @@ -255,6 +273,39 @@ public function lastInsertId($table = null, $column = null) $statement = $this->_connection->query("SELECT {$sequenceName}.CURRVAL FROM DUAL"); $statement->execute(); $result = $statement->fetch(); + if (count($result) === 0) { + return $this->_autoincrementSequenceId($table, $column); + } + + return $result[0]; + } + + + /** + * Returns last insert id by autoincrement sequence. + * + * @param string $table Table name. + * @param string $column Column name + * @return int + */ + protected function _autoincrementSequenceId($table, $column) + { + if ($this->isAutoQuotingEnabled()) { + $tableName = $table; + $columnName = $column; + } else { + $tableName = strtoupper($table); + $columnName = strtoupper($column); + } + $query = "select sequence_name from user_tab_identity_cols where table_name='$tableName' and column_name='$columnName'"; + $this->connect(); + $seqStatement = $this->_connection->query($query); + $result = $seqStatement->fetch(PDO::FETCH_NUM); + + $sequenceName = $result[0]; + + $statement = $this->_connection->query("SELECT {$sequenceName}.CURRVAL FROM DUAL"); + $result = $statement->fetch(PDO::FETCH_NUM); return $result[0]; }