Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions src/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

Check failure on line 3 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Expected 0 lines before declare statement, found 1.

/**
* MIT License
Expand Down Expand Up @@ -602,6 +603,34 @@
* @inheritDoc
*/
public function insert(TableMetadata $table, array $row): void
{
$sql = $this->generateInsertSql($table, $row);

if ($this->isDryRunEnabled()) {
$this->io->out($sql);
} else {
$vals = [];
foreach ($row as $value) {
$placeholder = '?';
if ($value instanceof Literal || $value instanceof PhinxLiteral) {
$placeholder = (string)$value;
}
if ($placeholder === '?') {
$vals[] = $value;
}
}
$this->getConnection()->execute($sql, $vals);
}
}

/**
* Generates the SQL for an insert.
*
* @param \Migrations\Db\Table\Table $table The table to insert into
* @param array $row The row to insert
* @return string
*/
protected function generateInsertSql(TableMetadata $table, array $row): string
{
$sql = sprintf(
'INSERT INTO %s ',
Expand All @@ -618,22 +647,18 @@

if ($this->isDryRunEnabled()) {
$sql .= ' VALUES (' . implode(', ', array_map($this->quoteValue(...), $row)) . ');';
$this->io->out($sql);
return $sql;

Check failure on line 650 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Missing blank line before return statement
} else {
$values = [];
$vals = [];
foreach ($row as $value) {
$placeholder = '?';
if ($value instanceof Literal || $value instanceof PhinxLiteral) {
$placeholder = (string)$value;
}
$values[] = $placeholder;
if ($placeholder === '?') {
$vals[] = $value;
}
}
$sql .= ' VALUES (' . implode(',', $values) . ')';
$this->getConnection()->execute($sql, $vals);
return $sql;

Check failure on line 661 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Missing blank line before return statement
}
}

Expand Down Expand Up @@ -683,6 +708,39 @@
* @inheritDoc
*/
public function bulkinsert(TableMetadata $table, array $rows): void
{
$sql = $this->generateBulkInsertSql($table, $rows);

if ($this->isDryRunEnabled()) {
$this->io->out($sql);
} else {
$vals = [];
foreach ($rows as $row) {
foreach ($row as $v) {
$placeholder = '?';
if ($v instanceof Literal || $v instanceof PhinxLiteral) {
$placeholder = (string)$v;
}
if ($placeholder == '?') {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
}
}
}
}
$this->getConnection()->execute($sql, $vals);
}
}

Check failure on line 735 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Every function/method needs a newline afterwards
/**

Check failure on line 736 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Every function/method needs a newline before
* Generates the SQL for a bulk insert.
*
* @param \Migrations\Db\Table\Table $table The table to insert into
* @param array $rows The rows to insert
* @return string
*/
protected function generateBulkInsertSql(TableMetadata $table, array $rows): string
{
$sql = sprintf(
'INSERT INTO %s ',
Expand All @@ -698,9 +756,8 @@
return '(' . implode(', ', array_map($this->quoteValue(...), $row)) . ')';
}, $rows);
$sql .= implode(', ', $values) . ';';
$this->io->out($sql);
return $sql;

Check failure on line 759 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Missing blank line before return statement
} else {
$vals = [];
$queries = [];
foreach ($rows as $row) {
$values = [];
Expand All @@ -710,19 +767,12 @@
$placeholder = (string)$v;
}
$values[] = $placeholder;
if ($placeholder == '?') {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
}
}
}
$query = '(' . implode(', ', $values) . ')';
$queries[] = $query;
}
$sql .= implode(',', $queries);
$this->getConnection()->execute($sql, $vals);
return $sql;

Check failure on line 775 in src/Db/Adapter/AbstractAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Missing blank line before return statement
}
}

Expand Down
101 changes: 91 additions & 10 deletions src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

Check failure on line 3 in src/Db/Adapter/SqlserverAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Expected 0 lines before declare statement, found 1.

