Skip to content

Commit

Permalink
Merge pull request #9 from 100DAYS/master
Browse files Browse the repository at this point in the history
Fix PostgreSQL problem with given ids
  • Loading branch information
MontealegreLuis committed Jan 27, 2022
2 parents a20fac4 + d30d042 commit bdfa29d
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions bin/doctrine-dbal
1 change: 1 addition & 0 deletions bin/phpunit
1 change: 1 addition & 0 deletions bin/yaml-lint
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -11,10 +11,10 @@
],
"require": {
"php": ">=7.1",
"doctrine/dbal": "^2.5",
"fzaninotto/faker": "^1.6",
"symfony/console": "^4.0",
"symfony/yaml": "^4.0"
"doctrine/dbal": "^2.5 | ^3.3",
"symfony/console": "^4.0 | ^5.0 | ^6.0",
"symfony/yaml": "^4.0 | ^5.0 | ^6.0",
"fakerphp/faker": "^1.12"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
Expand Down
11 changes: 9 additions & 2 deletions src/Database/DBALConnection.php
Expand Up @@ -18,14 +18,21 @@ public function __construct(DbConnection $connection)
$this->connection = $connection;
}

/** @throws \Doctrine\DBAL\DBALException */
/**
* @param string $table
* @param Row $row
* @throws \Doctrine\DBAL\DBALException
*/
public function insert(string $table, Row $row): void
{
$insert = Insert::into($table, $row);

$this->connection->executeUpdate($insert->toSQL($this->connection), $insert->parameters());

$row->assignId($this->connection->lastInsertId());
try {
$id = $this->connection->lastInsertId();
$row->assignId($id);
} catch (\Exception $e) {};
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Insert.php
Expand Up @@ -36,7 +36,9 @@ public function parameters(): array
{
$parameters = [];
foreach ($this->row->values() as $value) {
if (is_numeric($value) || trim($value, '`') === $value) {
if(is_array($value)) {
$parameters[] = json_encode($value);
} elseif (is_numeric($value) || trim($value, '`') === $value) {
$parameters[] = $value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Row.php
Expand Up @@ -76,7 +76,7 @@ public function placeholders(): array
{
$placeholders = [];
foreach ($this->values as $column => $value) {
if (is_numeric($value) || trim($value, '`') === $value) {
if (is_array($value) || is_numeric($value) || trim($value, '`') === $value) {
$placeholders[$column] = '?';
} else {
$placeholders[$column] = $value === null ? 'null' : trim($value, '`');
Expand Down
3 changes: 2 additions & 1 deletion src/Processors/FakerProcessor.php
Expand Up @@ -30,8 +30,9 @@ public function beforeInsert(Row $row): void
}
}

private function generateFakeDataIfNeeded(Row $row, string $column, ?string $value): void
private function generateFakeDataIfNeeded(Row $row, string $column, $value): void
{
if(!is_scalar($value)) return;
$formatted = $value;
while (FormatterCall::matches($formatted)) {
$formatted = FormatterCall::from($formatted)->run($this->generator);
Expand Down
3 changes: 2 additions & 1 deletion src/Processors/ForeignKeyProcessor.php
Expand Up @@ -59,8 +59,9 @@ public function addReference(Row $row)
/**
* @return mixed
*/
private function parseKeyIfNeeded(string $value)
private function parseKeyIfNeeded($value)
{
if (!is_scalar($value)) return $value;
if ($this->isAReference($value) && $this->referenceExistsFor($value)) {
return $this->references[substr($value, 1)];
}
Expand Down

0 comments on commit bdfa29d

Please sign in to comment.