Reindent code and apply to PSR-2 standards

Also make code more readable in some ways. :)
This commit is contained in:
Lucas Mezêncio 2016-08-19 13:06:06 -03:00
parent e8f17c13a7
commit b19daaa67c
9 changed files with 491 additions and 374 deletions

View File

@ -1,84 +1,103 @@
<?php <?php
namespace Cooperl\Database\DB2\Connectors; namespace Cooperl\Database\DB2\Connectors;
use Illuminate\Database\Connectors\Connector; use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface; use Illuminate\Database\Connectors\ConnectorInterface;
use PDO; /**
* Class IBMConnector
*
* @package Cooperl\Database\DB2\Connectors
*/
class IBMConnector extends Connector implements ConnectorInterface class IBMConnector extends Connector implements ConnectorInterface
{ {
/**
* @param array $config
*
* @return \PDO
*/
public function connect(array $config) public function connect(array $config)
{ {
$dsn = $this->getDsn($config); $dsn = $this->getDsn($config);
$options = [ $options = [
PDO::I5_ATTR_DBC_SYS_NAMING => false, \PDO::I5_ATTR_DBC_SYS_NAMING => false,
PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT, \PDO::I5_ATTR_COMMIT => \PDO::I5_TXN_NO_COMMIT,
PDO::I5_ATTR_JOB_SORT => false \PDO::I5_ATTR_JOB_SORT => false,
]; ];
// Naming mode // Naming mode
switch ($config['naming']) { switch ($config['naming']) {
case 1: case 1:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = true; $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true;
break; break;
case 0: case 0:
default: default:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = false; $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false;
break; break;
} }
// Isolation mode // Isolation mode
switch ($config['commitMode']) { switch ($config['commitMode']) {
case 1: case 1:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_COMMITTED; $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_COMMITTED;
break; break;
case 2: case 2:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_UNCOMMITTED; $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_UNCOMMITTED;
break; break;
case 3: case 3:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_REPEATABLE_READ; $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_REPEATABLE_READ;
break; break;
case 4: case 4:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_SERIALIZABLE; $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_SERIALIZABLE;
break; break;
case 0: case 0:
default: default:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_NO_COMMIT; $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_NO_COMMIT;
break; break;
} }
// Job sort mode // Job sort mode
switch ($config['jobSort']) { switch ($config['jobSort']) {
case 1: case 1:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = true; $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true;
break; break;
case 0: case 0:
default: default:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = false; $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false;
break; break;
} }
$options = $this->getOptions($config) + $options; $options = $this->getOptions($config) + $options;
$connection = $this->createConnection($dsn, $config, $options); $connection = $this->createConnection($dsn, $config, $options);
if (isset($config['schema'])) if (isset($config['schema'])) {
{
$schema = $config['schema']; $schema = $config['schema'];
$connection->prepare("set schema $schema")->execute(); $connection->prepare("set schema $schema")->execute();
} }
return $connection; return $connection;
} }
protected function getDsn(array $config) { /**
extract($config); * @param array $config
$dsn = "ibm:$database"; *
* @return string
*/
protected function getDsn(array $config)
{
$dsn = "ibm:{$config['database']}";
return $dsn; return $dsn;
} }
} }

View File

