Compatibilité avec DB2 Express C

This commit is contained in:
alebreton 2017-06-09 08:37:00 +02:00
parent 96670e0eaa
commit ebf2348485
8 changed files with 166 additions and 108 deletions

View File

@ -17,14 +17,15 @@ class ODBCZOSConnector extends ODBCConnector
protected function getDsn(array $config)
{
$dsnParts = [
'odbc:DRIVER={IBM DB2 ODBC DRIVER}',
"odbc:DRIVER=$driverName",
'Database=%s',
'Hostname=%s',
'Port=%s',
'Protocol=TCPIP',
'Uid=%s',
'Pwd=%s',
'', // Just to add a semicolon to the end of string
'',
// Just to add a semicolon to the end of string
];
$dsnConfig = [

View File

@ -101,7 +101,7 @@ class DB2Connection extends Connection
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar());
return $this->withTablePrefix(new SchemaGrammar($this->config['driver'] == "odbc"?"i":"c"));
}
/**
@ -115,6 +115,6 @@ class DB2Connection extends Connection
return new DB2ZOSProcessor();
}
return new DB2Processor();
return new DB2Processor($this->config['driver'] == "odbc"?"i":"c");
}
}

View File

@ -6,6 +6,7 @@ use Cooperl\Database\DB2\Connectors\ODBCConnector;
use Cooperl\Database\DB2\Connectors\IBMConnector;
use Cooperl\Database\DB2\Connectors\ODBCZOSConnector;
use Illuminate\Support\ServiceProvider;
use Config;
/**
* Class DB2ServiceProvider
@ -38,21 +39,27 @@ class DB2ServiceProvider extends ServiceProvider
public function register()
{
// get the configs
$conns = is_array(config('laravel-db2::database.connections')) ? config('laravel-db2::database.connections') : [];
$conns = is_array(Config::get('laravel-db2::database.connections'))
? Config::get('laravel-db2::database.connections')
: [];
// Add my database configurations to the default set of configurations
config(['database.connections' => array_merge($conns, config('database.connections'))]);
$this->app['config']['database.connections'] = array_merge(
$conns,
$this->app['config']['database.connections']
);
// Extend the connections with pdo_odbc and pdo_ibm drivers
foreach (config('database.connections') as $conn => $config) {
foreach (Config::get('database.connections') as $conn => $config) {
// Only use configurations that feature a "odbc", "ibm" or "odbczos" driver
if (!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm', 'odbczos'])) {
if (!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm', 'odbczos', 'odbcexpress'])) {
continue;
}
// Create a connector
$this->app['db']->extend($conn, function ($config) {
switch ($config['driver']) {
case 'odbcexpress':
case 'odbc':
$connector = new ODBCConnector();

View File

@ -181,16 +181,4 @@ class DB2Grammar extends Grammar
{
return 'Y-m-d H:i:s.u';
}
/**
* Compile the random statement into SQL.
*
* @param string $seed
* @return string
*/
public function compileRandom($seed)
{
return "RAND($seed)";
}
}

View File

