From 1c03860fc480b685fb7bc59bc8aa567ca0c874f6 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 3 Dec 2026 19:56:58 +0000 Subject: [PATCH] - WIP on Dataset and Migration attribute - Removed unused PdoObject and added a new PdoDebugQueryInterface which is still a hugh WIP --- src/Attribute/Property/Field/Dataset.php | 11 ++ src/Attribute/Property/Field/Migration.php | 10 ++ src/Attribute/Property/Filter.php | 2 +- src/Common/EntityField.php | 2 +- src/Common/PdoObject.php | 143 --------------------- src/Common/PdoObject/SqlPdoObject.php | 43 ------- src/Common/PdoObject/SqlitePdoObject.php | 40 ------ src/Entity/DatasetHandler.php | 10 +- src/Pdo/PdoDebugQueryInterface.php | 8 ++ src/Pdo/PdoTrait.php | 15 ++- src/Repository.php | 9 +- src/Repository/RelationBuilder.php | 8 +- 12 files changed, 66 insertions(+), 235 deletions(-) create mode 100644 src/Attribute/Property/Field/Dataset.php create mode 100644 src/Attribute/Property/Field/Migration.php delete mode 100644 src/Common/PdoObject.php delete mode 100644 src/Common/PdoObject/SqlPdoObject.php delete mode 100644 src/Common/PdoObject/SqlitePdoObject.php create mode 100644 src/Pdo/PdoDebugQueryInterface.php diff --git a/src/Attribute/Property/Field/Dataset.php b/src/Attribute/Property/Field/Dataset.php new file mode 100644 index 0000000..8776f5b --- /dev/null +++ b/src/Attribute/Property/Field/Dataset.php @@ -0,0 +1,11 @@ +entityResolver->searchFieldAnnotation($this->name, [Field\Select::class]); if ($select) { - return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS code"; + return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS $name"; } } diff --git a/src/Common/PdoObject.php b/src/Common/PdoObject.php deleted file mode 100644 index ab0798e..0000000 --- a/src/Common/PdoObject.php +++ /dev/null @@ -1,143 +0,0 @@ -fillDebugSqlParameters($sql, $parameters) ]); - - try { - if (false !== ( $statement = $this->prepare($sql) )) { - $statement->setFetchMode(\PDO::FETCH_ASSOC); - $this->execute($statement, $parameters, false); - } - } - catch (\Throwable $e) { - throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters)); - } - - return $statement; - } - - public function __destruct() - { - if ($this->onClose ?? null) { - call_user_func($this->onClose, $this); - } - } - - public function runQuery(string $sql, array $parameters = []): ? static - { - static::$dump && call_user_func_array(static::$dump, [ $this->fillDebugSqlParameters($sql, $parameters) ]); - - try { - if (false !== ( $statement = $this->prepare($sql) )) { - return $this->execute($statement, $parameters, true); - } - } - catch(\PDOException $pdo) { - if (!str_starts_with($pdo->getMessage(), 'There is no active transaction')) { - throw $pdo; - } - } - catch (\Throwable $e) { - throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters), (int) $e->getCode(), $e); - } - - return null; - } - - public function runInsertQuery(string $sql, array $parameters = []) : ? static - { - return $this->runQuery($sql, $parameters); - } - - public function runUpdateQuery(string $sql, array $parameters = []) : ? static - { - return $this->runQuery($sql, $parameters); - } - - public function runDeleteQuery(string $sql, array $parameters = []) : ? static - { - return $this->runQuery($sql, $parameters); - } - - public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? static - { - $this->executionStatus = false; - $this->lastInsertId = null; - - try { - if (! $this->inTransaction()) { - $this->beginTransaction(); - } - - $this->bindVariables($statement, $parameters); - - $this->executionStatus = $statement->execute(); - - if ( $this->executionStatus ) { - $this->lastInsertId = $this->lastInsertId(); - $this->rowCount = $statement->rowCount(); - - if ( $commit ) { - $this->commit(); - } - - return $this; - } - else { - throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo())); - } - } - catch (\PDOException $e) { - $this->rollback(); - - throw $e; - } - } - - protected function bindVariables(PDOStatement $statement, array &$parameters) : void - { - if ($parameters) { - if (array_is_list($parameters)) { - $parameters = array_combine(range(1, count($parameters)), array_values($parameters)); - } - else { - foreach ($parameters as $key => $value) { - $type = match (strtolower(gettype($value))) { - "boolean", "integer" => Pdo::PARAM_INT, - "null" => Pdo::PARAM_NULL, - default => Pdo::PARAM_STR, - }; - - $statement->bindValue($key, $value, $type); - } - } - } - } - - protected function fillDebugSqlParameters(string $sql, array $parameters) : string - { - $parameters = array_reverse($parameters); - - return str_replace(array_keys($parameters), array_map(fn($e) => is_numeric($e) ? $e : ( - is_bool($e) ? (int) $e : $this->quote($e) - ), array_values($parameters)), $sql); - } -} diff --git a/src/Common/PdoObject/SqlPdoObject.php b/src/Common/PdoObject/SqlPdoObject.php deleted file mode 100644 index 0ab3e06..0000000 --- a/src/Common/PdoObject/SqlPdoObject.php +++ /dev/null @@ -1,43 +0,0 @@ -openedTransaction++ ) { - return $this->openTransaction(); - } - - return $this->exec("SAVEPOINT transaction_{$this->openedTransaction}") !== false; - } - - public function commit() : bool - { - if ( 0 === --$this->openedTransaction) { - return parent::commit(); - } - - return $this->exec("RELEASE SAVEPOINT transaction_{$this->openedTransaction}") !== false; - } - - public function rollback() : bool - { - if ($this->openedTransaction > 1) { - return $this->exec('ROLLBACK TO transaction_' . $this->openedTransaction--) !== false; - } - elseif ($this->openedTransaction-- === 1) { - return parent::rollback(); - } - - return false; - } - - protected function openTransaction() : bool - { - return parent::beginTransaction(); - } -} \ No newline at end of file diff --git a/src/Common/PdoObject/SqlitePdoObject.php b/src/Common/PdoObject/SqlitePdoObject.php deleted file mode 100644 index 9502878..0000000 --- a/src/Common/PdoObject/SqlitePdoObject.php +++ /dev/null @@ -1,40 +0,0 @@ -openedTransaction > 0; - } - - public function commit(): bool - { - if ( 0 === --$this->openedTransaction) { - return $this->exec("COMMIT") !== false; - } - - return $this->exec("RELEASE SAVEPOINT transaction_{$this->openedTransaction}") !== false; - } - - public function rollback() : bool - { - if ($this->openedTransaction > 1) { - return $this->exec('ROLLBACK TO transaction_' . $this->openedTransaction--) !== false; - } - elseif ($this->openedTransaction-- === 1) { - # We must do it manually since opening a transaction manually stucks PDO into thinking we've got no transaction opened - return $this->exec('ROLLBACK') !== false; - } - - return false; - } - - protected function openTransaction(): bool - { - return $this->exec("BEGIN IMMEDIATE TRANSACTION") !== false; - } -} \ No newline at end of file diff --git a/src/Entity/DatasetHandler.php b/src/Entity/DatasetHandler.php index b7a5639..5b5e6ae 100644 --- a/src/Entity/DatasetHandler.php +++ b/src/Entity/DatasetHandler.php @@ -11,7 +11,6 @@ use Ulmus\Ulmus; class DatasetHandler { - public function __construct( protected EntityResolver $entityResolver, protected bool $entityStrictFieldsDeclaration = false, @@ -20,6 +19,10 @@ class DatasetHandler public function pull(object $entity, bool $nullable = true) : Generator { foreach($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) { + if (false === ($this->entityResolver->searchFieldAnnotation($key,[ Field\Dataset::class ])->allowPull ?? true)) { + continue; + } + $attribute = $this->entityResolver->searchFieldAnnotation($key,[ Field::class ]); if ( $entity->__isset($key) ) { @@ -28,6 +31,7 @@ class DatasetHandler elseif ( $nullable && $field->allowsNull() ) { yield $attribute->name ?? $key => null; } + } } @@ -36,6 +40,10 @@ class DatasetHandler $unmatched = []; foreach($dataset as $key => $value) { + if (false === ($this->entityResolver->searchFieldAnnotation($key,[ Field\Dataset::class ])->allowPush ?? true)) { + continue; + } + $field = $this->entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? $this->entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false); if ( $field === null ) { diff --git a/src/Pdo/PdoDebugQueryInterface.php b/src/Pdo/PdoDebugQueryInterface.php new file mode 100644 index 0000000..7b95191 --- /dev/null +++ b/src/Pdo/PdoDebugQueryInterface.php @@ -0,0 +1,8 @@ +fillDebugSqlParameters($sql, $parameters) ]); + isset(static::$debugQuery) && static::$debugQuery->debug($this->fillDebugSqlParameters($sql, $parameters)); try { + if (false !== ( $statement = $this->prepare($sql) )) { $statement->setFetchMode(\PDO::FETCH_ASSOC); $this->execute($statement, $parameters, false); @@ -28,6 +31,9 @@ trait PdoTrait catch (\Throwable $e) { throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters)); } + finally { + isset(static::$debugQuery) && static::$debugQuery->endQuery(); + } return $statement; } @@ -41,7 +47,7 @@ trait PdoTrait public function runQuery(string $sql, array $parameters = []): ? static { - # static::$dump && call_user_func_array(static::$dump, [ $this->fillDebugSqlParameters($sql, $parameters) ]); + isset(static::$debugQuery) && static::$debugQuery->debug($this->fillDebugSqlParameters($sql, $parameters)); try { if (false !== ( $statement = $this->prepare($sql) )) { @@ -56,6 +62,9 @@ trait PdoTrait catch (\Throwable $e) { throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters), (int) $e->getCode(), $e); } + finally { + isset(static::$debugQuery) && static::$debugQuery->endQuery(); + } return null; } @@ -135,7 +144,7 @@ trait PdoTrait $parameters = array_reverse($parameters); return str_replace(array_keys($parameters), array_map(fn($e) => is_numeric($e) ? $e : ( - is_bool($e) ? (int) $e : $this->quote($e) + is_bool($e) ? (int) $e : $this->quote($e) ), array_values($parameters)), $sql); } } \ No newline at end of file diff --git a/src/Repository.php b/src/Repository.php index ecb9e1e..c2e8fca 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -610,7 +610,7 @@ class Repository implements RepositoryInterface $this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryDatasetInterface::class, $this, $dataset); - foreach($dataset ?: Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) { + foreach($dataset ?: Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $i => $entityData) { $this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryItemInterface::class, $entityData); $entity = $this->instanciateEntity($entityClass); @@ -678,8 +678,15 @@ class Repository implements RepositoryInterface public function instanciateEntity(? string $entityClass = null, array $dataset = []) : object { + static $loopDebug = 0; + $entityClass ??= $this->entityClass; + /* if ($loopDebug++ >= 10256) { + debug_print_backtrace(); + die(); + }*/ + try { $entity = new $entityClass($dataset); } diff --git a/src/Repository/RelationBuilder.php b/src/Repository/RelationBuilder.php index 8bfd50c..ac989e1 100644 --- a/src/Repository/RelationBuilder.php +++ b/src/Repository/RelationBuilder.php @@ -84,7 +84,6 @@ class RelationBuilder } } catch(\Throwable $e) { - throw new $e(sprintf("An error occurred while calling method from your #[Virtual] attribute in entity '%s::%s'. Caught '%s'", $this->entity::class, $name, $e->getMessage(), $e->getCode(), $e )); } } @@ -156,7 +155,12 @@ class RelationBuilder protected function applyFilter(Repository $repository, string $name) : Repository { foreach($this->filters ?? [] as $filter) { - $repository = call_user_func_array([ $this->entity, $filter->method ], [ $repository, $name, false ]); + if (is_string($filter->method)) { + $repository = call_user_func_array([$this->entity, $filter->method], [$repository, $name, false]); + } + elseif (is_callable($filter->method)) { + $repository = call_user_func_array($filter->method, [ $repository, $this->entity, false, ], ); + } } return $repository;