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,4 +1,5 @@
<?php

declare(strict_types=1);

/**
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;
} 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;
}
}

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);

Check warning on line 726 in src/Db/Adapter/AbstractAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/AbstractAdapter.php#L726

Added line #L726 was not covered by tests
} else {
$vals[] = $v;
}
}
}
}
$this->getConnection()->execute($sql, $vals);
}
}
/**
* 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;
} 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;
}
}

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

declare(strict_types=1);

/**
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;
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]);

Check warning on line 111 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L110-L111

Added lines #L110 - L111 were not covered by tests

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']));

Check warning on line 347 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L343-L347

Added lines #L343 - L347 were not covered by tests

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;

Check warning on line 658 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L658

Added line #L658 was not covered by tests
}
}

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 @@ -1312,4 +1315,82 @@

return parent::migrated($migration, $direction, $startTime, $endTime);
}
/**
* @inheritDoc
*/
public function insert(TableMetadata $table, array $row): void

Check warning on line 1321 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1321

Added line #L1321 was not covered by tests
{
$sql = $this->generateInsertSql($table, $row);

Check warning on line 1323 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1323

Added line #L1323 was not covered by tests

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

Check warning on line 1325 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1325

Added line #L1325 was not covered by tests


if ($this->isDryRunEnabled()) {
$this->io->out($sql);

Check warning on line 1329 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1328-L1329

Added lines #L1328 - L1329 were not covered by tests
} else {
$vals = [];
foreach ($row as $value) {
$placeholder = '?';
if ($value instanceof Literal || $value instanceof PhinxLiteral) {
$placeholder = (string)$value;

Check warning on line 1335 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1331-L1335

Added lines #L1331 - L1335 were not covered by tests
}
if ($placeholder === '?') {
$vals[] = $value;

Check warning on line 1338 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1337-L1338

Added lines #L1337 - L1338 were not covered by tests
}
}
$this->getConnection()->execute($sql, $vals);

Check warning on line 1341 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1341

Added line #L1341 was not covered by tests
}
}
/**
* @inheritDoc
*/
public function bulkinsert(TableMetadata $table, array $rows): void

Check warning on line 1347 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1347

Added line #L1347 was not covered by tests
{
$sql = $this->generateBulkInsertSql($table, $rows);

Check warning on line 1349 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1349

Added line #L1349 was not covered by tests

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

Check warning on line 1351 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1351

Added line #L1351 was not covered by tests

if ($this->isDryRunEnabled()) {
$this->io->out($sql);

Check warning on line 1354 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1353-L1354

Added lines #L1353 - L1354 were not covered by tests
} else {
$vals = [];
foreach ($rows as $row) {
foreach ($row as $v) {
$placeholder = '?';
if ($v instanceof Literal || $v instanceof PhinxLiteral) {
$placeholder = (string)$v;

Check warning on line 1361 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1356-L1361

Added lines #L1356 - L1361 were not covered by tests
}
if ($placeholder == '?') {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);

Check warning on line 1365 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1363-L1365

Added lines #L1363 - L1365 were not covered by tests
} else {
$vals[] = $v;

Check warning on line 1367 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1367

Added line #L1367 was not covered by tests
}
}
}
}
$this->getConnection()->execute($sql, $vals);

Check warning on line 1372 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1372

Added line #L1372 was not covered by tests
}
}
/**
* @param string $tableName Table name
* @param string $sql SQL statement
* @return string
*/
private function updateSQLForIdentityInsert(string $tableName, string $sql): string

Check warning on line 1380 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1380

Added line #L1380 was not covered by tests
{
$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;

Check warning on line 1392 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1382-L1392

Added lines #L1382 - L1392 were not covered by tests
}
return $sql;

Check warning on line 1394 in src/Db/Adapter/SqlserverAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/SqlserverAdapter.php#L1394

Added line #L1394 was not covered by tests
}
}
Loading
Loading