- Fixed SQLite truncate, added AdapterProxy autoloading

This commit is contained in:
Dave Mc Nicoll 2025-05-12 15:11:00 +00:00
parent 1ec582d1ac
commit 761e0401b0
7 changed files with 105 additions and 6 deletions

View File

@ -2,12 +2,27 @@
namespace Ulmus\Container; namespace Ulmus\Container;
use Ulmus\ConnectionAdapter;
class AdapterProxy class AdapterProxy
{ {
public array $connections = []; public array $connections = [];
public function __construct(...$arguments) public function __construct(...$arguments)
{ {
$this->connections = $arguments; $this->push(array_filter($arguments));
}
public function push(array|ConnectionAdapter $connection) : void
{
foreach((array) $connection as $adapter) {
foreach($this->connections as $existing) {
if ($adapter === $existing) {
continue 2;
}
}
$this->connections[] = $adapter;
}
} }
} }

View File

@ -343,7 +343,20 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable {
$list[] = $entity instanceof \JsonSerializable ? $entity->jsonSerialize() : $entity->toArray($includeRelations); $list[] = $entity instanceof \JsonSerializable ? $entity->jsonSerialize() : $entity->toArray($includeRelations);
} }
return $list; return $this->utf8ize($list, "UTF-8", "UTF-8");
}
protected function utf8ize(mixed $content) {
if (is_array($content)) {
foreach ($content as $key => $value) {
$content[$key] = $this->utf8ize($value);
}
}
elseif (is_string($content)) {
return mb_convert_encoding($content, "UTF-8", "UTF-8");
}
return $content;
} }
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self

View File

@ -43,12 +43,12 @@ trait EntityTrait {
#[Ignore] #[Ignore]
public function initializeEntity(iterable|null $dataset = null) : void public function initializeEntity(iterable|null $dataset = null) : void
{ {
$this->datasetHandler = new DatasetHandler(static::resolveEntity(), $this->entityStrictFieldsDeclaration);
if ($dataset) { if ($dataset) {
$this->fromArray($dataset); $this->fromArray($dataset);
} }
$this->datasetHandler = new DatasetHandler(static::resolveEntity(), $this->entityStrictFieldsDeclaration);
$this->resetVirtualProperties(); $this->resetVirtualProperties();
} }

View File

@ -0,0 +1,28 @@
<?php
namespace Ulmus\Query\Sqlite;
use Ulmus\Query\Fragment;
class Truncate extends Fragment {
const SQL_TOKEN = "DELETE FROM";
public int $order = -100;
public string $table;
public function set(string $tableName) : self
{
$this->table = $tableName;
return $this;
}
public function render() : string
{
return $this->renderSegments([
static::SQL_TOKEN, $this->table,
]);
}
}

View File

@ -3,6 +3,7 @@
namespace Ulmus\QueryBuilder\Sql; namespace Ulmus\QueryBuilder\Sql;
use Ulmus\Query; use Ulmus\Query;
use Ulmus\Adapter\AdapterInterface;
# Soon to extends SqlQueryBuilder # Soon to extends SqlQueryBuilder
class SqliteQueryBuilder extends MysqlQueryBuilder class SqliteQueryBuilder extends MysqlQueryBuilder
@ -38,4 +39,41 @@ class SqliteQueryBuilder extends MysqlQueryBuilder
return $this; return $this;
} }
public function insert(array $fieldlist, string $table, ? string $alias = null, ? string $database = null, ? string $schema = null, bool $replace = false) : self
{
return parent::insert($fieldlist, $table, $alias, null, null, $replace);
}
public function update(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
return parent::update($table, $alias, null, null);
}
public function create(AdapterInterface $adapter, array $fieldlist, string $table, ? string $database = null, ? string $schema = null) : self
{
return parent::create($adapter, $fieldlist, $table, null, null);
}
public function alter(AdapterInterface $adapter, array $fieldlist, string $table, ? string $database = null, ? string $schema = null) : self
{
return parent::alter($adapter, $fieldlist, $table, null, null);
}
public function from(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
return parent::from($table, $alias, null, null);
}
public function truncate(string $table, ? string $alias = null, ? string $database = null, ? string $schema = null) : self
{
$truncate = new Query\Sqlite\Truncate($this);
$this->push($truncate);
$truncate->set($table);
return $this;
}
} }

View File

@ -5,7 +5,7 @@ namespace Ulmus\QueryBuilder;
use Ulmus\Query\QueryFragmentInterface; use Ulmus\Query\QueryFragmentInterface;
# TODO -> Extract from MysqlQueryBuilder to build an ISO/IEC 9075:2023 compatible layer for a basic SQL QueryBuilder # TODO -> Extract from MysqlQueryBuilder to build an ISO/IEC 9075:2023 compatible layer for a basic SQL QueryBuilder
class SqlQueryBuilder implements QueryBuilderInterface abstract class SqlQueryBuilder implements QueryBuilderInterface
{ {
protected string $rendered; protected string $rendered;

View File

@ -216,7 +216,12 @@ class Repository implements RepositoryInterface
} }
else { else {
if ( $primaryKeyDefinition === null ) { if ( $primaryKeyDefinition === null ) {
throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass)); if ( null !== $compoundKeyFields = Ulmus::resolveEntity($this->entityClass)->getCompoundKeyFields() ) {
throw new \Exception("TO DO!");
}
else {
throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass));
}
} }
$diff = $fieldsAndValue ?? $this->generateWritableDataset($entity); $diff = $fieldsAndValue ?? $this->generateWritableDataset($entity);