Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work with SQL Server connector
- Loading branch information
Showing
5 changed files
with
631 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Database\Dialect; | ||
|
||
use Cake\Database\SqlDialectTrait; | ||
|
||
/** | ||
* Contains functions that encapsulates the SQL dialect used by MySQL, | ||
* including query translators and schema introspection. | ||
*/ | ||
trait SqlserverDialectTrait { | ||
|
||
use SqlDialectTrait; | ||
|
||
/** | ||
* String used to start a database identifier quoting to make it safe | ||
* | ||
* @var string | ||
*/ | ||
protected $_startQuote = '['; | ||
|
||
/** | ||
* String used to end a database identifier quoting to make it safe | ||
* | ||
* @var string | ||
*/ | ||
protected $_endQuote = ']'; | ||
|
||
/** | ||
* Returns an dictionary of expressions to be transformed when compiling a Query | ||
* to SQL. Array keys are method names to be called in this class | ||
* | ||
* @return array | ||
*/ | ||
protected function _expressionTranslators() { | ||
$namespace = 'Cake\Database\Expression'; | ||
return [ | ||
$namespace . '\FunctionExpression' => '_transformFunctionExpression' | ||
]; | ||
} | ||
|
||
/** | ||
* Receives a FunctionExpression and changes it so that it conforms to this | ||
* SQL dialect. | ||
* | ||
* @param Cake\Database\Expression\FunctionExpression | ||
* @return void | ||
*/ | ||
protected function _transformFunctionExpression(FunctionExpression $expression) { | ||
switch ($expression->name()) { | ||
case 'CONCAT': | ||
// CONCAT function is expressed as exp1 + exp2 | ||
$expression->name('')->type(' +'); | ||
break; | ||
/* | ||
@todo Implement prepend method on FunctionExpression | ||
case 'DATEDIFF': | ||
$expression | ||
->name('') | ||
->type('-') | ||
->iterateParts(function($p) { | ||
return new FunctionExpression('DATE', [$p['value']], [$p['type']]); | ||
}); | ||
break; | ||
*/ | ||
case 'CURRENT_DATE': | ||
$time = new FunctionExpression('GETDATE'); | ||
$expression->name('CONVERT')->add(['date' => 'literal', $time]); | ||
break; | ||
case 'CURRENT_TIME': | ||
$time = new FunctionExpression('GETDATE'); | ||
$expression->name('CONVERT')->add(['time' => 'literal', $time]); | ||
break; | ||
case 'NOW': | ||
$expression->name('GETDATE'); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Get the schema dialect. | ||
* | ||
* Used by Cake\Schema package to reflect schema and | ||
* generate schema. | ||
* | ||
* @return Cake\Database\Schema\MysqlSchema | ||
*/ | ||
public function schemaDialect() { | ||
return new \Cake\Database\Schema\SqlserverSchema($this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Database\Driver; | ||
|
||
use Cake\Database\Dialect\SqlserverDialectTrait; | ||
use PDO; | ||
|
||
class Sqlserver extends \Cake\Database\Driver { | ||
|
||
use PDODriverTrait; | ||
use SqlserverDialectTrait; | ||
|
||
/** | ||
* Base configuration settings for Sqlserver driver | ||
* | ||
* @var array | ||
*/ | ||
protected $_baseConfig = [ | ||
'persistent' => true, | ||
'host' => 'localhost\SQLEXPRESS', | ||
'login' => '', | ||
'password' => '', | ||
'database' => 'cake', | ||
'encoding' => 'utf8', | ||
'flags' => [], | ||
'init' => [], | ||
'settings' => [], | ||
'dsn' => null | ||
]; | ||
|
||
/** | ||
* Establishes a connection to the databse server | ||
* | ||
* @return boolean true on success | ||
*/ | ||
public function connect() { | ||
if ($this->_connection) { | ||
return true; | ||
} | ||
$config = $this->_config; | ||
$config['flags'] += [ | ||
PDO::ATTR_PERSISTENT => $config['persistent'], | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION | ||
]; | ||
|
||
if (!empty($config['encoding'])) { | ||
$config['flags'][PDO::SQLSRV_ATTR_ENCODING] = $config['encoding']; | ||
} | ||
if (empty($config['dsn'])) { | ||
$config['dsn'] = "sqlsrv:server={$config['host']};Database={$config['database']}"; | ||
} | ||
|
||
$this->_connect($config); | ||
|
||
$connection = $this->connection(); | ||
if (!empty($config['init'])) { | ||
foreach ((array)$config['init'] as $command) { | ||
$connection->exec($command); | ||
} | ||
} | ||
if (!empty($config['settings']) && is_array($config['settings'])) { | ||
foreach ($config['settings'] as $key => $value) { | ||
$connection->exec("SET {$key} {$value}"); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Returns whether php is able to use this driver for connecting to database | ||
* | ||
* @return boolean true if it is valid to use this driver | ||
*/ | ||
|
||
public function enabled() { | ||
return in_array('sqlsrv', PDO::getAvailableDrivers()); | ||
} | ||
|
||
} |
Oops, something went wrong.