@ -1,99 +1,86 @@
<?php <?php
namespace Cooperl\Database\DB2\Connectors; namespace Cooperl\Database\DB2\Connectors;
use Illuminate\Database\Connectors\Connector; use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface; use Illuminate\Database\Connectors\ConnectorInterface;
/**
* Class ODBCConnector
*
* @package Cooperl\Database\DB2\Connectors
*/
class ODBCConnector extends Connector implements ConnectorInterface class ODBCConnector extends Connector implements ConnectorInterface
{ {
/**
* @param array $config
*
* @return \PDO
*/
public function connect(array $config) public function connect(array $config)
{ {
$dsn = $this->getDsn($config); $dsn = $this->getDsn($config);
$options = $this->getOptions($config); $options = $this->getOptions($config);
$connection = $this->createConnection($dsn, $config, $options); $connection = $this->createConnection($dsn, $config, $options);
if (isset($config['schema'])) if (isset($config['schema'])) {
{
$schema = $config['schema']; $schema = $config['schema'];
$connection->prepare("set schema $schema")->execute(); $connection->prepare('set schema '.$schema)->execute();
} }
return $connection; return $connection;
} }
protected function getDsn(array $config) { /**
extract($config); * @param array $config
*
* @return string
*/
protected function getDsn(array $config)
{
$dsnParts = [
'odbc:DRIVER=%s', 'SYSTEM=%s', 'UserID=%s', 'Password=%s', 'DATABASE=%s', 'SIGNON=%s', 'SSL=%s',
'CommitMode=%s', 'ConnectionType=%s', 'DefaultLibraries=%s', 'Naming=%s', 'UNICODESQL=%s', 'DateFormat=%s',
'DateSeperator=%s', 'Decimal=%s', 'TimeFormat=%s', 'TimeSeparator=%s', 'BLOCKFETCH=%s', 'BlockSizeKB=%s',
'AllowDataCompression=%s', 'CONCURRENCY=%s', 'LAZYCLOSE=%s', 'MaxFieldLength=%s', 'PREFETCH=%s',
'QUERYTIMEOUT=%s', 'DefaultPkgLibrary=%s', 'DefaultPackage=%s', 'ExtendedDynamic=%s', 'QAQQINILibrary=%s',
'SQDIAGCODE=%s', 'LANGUAGEID=%s', 'SORTTABLE=%s', 'SortSequence=%s', 'SORTWEIGHT=%s',
'AllowUnsupportedChar=%s', 'CCSID=%s', 'GRAPHIC=%s', 'ForceTranslation=%s', 'ALLOWPROCCALLS=%s',
'DB2SQLSTATES=%s', 'DEBUG=%s', 'TRUEAUTOCOMMIT=%s', 'CATALOGOPTIONS=%s', 'LibraryView=%s', 'ODBCRemarks=%s',
'SEARCHPATTERN=%s', 'TranslationDLL=%s', 'TranslationOption=%s', 'MAXTRACESIZE=%s', 'MultipleTraceFiles=%s',
'TRACE=%s', 'TRACEFILENAME=%s', 'ExtendedColInfo=%s',
'', // Just to add a semicolon to the end of string
];
$dsn = "odbc:" $dsnConfig = [
// General settings // General settings
. "DRIVER=$driverName;" $config['driverName'], $config['host'], $config['username'], $config['password'],
. "SYSTEM=$host;" //Server settings
. "UserID=$username;" $config['database'], $config['signon'], $config['ssl'], $config['commitMode'], $config['connectionType'],
. "Password=$password;" $config['defaultLibraries'], $config['naming'], $config['unicodeSql'],
//Server settings // Format settings
. "DATABASE=$database;" $config['dateFormat'], $config['dateSeperator'], $config['decimal'], $config['timeFormat'],
. "SIGNON=$signon;" $config['timeSeparator'],
. "SSL=$ssl;" // Performances settings
. "CommitMode=$commitMode;" $config['blockFetch'], $config['blockSizeKB'], $config['allowDataCompression'], $config['concurrency'],
. "ConnectionType=$connectionType;" $config['lazyClose'], $config['maxFieldLength'], $config['prefetch'], $config['queryTimeout'],
. "DefaultLibraries=$defaultLibraries;" // Modules settings
. "Naming=$naming;" $config['defaultPkgLibrary'], $config['defaultPackage'], $config['extendedDynamic'],
. "UNICODESQL=$unicodeSql;" // Diagnostic settings
// Format settings $config['QAQQINILibrary'], $config['sqDiagCode'],
. "DateFormat=$dateFormat;" // Sort settings
. "DateSeperator=$dateSeperator;" $config['languageId'], $config['sortTable'], $config['sortSequence'], $config['sortWeight'],
. "Decimal=$decimal;" // Conversion settings
. "TimeFormat=$timeFormat;" $config['allowUnsupportedChar'], $config['ccsid'], $config['graphic'], $config['forceTranslation'],
. "TimeSeparator=$timeSeparator;" // Other settings
// Performances settings $config['allowProcCalls'], $config['DB2SqlStates'], $config['debug'], $config['trueAutoCommit'],
. "BLOCKFETCH=$blockFetch;" $config['catalogOptions'], $config['libraryView'], $config['ODBCRemarks'], $config['searchPattern'],
. "BlockSizeKB=$blockSizeKB;" $config['translationDLL'], $config['translationOption'], $config['maxTraceSize'],
. "AllowDataCompression=$allowDataCompression;" $config['multipleTraceFiles'], $config['trace'], $config['traceFilename'], $config['extendedColInfo'],
. "CONCURRENCY=$concurrency;" ];
. "LAZYCLOSE=$lazyClose;"
. "MaxFieldLength=$maxFieldLength;"
. "PREFETCH=$prefetch;"
. "QUERYTIMEOUT=$queryTimeout;"
// Modules settings
. "DefaultPkgLibrary=$defaultPkgLibrary;"
. "DefaultPackage=$defaultPackage;"
. "ExtendedDynamic=$extendedDynamic;"
// Diagnostic settings
. "QAQQINILibrary=$QAQQINILibrary;"
. "SQDIAGCODE=$sqDiagCode;"
// Sort settings
. "LANGUAGEID=$languageId;"
. "SORTTABLE=$sortTable;"
. "SortSequence=$sortSequence;"
. "SORTWEIGHT=$sortWeight;"
// Conversion settings
. "AllowUnsupportedChar=$allowUnsupportedChar;"
. "CCSID=$ccsid;"
. "GRAPHIC=$graphic;"
. "ForceTranslation=$forceTranslation;"
// Other settings
. "ALLOWPROCCALLS=$allowProcCalls;"
. "DB2SQLSTATES=$DB2SqlStates;"
. "DEBUG=$debug;"
. "TRUEAUTOCOMMIT=$trueAutoCommit;"
. "CATALOGOPTIONS=$catalogOptions;"
. "LibraryView=$libraryView;"
. "ODBCRemarks=$ODBCRemarks;"
. "SEARCHPATTERN=$searchPattern;"
. "TranslationDLL=$translationDLL;"
. "TranslationOption=$translationOption;"
. "MAXTRACESIZE=$maxTraceSize;"
. "MultipleTraceFiles=$multipleTraceFiles;"
. "TRACE=$trace;"
. "TRACEFILENAME=$traceFilename;"
. "ExtendedColInfo=$extendedColInfo;"
;
return $dsn; return sprintf(implode(';', $dsnParts), ...$dsnConfig);
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Cooperl\Database\DB2; namespace Cooperl\Database\DB2;
use PDO; use PDO;
@ -10,9 +11,13 @@ use Cooperl\Database\DB2\Query\Processors\DB2Processor;
use Cooperl\Database\DB2\Query\Grammars\DB2Grammar as QueryGrammar; use Cooperl\Database\DB2\Query\Grammars\DB2Grammar as QueryGrammar;
use Cooperl\Database\DB2\Schema\Grammars\DB2Grammar as SchemaGrammar; use Cooperl\Database\DB2\Schema\Grammars\DB2Grammar as SchemaGrammar;
/**
* Class DB2Connection
*
* @package Cooperl\Database\DB2
*/
class DB2Connection extends Connection class DB2Connection extends Connection
{ {
/** /**
* The name of the default schema. * The name of the default schema.
* *
@ -20,6 +25,13 @@ class DB2Connection extends Connection
*/ */
protected $defaultSchema; protected $defaultSchema;
/**
* The name of the current schema in use.
*
* @var string
*/
protected $currentSchema;
public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = []) public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = [])
{ {
parent::__construct($pdo, $database, $tablePrefix, $config); parent::__construct($pdo, $database, $tablePrefix, $config);
@ -49,6 +61,8 @@ class DB2Connection extends Connection
/** /**
* Set the name of the current schema. * Set the name of the current schema.
* *
* @param $schema
*
* @return string * @return string
*/ */
public function setCurrentSchema($schema) public function setCurrentSchema($schema)
@ -64,37 +78,38 @@ class DB2Connection extends Connection
*/ */
public function getSchemaBuilder() public function getSchemaBuilder()
{ {
if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new Builder($this); return new Builder($this);
} }
/** /**
* @return Query\Grammars\Grammar * @return \Illuminate\Database\Grammar
*/ */
protected function getDefaultQueryGrammar() protected function getDefaultQueryGrammar()
{ {
return $this->withTablePrefix(new QueryGrammar); return $this->withTablePrefix(new QueryGrammar());
} }
/** /**
* Default grammar for specified Schema * Default grammar for specified Schema
* @return Schema\Grammars\Grammar *
* @return \Illuminate\Database\Grammar
*/ */
protected function getDefaultSchemaGrammar() protected function getDefaultSchemaGrammar()
{ {
return $this->withTablePrefix(new SchemaGrammar());
return $this->withTablePrefix(new SchemaGrammar);
} }
/** /**
* Get the default post processor instance. * Get the default post processor instance.
* *
* @return \Illuminate\Database\Query\Processors\PostgresProcessor * @return \Illuminate\Database\Query\Processors\PostgresProcessor
*/ */
protected function getDefaultPostProcessor() protected function getDefaultPostProcessor()
{ {
return new DB2Processor; return new DB2Processor();
} }
} }

View File

@ -1,15 +1,19 @@
<?php <?php
namespace Cooperl\Database\DB2; namespace Cooperl\Database\DB2;
use Cooperl\Database\DB2\Connectors\ODBCConnector; use Cooperl\Database\DB2\Connectors\ODBCConnector;
use Cooperl\Database\DB2\Connectors\IBMConnector; use Cooperl\Database\DB2\Connectors\IBMConnector;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\AliasLoader;
use Config; use Config;
class DB2ServiceProvider extends ServiceProvider { /**
* Class DB2ServiceProvider
*
* @package Cooperl\Database\DB2
*/
class DB2ServiceProvider extends ServiceProvider
{
/** /**
* Indicates if loading of the provider is deferred. * Indicates if loading of the provider is deferred.
* *
@ -33,42 +37,43 @@ class DB2ServiceProvider extends ServiceProvider {
*/ */
public function register() public function register()
{ {
// get the configs // get the configs
$conns = is_array(Config::get('laravel-db2::database.connections')) ? Config::get('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
$this->app['config']['database.connections'] = array_merge($conns, $this->app['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::get('database.connections') as $conn => $config) foreach (Config::get('database.connections') as $conn => $config) {
{ // Only use configurations that feature a "odbc" or "ibm" driver
if (!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm'])) {
//Only use configurations that feature a "odbc" or "ibm" driver
if(!isset($config['driver']) || !in_array($config['driver'], ['odbc', 'ibm']) )
{
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 'odbc': case 'odbc':
$connector = new ODBCConnector(); $connector = new ODBCConnector();
break; break;
case 'ibm': case 'ibm':
$connector = new IBMConnector();
break;
default: default:
$connector = new IBMConnector();
break; break;
} }
$db2Connection = $connector->connect($config); $db2Connection = $connector->connect($config);
return new DB2Connection($db2Connection, $config["database"], $config["prefix"], $config); return new DB2Connection($db2Connection, $config["database"], $config["prefix"], $config);
}); });
} }
} }
/** /**
@ -80,5 +85,4 @@ class DB2ServiceProvider extends ServiceProvider {
{ {
return []; return [];
} }
} }

View File

@ -1,30 +1,39 @@
<?php <?php
namespace Cooperl\Database\DB2\Query\Grammars; namespace Cooperl\Database\DB2\Query\Grammars;
use Illuminate\Database\Query\Grammars\Grammar; use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
/**
* Class DB2Grammar
*
* @package Cooperl\Database\DB2\Query\Grammars
*/
class DB2Grammar extends Grammar class DB2Grammar extends Grammar
{ {
/**
/**
* Wrap a single string in keyword identifiers. * Wrap a single string in keyword identifiers.
* *
* @param string $value * @param string $value
*
* @return string * @return string
*/ */
protected function wrapValue($value) protected function wrapValue($value)
{ {
if ($value === '*') return $value; if ($value === '*') {
return $value;
}
return str_replace('"', '""', $value); return str_replace('"', '""', $value);
} }
/** /**
* Compile the "limit" portions of the query. * Compile the "limit" portions of the query.
* *
* @param Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\Builder $query
* @param int $limit * @param int $limit
*
* @return string * @return string
*/ */
protected function compileLimit(Builder $query, $limit) protected function compileLimit(Builder $query, $limit)
@ -32,10 +41,11 @@ class DB2Grammar extends Grammar
return "FETCH FIRST $limit ROWS ONLY"; return "FETCH FIRST $limit ROWS ONLY";
} }
/** /**
* Compile a select query into SQL. * Compile a select query into SQL.
* *
* @param Illuminate\Database\Query\Builder * @param \Illuminate\Database\Query\Builder $query
*
* @return string * @return string
*/ */
public function compileSelect(Builder $query) public function compileSelect(Builder $query)
@ -45,19 +55,19 @@ class DB2Grammar extends Grammar
// If an offset is present on the query, we will need to wrap the query in // If an offset is present on the query, we will need to wrap the query in
// a big "ANSI" offset syntax block. This is very nasty compared to the // a big "ANSI" offset syntax block. This is very nasty compared to the
// other database systems but is necessary for implementing features. // other database systems but is necessary for implementing features.
if ($query->offset > 0) if ($query->offset > 0) {
{
return $this->compileAnsiOffset($query, $components); return $this->compileAnsiOffset($query, $components);
} }
return $this->concatenate($components); return $this->concatenate($components);
} }
/** /**
* Create a full ANSI offset clause for the query. * Create a full ANSI offset clause for the query.
* *
* @param Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\Builder $query
* @param array $components * @param array $components
*
* @return string * @return string
*/ */
protected function compileAnsiOffset(Builder $query, $components) protected function compileAnsiOffset(Builder $query, $components)
@ -65,8 +75,7 @@ class DB2Grammar extends Grammar
// An ORDER BY clause is required to make this offset query work, so if one does // An ORDER BY clause is required to make this offset query work, so if one does
// not exist we'll just create a dummy clause to trick the database and so it // not exist we'll just create a dummy clause to trick the database and so it
// does not complain about the queries for not having an "order by" clause. // does not complain about the queries for not having an "order by" clause.
if ( ! isset($components['orders'])) if (!isset($components['orders'])) {
{
$components['orders'] = 'order by 1'; $components['orders'] = 'order by 1';
} }
@ -77,7 +86,7 @@ class DB2Grammar extends Grammar
// the "select" that will give back the row numbers on each of the records. // the "select" that will give back the row numbers on each of the records.
$orderings = $components['orders']; $orderings = $components['orders'];
$columns = (!empty($components['columns']) ? $components['columns'] . ', ': 'select'); $columns = (!empty($components['columns']) ? $components['columns'].', ' : 'select');
$components['columns'] = $this->compileOver($orderings, $columns); $components['columns'] = $this->compileOver($orderings, $columns);
@ -101,7 +110,9 @@ class DB2Grammar extends Grammar
/** /**
* Compile the over statement for a table expression. * Compile the over statement for a table expression.
* *
* @param string $orderings * @param string $orderings
* @param $columns
*
* @return string * @return string
*/ */
protected function compileOver($orderings, $columns) protected function compileOver($orderings, $columns)
@ -109,12 +120,16 @@ class DB2Grammar extends Grammar
return "{$columns} row_number() over ({$orderings}) as row_num"; return "{$columns} row_number() over ({$orderings}) as row_num";
} }
/**
* @param $query
*
* @return string
*/
protected function compileRowConstraint($query) protected function compileRowConstraint($query)
{ {
$start = $query->offset + 1; $start = $query->offset + 1;
if ($query->limit > 0) if ($query->limit > 0) {
{
$finish = $query->offset + $query->limit; $finish = $query->offset + $query->limit;
return "between {$start} and {$finish}"; return "between {$start} and {$finish}";
@ -123,11 +138,12 @@ class DB2Grammar extends Grammar
return ">= {$start}"; return ">= {$start}";
} }
/** /**
* Compile a common table expression for a query. * Compile a common table expression for a query.
* *
* @param string $sql * @param string $sql
* @param string $constraint * @param string $constraint
*
* @return string * @return string
*/ */
protected function compileTableExpression($sql, $constraint) protected function compileTableExpression($sql, $constraint)
@ -138,8 +154,9 @@ class DB2Grammar extends Grammar
/** /**
* Compile the "offset" portions of the query. * Compile the "offset" portions of the query.
* *
* @param Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\Builder $query
* @param int $offset * @param int $offset
*
* @return string * @return string
*/ */
protected function compileOffset(Builder $query, $offset) protected function compileOffset(Builder $query, $offset)
@ -156,5 +173,4 @@ class DB2Grammar extends Grammar
{ {
return 'Y-m-d H:i:s.u'; return 'Y-m-d H:i:s.u';
} }
} }

