Commit 1991c4cb by resurtm

Add ODBC support to yii\db\Connection

parent 055455d8
......@@ -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
on the format of the DSN string.
......
......@@ -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: Added `isAssociative()` and `isIndexed()` to `yii\helpers\ArrayHelper` (qiangxue)
- 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 #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)
......
......@@ -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 string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the
* sequence object. This property is read-only.
......@@ -261,6 +262,10 @@ class Connection extends Component
* @var Schema the database schema
*/
private $_schema;
/**
* @var string driver name
*/
private $_driverName;
/**
* Returns a value indicating whether the DB connection is established.
......@@ -348,11 +353,13 @@ class Connection extends Component
$pdoClass = $this->pdoClass;
if ($pdoClass === null) {
$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));
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
}
/**
* 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
*/
public function getDriverName()
{
if (($pos = strpos($this->dsn, ':')) !== false) {
return strtolower(substr($this->dsn, 0, $pos));
} else {
$this->open();
return strtolower($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME));
if ($this->_driverName === null) {
if (($pos = strpos($this->dsn, ':')) !== false) {
$this->_driverName = strtolower(substr($this->dsn, 0, $pos));
} else {
$this->open();
$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