diff --git a/src/Oci8/Oci8Connection.php b/src/Oci8/Oci8Connection.php index fcd3ada9..78fa3876 100644 --- a/src/Oci8/Oci8Connection.php +++ b/src/Oci8/Oci8Connection.php @@ -2,15 +2,12 @@ namespace Yajra\Oci8; -use Doctrine\DBAL\Driver\OCI8\Driver as DoctrineDriver; -use Doctrine\DBAL\Version; use Illuminate\Database\Connection; use Illuminate\Database\Grammar; use Illuminate\Support\Str; use PDO; use PDOStatement; use Throwable; -use Yajra\Oci8\PDO\Oci8Driver; use Yajra\Oci8\Query\Grammars\OracleGrammar as QueryGrammar; use Yajra\Oci8\Query\OracleBuilder as QueryBuilder; use Yajra\Oci8\Query\Processors\OracleProcessor as Processor; @@ -187,16 +184,6 @@ public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS') return $this->setSessionVars($sessionVars); } - /** - * Get doctrine driver. - * - * @return \Doctrine\DBAL\Driver\OCI8\Driver|\Yajra\Oci8\PDO\Oci8Driver - */ - protected function getDoctrineDriver() - { - return class_exists(Version::class) ? new DoctrineDriver : new Oci8Driver(); - } - /** * Execute a PL/SQL Function and return its value. * Usage: DB::executeFunction('function_name', ['binding_1' => 'hi', 'binding_n' => diff --git a/src/Oci8/PDO/Oci8Driver.php b/src/Oci8/PDO/Oci8Driver.php deleted file mode 100644 index 4e8d7d26..00000000 --- a/src/Oci8/PDO/Oci8Driver.php +++ /dev/null @@ -1,19 +0,0 @@ -wrapTable($blueprint); $rs = []; - $rs[0] = 'alter table '.$table.' rename column '.$command->from.' to '.$command->to; + $rs[0] = 'alter table '.$table.' rename column '.$this->wrap($command->from).' to '.$this->wrap($command->to); - return (array) $rs; + return $rs; } /** @@ -905,4 +905,55 @@ protected function wrapValue($value) return parent::wrapValue($value); } + + /** + * Compile a change column command into a series of SQL statements. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Connection $connection + * @return array|string + * + * @throws \RuntimeException + */ + public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection) + { + $columns = []; + + foreach ($blueprint->getChangedColumns() as $column) { + $changes = [$this->getType($column).$this->modifyCollate($blueprint, $column)]; + + foreach ($this->modifiers as $modifier) { + if ($modifier === 'Collate') { + continue; + } + + if (method_exists($this, $method = "modify{$modifier}")) { + $constraints = (array) $this->{$method}($blueprint, $column); + + foreach ($constraints as $constraint) { + $changes[] = $constraint; + } + } + } + + $columns[] = 'modify '.$this->wrap($column).' '.implode(' ', array_filter(array_map('trim', $changes))); + } + + return 'alter table '.$this->wrapTable($blueprint).' '.implode(' ', $columns); + } + + /** + * Get the SQL for a collation column modifier. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * @return string|null + */ + protected function modifyCollate(Blueprint $blueprint, Fluent $column) + { + if (! is_null($column->collation)) { + return ' collate '.$this->wrapValue($column->collation); + } + } } diff --git a/tests/Database/Oci8SchemaGrammarTest.php b/tests/Database/Oci8SchemaGrammarTest.php index 3da378db..98ce7c01 100644 --- a/tests/Database/Oci8SchemaGrammarTest.php +++ b/tests/Database/Oci8SchemaGrammarTest.php @@ -250,6 +250,74 @@ public function testBasicAlterTable() $statements[0]); } + public function testAlterTableRenameColumn() + { + $blueprint = new Blueprint('users'); + $blueprint->renameColumn('email', 'email_address'); + + $conn = $this->getConnection(); + + $statements = $blueprint->toSql($conn, $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertEquals('alter table "USERS" rename column "EMAIL" to "EMAIL_ADDRESS"', $statements[0]); + } + + public function testAlterTableRenameMultipleColumns() + { + $blueprint = new Blueprint('users'); + $blueprint->renameColumn('email', 'email_address'); + $blueprint->renameColumn('address', 'address_1'); + + $conn = $this->getConnection(); + + $statements = $blueprint->toSql($conn, $this->getGrammar()); + + $this->assertCount(2, $statements); + $this->assertEquals('alter table "USERS" rename column "EMAIL" to "EMAIL_ADDRESS"', $statements[0]); + $this->assertEquals('alter table "USERS" rename column "ADDRESS" to "ADDRESS_1"', $statements[1]); + } + + public function testAlterTableModifyColumn() + { + $blueprint = new Blueprint('users'); + $blueprint->string('email')->change(); + + $conn = $this->getConnection(); + + $statements = $blueprint->toSql($conn, $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertEquals('alter table "USERS" modify "EMAIL" varchar2(255) not null', $statements[0]); + } + + public function testAlterTableModifyColumnWithCollate() + { + $blueprint = new Blueprint('users'); + $blueprint->string('email')->change()->collation('latin1_swedish_ci'); + + $conn = $this->getConnection(); + + $statements = $blueprint->toSql($conn, $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertEquals('alter table "USERS" modify "EMAIL" varchar2(255) collate "LATIN1_SWEDISH_CI" not null', $statements[0]); + } + + public function testAlterTableModifyMultipleColumns() + { + $blueprint = new Blueprint('users'); + $blueprint->string('email')->change(); + $blueprint->string('name')->change(); + + $conn = $this->getConnection(); + + $statements = $blueprint->toSql($conn, $this->getGrammar()); + + $this->assertCount(1, $statements); + $this->assertEquals('alter table "USERS" modify "EMAIL" varchar2(255) not null modify "NAME" varchar2(255) not null', $statements[0]); + } + public function testBasicAlterTableWithPrimary() { $blueprint = new Blueprint('users');