Skip to content

Commit

Permalink
Merge pull request #1 from cooperl22/master
Browse files Browse the repository at this point in the history
update from upstream
  • Loading branch information
darit authored Feb 19, 2020
2 parents f2b749c + de8965a commit 978bbcd
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ It provides DB2 Connection by extending the Illuminate Database component of the
Add laravel-db2 to your composer.json file:
```
"require": {
"cooperl/laravel-db2": "~5.8"
"cooperl/laravel-db2": "^6.0"
}
```
Use [composer](http://getcomposer.org) to install this package.
Use [composer](https://getcomposer.org) to install this package.
```
$ composer update
```
Expand All @@ -30,7 +30,7 @@ There are two ways to configure laravel-db2. You can choose the most convenient

Please check appropriate specific DSN parameters for your connection.
For instance here are the ODBC keywords for IBMi
https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_73/rzaik/connectkeywords.htm
https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_74/rzaik/connectkeywords.htm

If you encounter issues with char fields containing characters outside the invariant character set (for example: "ü") please see : https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014094907
For PHP applications using the UTF8 locale the workaround to prevent the extra garbage data is to set the following connection string keyword:
Expand Down Expand Up @@ -146,4 +146,4 @@ the same way as above

## Usage

Consult the [Laravel framework documentation](http://laravel.com/docs).
Consult the [Laravel framework documentation](https://laravel.com/docs).
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
}
],
"require": {
"php": ">=7.1.3",
"illuminate/database": "5.8.*"
"php": "^7.2",
"illuminate/database": "^6.0"
},
"require-dev": {
},
Expand Down
20 changes: 16 additions & 4 deletions src/DB2Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Cooperl\Database\DB2;

use Cooperl\Database\DB2\Query\Processors\DB2ZOSProcessor;
use PDO;

use Illuminate\Database\Connection;

use Cooperl\Database\DB2\Schema\Builder;
use Cooperl\Database\DB2\Query\Processors\DB2Processor;
use Cooperl\Database\DB2\Query\Processors\DB2ZOSProcessor;
use Cooperl\Database\DB2\Query\Grammars\DB2Grammar as QueryGrammar;
use Cooperl\Database\DB2\Schema\Grammars\DB2Grammar as SchemaGrammar;
use Cooperl\Database\DB2\Schema\Grammars\DB2ExpressCGrammar;

/**
* Class DB2Connection
Expand Down Expand Up @@ -67,14 +68,25 @@ public function resetCurrentSchema()
*/
public function setCurrentSchema($schema)
{
//$this->currentSchema = $schema;
$this->statement('SET SCHEMA ?', [strtoupper($schema)]);
}

/**
* Execute a system command on IBMi.
*
* @param $command
*
* @return string
*/
public function executeCommand($command)
{
$this->statement('CALL QSYS2.QCMDEXC(?)', [$command]);
}

/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\MySqlBuilder
* @return \Cooperl\Database\DB2\Schema\Builder
*/
public function getSchemaBuilder()
{
Expand Down Expand Up @@ -121,7 +133,7 @@ protected function getDefaultSchemaGrammar()
/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\PostgresProcessor
* @return \Cooperl\Database\DB2\Query\Processors\DB2Processor|\Cooperl\Database\DB2\Query\Processors\DB2ZOSProcessor
*/
protected function getDefaultPostProcessor()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Query/Grammars/DB2Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function compileAnsiOffset(Builder $query, $components)
}

$components['columns'] = $this->compileOver($orderings, $columns);

// if there are bindings in the order, we need to move them to the select since we are moving the parameter
// markers there with the OVER statement
if(isset($query->getRawBindings()['order'])){
Expand Down Expand Up @@ -198,7 +198,7 @@ public function compileExists(Builder $query)

$existsQuery->columns = [];

return $this->compileSelect($existsQuery->selectRaw('1')->limit(1));
return $this->compileSelect($existsQuery->selectRaw('1 exists')->limit(1));
}

/**
Expand Down
84 changes: 72 additions & 12 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Cooperl\Database\DB2\Schema;

use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Grammars\Grammar;

/**
* Class Blueprint
*
Expand All @@ -10,27 +13,62 @@
class Blueprint extends \Illuminate\Database\Schema\Blueprint
{

public function synchro($index, $masterizable = false)
/**
* The sequence number of reply list entries.
*
* @var int
*/
private $replyListSequenceNumber;

/**
* Get the sequence number of reply list entries.
*
* @return int
*/
public function getReplyListSequenceNumber()
{
return $this->replyListSequenceNumber;
}

$this->string('id_sync', 20)
->index($index);
$this->string('hashcode', 32);
/**
* Set the sequence number of reply list entries.
*
* @param int $replyListSequenceNumber
* @return void
*/
public function setReplyListSequenceNumber(int $replyListSequenceNumber)
{
return $this->replyListSequenceNumber = $replyListSequenceNumber;
}

if (true === $masterizable) {
$this->boolean('data_master')
->default(true);
}
/**
* Get the raw SQL statements for the blueprint.
*
* @param \Illuminate\Database\Connection $connection
* @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
* @return array
*/
public function toSql(Connection $connection, Grammar $grammar)
{
$this->addReplyListEntryCommands($connection);

return parent::toSql($connection, $grammar);
}

/**
* @param string $index
* Add the commands that are necessary to DROP and Rename statements on IBMi.
*
* @param \Illuminate\Database\Connection $connection
* @return void
*/
public function dropSynchro($index)
protected function addReplyListEntryCommands(Connection $connection)
{
$this->dropColumn('id_sync', 'hashcode');
$this->dropIndex($index);
if ($this->commandsNamed(['dropColumn', 'renameColumn'])->count() > 0) {
array_unshift($this->commands, $this->createCommand('addReplyListEntry'), $this->createCommand('changeJob'));
array_push($this->commands, $this->createCommand('removeReplyListEntry'));
}
}

/**
* Specify a system name for the table.
*
Expand Down Expand Up @@ -124,4 +162,26 @@ public function numeric($column, $total = 8, $places = 2)
{
return $this->addColumn('numeric', $column, compact('total', 'places'));
}

public function synchro($index, $masterizable = false)
{

$this->string('id_sync', 20)
->index($index);
$this->string('hashcode', 32);

if (true === $masterizable) {
$this->boolean('data_master')
->default(true);
}
}

/**
* @param string $index
*/
public function dropSynchro($index)
{
$this->dropColumn('id_sync', 'hashcode');
$this->dropIndex($index);
}
}
79 changes: 79 additions & 0 deletions src/Schema/Grammars/DB2Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,83 @@ protected function getDefaultValue($value)

return "'" . strval($value) . "'";
}

