Skip to content

Commit

Permalink
Initial work with SQL Server connector
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jan 14, 2014
1 parent cc2fcf7 commit 52c95e5
Show file tree
Hide file tree
Showing 5 changed files with 631 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Cake/Database/Dialect/SqlserverDialectTrait.php
@@ -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);
}

}
93 changes: 93 additions & 0 deletions Cake/Database/Driver/Sqlserver.php
@@ -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());
}

}

0 comments on commit 52c95e5

Please sign in to comment.