/**
* MIT License
Expand All @@ -17,6 +18,8 @@
use Migrations\Db\Table\Index;
use Migrations\Db\Table\Table;
use Migrations\MigrationInterface;
use Migrations\Db\Table\Table as TableMetadata;

Check failure on line 21 in src/Db/Adapter/SqlserverAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Use statements should be sorted alphabetically. The first wrong one is Migrations\Db\Table\Table.
use Phinx\Util\Literal as PhinxLiteral;

/**
* Migrations SqlServer Adapter.
Expand Down Expand Up @@ -104,8 +107,8 @@
// Handle id => "field_name" to support AUTO_INCREMENT
$column = new Column();
$column->setName($options['id'])
->setType('integer')
->setOptions(['identity' => true]);
->setType('integer')
->setOptions(['identity' => true]);

array_unshift($columns, $column);
if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) {
Expand Down Expand Up @@ -337,11 +340,11 @@

$column = new Column();
$column->setName($columnInfo['name'])
->setType($type)
->setNull($columnInfo['null'] !== 'NO')
->setDefault($this->parseDefault($columnInfo['default']))
->setIdentity($columnInfo['identity'] === '1')
->setComment($this->getColumnComment($columnInfo['table_name'], $columnInfo['name']));
->setType($type)
->setNull($columnInfo['null'] !== 'NO')
->setDefault($this->parseDefault($columnInfo['default']))
->setIdentity($columnInfo['identity'] === '1')
->setComment($this->getColumnComment($columnInfo['table_name'], $columnInfo['name']));

if (!empty($columnInfo['char_length'])) {
$column->setLimit((int)$columnInfo['char_length']);
Expand Down Expand Up @@ -652,7 +655,7 @@

foreach ($indexes as $name => $index) {
if ($name === $indexName) {
return true;
return true;
}
}

Expand Down Expand Up @@ -938,15 +941,15 @@
return ['name' => 'uniqueidentifier'];
case static::PHINX_TYPE_FILESTREAM:
return ['name' => 'varbinary', 'limit' => 'max'];
// Geospatial database types
// Geospatial database types
case static::PHINX_TYPE_GEOGRAPHY:
case static::PHINX_TYPE_POINT:
case static::PHINX_TYPE_LINESTRING:
case static::PHINX_TYPE_POLYGON:
// SQL Server stores all spatial data using a single data type.
// Specific types (point, polygon, etc) are set at insert time.
return ['name' => 'geography'];
// Geometry specific type
// Geometry specific type
case static::PHINX_TYPE_GEOMETRY:
return ['name' => 'geometry'];
default:
Expand Down Expand Up @@ -1311,5 +1314,83 @@
$endTime = str_replace(' ', 'T', $endTime);

return parent::migrated($migration, $direction, $startTime, $endTime);
}

Check failure on line 1317 in src/Db/Adapter/SqlserverAdapter.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Every function/method needs a newline afterwards
/**
* @inheritDoc
*/
public function insert(TableMetadata $table, array $row): void
{
$sql = $this->generateInsertSql($table, $row);

$sql = $this->updateSQLForIdentityInsert($table->getName(), $sql);


if ($this->isDryRunEnabled()) {
$this->io->out($sql);
} else {
$vals = [];
foreach ($row as $value) {
$placeholder = '?';
if ($value instanceof Literal || $value instanceof PhinxLiteral) {
$placeholder = (string)$value;
}
if ($placeholder === '?') {
$vals[] = $value;
}
}
$this->getConnection()->execute($sql, $vals);
}
}
/**
* @inheritDoc
*/
public function bulkinsert(TableMetadata $table, array $rows): void
{
$sql = $this->generateBulkInsertSql($table, $rows);

$sql = $this->updateSQLForIdentityInsert($table->getName(), $sql);

if ($this->isDryRunEnabled()) {
$this->io->out($sql);
} else {
$vals = [];
foreach ($rows as $row) {
foreach ($row as $v) {
$placeholder = '?';
if ($v instanceof Literal || $v instanceof PhinxLiteral) {
$placeholder = (string)$v;
}
if ($placeholder == '?') {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
}
}
}
}
$this->getConnection()->execute($sql, $vals);
}
}
/**
* @param string $tableName Table name
* @param string $sql SQL statement
* @return string
*/
private function updateSQLForIdentityInsert(string $tableName, string $sql): string
{
$options = $this->getOptions();
if (isset($options['identity_insert']) && $options['identity_insert'] == true) {
$identityInsertStart = sprintf(
'SET IDENTITY_INSERT %s ON',
$this->quoteTableName($tableName)
);
$identityInsertEnd = sprintf(
'SET IDENTITY_INSERT %s OFF',
$this->quoteTableName($tableName)
);
$sql = $identityInsertStart . ';' . PHP_EOL . $sql . ';' . PHP_EOL . $identityInsertEnd;
}
return $sql;
}
}
Loading
Loading