Skip to content

Commit 4572d73

Browse files
authored
Merge branch 'php-db:0.6.x' into platform-decorator-reinstate
2 parents a0fee77 + b40e849 commit 4572d73

21 files changed

Lines changed: 593 additions & 19 deletions

src/Adapter/Driver/Pdo/Result.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Result implements Iterator, ResultInterface
8181
/** @var string|int|false|null */
8282
protected $generatedValue;
8383

84-
protected Closure|int $rowCount;
84+
protected Closure|int|null $rowCount = null;
8585

8686
/**
8787
* Initialize
@@ -91,7 +91,7 @@ class Result implements Iterator, ResultInterface
9191
public function initialize(
9292
PDOStatement $resource,
9393
$generatedValue,
94-
Closure|int $rowCount = 0
94+
Closure|int|null $rowCount = null
9595
): ResultInterface&Result {
9696
$this->resource = $resource;
9797
$this->generatedValue = $generatedValue;
@@ -250,7 +250,6 @@ public function count()
250250
if (is_int($this->rowCount)) {
251251
return $this->rowCount;
252252
}
253-
/** @phpstan-ignore instanceof.alwaysTrue */
254253
if ($this->rowCount instanceof Closure) {
255254
$this->rowCount = (int) ($this->rowCount)();
256255
} else {

src/Adapter/Driver/Pdo/Statement.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use function is_array;
2323
use function is_int;
2424
use function ltrim;
25+
use function preg_match;
26+
use function sprintf;
2527

2628
class Statement implements StatementInterface, PdoDriverAwareInterface, ProfilerAwareInterface
2729
{
@@ -223,8 +225,19 @@ protected function bindParametersFromContainer(): void
223225
};
224226
}
225227

226-
// parameter is named or positional, value is reference
227-
$parameter = is_int($name) ? $name + 1 : ':' . ltrim($name, ':');
228+
if (is_int($name)) {
229+
$parameter = $name + 1;
230+
} else {
231+
if (! preg_match('/^:?[a-zA-Z0-9_]+$/', $name)) {
232+
throw new Exception\RuntimeException(sprintf(
233+
'The PDO param "%s" contains invalid characters.'
234+
. ' Only alphabetic characters, digits, and underscores (_) are allowed.',
235+
$name
236+
));
237+
}
238+
$parameter = ':' . ltrim($name, ':');
239+
}
240+
228241
$this->resource->bindParam($parameter, $value, $type);
229242
}
230243

