diff --git a/README.md b/README.md index fe99a41..84ebbc7 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Simply add this code at the end of your ``app/config/database.php`` file: */ 'ibmi' => [ - 'driver' => 'odbc' / 'ibm', + 'driver' => 'odbc' / 'ibm' / 'odbczos', 'driverName' => '{IBM i Access ODBC Driver}' / '{iSeries Access ODBC Driver}', // General settings 'host' => 'server', @@ -67,6 +67,7 @@ Simply add this code at the end of your ``app/config/database.php`` file: 'database' => 'WRKRDBDIRE entry', 'prefix' => '', 'schema' => 'default schema', + 'port' => 50000, 'signon' => 3, 'ssl' => 0, 'commitMode' => 2, diff --git a/src/Connectors/IBMConnector.php b/src/Connectors/IBMConnector.php index a86a87a..6e65eb2 100644 --- a/src/Connectors/IBMConnector.php +++ b/src/Connectors/IBMConnector.php @@ -1,84 +1,103 @@ getDsn($config); - $options = [ - PDO::I5_ATTR_DBC_SYS_NAMING => false, - PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT, - PDO::I5_ATTR_JOB_SORT => false + \PDO::I5_ATTR_DBC_SYS_NAMING => false, + \PDO::I5_ATTR_COMMIT => \PDO::I5_TXN_NO_COMMIT, + \PDO::I5_ATTR_JOB_SORT => false, ]; // Naming mode switch ($config['naming']) { case 1: - $options[PDO::I5_ATTR_DBC_SYS_NAMING] = true; + $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true; + break; case 0: default: - $options[PDO::I5_ATTR_DBC_SYS_NAMING] = false; + $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false; + break; } // Isolation mode switch ($config['commitMode']) { case 1: - $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_COMMITTED; + $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_COMMITTED; + break; case 2: - $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_UNCOMMITTED; + $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_UNCOMMITTED; + break; case 3: - $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_REPEATABLE_READ; + $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_REPEATABLE_READ; + break; case 4: - $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_SERIALIZABLE; + $options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_SERIALIZABLE; + break; case 0: default: $options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_NO_COMMIT; + break; } // Job sort mode switch ($config['jobSort']) { case 1: - $options[PDO::I5_ATTR_DBC_SYS_NAMING] = true; + $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true; + break; case 0: default: - $options[PDO::I5_ATTR_DBC_SYS_NAMING] = false; + $options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false; + break; } $options = $this->getOptions($config) + $options; - $connection = $this->createConnection($dsn, $config, $options); - if (isset($config['schema'])) - { + if (isset($config['schema'])) { $schema = $config['schema']; - $connection->prepare("set schema $schema")->execute(); + $connection->prepare("set schema $schema")->execute(); } return $connection; } - protected function getDsn(array $config) { - extract($config); - $dsn = "ibm:$database"; + /** + * @param array $config + * + * @return string + */ + protected function getDsn(array $config) + { + $dsn = "ibm:{$config['database']}"; + return $dsn; } - } diff --git a/src/Connectors/ODBCConnector.php b/src/Connectors/ODBCConnector.php index 3e535ac..5fabd22 100644 --- a/src/Connectors/ODBCConnector.php +++ b/src/Connectors/ODBCConnector.php @@ -1,99 +1,86 @@ getDsn($config); - $options = $this->getOptions($config); - $connection = $this->createConnection($dsn, $config, $options); - if (isset($config['schema'])) - { + if (isset($config['schema'])) { $schema = $config['schema']; - $connection->prepare("set schema $schema")->execute(); + $connection->prepare('set schema '.$schema)->execute(); } 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:" - // General settings - . "DRIVER=$driverName;" - . "SYSTEM=$host;" - . "UserID=$username;" - . "Password=$password;" - //Server settings - . "DATABASE=$database;" - . "SIGNON=$signon;" - . "SSL=$ssl;" - . "CommitMode=$commitMode;" - . "ConnectionType=$connectionType;" - . "DefaultLibraries=$defaultLibraries;" - . "Naming=$naming;" - . "UNICODESQL=$unicodeSql;" - // Format settings - . "DateFormat=$dateFormat;" - . "DateSeperator=$dateSeperator;" - . "Decimal=$decimal;" - . "TimeFormat=$timeFormat;" - . "TimeSeparator=$timeSeparator;" - // Performances settings - . "BLOCKFETCH=$blockFetch;" - . "BlockSizeKB=$blockSizeKB;" - . "AllowDataCompression=$allowDataCompression;" - . "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;" - ; + $dsnConfig = [ + // General settings + $config['driverName'], $config['host'], $config['username'], $config['password'], + //Server settings + $config['database'], $config['signon'], $config['ssl'], $config['commitMode'], $config['connectionType'], + $config['defaultLibraries'], $config['naming'], $config['unicodeSql'], + // Format settings + $config['dateFormat'], $config['dateSeperator'], $config['decimal'], $config['timeFormat'], + $config['timeSeparator'], + // Performances settings + $config['blockFetch'], $config['blockSizeKB'], $config['allowDataCompression'], $config['concurrency'], + $config['lazyClose'], $config['maxFieldLength'], $config['prefetch'], $config['queryTimeout'], + // Modules settings + $config['defaultPkgLibrary'], $config['defaultPackage'], $config['extendedDynamic'], + // Diagnostic settings + $config['QAQQINILibrary'], $config['sqDiagCode'], + // Sort settings + $config['languageId'], $config['sortTable'], $config['sortSequence'], $config['sortWeight'], + // Conversion settings + $config['allowUnsupportedChar'], $config['ccsid'], $config['graphic'], $config['forceTranslation'], + // Other settings + $config['allowProcCalls'], $config['DB2SqlStates'], $config['debug'], $config['trueAutoCommit'], + $config['catalogOptions'], $config['libraryView'], $config['ODBCRemarks'], $config['searchPattern'], + $config['translationDLL'], $config['translationOption'], $config['maxTraceSize'], + $config['multipleTraceFiles'], $config['trace'], $config['traceFilename'], $config['extendedColInfo'], + ]; - return $dsn; + return sprintf(implode(';', $dsnParts), ...$dsnConfig); } - } diff --git a/src/Connectors/ODBCZOSConnector.php b/src/Connectors/ODBCZOSConnector.php new file mode 100644 index 0000000..0a36d6a --- /dev/null +++ b/src/Connectors/ODBCZOSConnector.php @@ -0,0 +1,40 @@ +schemaGrammar)) { $this->useDefaultSchemaGrammar(); } + if (is_null($this->schemaGrammar)) { + $this->useDefaultSchemaGrammar(); + } return new Builder($this); } /** - * @return Query\Grammars\Grammar + * @return \Illuminate\Database\Grammar */ protected function getDefaultQueryGrammar() { - return $this->withTablePrefix(new QueryGrammar); + return $this->withTablePrefix(new QueryGrammar()); } /** * Default grammar for specified Schema - * @return Schema\Grammars\Grammar + * + * @return \Illuminate\Database\Grammar */ protected function getDefaultSchemaGrammar() { - - return $this->withTablePrefix(new SchemaGrammar); + return $this->withTablePrefix(new SchemaGrammar()); } /** - * Get the default post processor instance. - * - * @return \Illuminate\Database\Query\Processors\PostgresProcessor - */ + * Get the default post processor instance. + * + * @return \Illuminate\Database\Query\Processors\PostgresProcessor + */ protected function getDefaultPostProcessor() { - return new DB2Processor; - } + if ($this->config['driver'] == 'odbczos') { + return new DB2ZOSProcessor(); + } + return new DB2Processor(); + } } diff --git a/src/DB2ServiceProvider.php b/src/DB2ServiceProvider.php index 61e2d5a..f934a3a 100644 --- a/src/DB2ServiceProvider.php +++ b/src/DB2ServiceProvider.php @@ -1,15 +1,20 @@ 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 - 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']) ) - { + // Extend the connections with pdo_odbc and pdo_ibm drivers + 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'])) { continue; } - //Create a connector - $this->app['db']->extend($conn, function($config) - { + // Create a connector + $this->app['db']->extend($conn, function ($config) { switch ($config['driver']) { case 'odbc': $connector = new ODBCConnector(); + + break; + case 'odbczos': + $connector = new ODBCZOSConnector(); + break; case 'ibm': - $connector = new IBMConnector(); - break; default: + $connector = new IBMConnector(); + break; } + $db2Connection = $connector->connect($config); + return new DB2Connection($db2Connection, $config["database"], $config["prefix"], $config); }); - } - } /** @@ -80,5 +90,4 @@ class DB2ServiceProvider extends ServiceProvider { { return []; } - } diff --git a/src/Query/Grammars/DB2Grammar.php b/src/Query/Grammars/DB2Grammar.php index 0eecfc7..752c806 100644 --- a/src/Query/Grammars/DB2Grammar.php +++ b/src/Query/Grammars/DB2Grammar.php @@ -1,30 +1,39 @@ columns)) $query->columns = array('*'); + if (is_null($query->columns)) { + $query->columns = array('*'); + } $components = $this->compileComponents($query); // 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 // other database systems but is necessary for implementing features. - if ($query->offset > 0) - { + if ($query->offset > 0) { return $this->compileAnsiOffset($query, $components); } return $this->concatenate($components); } - /** + /** * Create a full ANSI offset clause for the query. * - * @param Illuminate\Database\Query\Builder $query - * @param array $components + * @param \Illuminate\Database\Query\Builder $query + * @param array $components + * * @return string */ protected function compileAnsiOffset(Builder $query, $components) @@ -67,8 +79,7 @@ class DB2Grammar extends Grammar // 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 // 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'; } @@ -79,12 +90,7 @@ class DB2Grammar extends Grammar // the "select" that will give back the row numbers on each of the records. $orderings = $components['orders']; - $columns = (!empty($components['columns']) ? $components['columns'] . ', ': 'select'); - - if($columns == 'select *, ' && $query->from) - { - $columns = 'select ' . $this->tablePrefix . $query->from . '.*, '; - } + $columns = (!empty($components['columns']) ? $components['columns'].', ' : 'select'); $components['columns'] = $this->compileOver($orderings, $columns); @@ -108,7 +114,9 @@ class DB2Grammar extends Grammar /** * Compile the over statement for a table expression. * - * @param string $orderings + * @param string $orderings + * @param $columns + * * @return string */ protected function compileOver($orderings, $columns) @@ -116,12 +124,16 @@ class DB2Grammar extends Grammar return "{$columns} row_number() over ({$orderings}) as row_num"; } + /** + * @param $query + * + * @return string + */ protected function compileRowConstraint($query) { $start = $query->offset + 1; - if ($query->limit > 0) - { + if ($query->limit > 0) { $finish = $query->offset + $query->limit; return "between {$start} and {$finish}"; @@ -130,11 +142,12 @@ class DB2Grammar extends Grammar return ">= {$start}"; } - /** + /** * Compile a common table expression for a query. * - * @param string $sql - * @param string $constraint + * @param string $sql + * @param string $constraint + * * @return string */ protected function compileTableExpression($sql, $constraint) @@ -145,8 +158,9 @@ class DB2Grammar extends Grammar /** * Compile the "offset" portions of the query. * - * @param Illuminate\Database\Query\Builder $query - * @param int $offset + * @param \Illuminate\Database\Query\Builder $query + * @param int $offset + * * @return string */ protected function compileOffset(Builder $query, $offset) @@ -163,5 +177,4 @@ class DB2Grammar extends Grammar { return 'Y-m-d H:i:s.u'; } - } diff --git a/src/Query/Processors/DB2Processor.php b/src/Query/Processors/DB2Processor.php index 571bfa0..b107d50 100644 --- a/src/Query/Processors/DB2Processor.php +++ b/src/Query/Processors/DB2Processor.php @@ -1,17 +1,24 @@ columnize($sequence); } - $sql = 'select ' . $sequenceStr . ' from new table (' . $sql; + + $sql = 'select '.$sequenceStr.' from new table ('.$sql; $sql .= ')'; $results = $query->getConnection()->select($sql, $values); - if (is_array($sequence)) - { + + if (is_array($sequence)) { return array_values((array) $results[0]); - } - else - { + } else { $result = (array) $results[0]; $id = $result[$sequenceStr]; + return is_numeric($id) ? (int) $id : $id; } } - } diff --git a/src/Query/Processors/DB2ZOSProcessor.php b/src/Query/Processors/DB2ZOSProcessor.php new file mode 100644 index 0000000..59ae6cc --- /dev/null +++ b/src/Query/Processors/DB2ZOSProcessor.php @@ -0,0 +1,49 @@ +columnize($sequence); + } + + $sqlStr = 'select %s from final table (%s)'; + + $finalSql = sprintf($sqlStr, $sequenceStr, $sql); + $results = $query->getConnection()->select($finalSql, $values); + + if (is_array($sequence)) { + return array_values((array) $results[0]); + } else { + $result = (array) $results[0]; + $id = $result[strtolower($sequenceStr)]; + + return is_numeric($id) ? (int) $id : $id; + } + } +} diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index b7a4907..5cbe049 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -1,12 +1,18 @@ createIndexName($type, $columns); + return $this->addCommand($type, compact('index', 'indexSystem', 'columns')); - break; default: break; } @@ -53,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 // convention of the table name, followed by the columns, followed by an // 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); } @@ -64,7 +72,8 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint { /** * Create a new boolean column on the table. * - * @param string $column + * @param string $column + * * @return \Illuminate\Support\Fluent */ public function boolean($column) @@ -72,24 +81,25 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint { $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 $schemaTable = explode(".", $this->table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $prefix = $schemaTable[1]; } + return $this->addColumn('boolean', $column, ['prefix' => $prefix]); } /** * Create a new numeric column on the table. * - * @param string $column - * @param int $total - * @param int $places + * @param string $column + * @param int $total + * @param int $places + * * @return \Illuminate\Support\Fluent */ public function numeric($column, $total = 8, $places = 2) { return $this->addColumn('numeric', $column, compact('total', 'places')); } - -} \ No newline at end of file +} diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index ded353f..cfb6210 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -1,29 +1,34 @@ grammar->compileTableExists(); - $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $schema = $schemaTable[0]; $table = $this->connection->getTablePrefix().$schemaTable[1]; - } - else - { + } else { $schema = $this->connection->getDefaultSchema(); $table = $this->connection->getTablePrefix().$table; } @@ -34,17 +39,15 @@ class Builder extends \Illuminate\Database\Schema\Builder { /** * Get the column listing for a given table. * - * @param string $table + * @param string $table + * * @return array */ public function getColumnListing($table) { $sql = $this->grammar->compileColumnExists(); - $database = $this->connection->getDatabaseName(); - $table = $this->connection->getTablePrefix().$table; - $results = $this->connection->select($sql, [$database, $table]); 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. * - * @param \Cooperl\Database\DB2\Schema\Blueprint $blueprint - * @return void + * @param Blueprint $blueprint */ protected function build(Blueprint $blueprint) { $schemaTable = explode(".", $blueprint->getTable()); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $this->connection->setCurrentSchema($schemaTable[0]); } $blueprint->build($this->connection, $this->grammar); - $this->connection->resetCurrentSchema(); } /** * Create a new command set with a Closure. * - * @param string $table - * @param \Closure $callback + * @param string $table + * @param \Closure $callback + * * @return \Cooperl\Database\DB2\Schema\Blueprint */ protected function createBlueprint($table, Closure $callback = null) { - if (isset($this->resolver)) - { + if (isset($this->resolver)) { return call_user_func($this->resolver, $table, $callback); } return new \Cooperl\Database\DB2\Schema\Blueprint($table, $callback); } - } diff --git a/src/Schema/Grammars/DB2Grammar.php b/src/Schema/Grammars/DB2Grammar.php index 18d0a95..91d7f66 100644 --- a/src/Schema/Grammars/DB2Grammar.php +++ b/src/Schema/Grammars/DB2Grammar.php @@ -1,14 +1,15 @@ getColumns($blueprint)); - $sql = 'create table '.$this->wrapTable($blueprint); - if (isset($blueprint->systemName)) - { + if (isset($blueprint->systemName)) { $sql .= ' for system name '.$blueprint->systemName; } @@ -86,28 +93,29 @@ class DB2Grammar extends Grammar { /** * Compile a label command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command - * @param \Illuminate\Database\Connection $connection + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Connection $connection + * * @return string */ 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. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * * @return array */ protected function getColumns(Blueprint $blueprint) { $columns = []; - foreach ($blueprint->getColumns() as $column) - { + foreach ($blueprint->getColumns() as $column) { // 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 // 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. * - * @param string $sql - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param string $sql + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function addPreModifiers($sql, Blueprint $blueprint, Fluent $column) { - foreach ($this->preModifiers as $preModifier) - { - if (method_exists($this, $method = "modify{$preModifier}")) - { + foreach ($this->preModifiers as $preModifier) { + if (method_exists($this, $method = "modify{$preModifier}")) { $sql .= $this->{$method}($blueprint, $column); } } @@ -145,41 +152,41 @@ class DB2Grammar extends Grammar { /** * Compile a create table command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ 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) - { - $statements[] = 'alter table '.$table.' '.$column; - } - - return $statements; + return $statements; } /** * Compile a primary key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compilePrimary(Blueprint $blueprint, Fluent $command) { $table = $this->wrapTable($blueprint); - $columns = $this->columnize($command->columns); // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Primary $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } @@ -189,44 +196,40 @@ class DB2Grammar extends Grammar { /** * Compile a foreign key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileForeign(Blueprint $blueprint, Fluent $command) { $table = $this->wrapTable($blueprint); - $on = $this->wrapTable($command->on); // 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 // an array of columns to comma-delimited strings for the SQL queries. $columns = $this->columnize($command->columns); - $onColumns = $this->columnize((array) $command->references); // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Foreign $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } $sql = "alter table {$table} add constraint {$command->index} "; - $sql .= "foreign key ({$columns}) references {$on} ({$onColumns})"; // 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 // 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}"; } - if ( ! is_null($command->onUpdate)) - { + if (!is_null($command->onUpdate)) { $sql .= " on update {$command->onUpdate}"; } @@ -236,20 +239,20 @@ class DB2Grammar extends Grammar { /** * Compile a unique key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileUnique(Blueprint $blueprint, Fluent $command) { $table = $this->wrapTable($blueprint); - $columns = $this->columnize($command->columns); // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Unique $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } @@ -259,28 +262,29 @@ class DB2Grammar extends Grammar { /** * Compile a plain index key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileIndex(Blueprint $blueprint, Fluent $command) { $table = $this->wrapTable($blueprint); - $columns = $this->columnize($command->columns); // Aucune utilité d'avoir le nom du schéma dans le nom de la contrainte Index $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } $sql = "create index {$command->index}"; - if ($command->indexSystem) - { + + if ($command->indexSystem) { $sql .= " for system name {$command->indexSystem}"; } + $sql .= " 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. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileDrop(Blueprint $blueprint, Fluent $command) @@ -302,8 +307,9 @@ class DB2Grammar extends Grammar { /** * Compile a drop table (if exists) command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) @@ -314,14 +320,14 @@ class DB2Grammar extends Grammar { /** * Compile a drop column command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) { $columns = $this->prefixArray('drop', $this->wrapArray($command->columns)); - $table = $this->wrapTable($blueprint); return 'alter table '.$table.' '.implode(', ', $columns); @@ -330,8 +336,9 @@ class DB2Grammar extends Grammar { /** * Compile a drop primary key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileDropPrimary(Blueprint $blueprint, Fluent $command) @@ -342,8 +349,9 @@ class DB2Grammar extends Grammar { /** * Compile a drop unique key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ 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 $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } @@ -363,8 +371,9 @@ class DB2Grammar extends Grammar { /** * Compile a drop index command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ 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 $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } @@ -384,8 +393,9 @@ class DB2Grammar extends Grammar { /** * Compile a drop foreign key command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ 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 $schemaTable = explode(".", $table); - if (count($schemaTable) > 1) - { + + if (count($schemaTable) > 1) { $command->index = str_replace($schemaTable[0]."_", "", $command->index); } @@ -405,8 +415,9 @@ class DB2Grammar extends Grammar { /** * Compile a rename table command. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * * @return string */ public function compileRename(Blueprint $blueprint, Fluent $command) @@ -419,7 +430,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a char type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeChar(Fluent $column) @@ -430,7 +442,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a string type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeString(Fluent $column) @@ -441,43 +454,50 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a text type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeText(Fluent $column) { $colLength = ($column->length ? $column->length : 16369); + return "varchar($colLength)"; } /** * Create the column definition for a medium text type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeMediumText(Fluent $column) { $colLength = ($column->length ? $column->length : 16369); + return "varchar($colLength)"; } /** * Create the column definition for a long text type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeLongText(Fluent $column) { $colLength = ($column->length ? $column->length : 16369); + return "varchar($colLength)"; } /** * Create the column definition for a big integer type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeBigInteger(Fluent $column) @@ -488,7 +508,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a integer type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeInteger(Fluent $column) @@ -499,7 +520,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a small integer type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeSmallInteger(Fluent $column) @@ -510,7 +532,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a numeric type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeNumeric(Fluent $column) @@ -521,7 +544,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a float type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeFloat(Fluent $column) @@ -532,25 +556,24 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a double type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeDouble(Fluent $column) { - if ($column->total && $column->places) - { + if ($column->total && $column->places) { return "double({$column->total}, {$column->places})"; } - else - { - return 'double'; - } + + return 'double'; } /** * Create the column definition for a decimal type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeDecimal(Fluent $column) @@ -561,18 +584,29 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a boolean type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ 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. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeEnum(Fluent $column) @@ -583,13 +617,15 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a date type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeDate(Fluent $column) { - if ( ! $column->nullable) + if (!$column->nullable) { return 'date default current_date'; + } return 'date'; } @@ -597,7 +633,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a date-time type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeDateTime(Fluent $column) @@ -608,13 +645,15 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a time type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeTime(Fluent $column) { - if ( ! $column->nullable) + if (!$column->nullable) { return 'time default current_time'; + } return 'time'; } @@ -622,13 +661,15 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a timestamp type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeTimestamp(Fluent $column) { - if ( ! $column->nullable) + if (!$column->nullable) { return 'timestamp default current_timestamp'; + } return 'timestamp'; } @@ -636,7 +677,8 @@ class DB2Grammar extends Grammar { /** * Create the column definition for a binary type. * - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Support\Fluent $column + * * @return string */ protected function typeBinary(Fluent $column) @@ -647,8 +689,9 @@ class DB2Grammar extends Grammar { /** * Get the SQL for a nullable column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) @@ -659,122 +702,140 @@ class DB2Grammar extends Grammar { /** * Get the SQL for a default column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) { - if ( ! is_null($column->default)) - { + if (!is_null($column->default)) { return " default ".$this->getDefaultValue($column->default); } + + return null; } /** * Get the SQL for an auto-increment column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ 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'; + if (in_array($column->type, $this->serials) && $column->autoIncrement) { + return ' as identity constraint '.$blueprint->getTable().'_'.$column->name.'_primary primary key'; } + + return null; } /** * Get the SQL for an "before" column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ protected function modifyBefore(Blueprint $blueprint, Fluent $column) { - if ( ! is_null($column->before)) - { + if (!is_null($column->before)) { return ' before '.$this->wrap($column->before); } + + return null; } /** * Get the SQL for an "for column" column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ 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 null; } /** * Get the SQL for a "generated" column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ 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 null; } /** * Get the SQL for a "startWith" column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ protected function modifyStartWith(Blueprint $blueprint, Fluent $column) { - if ( ! is_null($column->startWith)) - { + if (!is_null($column->startWith)) { return ' (start with '.$column->startWith.')'; } + + return null; } /** * Get the SQL for an "implicitly hidden" column modifier. * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * * @return string|null */ protected function modifyImplicitlyHidden(Blueprint $blueprint, Fluent $column) { - if ( ! is_null($column->implicitlyHidden)) - { + if (!is_null($column->implicitlyHidden)) { return ' implicitly hidden'; } + + return null; } /** * Format a value so that it can be used in "default" clauses. * - * @param mixed $value + * @param mixed $value + * * @return string */ protected function getDefaultValue($value) { - if ($value instanceof Expression + if ( + $value instanceof Expression || is_bool($value) - || is_numeric($value)) return $value; + || is_numeric($value) + ) { + return $value; + } return "'".strval($value)."'"; } - } diff --git a/src/config/config.php b/src/config/config.php index 6957b58..ee2b0ff 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -61,7 +61,7 @@ return [ 'connections' => [ 'ibmi' => [ - 'driver' => 'odbc' / 'ibm', + 'driver' => 'odbc' / 'ibm' / 'odbczos', 'driverName' => '{IBM i Access ODBC Driver}' / '{iSeries Access ODBC Driver}', // General settings 'host' => 'server', @@ -71,6 +71,7 @@ return [ 'database' => 'WRKRDBDIRE entry', 'prefix' => '', 'schema' => 'default schema', + 'port' => 50000, 'signon' => 3, 'ssl' => 0, 'commitMode' => 2,