Skip to content

Commit

Permalink
simplify concat assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 9, 2022
1 parent 8c7b706 commit af9b0da
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ parameters:
message: '~^(Call to an undefined method Doctrine\\DBAL\\Driver\\Connection::getWrappedConnection\(\)\.|Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\.|Call to an undefined static method Doctrine\\DBAL\\Exception::invalidPdoInstance\(\)\.|Call to method (getCreateTableSQL|getClobTypeDeclarationSQL|initializeCommentedDoctrineTypes)\(\) of deprecated class Doctrine\\DBAL\\Platforms\\\w+Platform:\n.+|Anonymous class extends deprecated class Doctrine\\DBAL\\Platforms\\(PostgreSQL94Platform|SQLServer2012Platform):\n.+|Call to deprecated method fetch(|All)\(\) of class Doctrine\\DBAL\\Result:\n.+|Call to deprecated method getSchemaManager\(\) of class Doctrine\\DBAL\\Connection:\n.+|Access to an undefined property Doctrine\\DBAL\\Driver\\PDO\\Connection::\$connection\.|Parameter #1 \$dsn of class Doctrine\\DBAL\\Driver\\PDO\\SQLSrv\\Connection constructor expects string, Doctrine\\DBAL\\Driver\\PDO\\Connection given\.|Method Atk4\\Data\\Persistence\\Sql\\Expression::execute\(\) should return Doctrine\\DBAL\\Result\|PDOStatement but returns bool\.|PHPDoc tag @return contains generic type Doctrine\\DBAL\\Schema\\AbstractSchemaManager<Doctrine\\DBAL\\Platforms\\AbstractPlatform> but class Doctrine\\DBAL\\Schema\\AbstractSchemaManager is not generic\.|Class Doctrine\\DBAL\\Platforms\\(MySqlPlatform|PostgreSqlPlatform) referenced with incorrect case: Doctrine\\DBAL\\Platforms\\(MySQLPlatform|PostgreSQLPlatform)\.)$~'
path: '*'
# count for DBAL 3.x matched in "src/Persistence/GenericPlatform.php" file
count: 42
count: 37

# TODO these rules are generated, this ignores should be fixed in the code
# for src/Schema/TestCase.php
Expand Down
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ public function save(array $data = [])
}

// No save needed, nothing was changed
if (!$data && !$dirty_join) {
if (count($data) === 0 && !$dirty_join) {
return $this;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Persistence/GenericPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ private function createNotSupportedException(): \Exception
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
$connection->getSchemaManager();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Schema/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;

abstract class TestCase extends BaseTestCase
Expand Down Expand Up @@ -118,7 +119,7 @@ function ($matches) use ($platform) {
return $platform->quoteSingleIdentifier($str);
}

return $platform->quoteStringLiteral($str);
return ($platform instanceof SQLServerPlatform ? 'N' : '') . $platform->quoteStringLiteral($str);
},
$sql
);
Expand Down
29 changes: 9 additions & 20 deletions tests/ExpressionSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Atk4\Data\Model;
use Atk4\Data\Schema\TestCase;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;

Expand Down Expand Up @@ -135,31 +134,21 @@ public function testExpressions(): void
$m->addFields(['name', 'surname', 'cached_name']);

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$m->addExpression('full_name', '[name] || " " || [surname]');
$concatExpr = '[name] || " " || [surname]';
} elseif ($this->getDatabasePlatform() instanceof OraclePlatform) {
$m->addExpression('full_name', '[name] || \' \' || [surname]');
$concatExpr = '[name] || \' \' || [surname]';
} else {
$m->addExpression('full_name', 'CONCAT([name], \' \', [surname])');
$concatExpr = 'CONCAT([name], \' \', [surname])';
}
$m->addExpression('full_name', $concatExpr);

$m->addCondition($m->expr('[full_name] != [cached_name]'));

if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$this->assertSame(
'select "id", "name", "surname", "cached_name", ("name" || " " || "surname") "full_name" from "user" where (("name" || " " || "surname") != "cached_name")',
$m->action('select')->render()[0]
);
} elseif ($this->getDatabasePlatform() instanceof OraclePlatform) {
$this->assertSame(
'select "id", "name", "surname", "cached_name", ("name" || \' \' || "surname") "full_name" from "user" where (("name" || \' \' || "surname") != "cached_name")',
$m->action('select')->render()[0]
);
} elseif ($this->getDatabasePlatform() instanceof MySQLPlatform) {
$this->assertSame(
'select `id`, `name`, `surname`, `cached_name`, (CONCAT(`name`, \' \', `surname`)) `full_name` from `user` where ((CONCAT(`name`, \' \', `surname`)) != `cached_name`)',
$m->action('select')->render()[0]
);
}
$concatSql = preg_replace('~\[(\w+)\]~', '"$1"', $concatExpr);
$this->assertSameSql(
'select "id", "name", "surname", "cached_name", (' . $concatSql . ') "full_name" from "user" where ((' . $concatSql . ') != "cached_name")',
$m->action('select')->render()[0]
);

$mm = $m->tryLoad(1);
$this->assertNull($mm->get('name'));
Expand Down

0 comments on commit af9b0da

Please sign in to comment.