/**
* Compile a executeCommand command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
*
* @return string
*/
private function compileExecuteCommand(Blueprint $blueprint, Fluent $command)
{
return "CALL QSYS2.QCMDEXC('" . $command->command . "')";
}

/**
* Compile an addReplyListEntry command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @param \Illuminate\Database\Connection $connection
*
* @return string
*/
public function compileAddReplyListEntry(Blueprint $blueprint, Fluent $command, Connection $connection)
{
$sequenceNumberQuery = <<<EOT
with reply_list_info(sequence_number) as (
values(1)
union all
select sequence_number + 1
from reply_list_info
where sequence_number + 1 between 2 and 9999
)
select min(sequence_number) sequence_number
from reply_list_info
where not exists (
select 1
from qsys2.reply_list_info rli
where rli.sequence_number = reply_list_info.sequence_number
)
EOT;

$blueprint->setReplyListSequenceNumber($sequenceNumber = $connection->selectOne($sequenceNumberQuery)->sequence_number);
$command->command = "ADDRPYLE SEQNBR($sequenceNumber) MSGID(CPA32B2) RPY(''I'')";

return $this->compileExecuteCommand($blueprint, $command);
}

/**
* Compile a removeReplyListEntry command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
*
* @return string
*/
public function compileRemoveReplyListEntry(Blueprint $blueprint, Fluent $command)
{
$sequenceNumber = $blueprint->getReplyListSequenceNumber();
$command->command = "RMVRPYLE SEQNBR($sequenceNumber)";

return $this->compileExecuteCommand($blueprint, $command);
}

/**
* Compile a changeJob command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
*
* @return string
*/
public function compileChangeJob(Blueprint $blueprint, Fluent $command)
{
$command->command = 'CHGJOB INQMSGRPY(*SYSRPYL)';

return $this->compileExecuteCommand($blueprint, $command);
}

}

0 comments on commit 978bbcd

Please sign in to comment.