Skip to content

Commit

Permalink
add dropColumn schema migration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperl22 committed Jul 16, 2019
1 parent 1f777ab commit d1dd7a6
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 18 deletions.
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 d1dd7a6

Please sign in to comment.