src/Metadata/Source/AbstractSource.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,22 @@ public function getColumn(string $columnName, string $table, ?string $schema = n
263263

264264
$column = new ColumnObject($columnName, $table, $schema);
265265

266-
$column->setOrdinalPosition($info['ordinal_position']);
266+
$column->setOrdinalPosition($info['ordinal_position'] ? (int) $info['ordinal_position'] : null);
267267
$column->setColumnDefault($info['column_default']);
268268
$column->setIsNullable($info['is_nullable']);
269269
$column->setDataType($info['data_type']);
270-
$column->setCharacterMaximumLength($info['character_maximum_length']);
271-
$column->setCharacterOctetLength($info['character_octet_length']);
272-
$column->setNumericPrecision($info['numeric_precision']);
273-
$column->setNumericScale($info['numeric_scale']);
270+
$column->setCharacterMaximumLength(
271+
$info['character_maximum_length'] ? (int) $info['character_maximum_length'] : null
272+
);
273+
$column->setCharacterOctetLength(
274+
$info['character_octet_length'] ? (int) $info['character_octet_length'] : null
275+
);
276+
$column->setNumericPrecision(
277+
$info['numeric_precision'] ? (int) $info['numeric_precision'] : null
278+
);
279+
$column->setNumericScale(
280+
$info['numeric_scale'] ? (int) $info['numeric_scale'] : null
281+
);
274282
$column->setNumericUnsigned($info['numeric_unsigned']);
275283
$column->setErratas($info['erratas']);
276284

src/Sql/Ddl/Column/Column.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use PhpDb\Sql\Argument\Identifier;
99
use PhpDb\Sql\Argument\Literal;
1010
use PhpDb\Sql\Argument\Value;
11+
use PhpDb\Sql\ArgumentInterface;
1112
use PhpDb\Sql\Ddl\Constraint\ConstraintInterface;
1213

1314
use function implode;
1415

1516
class Column implements ColumnInterface
1617
{
17-
protected string|int|null $default;
18+
protected string|int|float|bool|Literal|Value|null $default;
1819

1920
protected bool $isNullable = false;
2021

@@ -65,14 +66,14 @@ public function isNullable(): bool
6566
return $this->isNullable;
6667
}
6768

68-
public function setDefault(string|int|null $default): static
69+
public function setDefault(string|int|float|bool|Literal|Value|null $default): static
6970
{
7071
$this->default = $default;
7172
return $this;
7273
}
7374

7475
#[Override]
75-
public function getDefault(): string|int|null
76+
public function getDefault(): string|int|float|bool|Literal|Value|null
7677
{
7778
return $this->default;
7879
}
@@ -118,7 +119,9 @@ public function getExpressionData(): array
118119

119120
if ($this->default !== null) {
120121
$specParts[] = 'DEFAULT %s';
121-
$values[] = new Value($this->default);
122+
$values[] = $this->default instanceof ArgumentInterface
123+
? $this->default
124+
: new Value($this->default);
122125
}
123126

124127
foreach ($this->constraints as $constraint) {

src/Sql/Ddl/Column/ColumnInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpDb\Sql\Ddl\Column;
66

7+
use PhpDb\Sql\Argument\Literal;
8+
use PhpDb\Sql\Argument\Value;
79
use PhpDb\Sql\ExpressionInterface;
810

911
/**
@@ -15,7 +17,7 @@ public function getName(): string;
1517

1618
public function isNullable(): bool;
1719

18-
public function getDefault(): string|int|null;
20+
public function getDefault(): string|int|float|bool|Literal|Value|null;
1921

2022
public function getOptions(): array;
2123
}

src/Sql/Ddl/Column/Double.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Sql\Ddl\Column;
6+
7+
class Double extends AbstractPrecisionColumn
8+
{
9+
protected string $type = 'DOUBLE';
10+
}

src/Sql/Ddl/Column/Json.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Sql\Ddl\Column;
6+
7+
class Json extends Column
8+
{
9+
protected string $type = 'JSON';
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Sql\Ddl\Column;
6+
7+
class SmallInteger extends Integer
8+
{
9+
protected string $type = 'SMALLINT';
10+
}

src/Sql/Ddl/CreateTable.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ class CreateTable extends AbstractSql
2222

2323
protected array $constraints = [];
2424

25+
protected bool $ifNotExists = false;
26+
2527
protected bool $isTemporary = false;
2628

2729
/**
2830
* {@inheritDoc}
2931
*/
3032
protected array $specifications = [
31-
self::TABLE => 'CREATE %1$sTABLE %2$s (',
33+
self::TABLE => 'CREATE %1$sTABLE %2$s%3$s (',
3234
self::COLUMNS => [
3335
"\n %1\$s" => [
3436
[1 => '%1$s', 'combinedby' => ",\n "],
@@ -51,6 +53,17 @@ public function __construct(string|TableIdentifier $table = '', bool $isTemporar
5153
$this->setTemporary($isTemporary);
5254
}
5355

56+
public function ifNotExists(bool $ifNotExists = true): static
57+
{
58+
$this->ifNotExists = $ifNotExists;
59+
return $this;
60+
}
61+
62+
public function getIfNotExists(): bool
63+
{
64+
return $this->ifNotExists;
65+
}
66+
5467
public function setTemporary(string|int|bool $temporary): static
5568
{
5669
$this->isTemporary = (bool) $temporary;
@@ -102,6 +115,7 @@ protected function processTable(?PlatformInterface $adapterPlatform = null): arr
102115
{
103116
return [
104117
$this->isTemporary ? 'TEMPORARY ' : '',
118+
$this->ifNotExists ? 'IF NOT EXISTS ' : '',
105119
$this->resolveTable($this->table, $adapterPlatform),
106120
];
107121
}

src/Sql/Ddl/DropTable.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ class DropTable extends AbstractSql
1212
{
1313
final public const TABLE = 'table';
1414

15+
protected bool $ifExists = false;
16+
1517
protected array $specifications = [
16-
self::TABLE => 'DROP TABLE %1$s',
18+
self::TABLE => 'DROP TABLE %1$s%2$s',
1719
];
1820

1921
protected string|TableIdentifier $table = '';
@@ -23,9 +25,23 @@ public function __construct(string|TableIdentifier $table = '')
2325
$this->table = $table;
2426
}
2527

28+
public function ifExists(bool $ifExists = true): static
29+
{
30+
$this->ifExists = $ifExists;
31+
return $this;
32+
}
33+
34+
public function getIfExists(): bool
35+
{
36+
return $this->ifExists;
37+
}
38+
2639
/** @return string[] */
2740
protected function processTable(?PlatformInterface $adapterPlatform = null): array
2841
{
29-
return [$this->resolveTable($this->table, $adapterPlatform)];
42+
return [
43+
$this->ifExists ? 'IF EXISTS ' : '',
44+
$this->resolveTable($this->table, $adapterPlatform),
45+
];
3046
}
3147
}

0 commit comments

Comments
 (0)