View File

@ -1,17 +1,24 @@
<?php <?php
namespace Cooperl\Database\DB2\Query\Processors; namespace Cooperl\Database\DB2\Query\Processors;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Processors\Processor; use Illuminate\Database\Query\Processors\Processor;
use Cooperl\Database\DB2\Query\Grammars\DB2Grammar; use Cooperl\Database\DB2\Query\Grammars\DB2Grammar;
class DB2Processor extends Processor { /**
* Class DB2Processor
*
* @package Cooperl\Database\DB2\Query\Processors
*/
class DB2Processor extends Processor
{
/** /**
* Process the results of a "select" query. * Process the results of a "select" query.
* *
* @param \Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\Builder $query
* @param array $results * @param array $results
*
* @return array * @return array
*/ */
/*public function processSelect(Builder $query, $results) /*public function processSelect(Builder $query, $results)
@ -33,34 +40,33 @@ class DB2Processor extends Processor {
/** /**
* Process an "insert get ID" query. * Process an "insert get ID" query.
* *
* @param \Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Query\Builder $query
* @param string $sql * @param string $sql
* @param array $values * @param array $values
* @param string $sequence * @param string $sequence
*
* @return int/array * @return int/array
*/ */
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null) public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
{ {
$sequenceStr = $sequence ?: 'id'; $sequenceStr = $sequence ?: 'id';
if (is_array($sequence))
{ if (is_array($sequence)) {
$grammar = new DB2Grammar; $grammar = new DB2Grammar();
$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];
$id = $result[$sequenceStr]; $id = $result[$sequenceStr];
return is_numeric($id) ? (int) $id : $id; return is_numeric($id) ? (int) $id : $id;
} }
} }
} }

