Skip to content

Commit

Permalink
Compatibilité avec DB2 Express C
Browse files Browse the repository at this point in the history
  • Loading branch information
alebreton committed Jun 9, 2017
1 parent 96670e0 commit ebf2348
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 108 deletions.
5 changes: 3 additions & 2 deletions src/Connectors/ODBCZOSConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions src/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function getDefaultQueryGrammar()
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar());
return $this->withTablePrefix(new SchemaGrammar($this->config['driver'] == "odbc"?"i":"c"));
}

/**
Expand All @@ -115,6 +115,6 @@ protected function getDefaultPostProcessor()
return new DB2ZOSProcessor();
}

return new DB2Processor();
return new DB2Processor($this->config['driver'] == "odbc"?"i":"c");
}
}
15 changes: 11 additions & 4 deletions src/DB2ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cooperl\Database\DB2\Connectors\IBMConnector;
use Cooperl\Database\DB2\Connectors\ODBCZOSConnector;
use Illuminate\Support\ServiceProvider;
use Config;

/**
* Class DB2ServiceProvider
Expand Down Expand Up @@ -38,21 +39,27 @@ public function boot()
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();

Expand Down
12 changes: 0 additions & 12 deletions src/Query/Grammars/DB2Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,4 @@ public function getDateFormat()
{
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)";
}
}
35 changes: 27 additions & 8 deletions src/Query/Processors/DB2Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@
*/
class DB2Processor extends Processor
{

private $bdType;

/**
* DB2Processor constructor.
*
* @param $bdType
*/
public function __construct($bdType)
{
$this->bdType = $bdType;
}
/**
* Process the results of a "select" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $results
* @param array $results
*
* @return array
*/
Expand All @@ -37,13 +49,15 @@ class DB2Processor extends Processor
return $results;
}*/



/**
* Process an "insert get ID" query.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $sql
* @param array $values
* @param string $sequence
* @param string $sql
* @param array $values
* @param string $sequence
*
* @return int/array
*/
Expand All @@ -52,19 +66,24 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
$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];
$id = $result[$sequenceStr];
if (isset($result[$sequenceStr])) {
$id = $result[$sequenceStr];
} else {
$id = $result[strtoupper($sequenceStr)];
}

return is_numeric($id) ? (int) $id : $id;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Processors/DB2ZOSProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
$sequenceStr = $sequence ?: 'id';

if (is_array($sequence)) {
$grammar = new DB2Grammar();
$grammar = new DB2Grammar("z");
$sequenceStr = $grammar->columnize($sequence);
}

Expand Down
27 changes: 24 additions & 3 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
*/
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.
*
Expand Down Expand Up @@ -37,11 +59,10 @@ public function label($label)
* @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;

Expand All @@ -50,7 +71,7 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)
$indexSystem = false;

if (!is_null($index)) {
$indexSystem = $index;
//$indexSystem = $index;
}

$index = $this->createIndexName($type, $columns);
Expand Down
Loading

0 comments on commit ebf2348

Please sign in to comment.