Compatibilité avec DB2 Express C
This commit is contained in:
parent
96670e0eaa
commit
ebf2348485
@ -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 = [
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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)";
|
||||
}
|
||||
}
|
||||
|
@ -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 .= ')';
|
||||
$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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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.
|
||||
@ -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' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -639,7 +666,7 @@ class DB2Grammar extends Grammar
|
||||
*/
|
||||
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)
|
||||
{
|
||||
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.
|
||||
*
|
||||
@ -828,11 +854,7 @@ 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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user