View File

@ -1,12 +1,18 @@
<?php <?php
namespace Cooperl\Database\DB2\Schema; namespace Cooperl\Database\DB2\Schema;
class Blueprint extends \Illuminate\Database\Schema\Blueprint { /**
* Class Blueprint
*
* @package Cooperl\Database\DB2\Schema
*/
class Blueprint extends \Illuminate\Database\Schema\Blueprint
{
/** /**
* Specify a system name for the table. * Specify a system name for the table.
* *
* @param string $systemName * @param string $systemName
*/ */
public function forSystemName($systemName) public function forSystemName($systemName)
{ {
@ -16,7 +22,8 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
/** /**
* Specify a label for the table. * Specify a label for the table.
* *
* @param string $label * @param string $label
*
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
public function label($label) public function label($label)
@ -27,9 +34,10 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
/** /**
* Add a new index command to the blueprint. * Add a new index command to the blueprint.
* *
* @param string $type * @param string $type
* @param string|array $columns * @param string|array $columns
* @param string $index * @param string $index
*
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
protected function indexCommand($type, $columns, $index) protected function indexCommand($type, $columns, $index)
@ -39,14 +47,14 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
switch ($type) { switch ($type) {
case 'index': case 'index':
$indexSystem = false; $indexSystem = false;
if (!is_null($index))
{ if (!is_null($index)) {
$indexSystem = $index; $indexSystem = $index;
} }
$index = $this->createIndexName($type, $columns);
return $this->addCommand($type, compact('index', 'indexSystem', 'columns'));
break;
$index = $this->createIndexName($type, $columns);
return $this->addCommand($type, compact('index', 'indexSystem', 'columns'));
default: default:
break; break;
} }
@ -54,8 +62,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
// If no name was specified for this index, we will create one using a basic // If no name was specified for this index, we will create one using a basic
// convention of the table name, followed by the columns, followed by an // convention of the table name, followed by the columns, followed by an
// index type, such as primary or index, which makes the index unique. // index type, such as primary or index, which makes the index unique.
if (is_null($index)) if (is_null($index)) {
{
$index = $this->createIndexName($type, $columns); $index = $this->createIndexName($type, $columns);
} }
@ -65,7 +72,8 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
/** /**
* Create a new boolean column on the table. * Create a new boolean column on the table.
* *
* @param string $column * @param string $column
*
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
public function boolean($column) public function boolean($column)
@ -73,24 +81,25 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
$prefix = $this->table; $prefix = $this->table;
// Aucune utilité d'avoir le nom du schéma dans le préfixe de la contrainte check pour le type booléen // Aucune utilité d'avoir le nom du schéma dans le préfixe de la contrainte check pour le type booléen
$schemaTable = explode(".", $this->table); $schemaTable = explode(".", $this->table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$prefix = $schemaTable[1]; $prefix = $schemaTable[1];
} }
return $this->addColumn('boolean', $column, ['prefix' => $prefix]); return $this->addColumn('boolean', $column, ['prefix' => $prefix]);
} }
/** /**
* Create a new numeric column on the table. * Create a new numeric column on the table.
* *
* @param string $column * @param string $column
* @param int $total * @param int $total
* @param int $places * @param int $places
*
* @return \Illuminate\Support\Fluent * @return \Illuminate\Support\Fluent
*/ */
public function numeric($column, $total = 8, $places = 2) public function numeric($column, $total = 8, $places = 2)
{ {
return $this->addColumn('numeric', $column, compact('total', 'places')); return $this->addColumn('numeric', $column, compact('total', 'places'));
} }
} }

View File

@ -1,29 +1,34 @@
<?php <?php
namespace Cooperl\Database\DB2\Schema; namespace Cooperl\Database\DB2\Schema;
use Closure; use Closure;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
class Builder extends \Illuminate\Database\Schema\Builder { /**
* Class Builder
*
* @package Cooperl\Database\DB2\Schema
*/
class Builder extends \Illuminate\Database\Schema\Builder
{
/** /**
* Determine if the given table exists. * Determine if the given table exists.
* *
* @param string $table * @param string $table
*
* @return bool * @return bool
*/ */
public function hasTable($table) public function hasTable($table)
{ {
$sql = $this->grammar->compileTableExists(); $sql = $this->grammar->compileTableExists();
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$schema = $schemaTable[0]; $schema = $schemaTable[0];
$table = $this->connection->getTablePrefix().$schemaTable[1]; $table = $this->connection->getTablePrefix().$schemaTable[1];
} } else {
else
{
$schema = $this->connection->getDefaultSchema(); $schema = $this->connection->getDefaultSchema();
$table = $this->connection->getTablePrefix().$table; $table = $this->connection->getTablePrefix().$table;
} }
@ -34,17 +39,15 @@ class Builder extends \Illuminate\Database\Schema\Builder {
/** /**
* Get the column listing for a given table. * Get the column listing for a given table.
* *
* @param string $table * @param string $table
*
* @return array * @return array
*/ */
public function getColumnListing($table) public function getColumnListing($table)
{ {
$sql = $this->grammar->compileColumnExists(); $sql = $this->grammar->compileColumnExists();
$database = $this->connection->getDatabaseName(); $database = $this->connection->getDatabaseName();
$table = $this->connection->getTablePrefix().$table; $table = $this->connection->getTablePrefix().$table;
$results = $this->connection->select($sql, [$database, $table]); $results = $this->connection->select($sql, [$database, $table]);
return $this->connection->getPostProcessor()->processColumnListing($results); return $this->connection->getPostProcessor()->processColumnListing($results);
@ -53,37 +56,34 @@ class Builder extends \Illuminate\Database\Schema\Builder {
/** /**
* Execute the blueprint to build / modify the table. * Execute the blueprint to build / modify the table.
* *
* @param \Cooperl\Database\DB2\Schema\Blueprint $blueprint * @param Blueprint $blueprint
* @return void
*/ */
protected function build(Blueprint $blueprint) protected function build(Blueprint $blueprint)
{ {
$schemaTable = explode(".", $blueprint->getTable()); $schemaTable = explode(".", $blueprint->getTable());
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$this->connection->setCurrentSchema($schemaTable[0]); $this->connection->setCurrentSchema($schemaTable[0]);
} }
$blueprint->build($this->connection, $this->grammar); $blueprint->build($this->connection, $this->grammar);
$this->connection->resetCurrentSchema(); $this->connection->resetCurrentSchema();
} }
/** /**
* Create a new command set with a Closure. * Create a new command set with a Closure.
* *
* @param string $table * @param string $table
* @param \Closure $callback * @param \Closure $callback
*
* @return \Cooperl\Database\DB2\Schema\Blueprint * @return \Cooperl\Database\DB2\Schema\Blueprint
*/ */
protected function createBlueprint($table, Closure $callback = null) protected function createBlueprint($table, Closure $callback = null)
{ {
if (isset($this->resolver)) if (isset($this->resolver)) {
{
return call_user_func($this->resolver, $table, $callback); return call_user_func($this->resolver, $table, $callback);
} }
return new \Cooperl\Database\DB2\Schema\Blueprint($table, $callback); return new \Cooperl\Database\DB2\Schema\Blueprint($table, $callback);
} }
} }

View File

@ -1,14 +1,15 @@
<?php <?php
namespace Cooperl\Database\DB2\Schema\Grammars; namespace Cooperl\Database\DB2\Schema\Grammars;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Fluent; use Illuminate\Support\Fluent;
use Illuminate\Database\Connection; use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Grammars\Grammar; use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
class DB2Grammar extends Grammar { class DB2Grammar extends Grammar
{
/** /**
* The possible column modifiers. * The possible column modifiers.
* *
@ -27,12 +28,15 @@ class DB2Grammar extends Grammar {
/** /**
* Wrap a single string in keyword identifiers. * Wrap a single string in keyword identifiers.
* *
* @param string $value * @param string $value
*
* @return string * @return string
*/ */
protected function wrapValue($value) protected function wrapValue($value)
{ {
if ($value === '*') return $value; if ($value === '*') {
return $value;
}
return str_replace('"', '""', $value); return str_replace('"', '""', $value);
} }
@ -50,31 +54,34 @@ class DB2Grammar extends Grammar {
/** /**
* Compile the query to determine the list of columns. * Compile the query to determine the list of columns.
* *
* @param string $table
* @return string * @return string
*/ */
public function compileColumnExists() public function compileColumnExists()
{ {
return "select column_name from information_schema.columns where table_schema = upper(?) and table_name = upper(?)"; return "
select column_name
from information_schema.columns
where table_schema = upper(?)
and table_name = upper(?)
";
} }
/** /**
* Compile a create table command. * Compile a create table command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
* @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Connection $connection
*
* @return string * @return string
*/ */
public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection) public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection)
{ {
$columns = implode(', ', $this->getColumns($blueprint)); $columns = implode(', ', $this->getColumns($blueprint));
$sql = 'create table '.$this->wrapTable($blueprint); $sql = 'create table '.$this->wrapTable($blueprint);
if (isset($blueprint->systemName)) if (isset($blueprint->systemName)) {
{
$sql .= ' for system name '.$blueprint->systemName; $sql .= ' for system name '.$blueprint->systemName;
} }
@ -86,28 +93,29 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a label command. * Compile a label command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
* @param \Illuminate\Database\Connection $connection * @param \Illuminate\Database\Connection $connection
*
* @return string * @return string
*/ */
public function compileLabel(Blueprint $blueprint, Fluent $command, Connection $connection) 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.'\'';
} }
/** /**
* Compile the blueprint's column definitions. * Compile the blueprint's column definitions.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
*
* @return array * @return array
*/ */
protected function getColumns(Blueprint $blueprint) protected function getColumns(Blueprint $blueprint)
{ {
$columns = []; $columns = [];
foreach ($blueprint->getColumns() as $column) foreach ($blueprint->getColumns() as $column) {
{
// Each of the column types have their own compiler functions which are tasked // Each of the column types have their own compiler functions which are tasked
// with turning the column definition into its SQL format for this platform // with turning the column definition into its SQL format for this platform
// used by the connection. The column's modifiers are compiled and added. // used by the connection. The column's modifiers are compiled and added.
@ -124,17 +132,16 @@ class DB2Grammar extends Grammar {
/** /**
* Add the column modifiers to the definition. * Add the column modifiers to the definition.
* *
* @param string $sql * @param string $sql
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function addPreModifiers($sql, Blueprint $blueprint, Fluent $column) protected function addPreModifiers($sql, Blueprint $blueprint, Fluent $column)
{ {
foreach ($this->preModifiers as $preModifier) foreach ($this->preModifiers as $preModifier) {
{ if (method_exists($this, $method = "modify{$preModifier}")) {
if (method_exists($this, $method = "modify{$preModifier}"))
{
$sql .= $this->{$method}($blueprint, $column); $sql .= $this->{$method}($blueprint, $column);
} }
} }
@ -145,41 +152,41 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a create table command. * Compile a create table command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileAdd(Blueprint $blueprint, Fluent $command) public function compileAdd(Blueprint $blueprint, Fluent $command)
{ {
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
$columns = $this->prefixArray('add', $this->getColumns($blueprint));
$statements = [];
$columns = $this->prefixArray('add', $this->getColumns($blueprint)); foreach ($columns as $column) {
$statements[] = 'alter table '.$table.' '.$column;
}
foreach ($columns as $column) return $statements;
{
$statements[] = 'alter table '.$table.' '.$column;
}
return $statements;
} }
/** /**
* Compile a primary key command. * Compile a primary key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compilePrimary(Blueprint $blueprint, Fluent $command) public function compilePrimary(Blueprint $blueprint, Fluent $command)
{ {
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
$columns = $this->columnize($command->columns); $columns = $this->columnize($command->columns);
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Primary // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Primary
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index); $command->index = str_replace($schemaTable[0]."_", "", $command->index);
} }
@ -189,44 +196,40 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a foreign key command. * Compile a foreign key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileForeign(Blueprint $blueprint, Fluent $command) public function compileForeign(Blueprint $blueprint, Fluent $command)
{ {
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
$on = $this->wrapTable($command->on); $on = $this->wrapTable($command->on);
// We need to prepare several of the elements of the foreign key definition // We need to prepare several of the elements of the foreign key definition
// before we can create the SQL, such as wrapping the tables and convert // before we can create the SQL, such as wrapping the tables and convert
// an array of columns to comma-delimited strings for the SQL queries. // an array of columns to comma-delimited strings for the SQL queries.
$columns = $this->columnize($command->columns); $columns = $this->columnize($command->columns);
$onColumns = $this->columnize((array) $command->references); $onColumns = $this->columnize((array) $command->references);
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Foreign // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Foreign
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ 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} "; $sql = "alter table {$table} add constraint {$command->index} ";
$sql .= "foreign key ({$columns}) references {$on} ({$onColumns})"; $sql .= "foreign key ({$columns}) references {$on} ({$onColumns})";
// Once we have the basic foreign key creation statement constructed we can // Once we have the basic foreign key creation statement constructed we can
// build out the syntax for what should happen on an update or delete of // build out the syntax for what should happen on an update or delete of
// the affected columns, which will get something like "cascade", etc. // the affected columns, which will get something like "cascade", etc.
if ( ! is_null($command->onDelete)) if (!is_null($command->onDelete)) {
{
$sql .= " on delete {$command->onDelete}"; $sql .= " on delete {$command->onDelete}";
} }
if ( ! is_null($command->onUpdate)) if (!is_null($command->onUpdate)) {
{
$sql .= " on update {$command->onUpdate}"; $sql .= " on update {$command->onUpdate}";
} }
@ -236,20 +239,20 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a unique key command. * Compile a unique key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileUnique(Blueprint $blueprint, Fluent $command) public function compileUnique(Blueprint $blueprint, Fluent $command)
{ {
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
$columns = $this->columnize($command->columns); $columns = $this->columnize($command->columns);
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Unique // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Unique
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index); $command->index = str_replace($schemaTable[0]."_", "", $command->index);
} }
@ -259,28 +262,29 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a plain index key command. * Compile a plain index key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileIndex(Blueprint $blueprint, Fluent $command) public function compileIndex(Blueprint $blueprint, Fluent $command)
{ {
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
$columns = $this->columnize($command->columns); $columns = $this->columnize($command->columns);
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Index // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Index
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ 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}"; $sql = "create index {$command->index}";
if ($command->indexSystem)
{ if ($command->indexSystem) {
$sql .= " for system name {$command->indexSystem}"; $sql .= " for system name {$command->indexSystem}";
} }
$sql .= " on {$table}($columns)"; $sql .= " on {$table}($columns)";
//return "create index {$command->index} for system name on {$table}($columns)"; //return "create index {$command->index} for system name on {$table}($columns)";
@ -290,8 +294,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop table command. * Compile a drop table command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDrop(Blueprint $blueprint, Fluent $command) public function compileDrop(Blueprint $blueprint, Fluent $command)
@ -302,8 +307,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop table (if exists) command. * Compile a drop table (if exists) command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropIfExists(Blueprint $blueprint, Fluent $command) public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
@ -314,14 +320,14 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop column command. * Compile a drop column command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropColumn(Blueprint $blueprint, Fluent $command) public function compileDropColumn(Blueprint $blueprint, Fluent $command)
{ {
$columns = $this->prefixArray('drop', $this->wrapArray($command->columns)); $columns = $this->prefixArray('drop', $this->wrapArray($command->columns));
$table = $this->wrapTable($blueprint); $table = $this->wrapTable($blueprint);
return 'alter table '.$table.' '.implode(', ', $columns); return 'alter table '.$table.' '.implode(', ', $columns);
@ -330,8 +336,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop primary key command. * Compile a drop primary key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropPrimary(Blueprint $blueprint, Fluent $command) public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
@ -342,8 +349,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop unique key command. * Compile a drop unique key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropUnique(Blueprint $blueprint, Fluent $command) public function compileDropUnique(Blueprint $blueprint, Fluent $command)
@ -352,8 +360,8 @@ class DB2Grammar extends Grammar {
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Unique // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Unique
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index); $command->index = str_replace($schemaTable[0]."_", "", $command->index);
} }
@ -363,8 +371,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop index command. * Compile a drop index command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropIndex(Blueprint $blueprint, Fluent $command) public function compileDropIndex(Blueprint $blueprint, Fluent $command)
@ -373,8 +382,8 @@ class DB2Grammar extends Grammar {
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Index // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Index
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index); $command->index = str_replace($schemaTable[0]."_", "", $command->index);
} }
@ -384,8 +393,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a drop foreign key command. * Compile a drop foreign key command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileDropForeign(Blueprint $blueprint, Fluent $command) public function compileDropForeign(Blueprint $blueprint, Fluent $command)
@ -394,8 +404,8 @@ class DB2Grammar extends Grammar {
// Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Foreign // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Foreign
$schemaTable = explode(".", $table); $schemaTable = explode(".", $table);
if (count($schemaTable) > 1)
{ if (count($schemaTable) > 1) {
$command->index = str_replace($schemaTable[0]."_", "", $command->index); $command->index = str_replace($schemaTable[0]."_", "", $command->index);
} }
@ -405,8 +415,9 @@ class DB2Grammar extends Grammar {
/** /**
* Compile a rename table command. * Compile a rename table command.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command * @param \Illuminate\Support\Fluent $command
*
* @return string * @return string
*/ */
public function compileRename(Blueprint $blueprint, Fluent $command) public function compileRename(Blueprint $blueprint, Fluent $command)
@ -419,7 +430,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a char type. * Create the column definition for a char type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeChar(Fluent $column) protected function typeChar(Fluent $column)
@ -430,7 +442,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a string type. * Create the column definition for a string type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeString(Fluent $column) protected function typeString(Fluent $column)
@ -441,43 +454,50 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a text type. * Create the column definition for a text type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeText(Fluent $column) protected function typeText(Fluent $column)
{ {
$colLength = ($column->length ? $column->length : 16369); $colLength = ($column->length ? $column->length : 16369);
return "varchar($colLength)"; return "varchar($colLength)";
} }
/** /**
* Create the column definition for a medium text type. * Create the column definition for a medium text type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeMediumText(Fluent $column) protected function typeMediumText(Fluent $column)
{ {
$colLength = ($column->length ? $column->length : 16369); $colLength = ($column->length ? $column->length : 16369);
return "varchar($colLength)"; return "varchar($colLength)";
} }
/** /**
* Create the column definition for a long text type. * Create the column definition for a long text type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeLongText(Fluent $column) protected function typeLongText(Fluent $column)
{ {
$colLength = ($column->length ? $column->length : 16369); $colLength = ($column->length ? $column->length : 16369);
return "varchar($colLength)"; return "varchar($colLength)";
} }
/** /**
* Create the column definition for a big integer type. * Create the column definition for a big integer type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeBigInteger(Fluent $column) protected function typeBigInteger(Fluent $column)
@ -488,7 +508,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a integer type. * Create the column definition for a integer type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeInteger(Fluent $column) protected function typeInteger(Fluent $column)
@ -499,7 +520,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a small integer type. * Create the column definition for a small integer type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeSmallInteger(Fluent $column) protected function typeSmallInteger(Fluent $column)
@ -510,7 +532,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a numeric type. * Create the column definition for a numeric type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeNumeric(Fluent $column) protected function typeNumeric(Fluent $column)
@ -521,7 +544,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a float type. * Create the column definition for a float type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeFloat(Fluent $column) protected function typeFloat(Fluent $column)
@ -532,25 +556,24 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a double type. * Create the column definition for a double type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeDouble(Fluent $column) protected function typeDouble(Fluent $column)
{ {
if ($column->total && $column->places) if ($column->total && $column->places) {
{
return "double({$column->total}, {$column->places})"; return "double({$column->total}, {$column->places})";
} }
else
{ return 'double';
return 'double';
}
} }
/** /**
* Create the column definition for a decimal type. * Create the column definition for a decimal type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeDecimal(Fluent $column) protected function typeDecimal(Fluent $column)
@ -561,18 +584,29 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a boolean type. * Create the column definition for a boolean type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeBoolean(Fluent $column) protected function typeBoolean(Fluent $column)
{ {
return 'smallint constraint '.$column->type.'_'.$column->prefix.'_'.$column->name.' check('.$column->name.' in(0,1))'.(is_null($column->default) ? ' default 0' : ''); $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' : ''
);
} }
/** /**
* Create the column definition for an enum type. * Create the column definition for an enum type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeEnum(Fluent $column) protected function typeEnum(Fluent $column)
@ -583,13 +617,15 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a date type. * Create the column definition for a date type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeDate(Fluent $column) protected function typeDate(Fluent $column)
{ {
if ( ! $column->nullable) if (!$column->nullable) {
return 'date default current_date'; return 'date default current_date';
}
return 'date'; return 'date';
} }
@ -597,7 +633,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a date-time type. * Create the column definition for a date-time type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeDateTime(Fluent $column) protected function typeDateTime(Fluent $column)
@ -608,13 +645,15 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a time type. * Create the column definition for a time type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeTime(Fluent $column) protected function typeTime(Fluent $column)
{ {
if ( ! $column->nullable) if (!$column->nullable) {
return 'time default current_time'; return 'time default current_time';
}
return 'time'; return 'time';
} }
@ -622,13 +661,15 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a timestamp type. * Create the column definition for a timestamp type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeTimestamp(Fluent $column) protected function typeTimestamp(Fluent $column)
{ {
if ( ! $column->nullable) if (!$column->nullable) {
return 'timestamp default current_timestamp'; return 'timestamp default current_timestamp';
}
return 'timestamp'; return 'timestamp';
} }
@ -636,7 +677,8 @@ class DB2Grammar extends Grammar {
/** /**
* Create the column definition for a binary type. * Create the column definition for a binary type.
* *
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string * @return string
*/ */
protected function typeBinary(Fluent $column) protected function typeBinary(Fluent $column)
@ -647,8 +689,9 @@ class DB2Grammar extends Grammar {
/** /**
* Get the SQL for a nullable column modifier. * Get the SQL for a nullable column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyNullable(Blueprint $blueprint, Fluent $column) protected function modifyNullable(Blueprint $blueprint, Fluent $column)
@ -659,122 +702,140 @@ class DB2Grammar extends Grammar {
/** /**
* Get the SQL for a default column modifier. * Get the SQL for a default column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyDefault(Blueprint $blueprint, Fluent $column) protected function modifyDefault(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->default)) if (!is_null($column->default)) {
{
return " default ".$this->getDefaultValue($column->default); return " default ".$this->getDefaultValue($column->default);
} }
return null;
} }
/** /**
* Get the SQL for an auto-increment column modifier. * Get the SQL for an auto-increment column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
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 ' as identity constraint ' . $blueprint->getTable() . '_' . $column->name . '_primary primary key';
} }
return null;
} }
/** /**
* Get the SQL for an "before" column modifier. * Get the SQL for an "before" column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyBefore(Blueprint $blueprint, Fluent $column) protected function modifyBefore(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->before)) if (!is_null($column->before)) {
{
return ' before '.$this->wrap($column->before); return ' before '.$this->wrap($column->before);
} }
return null;
} }
/** /**
* Get the SQL for an "for column" column modifier. * Get the SQL for an "for column" column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyForColumn(Blueprint $blueprint, Fluent $column) protected function modifyForColumn(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->forColumn)) if (!is_null($column->forColumn)) {
{
return ' for column '.$this->wrap($column->forColumn); return ' for column '.$this->wrap($column->forColumn);
} }
return null;
} }
/** /**
* Get the SQL for a "generated" column modifier. * Get the SQL for a "generated" column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyGenerated(Blueprint $blueprint, Fluent $column) protected function modifyGenerated(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->generated)) 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;
} }
/** /**
* Get the SQL for a "startWith" column modifier. * Get the SQL for a "startWith" column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyStartWith(Blueprint $blueprint, Fluent $column) protected function modifyStartWith(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->startWith)) if (!is_null($column->startWith)) {
{
return ' (start with '.$column->startWith.')'; return ' (start with '.$column->startWith.')';
} }
return null;
} }
/** /**
* Get the SQL for an "implicitly hidden" column modifier. * Get the SQL for an "implicitly hidden" column modifier.
* *
* @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column * @param \Illuminate\Support\Fluent $column
*
* @return string|null * @return string|null
*/ */
protected function modifyImplicitlyHidden(Blueprint $blueprint, Fluent $column) protected function modifyImplicitlyHidden(Blueprint $blueprint, Fluent $column)
{ {
if ( ! is_null($column->implicitlyHidden)) if (!is_null($column->implicitlyHidden)) {
{
return ' implicitly hidden'; return ' implicitly hidden';
} }
return null;
} }
/** /**
* Format a value so that it can be used in "default" clauses. * Format a value so that it can be used in "default" clauses.
* *
* @param mixed $value * @param mixed $value
*
* @return string * @return string
*/ */
protected function getDefaultValue($value) protected function getDefaultValue($value)
{ {
if ($value instanceof Expression if (
$value instanceof Expression
|| is_bool($value) || is_bool($value)
|| is_numeric($value)) return $value; || is_numeric($value)
) {
return $value;
}
return "'".strval($value)."'"; return "'".strval($value)."'";
} }
} }