Skip to content

Commit

Permalink
Tidy Query
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 2, 2024
1 parent bf57b5c commit e9060c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
1 change: 1 addition & 0 deletions lib/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function save(bool $skip_validation = false): void
assert(is_numeric($id));
}

// @phpstan-ignore-next-line
$rc = $model->save($properties, $id);

if ($id === null) {
Expand Down
8 changes: 0 additions & 8 deletions lib/ActiveRecord/Driver/TableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@

abstract class TableRenderer
{
/**
* @param non-empty-string $prefixed_table_name
*
* @return non-empty-string
*/
public function render(Schema $schema, string $prefixed_table_name): string
{
$column_defs = implode(",\n", $this->render_column_defs($schema));
Expand Down Expand Up @@ -54,9 +49,6 @@ abstract protected function render_table_constraints(Schema $schema): array;

/**
* @param Schema $schema
* @param non-empty-string $prefixed_table_name
*
* @return non-empty-string
*/
abstract protected function render_create_index(Schema $schema, string $prefixed_table_name): string;

Expand Down
56 changes: 25 additions & 31 deletions lib/ActiveRecord/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\ActiveRecord\Config\TableDefinition;
use LogicException;
use RuntimeException;
use Throwable;

use function array_combine;
Expand Down Expand Up @@ -93,8 +94,6 @@ private function make_select_join(): string

public Schema $extended_schema;

public Schema $extended_schema;

/**
* Returns the extended schema.
*/
Expand Down Expand Up @@ -251,9 +250,9 @@ public function execute(string $query, array $args = []): Statement
/**
* Filters mass assignment values.
*
* @param array<non-empty-string, mixed> $values
* @param array<string, mixed> $values
*
* @return array{ array, array<non-empty-string, non-empty-string>, non-empty-string[] }
* @return array{ array<int|string|null>, array<string, string>, array<string> }
*/
private function filter_values(array $values, bool $extended = false): array
{
Expand Down Expand Up @@ -282,10 +281,14 @@ private function filter_values(array $values, bool $extended = false): array
*
* @throws Throwable
*/
public function save(array $values, mixed $id = null, array $options = []): mixed
public function save(array $values, int $id = null, array $options = []): int|false
{
// TODO: If we have a parent, we should do the changes in a transaction.

if ($id) {
return $this->update($values, $id) ? $id : false;
$this->update($values, $id);

return $id;
}

return $this->save_callback($values, $id, $options);
Expand All @@ -295,8 +298,10 @@ public function save(array $values, mixed $id = null, array $options = []): mixe
* @param array<string, mixed> $values
* @param array<string, mixed> $options
*/
private function save_callback(array $values, mixed $id = null, array $options = []): int
private function save_callback(array $values, int $id = null, array $options = []): int
{
assert(count($values) > 0);

if ($id) {
$this->update($values, $id);

Expand All @@ -307,7 +312,7 @@ private function save_callback(array $values, mixed $id = null, array $options =

if ($this->parent) {
$parent_id = $this->parent->save_callback($values, null, $options)
?: throw new \Exception("Parent save failed: {$this->parent->name} returning {$parent_id}.");
?: throw new RuntimeException("Parent save failed: {$this->parent->name} returning $parent_id.");

assert(is_string($this->primary));
assert(is_numeric($parent_id));
Expand All @@ -322,13 +327,13 @@ private function save_callback(array $values, mixed $id = null, array $options =
// FIXME: ALL THIS NEED REWRITE !

if ($holders) {
// faire attention à l'id, si l'on revient du parent qui a inséré, on doit insérer aussi, avec son id
// If we have a parent, its primary key values must be used.

if ($driver_name === 'mysql') {
// if ($parent_id && empty($holders[$this->primary])) {
// $filtered[] = $parent_id;
// $holders[] = '`{primary}` = ?';
// }
if ($parent_id && empty($holders[$this->primary])) {
$filtered[] = $parent_id;
$holders[] = '`{primary}` = ?';
}

$statement = 'INSERT INTO `{self}` SET ' . implode(', ', $holders);
$statement = $this->prepare($statement);
Expand Down Expand Up @@ -446,12 +451,9 @@ public function insert(array $values, bool $ignore = false, bool $upsert = false
* Even if the entry is spread over multiple tables, all the tables are updated in a single
* step.
*
* @param array $values
* @param mixed $key
*
* @return bool
* @param array<string, mixed> $values
*/
public function update(array $values, $key)
public function update(array $values, int|string $key): void
{
#
# SQLite doesn't support UPDATE with INNER JOIN.
Expand All @@ -473,7 +475,7 @@ public function update(array $values, $key)
$table = $table->parent;
}

return true;
return;
}

[ $values, $holders ] = $this->filter_values($values, true);
Expand All @@ -482,30 +484,24 @@ public function update(array $values, $key)
$values[] = $key;

$this->execute($query, $values);

return true;
}

/**
* Deletes a record.
*
* @param mixed $key Identifier of the record.
*
* @return bool
* @param int|string|array<int|string> $key
*/
public function delete($key)
public function delete(int|string|array $key): void
{
if ($this->parent) {
$this->parent->delete($key);
}
$this->parent?->delete($key);

$where = 'WHERE ';

if (is_array($this->primary)) {
$parts = [];

foreach ($this->primary as $identifier) {
$parts[] = '`' . $identifier . '` = ?';
$parts[] = "`$identifier` = ?";
}

$where .= implode(' AND ', $parts);
Expand All @@ -515,8 +511,6 @@ public function delete($key)

$statement = $this->prepare('DELETE FROM `{self}` ' . $where);
$statement((array)$key);

return !!$statement->pdo_statement->rowCount();
}

/**
Expand Down

0 comments on commit e9060c8

Please sign in to comment.