@ -13,6 +13,18 @@ use Cooperl\Database\DB2\Query\Grammars\DB2Grammar;
*/
class DB2Processor extends Processor
{
private $bdType;
/**
* DB2Processor constructor.
*
* @param $bdType
*/
public function __construct($bdType)
{
$this->bdType = $bdType;
}
/**
* Process the results of a "select" query.
*
@ -37,6 +49,8 @@ class DB2Processor extends Processor
return $results;
}*/
/**
* Process an "insert get ID" query.
*
@ -52,19 +66,24 @@ class DB2Processor extends Processor
$sequenceStr = $sequence ?: 'id';
if (is_array($sequence)) {
$grammar = new DB2Grammar();
$grammar = new DB2Grammar($this->bdType);
$sequenceStr = $grammar->columnize($sequence);
}
$sql = 'select '.$sequenceStr.' from new table ('.$sql;
$sql = 'select ' . $sequenceStr . ' from new table (' . $sql;
$sql .= ')';
$results = $query->getConnection()->select($sql, $values);
$results = $query->getConnection()
->select($sql, $values);
if (is_array($sequence)) {
return array_values((array) $results[0]);
} else {
$result = (array) $results[0];
if (isset($result[$sequenceStr])) {
$id = $result[$sequenceStr];
} else {
$id = $result[strtoupper($sequenceStr)];
}
return is_numeric($id) ? (int) $id : $id;
}

View File

@ -28,7 +28,7 @@ class DB2ZOSProcessor extends Processor
$sequenceStr = $sequence ?: 'id';
if (is_array($sequence)) {
$grammar = new DB2Grammar();
$grammar = new DB2Grammar("z");
$sequenceStr = $grammar->columnize($sequence);
}

View File

@ -9,6 +9,28 @@ namespace Cooperl\Database\DB2\Schema;
*/
class Blueprint extends \Illuminate\Database\Schema\Blueprint
{
public function synchro($index, $masterizable = false)
{
$this->string('id_sync', 20)
->index($index);
$this->string('hashcode', 32);
if (true === $masterizable) {
$this->boolean('data_master')
->default(true);
}
}
/**
* @param string $index
*/
public function dropSynchro($index)
{
$this->dropColumn('id_sync', 'hashcode');
$this->dropIndex($index);
}
/**
* Specify a system name for the table.
*
@ -37,11 +59,10 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* @param string $type
* @param string|array $columns
* @param string $index
* @param string|null $algorithm
*
* @return \Illuminate\Support\Fluent
*/
protected function indexCommand($type, $columns, $index, $algorithm = null)
protected function indexCommand($type, $columns, $index, $algorithm = NULL)
{
$columns = (array) $columns;
@ -50,7 +71,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$indexSystem = false;
if (!is_null($index)) {
$indexSystem = $index;
//$indexSystem = $index;
}
$index = $this->createIndexName($type, $columns);

View File

@ -10,20 +10,42 @@ use Illuminate\Database\Schema\Blueprint;
class DB2Grammar extends Grammar
{
private $dbType;
/**
* The possible column modifiers.
*
* @var array
*/
protected $preModifiers = ['ForColumn'];
protected $modifiers = ['Nullable', 'Default', 'Generated', 'Increment', 'StartWith', 'Before', 'ImplicitlyHidden'];
protected $modifiers = [
'Nullable',
'Default',
'Generated',
'Increment',
'StartWith',
'Before',
'ImplicitlyHidden',
];
/**
* The possible column serials
*
* @var array
*/
protected $serials = ['smallInteger', 'integer', 'bigInteger'];
protected $serials = [
'smallInteger',
'integer',
'bigInteger',
];
/**
* DB2Grammar constructor.
*
* @param $dbType
*/
public function __construct($dbType)
{
$this->dbType = $dbType;
}
/**
* Wrap a single string in keyword identifiers.
@ -48,7 +70,11 @@ class DB2Grammar extends Grammar
*/
public function compileTableExists()
{
if ($this->dbType == "i") {
return 'select * from information_schema.tables where table_schema = upper(?) and table_name = upper(?)';
} else {
return 'select * from syspublic.all_tables where table_schema = upper(?) and table_name = upper(?)';
}
}
/**
@ -58,14 +84,22 @@ class DB2Grammar extends Grammar
*/
public function compileColumnExists()
{
if ($this->dbType == "i") {
return "
select column_name
from information_schema.columns
where table_schema = upper(?)
and table_name = upper(?)
";
} else {
return "
select column_name
from syspublic.all_ind_columns
where table_schema = upper(?)
and table_name = upper(?)
";
}
}
/**
* Compile a create table command.
@ -79,10 +113,10 @@ class DB2Grammar extends Grammar
public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection)
{
$columns = implode(', ', $this->getColumns($blueprint));
$sql = 'create table '.$this->wrapTable($blueprint);
$sql = 'create table ' . $this->wrapTable($blueprint);
if (isset($blueprint->systemName)) {
$sql .= ' for system name '.$blueprint->systemName;
$sql .= ' for system name ' . $blueprint->systemName;
}
$sql .= " ($columns)";
@ -101,7 +135,7 @@ class DB2Grammar extends Grammar
*/
public function compileLabel(Blueprint $blueprint, Fluent $command, Connection $connection)
{
return 'label on table '.$this->wrapTable($blueprint).' is \''.$command->label.'\'';
return 'label on table ' . $this->wrapTable($blueprint) . ' is \'' . $command->label . '\'';
}
/**
@ -121,7 +155,7 @@ class DB2Grammar extends Grammar
// used by the connection. The column's modifiers are compiled and added.
//$sql = $this->wrap($column).' '.$this->getType($column);
$sql = $this->addPreModifiers($this->wrap($column), $blueprint, $column);
$sql .= ' '.$this->getType($column);
$sql .= ' ' . $this->getType($column);
$columns[] = $this->addModifiers($sql, $blueprint, $column);
}
@ -164,7 +198,7 @@ class DB2Grammar extends Grammar
$statements = [];
foreach ($columns as $column) {
$statements[] = 'alter table '.$table.' '.$column;
$statements[] = 'alter table ' . $table . ' ' . $column;
}
return $statements;
@ -187,7 +221,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
return "alter table {$table} add constraint {$command->index} primary key ({$columns})";
@ -216,7 +250,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
$sql = "alter table {$table} add constraint {$command->index} ";
@ -253,7 +287,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
return "alter table {$table} add constraint {$command->index} unique({$columns})";
@ -276,7 +310,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
$sql = "create index {$command->index}";
@ -301,7 +335,7 @@ class DB2Grammar extends Grammar
*/
public function compileDrop(Blueprint $blueprint, Fluent $command)
{
return 'drop table '.$this->wrapTable($blueprint);
return 'drop table ' . $this->wrapTable($blueprint);
}
/**
@ -314,7 +348,7 @@ class DB2Grammar extends Grammar
*/
public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
{
return 'drop table if exists '.$this->wrapTable($blueprint);
return 'drop table if exists ' . $this->wrapTable($blueprint);
}
/**
@ -330,7 +364,7 @@ class DB2Grammar extends Grammar
$columns = $this->prefixArray('drop', $this->wrapArray($command->columns));
$table = $this->wrapTable($blueprint);
return 'alter table '.$table.' '.implode(', ', $columns);
return 'alter table ' . $table . ' ' . implode(', ', $columns);
}
/**
@ -343,7 +377,7 @@ class DB2Grammar extends Grammar
*/
public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
{
return 'alter table '.$this->wrapTable($blueprint).' drop primary key';
return 'alter table ' . $this->wrapTable($blueprint) . ' drop primary key';
}
/**
@ -362,7 +396,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
return "alter table {$table} drop index {$command->index}";
@ -384,7 +418,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
return "alter table {$table} drop index {$command->index}";
@ -406,7 +440,7 @@ class DB2Grammar extends Grammar
$schemaTable = explode(".", $table);
if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index);
$command->index = str_replace($schemaTable[0] . "_", "", $command->index);
}
return "alter table {$table} drop foreign key {$command->index}";
@ -424,7 +458,7 @@ class DB2Grammar extends Grammar
{
$from = $this->wrapTable($blueprint);
return "rename table {$from} to ".$this->wrapTable($command->to);
return "rename table {$from} to " . $this->wrapTable($command->to);
}
/**
@ -474,7 +508,7 @@ class DB2Grammar extends Grammar
*/
protected function typeMediumText(Fluent $column)
{
$colLength = ($column->length ? $column->length : 16369);
$colLength = ($column->length ? $column->length : 16000);
return "varchar($colLength)";
}
@ -488,7 +522,7 @@ class DB2Grammar extends Grammar
*/
protected function typeLongText(Fluent $column)
{
$colLength = ($column->length ? $column->length : 16369);
$colLength = ($column->length ? $column->length : 16000);
return "varchar($colLength)";
}
@ -550,7 +584,7 @@ class DB2Grammar extends Grammar
*/
protected function typeFloat(Fluent $column)
{
return "float({$column->total}, {$column->places})";
return "decimal({$column->total}, {$column->places})";
}
/**
@ -592,14 +626,7 @@ class DB2Grammar extends Grammar
{
$definition = 'smallint constraint %s_%s_%s check(%s in(0, 1)) %s';
return sprintf(
$definition,
$column->type,
$column->prefix,
$column->name,
$column->name,
is_null($column->default) ? ' default 0' : ''
);
return sprintf($definition, $column->type, $column->prefix, $column->name, $column->name, is_null($column->default) ? ' default 0' : '');
}
/**
@ -611,7 +638,7 @@ class DB2Grammar extends Grammar
*/
protected function typeEnum(Fluent $column)
{
return "enum('".implode("', '", $column->allowed)."')";
return "enum('" . implode("', '", $column->allowed) . "')";
}
/**
@ -639,7 +666,7 @@ class DB2Grammar extends Grammar
*/
protected function typeDateTime(Fluent $column)
{
return 'timestamp default current_timestamp';
return 'datetime';
}
/**
@ -710,7 +737,7 @@ class DB2Grammar extends Grammar
protected function modifyDefault(Blueprint $blueprint, Fluent $column)
{
if (!is_null($column->default)) {
return " default ".$this->getDefaultValue($column->default);
return " default " . $this->getDefaultValue($column->default);
}
return null;
@ -727,13 +754,12 @@ class DB2Grammar extends Grammar
protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
{
if (in_array($column->type, $this->serials) && $column->autoIncrement) {
return ' as identity constraint '.$blueprint->getTable().'_'.$column->name.'_primary primary key';
return ' generated by default as identity constraint ' . $blueprint->getTable() . '_' . $column->name . '_primary primary key';
}
return null;
}
/**
* Get the SQL for an "before" column modifier.
*
@ -745,7 +771,7 @@ class DB2Grammar extends Grammar
protected function modifyBefore(Blueprint $blueprint, Fluent $column)
{
if (!is_null($column->before)) {
return ' before '.$this->wrap($column->before);
return ' before ' . $this->wrap($column->before);
}
return null;
@ -762,7 +788,7 @@ class DB2Grammar extends Grammar
protected function modifyForColumn(Blueprint $blueprint, Fluent $column)
{
if (!is_null($column->forColumn)) {
return ' for column '.$this->wrap($column->forColumn);
return ' for column ' . $this->wrap($column->forColumn);
}
return null;
@ -779,7 +805,7 @@ class DB2Grammar extends Grammar
protected function modifyGenerated(Blueprint $blueprint, Fluent $column)
{
if (!is_null($column->generated)) {
return ' generated '.($column->generated === true ? 'always' : $this->wrap($column->generated));
return ' generated ' . ($column->generated === true ? 'always' : $this->wrap($column->generated));
}
return null;
@ -796,7 +822,7 @@ class DB2Grammar extends Grammar
protected function modifyStartWith(Blueprint $blueprint, Fluent $column)
{
if (!is_null($column->startWith)) {
return ' (start with '.$column->startWith.')';
return ' (start with ' . $column->startWith . ')';
}
return null;
@ -828,14 +854,10 @@ class DB2Grammar extends Grammar
*/
protected function getDefaultValue($value)
{
if (
$value instanceof Expression
|| is_bool($value)
|| is_numeric($value)
) {
if ($value instanceof Expression || is_bool($value) || is_numeric($value)) {
return $value;
}
return "'".strval($value)."'";
return "'" . strval($value) . "'";
}
}