Skip to content

Commit ebf2348

Browse files
author
alebreton
committed
Compatibilité avec DB2 Express C
1 parent 96670e0 commit ebf2348

File tree

8 files changed

+166
-108
lines changed

8 files changed

+166
-108
lines changed

src/Connectors/ODBCZOSConnector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ class ODBCZOSConnector extends ODBCConnector
1717
protected function getDsn(array $config)
1818
{
1919
$dsnParts = [
20-
'odbc:DRIVER={IBM DB2 ODBC DRIVER}',
20+
"odbc:DRIVER=$driverName",
2121
'Database=%s',
2222
'Hostname=%s',
2323
'Port=%s',
2424
'Protocol=TCPIP',
2525
'Uid=%s',
2626
'Pwd=%s',
27-
'', // Just to add a semicolon to the end of string
27+
'',
28+
// Just to add a semicolon to the end of string
2829
];
2930

3031
$dsnConfig = [

src/DB2Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function getDefaultQueryGrammar()
101101
*/
102102
protected function getDefaultSchemaGrammar()
103103
{
104-
return $this->withTablePrefix(new SchemaGrammar());
104+
return $this->withTablePrefix(new SchemaGrammar($this->config['driver'] == "odbc"?"i":"c"));
105105
}
106106

107107
/**
@@ -115,6 +115,6 @@ protected function getDefaultPostProcessor()
115115
return new DB2ZOSProcessor();
116116
}
117117

118-
return new DB2Processor();
118+
return new DB2Processor($this->config['driver'] == "odbc"?"i":"c");
119119
}
120120
}

src/DB2ServiceProvider.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cooperl\Database\DB2\Connectors\IBMConnector;
77
use Cooperl\Database\DB2\Connectors\ODBCZOSConnector;
88
use Illuminate\Support\ServiceProvider;
9+
use Config;
910

1011
/**
1112
* Class DB2ServiceProvider
@@ -38,21 +39,27 @@ public function boot()
3839
public function register()
3940
{
4041
// get the configs
41-
$conns = is_array(config('laravel-db2::database.connections')) ? config('laravel-db2::database.connections') : [];
42+
$conns = is_array(Config::get('laravel-db2::database.connections'))
43+
? Config::get('laravel-db2::database.connections')
44+
: [];
4245

4346
// Add my database configurations to the default set of configurations
44-
config(['database.connections' => array_merge($conns, config('database.connections'))]);
47+
$this->app['config']['database.connections'] = array_merge(
48+
$conns,
49+
$this->app['config']['database.connections']
50+
);
4551

4652
// Extend the connections with pdo_odbc and pdo_ibm drivers
47-
foreach (config('database.connections') as $conn => $config) {
53+
foreach (Config::get('database.connections') as $conn => $config) {
4854
// Only use configurations that feature a "odbc", "ibm" or "odbczos" driver
49-
if (!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm', 'odbczos'])) {
55+
if (!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm', 'odbczos', 'odbcexpress'])) {
5056
continue;
5157
}
5258

5359
// Create a connector
5460
$this->app['db']->extend($conn, function ($config) {
5561
switch ($config['driver']) {
62+
case 'odbcexpress':
5663
case 'odbc':
5764
$connector = new ODBCConnector();
5865

src/Query/Grammars/DB2Grammar.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,4 @@ public function getDateFormat()
181181
{
182182
return 'Y-m-d H:i:s.u';
183183
}
184-
185-
186-
/**
187-
* Compile the random statement into SQL.
188-
*
189-
* @param string $seed
190-
* @return string
191-
*/
192-
public function compileRandom($seed)
193-
{
194-
return "RAND($seed)";
195-
}
196184
}

src/Query/Processors/DB2Processor.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@
1313
*/
1414
class DB2Processor extends Processor
1515
{
16+
17+
private $bdType;
18+
19+
/**
20+
* DB2Processor constructor.
21+
*
22+
* @param $bdType
23+
*/
24+
public function __construct($bdType)
25+
{
26+
$this->bdType = $bdType;
27+
}
1628
/**
1729
* Process the results of a "select" query.
1830
*
1931
* @param \Illuminate\Database\Query\Builder $query
20-
* @param array $results
32+
* @param array $results
2133
*
2234
* @return array
2335
*/
@@ -37,13 +49,15 @@ class DB2Processor extends Processor
3749
return $results;
3850
}*/
3951

52+
53+
4054
/**
4155
* Process an "insert get ID" query.
4256
*
4357
* @param \Illuminate\Database\Query\Builder $query
44-
* @param string $sql
45-
* @param array $values
46-
* @param string $sequence
58+
* @param string $sql
59+
* @param array $values
60+
* @param string $sequence
4761
*
4862
* @return int/array
4963
*/
@@ -52,19 +66,24 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
5266
$sequenceStr = $sequence ?: 'id';
5367

5468
if (is_array($sequence)) {
55-
$grammar = new DB2Grammar();
69+
$grammar = new DB2Grammar($this->bdType);
5670
$sequenceStr = $grammar->columnize($sequence);
5771
}
5872

59-
$sql = 'select '.$sequenceStr.' from new table ('.$sql;
73+
$sql = 'select ' . $sequenceStr . ' from new table (' . $sql;
6074
$sql .= ')';
61-
$results = $query->getConnection()->select($sql, $values);
75+
$results = $query->getConnection()
76+
->select($sql, $values);
6277

6378
if (is_array($sequence)) {
6479
return array_values((array) $results[0]);
6580
} else {
6681
$result = (array) $results[0];
67-
$id = $result[$sequenceStr];
82+
if (isset($result[$sequenceStr])) {
83+
$id = $result[$sequenceStr];
84+
} else {
85+
$id = $result[strtoupper($sequenceStr)];
86+
}
6887

6988
return is_numeric($id) ? (int) $id : $id;
7089
}

src/Query/Processors/DB2ZOSProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function processInsertGetId(Builder $query, $sql, $values, $sequence = nu
2828
$sequenceStr = $sequence ?: 'id';
2929

3030
if (is_array($sequence)) {
31-
$grammar = new DB2Grammar();
31+
$grammar = new DB2Grammar("z");
3232
$sequenceStr = $grammar->columnize($sequence);
3333
}
3434

src/Schema/Blueprint.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
*/
1010
class Blueprint extends \Illuminate\Database\Schema\Blueprint
1111
{
12+
13+
public function synchro($index, $masterizable = false)
14+
{
15+
16+
$this->string('id_sync', 20)
17+
->index($index);
18+
$this->string('hashcode', 32);
19+
20+
if (true === $masterizable) {
21+
$this->boolean('data_master')
22+
->default(true);
23+
}
24+
}
25+
26+
/**
27+
* @param string $index
28+
*/
29+
public function dropSynchro($index)
30+
{
31+
$this->dropColumn('id_sync', 'hashcode');
32+
$this->dropIndex($index);
33+
}
1234
/**
1335
* Specify a system name for the table.
1436
*
@@ -37,11 +59,10 @@ public function label($label)
3759
* @param string $type
3860
* @param string|array $columns
3961
* @param string $index
40-
* @param string|null $algorithm
4162
*
4263
* @return \Illuminate\Support\Fluent
4364
*/
44-
protected function indexCommand($type, $columns, $index, $algorithm = null)
65+
protected function indexCommand($type, $columns, $index, $algorithm = NULL)
4566
{
4667
$columns = (array) $columns;
4768

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

5273
if (!is_null($index)) {
53-
$indexSystem = $index;
74+
//$indexSystem = $index;
5475
}
5576

5677
$index = $this->createIndexName($type, $columns);

0 commit comments

Comments
 (0)