Skip to content

Commit

Permalink
Make Connection constructor protected (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 14, 2023
1 parent 947664c commit 8e53675
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 76 deletions.
16 changes: 16 additions & 0 deletions docs/.readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2

build:
# https://github.com/readthedocs/readthedocs.org/issues/8861
os: ubuntu-22.04
tools:
# https://github.com/readthedocs/readthedocs.org/issues/9719
python: '3'

python:
install:
# https://github.com/readthedocs/readthedocs.org/issues/10806
- requirements: docs/requirements.txt

formats:
- pdf
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ parameters:
-
message: '~^Class Doctrine\\DBAL\\Platforms\\SqlitePlatform referenced with incorrect case: Doctrine\\DBAL\\Platforms\\SQLitePlatform\.$~'
path: '*'
count: 31
count: 28

# TODO these rules are generated, this ignores should be fixed in the code
# for src/Schema/TestCase.php
Expand Down
4 changes: 4 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function normalize($value)

break;
case 'float':
case 'decimal':
case 'atk4_money':
$value = preg_replace('~\s+|[`\']|,(?=.*\.)~', '', $value);

Expand All @@ -163,6 +164,7 @@ public function normalize($value)
case 'boolean':
case 'integer':
case 'float':
case 'decimal':
case 'atk4_money':
if ($value === '') {
$value = null;
Expand All @@ -178,6 +180,7 @@ public function normalize($value)
case 'text':
case 'integer':
case 'float':
case 'decimal':
case 'atk4_money':
if (is_bool($value)) {
throw new Exception('Must not be boolean type');
Expand Down Expand Up @@ -223,6 +226,7 @@ public function normalize($value)
break;
case 'integer':
case 'float':
case 'decimal':
case 'atk4_money':
if ($this->required && !$value) {
throw new Exception('Must not be a zero');
Expand Down
6 changes: 3 additions & 3 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public function typecastSaveField(Field $field, $value)
try {
$v = $this->_typecastSaveField($field, $value);
if ($v !== null && !is_scalar($v)) { // @phpstan-ignore-line
throw new Exception('Unexpected non-scalar value');
throw new \TypeError('Unexpected non-scalar value');
}

return $v;
Expand All @@ -382,7 +382,7 @@ public function typecastLoadField(Field $field, $value)
if ($value === null) {
return null;
} elseif (!is_scalar($value)) { // @phpstan-ignore-line
throw new Exception('Unexpected non-scalar value');
throw new \TypeError('Unexpected non-scalar value');
}

try {
Expand Down Expand Up @@ -446,7 +446,7 @@ protected function _typecastSaveField(Field $field, $value)
protected function _typecastLoadField(Field $field, $value)
{
// TODO casting optionally to null should be handled by type itself solely
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'float', 'datetime', 'date', 'time', 'json', 'object'], true)) {
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'float', 'decimal', 'datetime', 'date', 'time', 'json', 'object'], true)) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/Sql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class Connection
/**
* @param array<string, mixed> $defaults
*/
public function __construct(array $defaults = [])
protected function __construct(array $defaults = [])
{
$this->setDefaults($defaults);
}
Expand Down Expand Up @@ -177,7 +177,7 @@ public static function resolveConnectionClass(string $driverName): string
}

/**
* Connect to database and return connection class.
* Connect to database and return connection instance.
*
* @param string|array<string, string>|DbalConnection|DbalDriverConnection $dsn
* @param string|null $user
Expand Down
10 changes: 9 additions & 1 deletion src/Persistence/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,15 @@ public function getDebugQuery(): string
{
[$sql, $params] = $this->render();

if (class_exists('SqlFormatter')) { // requires optional "jdorn/sql-formatter" package
if (class_exists(\SqlFormatter::class)) { // requires optional "jdorn/sql-formatter" package
\Closure::bind(static function () {
// fix latest/1.2.16 release from 2013-11-28
if (end(\SqlFormatter::$reserved_toplevel) === 'INTERSECT') {
\SqlFormatter::$reserved_toplevel[] = 'OFFSET';
\SqlFormatter::$reserved_toplevel[] = 'FETCH';
}
}, null, \SqlFormatter::class)();

$sql = preg_replace('~ +(?=\n|$)|(?<=:) (?=\w)~', '', \SqlFormatter::format($sql, false));
}

Expand Down
5 changes: 2 additions & 3 deletions src/Persistence/Sql/Oracle/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
$aiSequenceName = $this->getIdentitySequenceName($tableIdentifier->getQuotedName($this), $nameIdentifier->getQuotedName($this));
assert(str_starts_with($sqls[count($sqls) - 1], 'CREATE TRIGGER ' . $aiTriggerName . "\n"));

$conn = new Connection();
$pkSeq = \Closure::bind(fn () => $this->normalizeIdentifier($aiSequenceName), $this, OraclePlatform::class)()->getName();
$sqls[count($sqls) - 1] = $conn->expr(
$sqls[count($sqls) - 1] = (new Expression(
// else branch should be maybe (because of concurrency) put into after update trigger
str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOF'
CREATE TRIGGER {{trigger}}
Expand All @@ -100,7 +99,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
'pk' => $nameIdentifier->getName(),
'pk_seq' => $pkSeq,
]
)->render()[0];
))->render()[0];

return $sqls;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Persistence/Sql/Postgresql/PlatformTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar

$pkSeqName = $this->getIdentitySequenceName($table->getName(), $pkColumn->getName());

$conn = new Connection();

$sqls[] = $conn->expr(
$sqls[] = (new Expression(
// else branch should be maybe (because of concurrency) put into after update trigger
// with pure nextval instead of setval with a loop like in Oracle trigger
str_replace('[pk_seq]', '\'' . $pkSeqName . '\'', <<<'EOF'
Expand All @@ -111,9 +109,9 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar
'pk_seq' => $pkSeqName,
'trigger_func' => $table->getName() . '_AI_FUNC',
]
)->render()[0];
))->render()[0];

$sqls[] = $conn->expr(
$sqls[] = (new Expression(
<<<'EOF'
CREATE TRIGGER {trigger}
BEFORE INSERT OR UPDATE
Expand All @@ -126,7 +124,7 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar
'trigger' => $table->getShortestName($table->getNamespaceName()) . '_AI_PK',
'trigger_func' => $table->getName() . '_AI_FUNC',
]
)->render()[0];
))->render()[0];

return $sqls;
}
Expand Down
Loading

0 comments on commit 8e53675

Please sign in to comment.