Compare commits
2
Commits
v2.2.5
...
e68f55d1ea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e68f55d1ea | ||
|
|
1c03860fc4 |
No files matched your search
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property\Field;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Dataset {
|
||||||
|
public function __construct(
|
||||||
|
public bool $allowPull = true,
|
||||||
|
public bool $allowPush = true,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property\Field;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Migration {
|
||||||
|
public function __construct(
|
||||||
|
public bool $skipField = false
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -5,6 +5,6 @@ namespace Ulmus\Attribute\Property;
|
|||||||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
||||||
class Filter {
|
class Filter {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $method = ""
|
public \Closure|string $method
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ class EntityField implements WhereRawParameter
|
|||||||
$select = $this->entityResolver->searchFieldAnnotation($this->name, [Field\Select::class]);
|
$select = $this->entityResolver->searchFieldAnnotation($this->name, [Field\Select::class]);
|
||||||
|
|
||||||
if ($select) {
|
if ($select) {
|
||||||
return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS code";
|
return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS $name";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,143 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Common;
|
|
||||||
|
|
||||||
use PDO,
|
|
||||||
PDOStatement;
|
|
||||||
|
|
||||||
class PdoObject extends PDO {
|
|
||||||
|
|
||||||
public static ? string $dump = null;
|
|
||||||
|
|
||||||
public bool $executionStatus;
|
|
||||||
|
|
||||||
public int $rowCount = 0;
|
|
||||||
|
|
||||||
public mixed $lastInsertId = null;
|
|
||||||
|
|
||||||
public \Closure $onClose;
|
|
||||||
|
|
||||||
public function select(string $sql, array $parameters = []): PDOStatement
|
|
||||||
{
|
|
||||||
static::$dump && call_user_func_array(static::$dump, [ $this->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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Common\PdoObject;
|
|
||||||
|
|
||||||
class SqlPdoObject extends \Ulmus\Common\PdoObject
|
|
||||||
{
|
|
||||||
protected int $openedTransaction = 0;
|
|
||||||
|
|
||||||
public function beginTransaction(): bool
|
|
||||||
{
|
|
||||||
if ( 0 === $this->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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Common\PdoObject;
|
|
||||||
|
|
||||||
class SqlitePdoObject extends SqlPdoObject
|
|
||||||
{
|
|
||||||
public bool $inTransaction = false;
|
|
||||||
|
|
||||||
public function inTransaction(): bool
|
|
||||||
{
|
|
||||||
return $this->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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ use Ulmus\Ulmus;
|
|||||||
|
|
||||||
class DatasetHandler
|
class DatasetHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected EntityResolver $entityResolver,
|
protected EntityResolver $entityResolver,
|
||||||
protected bool $entityStrictFieldsDeclaration = false,
|
protected bool $entityStrictFieldsDeclaration = false,
|
||||||
@@ -20,6 +19,10 @@ class DatasetHandler
|
|||||||
public function pull(object $entity, bool $nullable = true) : Generator
|
public function pull(object $entity, bool $nullable = true) : Generator
|
||||||
{
|
{
|
||||||
foreach($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
|
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 ]);
|
$attribute = $this->entityResolver->searchFieldAnnotation($key,[ Field::class ]);
|
||||||
|
|
||||||
if ( $entity->__isset($key) ) {
|
if ( $entity->__isset($key) ) {
|
||||||
@@ -28,6 +31,7 @@ class DatasetHandler
|
|||||||
elseif ( $nullable && $field->allowsNull() ) {
|
elseif ( $nullable && $field->allowsNull() ) {
|
||||||
yield $attribute->name ?? $key => null;
|
yield $attribute->name ?? $key => null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,6 +40,10 @@ class DatasetHandler
|
|||||||
$unmatched = [];
|
$unmatched = [];
|
||||||
|
|
||||||
foreach($dataset as $key => $value) {
|
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);
|
$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 ) {
|
if ( $field === null ) {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Pdo;
|
||||||
|
|
||||||
|
interface PdoDebugQueryInterface
|
||||||
|
{
|
||||||
|
public function debug(string $query) : void;
|
||||||
|
}
|
||||||
+11
-2
@@ -7,6 +7,8 @@ use PDO,
|
|||||||
|
|
||||||
trait PdoTrait
|
trait PdoTrait
|
||||||
{
|
{
|
||||||
|
public static PdoDebugQueryInterface $debugQuery;
|
||||||
|
|
||||||
public bool $executionStatus;
|
public bool $executionStatus;
|
||||||
|
|
||||||
public int $rowCount = 0;
|
public int $rowCount = 0;
|
||||||
@@ -17,9 +19,10 @@ trait PdoTrait
|
|||||||
|
|
||||||
public function select(string $sql, array $parameters = []): PDOStatement
|
public function select(string $sql, array $parameters = []): PDOStatement
|
||||||
{
|
{
|
||||||
# static::$dump && call_user_func_array(static::$dump, [ $this->fillDebugSqlParameters($sql, $parameters) ]);
|
isset(static::$debugQuery) && static::$debugQuery->debug($this->fillDebugSqlParameters($sql, $parameters));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||||
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||||||
$this->execute($statement, $parameters, false);
|
$this->execute($statement, $parameters, false);
|
||||||
@@ -28,6 +31,9 @@ trait PdoTrait
|
|||||||
catch (\Throwable $e) {
|
catch (\Throwable $e) {
|
||||||
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
isset(static::$debugQuery) && static::$debugQuery->endQuery();
|
||||||
|
}
|
||||||
|
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
@@ -41,7 +47,7 @@ trait PdoTrait
|
|||||||
|
|
||||||
public function runQuery(string $sql, array $parameters = []): ? static
|
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 {
|
try {
|
||||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||||
@@ -56,6 +62,9 @@ trait PdoTrait
|
|||||||
catch (\Throwable $e) {
|
catch (\Throwable $e) {
|
||||||
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters), (int) $e->getCode(), $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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -610,7 +610,7 @@ class Repository implements RepositoryInterface
|
|||||||
|
|
||||||
$this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryDatasetInterface::class, $this, $dataset);
|
$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);
|
$this->eventExecute(\Ulmus\Event\Repository\CollectionFromQueryItemInterface::class, $entityData);
|
||||||
|
|
||||||
$entity = $this->instanciateEntity($entityClass);
|
$entity = $this->instanciateEntity($entityClass);
|
||||||
@@ -678,8 +678,15 @@ class Repository implements RepositoryInterface
|
|||||||
|
|
||||||
public function instanciateEntity(? string $entityClass = null, array $dataset = []) : object
|
public function instanciateEntity(? string $entityClass = null, array $dataset = []) : object
|
||||||
{
|
{
|
||||||
|
static $loopDebug = 0;
|
||||||
|
|
||||||
$entityClass ??= $this->entityClass;
|
$entityClass ??= $this->entityClass;
|
||||||
|
|
||||||
|
/* if ($loopDebug++ >= 10256) {
|
||||||
|
debug_print_backtrace();
|
||||||
|
die();
|
||||||
|
}*/
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$entity = new $entityClass($dataset);
|
$entity = new $entityClass($dataset);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,8 +101,8 @@ trait JoinBuilderTrait
|
|||||||
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
|
foreach($this->entityResolver->searchFieldAnnotationList($item, [ Where::class ]) as $condition) {
|
||||||
$field = $this->joinFieldObject($condition->field, $this->entityClass);
|
$field = $this->joinFieldObject($condition->field, $this->entityClass);
|
||||||
|
|
||||||
# Adding directly
|
# Adding directly to the join
|
||||||
if ( $field->entityClass === $entity ) {
|
if ( ($field->entityClass === $entity) || ($field->entityClass !== $this->entityClass) ) {
|
||||||
if ($field->alias === $defaultAlias) {
|
if ($field->alias === $defaultAlias) {
|
||||||
$field->alias = $this->escapeIdentifier($alias);
|
$field->alias = $this->escapeIdentifier($alias);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ class RelationBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(\Throwable $e) {
|
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 ));
|
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,8 +155,13 @@ class RelationBuilder
|
|||||||
protected function applyFilter(Repository $repository, string $name) : Repository
|
protected function applyFilter(Repository $repository, string $name) : Repository
|
||||||
{
|
{
|
||||||
foreach($this->filters ?? [] as $filter) {
|
foreach($this->filters ?? [] as $filter) {
|
||||||
|
if (is_string($filter->method)) {
|
||||||
$repository = call_user_func_array([$this->entity, $filter->method], [$repository, $name, false]);
|
$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;
|
return $repository;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user