Skip to content

Commit

Permalink
Use SQL read statement instead of simple list of escaped field names
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Apr 24, 2022
1 parent 07aa380 commit 21043ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ public function getGeneratedTypeFields(string $type): array
return $this->types[$this->requireRegisteredType($type)]['generated_fields'];
}

public function getTypeSqlReadStatements(string $type): array
{
return $this->types[$this->requireRegisteredType($type)]['sql_read_statements'];
}
public function getTypeProperty(string $type, string $property, callable $callback): mixed
{
$registered_type = $this->requireRegisteredType($type);
Expand All @@ -456,7 +460,7 @@ private function getSelectOneByType(string $type): string
if (empty($this->types[$type]['sql_select_by_ids'])) {
$this->types[$type]['sql_select_by_ids'] = sprintf(
'SELECT %s FROM %s WHERE `id` IN ? ORDER BY `id`',
$this->getEscapedTypeFields($type),
$this->getTypeFieldsReadStatement($type),
$this->getTypeTable($type, true)
);
}
Expand Down Expand Up @@ -491,21 +495,21 @@ public function getTypeFieldsReadStatement(string $type): string
{
return $this->getTypeProperty(
$type,
'escaped_fields',
'field_read_statements',
function () use ($type) {
$table_name = $this->getTypeTable($type, true);

$escaped_field_names = [];
$field_read_statements = [];

foreach ($this->getTypeFields($type) as $field_name) {
$escaped_field_names[] = $table_name . '.' . $this->connection->escapeFieldName($field_name);
foreach ($this->getTypeSqlReadStatements($type) as $sql_read_statement) {
$field_read_statements[] = $sql_read_statement;
}

foreach ($this->getGeneratedTypeFields($type) as $field_name) {
$escaped_field_names[] = $table_name . '.' . $this->connection->escapeFieldName($field_name);
$field_read_statements[] = $table_name . '.' . $this->connection->escapeFieldName($field_name);
}

return implode(',', $escaped_field_names);
return implode(',', $field_read_statements);
}
);
}
Expand Down Expand Up @@ -671,6 +675,7 @@ public function registerType(string ...$types): PoolInterface
'table_name' => $default_properties['table_name'],
'fields' => $default_properties['entity_fields'],
'generated_fields' => $default_properties['generated_entity_fields'],
'sql_read_statements' => $default_properties['sql_read_statements'],
'order_by' => $default_properties['order_by'],
];
}
Expand Down
4 changes: 4 additions & 0 deletions test/src/GeneratedFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public function testPoolIncludesGeneratedFieldsInTypeFieldsList()
$escaped_type_fields = $this->pool->getEscapedTypeFields(StatsSnapshot::class);
$this->assertIsString($escaped_type_fields);
$this->assertStringContainsString('is_used_on_day', $escaped_type_fields);

$type_fields_read_statement = $this->pool->getTypeFieldsReadStatement(StatsSnapshot::class);
$this->assertIsString($type_fields_read_statement);
$this->assertStringContainsString('is_used_on_day', $type_fields_read_statement);
}

public function testGeneratedFieldsAreHydrated()
Expand Down

0 comments on commit 21043ab

Please sign in to comment.