Commit 3349af1a by Qiang Xue

Merge pull request #3049 from resurtm/db-connection-odbc

Add ODBC support to yii\db\Connection
parents 629ed629 1991c4cb
...@@ -44,6 +44,20 @@ return [ ...@@ -44,6 +44,20 @@ return [
]; ];
``` ```
There is a peculiarity when you want to work with the database through the `ODBC` layer. When using `ODBC`,
connection `DSN` doesn't indicate uniquely what database type is being used. That's why you have to override
`driverName` property of [[yii\db\Connection]] class to disambiguate that:
```php
'db' => [
'class' => 'yii\db\Connection',
'driverName' => 'mysql',
'dsn' => 'odbc:Driver={MySQL};Server=localhost;Database=test',
'username' => 'root',
'password' => '',
],
```
Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) for more details Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) for more details
on the format of the DSN string. on the format of the DSN string.
......
...@@ -190,6 +190,7 @@ Yii Framework 2 Change Log ...@@ -190,6 +190,7 @@ Yii Framework 2 Change Log
- Enh: Improved action filter and action execution flow by supporting installing action filters at controller, module and application levels (qiangxue) - Enh: Improved action filter and action execution flow by supporting installing action filters at controller, module and application levels (qiangxue)
- Enh: Added `isAssociative()` and `isIndexed()` to `yii\helpers\ArrayHelper` (qiangxue) - Enh: Added `isAssociative()` and `isIndexed()` to `yii\helpers\ArrayHelper` (qiangxue)
- Enh: Added `addSelect` to `yii\db\Query` (Alex-Code) - Enh: Added `addSelect` to `yii\db\Query` (Alex-Code)
- Enh: Added ODBC support in `yii\db\Connection` (nineinchnick, resurtm)
- Chg #47: Changed Markdown library to cebe/markdown and adjusted Markdown helper API (cebe) - Chg #47: Changed Markdown library to cebe/markdown and adjusted Markdown helper API (cebe)
- Chg #735: Added back `ActiveField::hiddenInput()` (qiangxue) - Chg #735: Added back `ActiveField::hiddenInput()` (qiangxue)
- Chg #1186: Changed `Sort` to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue) - Chg #1186: Changed `Sort` to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue)
......
...@@ -89,7 +89,8 @@ use yii\caching\Cache; ...@@ -89,7 +89,8 @@ use yii\caching\Cache;
* ] * ]
* ~~~ * ~~~
* *
* @property string $driverName Name of the DB driver. This property is read-only. * @property string $driverName Name of the DB driver. Change this property if you want to override the
* driver name specified in [[dsn]]. This can be useful if you're working with the database via ODBC layer.
* @property boolean $isActive Whether the DB connection is established. This property is read-only. * @property boolean $isActive Whether the DB connection is established. This property is read-only.
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the * @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the
* sequence object. This property is read-only. * sequence object. This property is read-only.
...@@ -261,6 +262,10 @@ class Connection extends Component ...@@ -261,6 +262,10 @@ class Connection extends Component
* @var Schema the database schema * @var Schema the database schema
*/ */
private $_schema; private $_schema;
/**
* @var string driver name
*/
private $_driverName;
/** /**
* Returns a value indicating whether the DB connection is established. * Returns a value indicating whether the DB connection is established.
...@@ -348,11 +353,13 @@ class Connection extends Component ...@@ -348,11 +353,13 @@ class Connection extends Component
$pdoClass = $this->pdoClass; $pdoClass = $this->pdoClass;
if ($pdoClass === null) { if ($pdoClass === null) {
$pdoClass = 'PDO'; $pdoClass = 'PDO';
if (($pos = strpos($this->dsn, ':')) !== false) { if ($this->_driverName !== null) {
$driver = $this->_driverName;
} elseif (($pos = strpos($this->dsn, ':')) !== false) {
$driver = strtolower(substr($this->dsn, 0, $pos)); $driver = strtolower(substr($this->dsn, 0, $pos));
if ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv') { }
$pdoClass = 'yii\db\mssql\PDO'; if (isset($driver) && ($driver === 'mssql' || $driver === 'dblib' || $driver === 'sqlsrv')) {
} $pdoClass = 'yii\db\mssql\PDO';
} }
} }
...@@ -533,17 +540,29 @@ class Connection extends Component ...@@ -533,17 +540,29 @@ class Connection extends Component
} }
/** /**
* Returns the name of the DB driver for the current [[dsn]]. * Returns the name of the DB driver. Based on the the current [[dsn]], in case it was not set explicitly
* by an end user.
* @return string name of the DB driver * @return string name of the DB driver
*/ */
public function getDriverName() public function getDriverName()
{ {
if (($pos = strpos($this->dsn, ':')) !== false) { if ($this->_driverName === null) {
return strtolower(substr($this->dsn, 0, $pos)); if (($pos = strpos($this->dsn, ':')) !== false) {
} else { $this->_driverName = strtolower(substr($this->dsn, 0, $pos));
$this->open(); } else {
$this->open();
return strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); $this->_driverName = strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
}
} }
return $this->_driverName;
}
/**
* Changes the current driver name.
* @param string $driverName name of the DB driver
*/
public function setDriverName($driverName)
{
$this->_driverName = strtolower($driverName);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment