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) protected function getDsn(array $config)
{ {
$dsnParts = [ $dsnParts = [
'odbc:DRIVER={IBM DB2 ODBC DRIVER}', "odbc:DRIVER=$driverName",
'Database=%s', 'Database=%s',
'Hostname=%s', 'Hostname=%s',
'Port=%s', 'Port=%s',
'Protocol=TCPIP', 'Protocol=TCPIP',
'Uid=%s', 'Uid=%s',
'Pwd=%s', 'Pwd=%s',
'', // Just to add a semicolon to the end of string '',
// Just to add a semicolon to the end of string
]; ];
$dsnConfig = [ $dsnConfig = [

View File

@ -101,7 +101,7 @@ class DB2Connection extends Connection
*/ */
protected function getDefaultSchemaGrammar() 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 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\IBMConnector;
use Cooperl\Database\DB2\Connectors\ODBCZOSConnector; use Cooperl\Database\DB2\Connectors\ODBCZOSConnector;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Config;
/** /**
* Class DB2ServiceProvider * Class DB2ServiceProvider
@ -38,21 +39,27 @@ class DB2ServiceProvider extends ServiceProvider
public function register() public function register()
{ {
// get the configs // 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 // 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 // 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 // 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; continue;
} }
// Create a connector // Create a connector
$this->app['db']->extend($conn, function ($config) { $this->app['db']->extend($conn, function ($config) {
switch ($config['driver']) { switch ($config['driver']) {
case 'odbcexpress':
case 'odbc': case 'odbc':
$connector = new ODBCConnector(); $connector = new ODBCConnector();

View File

@ -181,16 +181,4 @@ class DB2Grammar extends Grammar
{ {
return 'Y-m-d H:i:s.u'; 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 class DB2Processor extends Processor
{ {
private $bdType;
/**
* DB2Processor constructor.
*
* @param $bdType
*/
public function __construct($bdType)
{
$this->bdType = $bdType;
}
/** /**
* Process the results of a "select" query. * Process the results of a "select" query.
* *
@ -37,6 +49,8 @@ class DB2Processor extends Processor
return $results; return $results;
}*/ }*/
/** /**
* Process an "insert get ID" query. * Process an "insert get ID" query.
* *
@ -52,19 +66,24 @@ class DB2Processor extends Processor
$sequenceStr = $sequence ?: 'id'; $sequenceStr = $sequence ?: 'id';
if (is_array($sequence)) { if (is_array($sequence)) {
$grammar = new DB2Grammar(); $grammar = new DB2Grammar($this->bdType);
$sequenceStr = $grammar->columnize($sequence); $sequenceStr = $grammar->columnize($sequence);
} }
$sql = 'select ' . $sequenceStr . ' from new table (' . $sql; $sql = 'select ' . $sequenceStr . ' from new table (' . $sql;
$sql .= ')'; $sql .= ')';
$results = $query->getConnection()->select($sql, $values); $results = $query->getConnection()
->select($sql, $values);
if (is_array($sequence)) { if (is_array($sequence)) {
return array_values((array) $results[0]); return array_values((array) $results[0]);
} else { } else {
$result = (array) $results[0]; $result = (array) $results[0];
if (isset($result[$sequenceStr])) {
$id = $result[$sequenceStr]; $id = $result[$sequenceStr];
} else {
$id = $result[strtoupper($sequenceStr)];
}
return is_numeric($id) ? (int) $id : $id; return is_numeric($id) ? (int) $id : $id;
} }

View File

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

View File

@ -9,6 +9,28 @@ namespace Cooperl\Database\DB2\Schema;
*/ */
class Blueprint extends \Illuminate\Database\Schema\Blueprint 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. * Specify a system name for the table.
* *
@ -37,11 +59,10 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
* @param string $type * @param string $type
* @param string|array $columns * @param string|array $columns
* @param string $index * @param string $index
* @param string|null $algorithm
* *
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
protected function indexCommand($type, $columns, $index, $algorithm = null) protected function indexCommand($type, $columns, $index, $algorithm = NULL)
{ {
$columns = (array) $columns; $columns = (array) $columns;
@ -50,7 +71,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$indexSystem = false; $indexSystem = false;
if (!is_null($index)) { if (!is_null($index)) {
$indexSystem = $index; //$indexSystem = $index;
} }
$index = $this->createIndexName($type, $columns); $index = $this->createIndexName($type, $columns);

View File

@ -10,20 +10,42 @@ use Illuminate\Database\Schema\Blueprint;
class DB2Grammar extends Grammar class DB2Grammar extends Grammar
{ {
private $dbType;
/** /**
* The possible column modifiers. * The possible column modifiers.
* *
* @var array * @var array
*/ */
protected $preModifiers = ['ForColumn']; 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 * The possible column serials
* *
* @var array * @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. * Wrap a single string in keyword identifiers.
@ -48,7 +70,11 @@ class DB2Grammar extends Grammar
*/ */
public function compileTableExists() public function compileTableExists()
{ {
if ($this->dbType == "i") {
return 'select * from information_schema.tables where table_schema = upper(?) and table_name = upper(?)'; 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() public function compileColumnExists()
{ {
if ($this->dbType == "i") {
return " return "
select column_name select column_name
from information_schema.columns from information_schema.columns
where table_schema = upper(?) where table_schema = upper(?)
and table_name = 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. * Compile a create table command.
@ -474,7 +508,7 @@ class DB2Grammar extends Grammar
*/ */
protected function typeMediumText(Fluent $column) protected function typeMediumText(Fluent $column)
{ {
$colLength = ($column->length ? $column->length : 16369); $colLength = ($column->length ? $column->length : 16000);
return "varchar($colLength)"; return "varchar($colLength)";
} }
@ -488,7 +522,7 @@ class DB2Grammar extends Grammar
*/ */
protected function typeLongText(Fluent $column) protected function typeLongText(Fluent $column)
{ {
$colLength = ($column->length ? $column->length : 16369); $colLength = ($column->length ? $column->length : 16000);
return "varchar($colLength)"; return "varchar($colLength)";
} }
@ -550,7 +584,7 @@ class DB2Grammar extends Grammar
*/ */
protected function typeFloat(Fluent $column) 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'; $definition = 'smallint constraint %s_%s_%s check(%s in(0, 1)) %s';
return sprintf( return sprintf($definition, $column->type, $column->prefix, $column->name, $column->name, is_null($column->default) ? ' default 0' : '');
$definition,
$column->type,
$column->prefix,
$column->name,
$column->name,
is_null($column->default) ? ' default 0' : ''
);
} }
/** /**
@ -639,7 +666,7 @@ class DB2Grammar extends Grammar
*/ */
protected function typeDateTime(Fluent $column) protected function typeDateTime(Fluent $column)
{ {
return 'timestamp default current_timestamp'; return 'datetime';
} }
/** /**
@ -727,13 +754,12 @@ class DB2Grammar extends Grammar
protected function modifyIncrement(Blueprint $blueprint, Fluent $column) protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
{ {
if (in_array($column->type, $this->serials) && $column->autoIncrement) { 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; return null;
} }
/** /**
* Get the SQL for an "before" column modifier. * Get the SQL for an "before" column modifier.
* *
@ -828,11 +854,7 @@ class DB2Grammar extends Grammar
*/ */
protected function getDefaultValue($value) protected function getDefaultValue($value)
{ {
if ( if ($value instanceof Expression || is_bool($value) || is_numeric($value)) {
$value instanceof Expression
|| is_bool($value)
|| is_numeric($value)
) {
return $value; return $value;
} }