Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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)]
|
||||
class Filter {
|
||||
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]);
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
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 ) {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Pdo;
|
||||
|
||||
interface PdoDebugQueryInterface
|
||||
{
|
||||
public function debug(string $query) : void;
|
||||
}
|
||||
+12
-3
@@ -7,6 +7,8 @@ use PDO,
|
||||
|
||||
trait PdoTrait
|
||||
{
|
||||
public static PdoDebugQueryInterface $debugQuery;
|
||||
|
||||
public bool $executionStatus;
|
||||
|
||||
public int $rowCount = 0;
|
||||
@@ -17,9 +19,10 @@ trait PdoTrait
|
||||
|
||||
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 {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
+8
-1
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user