94 Commits
Author SHA1 Message Date
Dave Mc Nicoll 0fd6889c6f - ID attribute is now readonly by default (since autoincremented) 2025-02-03 19:56:35 +00:00
mcndave bd0d928f35 - Added SearchableInterace as base of EntityInterface 2024-12-16 19:44:07 +00:00
mcndave 5a5d326d70 - Merging 2024-12-16 18:57:01 +00:00
mcndave b5e96eb7e0 - WIP on Collation 2024-12-16 18:46:24 +00:00
Dave Mc Nicoll 7c589ab7b4 - Fixed forgotten return value form listColumns in SQLite driver 2024-12-09 20:42:37 +00:00
Dave Mc Nicoll 589524ca3a - Added limitation on fixedValue in Where attribute 2024-12-09 16:40:50 +00:00
Dave Mc Nicoll 1fac12c928 - Added relation's shortcut attributes 2024-12-02 21:36:03 +00:00
Dave Mc Nicoll 9eed2fe1a8 - Added caching mechanism as an event listener 2024-12-02 15:22:30 -05:00
mcndave b601939459 - Bugfixes in search request, WIP on JsonTrait 2024-11-21 19:16:40 -05:00
mcndave e3314f40ac - WIP on JsonConditionTrait for SQL flavors that supports it; fixed a bug within Entity initialization from a new method to instanciate it which was implemented 2024-11-16 16:05:26 +00:00
mcndave 4ad44f73d7 - Fixed month() and days() format which were prepending zero ; adjusted to make it's behaviour like MySQL method 2024-11-14 13:40:47 +00:00
mcndave 68a6636c06 - Fixed a bug where default value was set to empty instead of nothing 2024-11-13 20:48:38 -05:00
Dave Mc Nicoll 70c6cde603 - Latest commit into this branch, merging after 2024-11-13 14:02:03 -05:00
Dave Mc Nicoll 079a3fd110 Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-11-11 20:42:51 +00:00
Dave Mc Nicoll ec4d02b9d9 - Removed 'description' property and moved it into an appropriate attribute from lean-api package 2024-11-11 20:42:29 +00:00
Dave Mc Nicoll 35ceb97241 - Added Exists method 2024-11-10 13:08:05 +00:00
Dave Mc Nicoll 83f673b37f - Mostly bug fixes done on this code push 2024-11-01 16:14:14 -04:00
Dave Mc Nicoll 411992c7a8 - Added a new Field Mapper to SQLs Adapters.
- Defined missing MySQL fields attributes
- WIP on lean-console's migration methods
2024-10-21 18:12:24 +00:00
Dave Mc Nicoll 5f4f23a8e4 - Allowed a JOINed entity to be null if nothing is loaded from a query and it's field is nullable 2024-10-14 16:32:23 +00:00
Dave Mc Nicoll 2fda7e82d7 Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-10-14 16:07:28 +00:00
Dave Mc Nicoll 573d4cc06b - RelationBuilder now returns NULL if no entity were found AND given field is nullable 2024-10-14 16:07:12 +00:00
Dave Mc Nicoll 18313fd7f5 - Forgot to set escapeIdentifier as static 2024-10-14 14:43:03 +00:00
Dave Mc Nicoll 9503b24467 - Fixed some bugs within SearchRequestFromRequestTrait 2024-10-14 14:41:35 +00:00
Dave Mc Nicoll 3c2ae86653 - Field mapper is now removed from Adapters and set into it's own object
- Added a PK to Column (IS)
2024-10-14 12:53:15 +00:00
Dave Mc Nicoll 9977a25cf5 - Forgot a dump() 2024-10-07 19:39:52 +00:00
Dave Mc Nicoll 3ba8a2a5d0 - Fixed 'value' for SearchRequestOrderBy and also fixed a typo where SearchManual would not be set 2024-10-07 19:38:29 +00:00
Dave Mc Nicoll 4106c8db91 Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-10-07 19:37:10 +00:00
Dave Mc Nicoll d5cca085bc - Removed offset if limit = 0 2024-10-07 19:36:53 +00:00
mcndave c56b584140 - Merges 2024-10-02 14:18:35 -04:00
Dave Mc Nicoll 8b1c11676d - Added OrderBy (heavy WIP, will be recoded 2024-08-30 18:59:48 +00:00
mcndave aa07c728fe - Fixed transactions in SQLite 2024-06-28 07:47:53 -04:00
Dave Mc Nicoll d4181065cf Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-06-28 11:46:46 +00:00
Dave Mc Nicoll 62998faf40 - WIP on SearchRequest enums and manual searchs 2024-06-28 11:46:09 +00:00
mcndave feee26cd26 Merge branch 'notes-2.x' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-06-21 13:01:48 +00:00
mcndave e37944c63a - Began WIP on SQL specific PdoObject 2024-06-21 13:01:21 +00:00
mcndave 1d38ae8245 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus into notes-2.x 2024-06-21 13:00:57 +00:00
Dave Mc Nicoll 0c8591f238 - Added multiple parameters on SearchParameter 2024-06-05 17:47:31 +00:00
Dave Mc Nicoll bbfd7c02b4 - WIP on SearchAttributes supporting Request's attributes 2024-06-04 13:33:48 +00:00
Dave Mc Nicoll bd9078230d - Multiple bug fixes related to notes 2.x 2024-05-31 12:26:39 +00:00
mcndave f3be11a590 - WIP on notes 2.x 2024-05-28 08:59:12 -04:00
Dave Mc Nicoll 2de3139c80 - Added description to most attributes 2024-05-27 18:09:22 +00:00
Dave Mc Nicoll e37eeb85f1 Merge branch 'master' into notes-2.x 2024-05-21 14:01:50 +00:00
Dave Mc Nicoll 7de0ecb028 - Fixed SearchLike attribute 2024-05-21 13:00:19 +00:00
mcndave 75231f32b3 - WIP on Notes v2.x 2024-05-09 19:49:27 +00:00
Dave Mc Nicoll 1dd9da6eb6 - Fixes done for QueryBuilderInterface 2024-04-24 11:23:10 -04:00
mcndave b38b81d03c - Still WIP on SearchRequest attributes migration 2024-04-22 13:54:48 +00:00
mcndave 950df5eb99 - Continued work on SearchRequest migration and fixed some more QueryBuilder migration too 2024-04-18 19:59:28 +00:00
Dave Mc Nicoll 9fdebf454d - Started working on decoupling QueryBuilder from SqlQueryBuilder
- WIP on SearchRequest attributes
- Began working on removing 'Ulmus' class
- WIP on removing SQL query building from Repository (which will become SqlRepository)
2024-04-17 15:54:48 -04:00
Dave Mc Nicoll 4571517dc8 - Added some SearchRequest attributes ; now it's even simpler to add searchable fields
- Began decoupling of QueryBuilder from native MySQL method ; some works needs to be done on for the Repository object splitting also.
2024-04-09 19:14:27 -04:00
mcndave 15be1597b8 - Fixed a recurrent bug within deleteOne() method 2024-02-01 16:17:08 +00:00
mcndave fa4e686f35 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-12-04 12:36:20 -05:00
mcndave d9fa51fa18 - Still WIP on removing annotations 2023-12-04 12:36:06 -05:00
Dave Mc Nicoll d4d68a029d - Fixed a problem of invalid comparison types in empty IN() query fragment 2023-11-24 11:31:54 -05:00
Dave Mc Nicoll ea667db552 - Testing another boolean handling method 2023-11-20 15:24:23 -05:00
Dave Mc Nicoll afe5144dc7 - Added a new value binding methods 2023-11-20 15:21:17 -05:00
mcndave 62792e8325 - Fixed a bug within INSERT query 2023-11-17 23:34:55 -05:00
mcndave 3a80fee9c3 - Added support for Ignore attribute in dataset export of EntityTrait
- Work done on JSONification of entity
2023-11-17 22:40:21 -05:00
Dave Mc Nicoll 7f8780d328 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-17 16:00:59 -05:00
Dave Mc Nicoll 9a9f473f3f - PDO Params are now properly (mostly..) binded, bool, null and int are now handled correctly 2023-11-17 16:00:29 -05:00
mcndave fab70aec6a fixed a missing alias 2023-11-17 08:19:12 -05:00
Dave Mc Nicoll c1755d250b Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-08 09:27:26 -05:00
Dave Mc Nicoll acab934f6f - Some bugfixes applied live 2023-11-08 09:27:03 -05:00
mcndave 2e377374d4 - Removed unused annotations 2023-11-08 06:52:38 -05:00
Dave Mc Nicoll c2cccc0222 - Fixed a proble within EntityInterface with field(s) which needed a false instead of bool 2023-11-07 11:12:59 -05:00
Dave Mc Nicoll ab582c9318 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-07 11:02:06 -05:00
Dave Mc Nicoll a4b81f1932 - Added support for closure in Virtual field 2023-11-07 11:01:55 -05:00
mcndave ab1d7e4f5b Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-03 19:45:31 -04:00
mcndave bfc1e1cf93 - WIP on removing annotations 2023-11-03 19:45:21 -04:00
mcndave 531908b04c Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-03 08:26:16 -04:00
mcndave 07ae8faaac - Worked on SQLite adapter mostly 2023-11-03 08:25:35 -04:00
Dave Mc Nicoll 756474d460 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-11-01 11:26:10 -04:00
Dave Mc Nicoll d297fd9e86 - Fixed a bug which caused a problem using offset without limits 2023-11-01 11:25:59 -04:00
mcndave fed7d2e302 - Some minors adjustments to match PHP 8.x coding styles 2023-11-01 09:55:19 -04:00
mcndave 3736fbe0f6 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-10-31 18:05:00 +00:00
mcndave 667c1fe9ab - Fixed an alias problem within deleteFromPk() method 2023-10-31 18:04:49 +00:00
mcndave d2bd6c2f58 - minor fixes 2023-10-31 16:34:45 +00:00
mcndave 953fc35680 - Completed SQLite pragma's missing code 2023-10-31 16:11:23 +00:00
mcndave 6c6733b503 - Merging.. 2023-10-31 14:40:02 +00:00
mcndave e641bc321d - SQLite pragma handling was not applied ; fixed 2023-10-31 14:37:46 +00:00
mcndave da5d203768 - Small quick fix on espaceIdentifier whichi misses a type 2023-10-31 14:17:56 +00:00
mcndave f77c93ad20 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-10-30 19:41:50 +00:00
mcndave c9d9281c63 - Added a new clone() method to Repository() base class 2023-10-30 19:41:40 +00:00
Dave Mc Nicoll 8e0dce3e71 - Still WIP on Api's wrapper 2023-10-27 18:33:48 -04:00
mcndave 1927b43999 - Some overdue modification made to allow more flexibility 2023-10-27 17:53:55 +00:00
Dave Mc Nicoll cc741566fb - Bugfixes made while working on lean/console database migrations 2023-08-31 09:15:50 -04:00
Dave Mc Nicoll 8489cb4841 - Added some options to withJoin() method of repository 2023-08-29 16:32:25 -04:00
mcndave 9ebf7b05d7 - Bugfixes made while working on lean/console 2023-08-29 20:29:33 +00:00
mcndave 338b599d39 - Added a default searchRequest() method to entityTrait 2023-07-31 14:28:19 -04:00
mcndave d01624b8aa - WIP on relations, constrains and foreign keys 2023-07-14 19:57:29 -04:00
mcndave 01ab6e82d4 - Some small bugfixes made 2023-07-09 12:38:52 -04:00
Dave Mc Nicoll a4743b471c - saveAll() now also supports arrays 2023-05-12 18:56:59 +00:00
Dave Mc Nicoll bd61522a07 Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus 2023-04-13 15:39:21 -04:00
Dave Mc Nicoll f3712c9cc7 - Changed returned object from PdoObject runQuery method correcting 'Creation of dynamic property PDOStatement:: is deprecated' 2023-04-13 15:39:16 -04:00
mcndave cfaebbecc4 - Minor enhancements 2023-04-05 17:42:02 +00:00
156 changed files with 3490 additions and 2162 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
# Search Request
Ulmus comes with a simple search request processor which allows both flexibility and simplicity.
## Quick start
Creating a simple user exemple entity:
View File
Whitespace-only changes.
-12
View File
@@ -15,19 +15,7 @@ interface AdapterInterface {
public function connect() : object /* | PdoObject|mixed */;
public function buildDataSourceName() : string;
public function setup(array $configuration) : void;
public function escapeIdentifier(string $segment, int $type) : string;
public function defaultEngine() : ? string;
public function writableValue(/* mixed */ $value); /*: mixed*/
/* public function databaseName() : string;
public function mapFieldType(FieldDefinition $field) : string;
public function schemaTable(string $databaseName, string $tableName) /*: object|EntityCollection
*/
public function repositoryClass() : string;
public function queryBuilderClass() : string;
public function tableSyntax() : array;
public function whitelistAttributes(array &$parameters) : void;
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable;
}
+3 -3
View File
@@ -2,7 +2,7 @@
namespace Ulmus\Adapter;
use Ulmus\{ConnectionAdapter, Entity\InformationSchema\Table, Migration\FieldDefinition, Repository, QueryBuilder};
use Ulmus\{ConnectionAdapter, Entity\InformationSchema\Table, Migration\FieldDefinition, Repository, QueryBuilder\Sql\MysqlQueryBuilder, Entity};
trait DefaultAdapterTrait
{
@@ -13,7 +13,7 @@ trait DefaultAdapterTrait
public function queryBuilderClass() : string
{
return QueryBuilder::class;
return MysqlQueryBuilder::class;
}
public function tableSyntax() : array
@@ -104,7 +104,7 @@ trait DefaultAdapterTrait
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
{
if ($field['previous']) {
$position = sprintf('AFTER %s', $adapter->escapeIdentifier($field['previous']['field'], AdapterInterface::IDENTIFIER_FIELD));
$position = sprintf('AFTER %s', $this->escapeIdentifier($field['previous']['field'], AdapterInterface::IDENTIFIER_FIELD));
}
else {
$position = "FIRST";
+9 -26
View File
@@ -9,10 +9,10 @@ use Ulmus\Exception\AdapterConfigurationException;
use Ulmus\Ulmus;
use Ulmus\Migration\FieldDefinition;
use Ulmus\{Entity\InformationSchema\Table, Repository, QueryBuilder};
use Ulmus\{Entity\InformationSchema\Table, Migration\MigrateInterface, Repository, QueryBuilder};
class MsSQL implements AdapterInterface {
use DefaultAdapterTrait;
class MsSQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
use SqlAdapterTrait;
const ALLOWED_ATTRIBUTES = [
'default', 'primary_key', 'auto_increment',
@@ -51,7 +51,9 @@ class MsSQL implements AdapterInterface {
public function __construct(
?string $server = null,
?string $database = null,
#[\SensitiveParameter]
?string $username = null,
#[\SensitiveParameter]
?string $password = null,
?int $port = null
) {
@@ -84,7 +86,7 @@ class MsSQL implements AdapterInterface {
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
$pdo->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_UTF8);
}
catch(PDOException $ex){
catch(\PDOException $ex){
throw $ex;
}
finally {
@@ -204,9 +206,10 @@ class MsSQL implements AdapterInterface {
}
}
public function escapeIdentifier(string $segment, int $type) : string
public static function escapeIdentifier(string $segment, int $type) : string
{
switch($type) {
default:
case static::IDENTIFIER_SCHEMA:
case static::IDENTIFIER_DATABASE:
case static::IDENTIFIER_TABLE:
@@ -218,26 +221,6 @@ class MsSQL implements AdapterInterface {
}
}
public function writableValue(mixed $value) /*: mixed*/
{
switch (true) {
case $value instanceof \UnitEnum:
return Ulmus::convertEnum($value);
case is_object($value):
return Ulmus::convertObject($value);
case is_array($value):
return json_encode($array);
case is_bool($value):
return (int) $value;
}
return $value;
}
public function defaultEngine(): ? string
{
return null;
@@ -250,6 +233,6 @@ class MsSQL implements AdapterInterface {
public function queryBuilderClass() : string
{
return QueryBuilder\MssqlQueryBuilder::class;
return QueryBuilder\Sql\MssqlQueryBuilder::class;
}
}
+35 -30
View File
@@ -2,17 +2,18 @@
namespace Ulmus\Adapter;
use Ulmus\Entity\InformationSchema\Table;
use Ulmus\QueryBuilder;
use Ulmus\Repository;
use Ulmus\ConnectionAdapter;
use Ulmus\Entity\Mysql\Table;
use Ulmus\Migration\FieldDefinition;
use Ulmus\Migration\MigrateInterface;
use Ulmus\QueryBuilder\Sql;
use Ulmus\Common\PdoObject;
use Ulmus\Exception\AdapterConfigurationException;
use Ulmus\Ulmus;
use Ulmus\Migration\FieldDefinition;
use Ulmus\Repository;
class MySQL implements AdapterInterface {
use DefaultAdapterTrait;
class MySQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
use SqlAdapterTrait;
const ALLOWED_ATTRIBUTES = [
'default', 'primary_key', 'auto_increment', 'update',
@@ -24,9 +25,9 @@ class MySQL implements AdapterInterface {
public string $database;
public string $username;
protected string $username;
public string $password;
protected string $password;
public string $charset = "utf8mb4";
@@ -37,7 +38,9 @@ class MySQL implements AdapterInterface {
public function __construct(
?string $hostname = null,
?string $database = null,
#[\SensitiveParameter]
?string $username = null,
#[\SensitiveParameter]
?string $password = null,
?int $port = null,
?string $charset = null
@@ -77,7 +80,7 @@ class MySQL implements AdapterInterface {
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}
catch(PDOException $ex){
catch(\PDOException $ex){
throw $ex;
}
finally {
@@ -139,9 +142,17 @@ class MySQL implements AdapterInterface {
}
}
public function escapeIdentifier(string $segment, int $type) : string
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
{
$mapper = new MySQLFieldMapper($field);
return $typeOnly ? $mapper->type : $mapper->render();
}
public static function escapeIdentifier(string $segment, int $type) : string
{
switch($type) {
default:
case static::IDENTIFIER_DATABASE:
case static::IDENTIFIER_TABLE:
case static::IDENTIFIER_FIELD:
@@ -152,27 +163,21 @@ class MySQL implements AdapterInterface {
}
}
public function writableValue(mixed $value) : mixed
{
switch (true) {
case $value instanceof \UnitEnum:
return Ulmus::convertEnum($value);
case is_object($value):
return Ulmus::convertObject($value);
case is_array($value):
return json_encode($value);
case is_bool($value):
return (int) $value;
}
return $value;
}
public function defaultEngine(): ? string
{
return "InnoDB";
}
public function queryBuilderClass() : string
{
return Sql\MysqlQueryBuilder::class;
}
public function schemaTable(ConnectionAdapter $adapter, $databaseName, string $tableName) : null|object
{
return Table::repository(Repository::DEFAULT_ALIAS, $adapter)
->select(\Ulmus\Common\Sql::raw('this.*'))
->where($this->escapeIdentifier('table_schema', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
->loadOneFromField($this->escapeIdentifier('table_name', AdapterInterface::IDENTIFIER_FIELD), $tableName);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Ulmus\Adapter;
use Ulmus\Migration\FieldDefinition;
class MySQLFieldMapper extends SqlFieldMapper
{
public readonly string $type;
public readonly string $length;
public function map() : void
{
$type = $this->field->type;
$length = $this->field->length;
if ( enum_exists($type) ) {
# Haven't found a better way yet to check for BackendEnum without an instance of the object
if ( ! method_exists($type, 'tryFrom') ) {
throw new \Ulmus\Exception\BackedEnumRequired(sprintf("You must define your enum as a BackedEnum instead of an UnitEnum for field '%s'.", $this->field->getColumnName()));
}
$this->length = implode(',', array_map(fn($e) => MySQL::escapeIdentifier($e->value, MySQL::IDENTIFIER_VALUE) , $type::cases()));
$this->type = "ENUM";
}
else {
parent::map();
}
}
public function postProcess() : void
{
if (
in_array($this->type, [ 'BLOB', 'TINYBLOB', 'MEDIUMBLOB', 'LONGBLOB', 'JSON', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'GEOMETRY' ]) &&
! is_object($this->field->default ?? false) # Could be a functional default, which would now be valid
) {
unset($this->field->default);
}
}
}
+97 -50
View File
@@ -2,50 +2,45 @@
namespace Ulmus\Adapter;
use Collator;
use Ulmus\Common\PdoObject;
use Ulmus\ConnectionAdapter;
use Ulmus\Entity\Sqlite\Table;
use Ulmus\Exception\AdapterConfigurationException;
use Ulmus\Entity;
use Ulmus\Migration\FieldDefinition;
use Ulmus\{Repository, QueryBuilder, Ulmus};
use Ulmus\{Migration\MigrateInterface, Repository, QueryBuilder};
class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
use SqlAdapterTrait;
class SQLite implements AdapterInterface {
use DefaultAdapterTrait;
const ALLOWED_ATTRIBUTES = [
'default', 'primary_key', 'auto_increment'
'default', 'primary_key', 'auto_increment', 'collate nocase', 'collate binary', 'collate rtrim',
];
const DSN_PREFIX = "sqlite";
public string $path;
public array $pragma;
public function __construct(
? string $path = null,
? array $pragma = null
) {
if ($path !== null) {
$this->path = $path;
}
if ($pragma !== null) {
$this->pragma = $pragma;
}
}
public null|string $path = null,
public null|array $pragmaBegin = null,
public null|array $pragmaClose = null,
) { }
public function connect() : PdoObject
{
try {
$pdo = new PdoObject($this->buildDataSourceName(), null, null);
$pdo = new PdoObject\SqlitePdoObject($this->buildDataSourceName(), null, null);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
$pdo->onClose = function(PdoObject $obj) {
static::registerPragma($obj, $this->pragmaClose);
};
$this->exportFunctions($pdo);
$this->exportCollations($pdo);
$this->registerPragma($pdo, $this->pragmaBegin);
}
catch(PDOException $ex){
catch(\PDOException $ex){
throw $ex;
}
@@ -62,13 +57,15 @@ class SQLite implements AdapterInterface {
public function setup(array $configuration) : void
{
$this->path = $configuration['path'] ?? "";
$this->pragma = $configuration['pragma'] ?? [];
$this->pragmaBegin = array_filter($configuration['pragma_begin'] ?? []);
$this->pragmaClose = array_filter($configuration['pragma_close'] ?? []);
}
# https://sqlite.org/lang_keywords.html
public function escapeIdentifier(string $segment, int $type) : string
public static function escapeIdentifier(string $segment, int $type) : string
{
switch($type) {
default:
case static::IDENTIFIER_DATABASE:
case static::IDENTIFIER_TABLE:
case static::IDENTIFIER_FIELD:
@@ -93,15 +90,14 @@ class SQLite implements AdapterInterface {
public function schemaTable(ConnectionAdapter $adapter, string $databaseName, string $tableName) : null|object
{
return Table::repository(Repository::DEFAULT_ALIAS, $adapter)
return Entity\Sqlite\Table::repository(Repository::DEFAULT_ALIAS, $adapter)
->select(\Ulmus\Common\Sql::raw('this.*'))
->loadOneFromField(Table::field('tableName'), $tableName);
->loadOneFromField(Entity\Sqlite\Table::field('tableName'), $tableName);
}
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
{
$type = $field->type;
$length = $field->length;
if ( is_a($type, Entity\Field\Date::class, true) || is_a($type, Entity\Field\Time::class, true) || is_a($type, \DateTime::class, true) ) {
@@ -119,6 +115,10 @@ class SQLite implements AdapterInterface {
break;
case "array":
$type = "JSON";
$length = null;
break;
case "string":
$type = "TEXT";
$length = null;
@@ -134,26 +134,11 @@ class SQLite implements AdapterInterface {
}
}
return $typeOnly ? $type : $type . ( $length ? "($length" . ( isset($precision) ? ",$precision" : "" ) . ")" : "" );
if (in_array($type, [ 'JSON', 'TEXT', 'BLOB', 'GEOMETRY' ])) {
unset($field->default);
}
public function writableValue(mixed $value) /*: mixed*/
{
switch (true) {
case $value instanceof \UnitEnum:
return Ulmus::convertEnum($value);
case is_object($value):
return Ulmus::convertObject($value);
case is_array($value):
return json_encode($value);
case is_bool($value):
return (int) $value;
}
return $value;
return $typeOnly ? $type : $type . ( $length ? "($length" . ")" : "" );
}
public function tableSyntax() : array
@@ -172,7 +157,7 @@ class SQLite implements AdapterInterface {
public function queryBuilderClass() : string
{
return QueryBuilder\SqliteQueryBuilder::class;
return QueryBuilder\Sql\SqliteQueryBuilder::class;
}
public function exportFunctions(PdoObject $pdo) : void
@@ -181,6 +166,14 @@ class SQLite implements AdapterInterface {
$pdo->sqliteCreateFunction('length', fn($string) => strlen($string), 1);
$pdo->sqliteCreateFunction('lcase', fn($string) => strtolower($string), 1);
$pdo->sqliteCreateFunction('ucase', fn($string) => strtoupper($string), 1);
$pdo->sqliteCreateFunction('md5', fn($string) => md5($string), 1);
$pdo->sqliteCreateFunction('sha1', fn($string) => sha1($string), 1);
$pdo->sqliteCreateFunction('sha256', fn($string) => hash('sha256', $string), 1);
$pdo->sqliteCreateFunction('sha512', fn($string) => hash('sha512', $string), 1);
$pdo->sqliteCreateFunction('whirlpool', fn($string) => hash('whirlpool', $string), 1);
$pdo->sqliteCreateFunction('murmur3a', fn($string) => hash('murmur3a', $string), 1);
$pdo->sqliteCreateFunction('murmur3c', fn($string) => hash('murmur3c', $string), 1);
$pdo->sqliteCreateFunction('murmur3f', fn($string) => hash('murmur3f', $string), 1);
$pdo->sqliteCreateFunction('left', fn($string, $length) => substr($string, 0, $length), 2);
$pdo->sqliteCreateFunction('right', fn($string, $length) => substr($string, -$length), 2);
$pdo->sqliteCreateFunction('strcmp', fn($s1, $s2) => strcmp($s1, $s2), 2);
@@ -199,11 +192,60 @@ class SQLite implements AdapterInterface {
return (int) in_array($string, explode(',', $string_list));
}, 2);
$pdo->sqliteCreateFunction('day', fn($date) => ( new \DateTime($date) )->format('d'), 1);
$pdo->sqliteCreateFunction('month', fn($date) => ( new \DateTime($date) )->format('m'), 1);
$pdo->sqliteCreateFunction('day', fn($date) => ( new \DateTime($date) )->format('j'), 1);
$pdo->sqliteCreateFunction('month', fn($date) => ( new \DateTime($date) )->format('n'), 1);
$pdo->sqliteCreateFunction('year', fn($date) => ( new \DateTime($date) )->format('Y'), 1);
}
public function exportCollations(PdoObject $pdo) : void
{
if ( class_exists('Locale') ) {
# @TODO debug this, case-sensitivity not working properly !!
$collator = new Collator(\Locale::getDefault());
$collator->setStrength(Collator::TERTIARY);
$collator->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$pdo->sqliteCreateCollation('locale_cmp', fn($e1, $e2) => $collator->compare($e1, $e2));
$pdo->sqliteCreateCollation('locale_cmp_desc', fn($e1, $e2) => $collator->compare($e2, $e1));
$collatorCi = new Collator(\Locale::getDefault());
$collatorCi->setStrength(Collator::SECONDARY);
$collatorCi->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);
$pdo->sqliteCreateCollation('locale_cmp_ci', fn($e1, $e2) => $collatorCi->compare($e1, $e2));
$pdo->sqliteCreateCollation('locale_cmp_ci_desc', fn($e1, $e2) => $collatorCi->compare($e2, $e1));
}
else {
$comp = fn(string $e1, string $e2) => \iconv('UTF-8', 'ASCII//TRANSLIT', $e1) <=> \iconv('UTF-8', 'ASCII//TRANSLIT', $e2);
$pdo->sqliteCreateCollation('locale_cmp', fn($e1, $e2) => $comp);
$pdo->sqliteCreateCollation('locale_cmp_desc', fn($e2, $e1) => $comp);
$compCi = fn(string $e1, string $e2) => strtoupper(\iconv('UTF-8', 'ASCII//TRANSLIT', $e1)) <=> strtoupper(\iconv('UTF-8', 'ASCII//TRANSLIT', $e2));
$pdo->sqliteCreateCollation('locale_cmp_ci', fn($e1, $e2) => $compCi);
$pdo->sqliteCreateCollation('locale_cmp_ci_desc', fn($e2, $e1) => $compCi);
}
}
public static function registerPragma(PdoObject $pdo, array $pragmaList) : void
{
$builder = new QueryBuilder\Sql\SqliteQueryBuilder();
foreach($pragmaList as $pragma) {
list($key, $value) = explode('=', $pragma) + [ null, null ];
$sql = $builder->pragma($key, $value)->render();
$query = $pdo->query($sql);
if ( ! $query->execute() ) {
throw new \InvalidArgumentException(sprintf("Pragma query could not be executed : %s", $sql));
}
$builder->reset();
}
}
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
{
return implode(" ", [
@@ -213,4 +255,9 @@ class SQLite implements AdapterInterface {
$definition->getSqlParams(),
]);
}
public function splitAlterQuery() : bool
{
return true;
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Ulmus\Adapter;
interface SqlAdapterInterface
{
public static function escapeIdentifier(string $segment, int $type) : string;
public function defaultEngine() : ? string;
}
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace Ulmus\Adapter;
use Ulmus\{Common\Sql,
ConnectionAdapter,
Entity\InformationSchema\Table,
Migration\FieldDefinition,
Repository,
QueryBuilder\SqlQueryBuilder,
Ulmus};
trait SqlAdapterTrait
{
public function repositoryClass() : string
{
return Repository::class;
}
public function queryBuilderClass() : string
{
return SqlQueryBuilder::class;
}
public function tableSyntax() : array
{
return [
'ai' => "AUTO_INCREMENT",
'pk' => "PRIMARY KEY",
'unsigned' => "UNSIGNED",
];
}
public function databaseName() : string
{
return $this->database;
}
public function schemaTable(ConnectionAdapter $adapter, $databaseName, string $tableName) : null|object
{
return Table::repository(Repository::DEFAULT_ALIAS, $adapter)
->select(\Ulmus\Common\Sql::raw('this.*'))
->where($this->escapeIdentifier('table_schema', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
->loadOneFromField($this->escapeIdentifier('table_name', AdapterInterface::IDENTIFIER_FIELD), $tableName);
}
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
{
$mapper = new SqlFieldMapper($field);
return $typeOnly ? $mapper->type : $mapper->render();
}
public function whitelistAttributes(array &$parameters) : void
{
$parameters = array_intersect_key($parameters, array_flip(static::ALLOWED_ATTRIBUTES));
}
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
{
if (! empty($field['previous']) ) {
$position = sprintf('AFTER %s', $this->escapeIdentifier($field['previous']->field, AdapterInterface::IDENTIFIER_FIELD));
}
else {
$position = "FIRST";
}
return implode(" ", [
strtoupper($field['action']),
$this->escapeIdentifier($definition->getSqlName(), AdapterInterface::IDENTIFIER_FIELD),
$definition->getSqlType(),
$definition->getSqlParams(),
$position,
]);
}
public function writableValue(mixed $value) : mixed
{
switch (true) {
case $value instanceof \UnitEnum:
return Ulmus::convertEnum($value);
case is_object($value):
return Ulmus::convertObject($value);
case is_array($value):
return json_encode($value);
case is_bool($value):
return (int) $value;
}
return $value;
}
public function splitAlterQuery() : bool
{
return false;
}
}
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace Ulmus\Adapter;
use Ulmus\Migration\FieldDefinition;
use Ulmus\Entity;
class SqlFieldMapper
{
public readonly string $type;
public readonly string $length;
public function __construct(
public FieldDefinition $field,
) {
$this->map();
}
public function map() : void
{
$type = $this->field->type;
$length = $this->field->length;
if ( is_a($type, Entity\Field\Date::class, true) ) {
$type = "DATE";
}
elseif ( is_a($type, Entity\Field\Time::class, true) ) {
$type = "TIME";
}
elseif ( is_a($type, \DateTime::class, true) ) {
$type = "DATETIME";
}
elseif ( enum_exists($type) ) {
# Haven't found a better way yet to check for BackendEnum without an instance of the object
if ( ! method_exists($type, 'tryFrom') ) {
throw new \Ulmus\Exception\BackedEnumRequired(sprintf("You must define your enum as a BackedEnum instead of an UnitEnum for field '%s'.", $this->field->getColumnName()));
}
$type = "string";
}
switch($type) {
case "bool":
$this->type = "TINYINT";
$this->length = 1;
break;
case "array":
$this->type = "JSON";
break;
case "string":
if ($length && $length <= 255) {
$this->type = "VARCHAR";
$this->length = $length;
break;
}
elseif (! $length || ( $length <= 65535 ) ) {
$this->type = "TEXT";
}
elseif ( $length <= 16777215 ) {
$this->type = "MEDIUMTEXT";
}
elseif ($length <= 4294967295) {
$this->type = "LONGTEXT";
}
else {
throw new \Exception("A column with a length bigger than 4GB cannot be created.");
}
# Length is unnecessary on TEXT fields
unset($length);
break;
case "float":
$this->type = "DOUBLE";
default:
if ($length) {
$this->length = $length;
}
$this->type ??= strtoupper($type);
}
$this->postProcess();
}
public function render() : string
{
return $this->type . ( isset($this->length) ? "($this->length)" : "" );
}
public function postProcess() : void
{
}
}
-7
View File
@@ -1,7 +0,0 @@
<?php
namespace Ulmus\Annotation\Classes;
class Collation implements \Notes\Annotation {
}
-7
View File
@@ -1,7 +0,0 @@
<?php
namespace Ulmus\Annotation\Classes;
class Method implements \Notes\Annotation {
}
-27
View File
@@ -1,27 +0,0 @@
<?php
namespace Ulmus\Annotation\Classes;
class Table implements \Notes\Annotation {
public string $name;
public string $database;
public string $schema;
public string $adapter;
public string $engine;
public function __construct($name = null, $engine = null)
{
if ( $name !== null ) {
$this->name = $name;
}
if ( $engine !== null ) {
$this->engine = $engine;
}
}
}
-33
View File
@@ -1,33 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Field implements \Notes\Annotation {
public string $type;
public string $name;
public int $length;
public int $precision;
public array $attributes = [];
public bool $nullable;
public /* mixed */ $default;
public bool $readonly = false;
public function __construct(? string $type = null, ? int $length = null)
{
if ( $type !== null ) {
$this->type = $type;
}
if ( $length !== null ) {
$this->length = $length;
}
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Bigint extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "bigint", ? int $length = null)
{
parent::__construct($type, $length);
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Blob extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "blob", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,13 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class CreatedAt extends \Ulmus\Annotation\Property\Field {
public function __construct()
{
$this->nullable = false;
$this->type = "timestamp";
$this->attributes['default'] = "CURRENT_TIMESTAMP";
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Date extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "date", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Datetime extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "datetime", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,20 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
/**
* Since we need consistancy between the declaration of our ID and FK fields, it
* was decided to extend the Id class instead of Field for this case.
*/
class ForeignKey extends PrimaryKey {
public function __construct(? string $type = null, ? int $length = null)
{
parent::__construct($type, $length);
unset($this->nullable);
$this->attributes['primary_key'] = false;
$this->attributes['auto_increment'] = false;
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Id extends \Ulmus\Annotation\Property\Field\PrimaryKey {
public function __construct()
{
$this->attributes['unsigned'] = true;
$this->attributes['auto_increment'] = true;
parent::__construct('bigint');
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Longblob extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "longblob", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Longtext extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "longtext", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Mediumblob extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "mediumblob", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Mediumtext extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "mediumtext", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class PrimaryKey extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = null, ? int $length = null)
{
$this->nullable = false;
$this->attributes['primary_key'] = true;
parent::__construct($type, $length);
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Text extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "text", ? int $length = null)
{
parent::__construct($type, $length);
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Time extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "time", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Blob extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "tinyblob", ? int $length = null)
{
parent::__construct($type, $length);
}
}
-11
View File
@@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Tinyint extends \Ulmus\Annotation\Property\Field
{
public function __construct(? string $type = "tinyint", ? int $length = null)
{
parent::__construct($type, $length);
}
}
@@ -1,14 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class UpdatedAt extends \Ulmus\Annotation\Property\Field {
public function __construct()
{
$this->nullable = true;
$this->type = "timestamp";
$this->attributes['update'] = "CURRENT_TIMESTAMP";
$this->attributes['default'] = null;
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Filter implements \Notes\Annotation {
public string $method;
public function __construct(string $method = null)
{
if ( $method !== null ) {
$this->method = $method;
}
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class FilterJoin implements \Notes\Annotation {
public string $method;
public function __construct(string $method = null)
{
if ( $method !== null ) {
$this->method = $method;
}
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class GroupBy implements \Notes\Annotation {
public array $fields = [];
public function __construct(...$field)
{
if ( $field ) {
$this->fields = $field;
}
}
}
-5
View File
@@ -1,5 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Having extends Where {}
-31
View File
@@ -1,31 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Join implements \Notes\Annotation {
public string $type;
public string|Stringable $key;
public string|Stringable $foreignKey;
public string $entity;
public string $alias;
public function __construct(? string $type = null, /*? string|Stringable*/ $key = null, /*? string|Stringable*/ $foreignKey = null)
{
if ($type !== null) {
$this->type = $type;
}
if ($key !== null) {
$this->key = $key;
}
if ($foreignKey !== null) {
$this->foreignKey = $foreignKey;
}
}
}
-5
View File
@@ -1,5 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class On extends Where {}
-14
View File
@@ -1,14 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
use Ulmus\Query;
class OrWhere extends Where {
public function __construct(/* ? Stringable */ $field = null, $value = null, ? string $operator = null, ? string $condition = null)
{
parent::__construct($field, $value, $operator, Query\Where::CONDITION_OR);
}
}
-21
View File
@@ -1,21 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class OrderBy implements \Notes\Annotation {
public string $field;
public string $order = "ASC";
public function __construct(string $field = null, string $order = null)
{
if ( $field !== null ) {
$this->field = $field;
}
if ( $order !== null ) {
$this->order = $order;
}
}
}
-88
View File
@@ -1,88 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Relation implements \Notes\Annotation {
public string $type;
public /*stringable*/ $key;
public /* callable */ $generateKey;
public /*stringable*/ $foreignKey;
public array $foreignKeys;
public string $bridge;
public /*stringable*/ $bridgeKey;
public /*stringable*/ $bridgeForeignKey;
public string $entity;
public string $join;
public string $function = "loadAll";
public function __construct(string $type = null)
{
if ( $type !== null ) {
$this->type = $type;
}
}
public function entity() {
try {
$e = $this->entity;
} catch (\Throwable $ex) {
throw new \Exception("Your @Relation annotation seems to be missing an `entity` entry.");
}
return new $e();
}
public function bridge() {
$e = $this->bridge;
return new $e();
}
public function normalizeType() : string
{
return strtolower(str_replace(['-', '_', ' '], '', $this->type));
}
public function isOneToOne() : bool
{
return $this->normalizeType() === 'onetoone';
}
public function isOneToMany() : bool
{
return $this->normalizeType() === 'onetomany';
}
public function isManyToMany() : bool
{
return $this->normalizeType() === 'manytomany';
}
public function function() : string
{
if ($this->function) {
return $this->function;
}
elseif ($this->isOneToOne()) {
return 'load';
}
return 'loadAll';
}
public function hasBridge() : bool
{
return isset($this->bridge);
}
}
@@ -1,5 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Relation;
class Ignore implements \Notes\Annotation {}
-19
View File
@@ -1,19 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Virtual extends Field {
public bool $readonly = true;
public \Closure $closure;
public string $method;
public function __construct(? \Closure $closure = null)
{
if ( $closure !== null ) {
$this->closure = $closure;
}
}
}
-30
View File
@@ -1,30 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
use Ulmus\Query;
class Where implements \Notes\Annotation {
public /* stringable */ $field;
public $value;
public string $operator;
public string $condition;
public function __construct(/* ? Stringable */ $field = null, $value = null, ? string $operator = null, ? string $condition = null)
{
if ( $field !== null ) {
$this->field = $field;
}
if ( $value !== null ) {
$this->value = $value;
}
$this->operator = $operator !== null ? $operator : Query\Where::OPERATOR_EQUAL;
$this->condition = $condition !== null ? $condition : Query\Where::CONDITION_AND;
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class WithJoin implements \Notes\Annotation {
protected array $joins;
public function __construct(/*Stringable|array|null*/ $joins = null)
{
if ( $joins ) {
$this->joins = (array)$joins;
}
}
}
+8 -1
View File
@@ -3,13 +3,20 @@
namespace Ulmus\Attribute;
use Ulmus\Common\EntityField;
use Ulmus\Repository;
class Attribute
{
public static function handleArrayField(null|\Stringable|string|array $field) : mixed
public static function handleArrayField(null|\Stringable|string|array $field, null|string|bool $alias = Repository::DEFAULT_ALIAS, string $separator = ', ') : mixed
{
if ( is_array($field) ) {
$class = array_shift($field);
$field[1] ??= $alias;
if (is_array($field[0])) {
$field[] = $separator;
return $class::fields(...$field);
}
return $class::field(...$field);
}
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Ulmus\Attribute;
enum ConstrainActionEnum : string
{
case Cascade = 'cascade';
case NoAction = 'noaction';
case Restrict = 'restrict';
case SetNull = 'setnull';
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Ulmus\Attribute;
enum IndexTypeEnum : string
{
case Primary = 'primary';
case Index = 'index';
case Unique = 'unique';
case Spatial = 'spatial';
case Fulltext = 'fulltext';
}
@@ -0,0 +1,8 @@
<?php
namespace Ulmus\Attribute\Obj;
interface AdapterAttributeInterface
{
public function adapter() : false|string;
}
+3 -1
View File
@@ -4,5 +4,7 @@ namespace Ulmus\Attribute\Obj;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Collation {
public function __construct(
public string $name = ""
) {}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Ulmus\Attribute\Obj;
use Ulmus\Attribute\Attribute;
use Ulmus\Attribute\ConstrainActionEnum;
use Ulmus\Attribute\IndexTypeEnum;
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class Constrain {
public function __construct(
public array $columns,
public null|IndexTypeEnum $type = null,
public null|string $name = null,
public null|array|string|\Stringable $foreignKey = null,
public null|array|string|\Stringable $references = null,
public ConstrainActionEnum $onDelete = ConstrainActionEnum::NoAction,
public ConstrainActionEnum $onUpdate = ConstrainActionEnum::NoAction,
) {
$this->foreignKey = Attribute::handleArrayField($this->foreignKey, false);
$this->references = Attribute::handleArrayField($this->references, false);
}
}
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Ulmus\Attribute\Obj;
use Ulmus\Attribute\IndexTypeEnum;
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class Index {
public function __construct(
public string|array $column,
public IndexTypeEnum $type = IndexTypeEnum::Unique,
public null|string $name = null,
) {}
}
+7 -1
View File
@@ -3,12 +3,18 @@
namespace Ulmus\Attribute\Obj;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Table {
class Table implements AdapterAttributeInterface {
public function __construct(
public ? string $name = null,
public ? string $database = null,
public ? string $schema = null,
public ? string $adapter = null,
public ? string $engine = null,
public string $description = "",
) {}
public function adapter() : false|string
{
return $this->adapter ?: false;
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Collation {
public function __construct(
public string $name = ""
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Binary extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "binary",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Char extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "char",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Double extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "float",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+6 -3
View File
@@ -3,6 +3,7 @@
namespace Ulmus\Attribute\Property\Field;
use Ulmus\Attribute\Attribute;
use Ulmus\Attribute\ConstrainActionEnum;
/**
* Since we need consistancy between the declaration of our ID and FK fields, it
@@ -12,7 +13,7 @@ use Ulmus\Attribute\Attribute;
class ForeignKey extends PrimaryKey {
public function __construct(
public ? string $name = null,
public ? string $type = 'bigint',
public ? string $type = null,
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [
@@ -22,8 +23,10 @@ class ForeignKey extends PrimaryKey {
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public null|array|string|\Stringable $references = null,
public null|string $relation = null,
public ConstrainActionEnum $onDelete = ConstrainActionEnum::NoAction,
public ConstrainActionEnum $onUpdate = ConstrainActionEnum::NoAction,
) {
$this->references = Attribute::handleArrayField($this->references);
#$this->references = Attribute::handleArrayField($this->references, false);
}
}
+1 -1
View File
@@ -16,6 +16,6 @@ class Id extends \Ulmus\Attribute\Property\Field {
],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public bool $readonly = true,
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Json extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "json",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+8 -4
View File
@@ -5,8 +5,12 @@ namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Time extends \Ulmus\Attribute\Property\Field
{
public function __construct(? string $type = "time", ? int $length = null)
{
parent::__construct($type, $length);
}
public function __construct(
public ? string $name = null,
public ? string $type = "time",
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
+8 -4
View File
@@ -5,8 +5,12 @@ namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Timestamp extends \Ulmus\Attribute\Property\Field
{
public function __construct(? string $type = "timestamp", ? int $length = null)
{
parent::__construct($type, $length);
}
public function __construct(
public ? string $name = null,
public ? string $type = "timestamp",
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
) {}
}
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Varbinary extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "varbinary",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Varchar extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "varchar",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace Ulmus\Attribute\Property\Field;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Year extends \Ulmus\Attribute\Property\Field
{
public function __construct(
public ? string $name = null,
public ? string $type = "year",
public null|int|string $length = null,
public ? int $precision = null,
public array $attributes = [],
public bool $nullable = false,
public mixed $default = null,
public bool $readonly = false,
public string $description = "",
) {}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\IndexTypeEnum;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Index {
public function __construct(
public IndexTypeEnum $type = IndexTypeEnum::Index,
) {}
}
+2 -2
View File
@@ -5,11 +5,11 @@ namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
#[\Attribute]
class Join {
class Join implements ResettablePropertyInterface {
public function __construct(
public string $type,
public null|string|\Stringable|array $key = null,
public null|string|\Stringable $foreignKey = null,
public null|string|\Stringable|array $foreignKey = null,
public null|string $entity = null,
public null|string $alias = null,
) {
+5 -4
View File
@@ -10,10 +10,11 @@ class OrWhere extends Where {
public function __construct(
public string|\Stringable|array $field,
public mixed $value = null,
public null|string $operator = null,
public null|string $condition = Query\Where::CONDITION_OR,
public string $operator = Query\Where::OPERATOR_EQUAL,
public string $condition = Query\Where::CONDITION_OR,
public string|\Stringable|array|null $fieldValue = null,
public null|array|\Closure $generateValue = null,
) {
$this->key = Attribute::handleArrayField($this->key);
parent::__construct($field, $value, $operator, $condition, $fieldValue, $generateValue);
}
}
+2 -2
View File
@@ -5,7 +5,7 @@ namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Relation {
class Relation implements ResettablePropertyInterface {
public function __construct(
public Relation\RelationTypeEnum|string $type,
public \Stringable|string|array $key = "",
@@ -35,7 +35,7 @@ class Relation {
try {
$e = $this->entity;
} catch (\Throwable $ex) {
throw new \Exception("Your @Relation annotation seems to be missing an `entity` entry.");
throw new \Exception("Your @Relation attribute seems to be missing an `entity` entry.");
}
return new $e();
+7 -1
View File
@@ -3,4 +3,10 @@
namespace Ulmus\Attribute\Property\Relation;
#[\Attribute]
class Ignore {}
class Ignore {
public function __construct(
public bool $ignoreExport = false,
) {}
}
@@ -0,0 +1,42 @@
<?php
namespace Ulmus\Attribute\Property\Relation;
use Ulmus\Attribute\Property\Relation;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class ManyToMany extends Relation
{
public function __construct(
public \Stringable|string|array $key = "",
public null|\Closure|array $generateKey = null,
public null|\Stringable|string|array $foreignKey = null,
public null|\Stringable|string|array $foreignField = null,
public array $foreignKeys = [],
public null|string $bridge = null,
public null|\Stringable|string|array $bridgeKey = null,
public null|\Stringable|string|array $bridgeField = null,
public null|\Stringable|string|array $bridgeForeignKey = null,
public null|\Stringable|string|array $field = null,
public null|string $entity = null,
public null|string $join = null,
public null|string $function = null,
) {
parent::__construct(
RelationTypeEnum::manyToMany,
$this->key,
$this->generateKey,
$this->foreignKey,
$this->foreignField,
$this->foreignKeys,
$this->bridge,
$this->bridgeKey,
$this->bridgeField,
$this->bridgeForeignKey,
$this->field,
$this->entity,
$this->join,
$this->function,
);
}
}
@@ -0,0 +1,42 @@
<?php
namespace Ulmus\Attribute\Property\Relation;
use Ulmus\Attribute\Property\Relation;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToMany extends Relation
{
public function __construct(
public \Stringable|string|array $key = "",
public null|\Closure|array $generateKey = null,
public null|\Stringable|string|array $foreignKey = null,
public null|\Stringable|string|array $foreignField = null,
public array $foreignKeys = [],
public null|string $bridge = null,
public null|\Stringable|string|array $bridgeKey = null,
public null|\Stringable|string|array $bridgeField = null,
public null|\Stringable|string|array $bridgeForeignKey = null,
public null|\Stringable|string|array $field = null,
public null|string $entity = null,
public null|string $join = null,
public null|string $function = null,
) {
parent::__construct(
RelationTypeEnum::oneToMany,
$this->key,
$this->generateKey,
$this->foreignKey,
$this->foreignField,
$this->foreignKeys,
$this->bridge,
$this->bridgeKey,
$this->bridgeField,
$this->bridgeForeignKey,
$this->field,
$this->entity,
$this->join,
$this->function,
);
}
}
@@ -0,0 +1,42 @@
<?php
namespace Ulmus\Attribute\Property\Relation;
use Ulmus\Attribute\Property\Relation;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class OneToOne extends Relation
{
public function __construct(
public \Stringable|string|array $key = "",
public null|\Closure|array $generateKey = null,
public null|\Stringable|string|array $foreignKey = null,
public null|\Stringable|string|array $foreignField = null,
public array $foreignKeys = [],
public null|string $bridge = null,
public null|\Stringable|string|array $bridgeKey = null,
public null|\Stringable|string|array $bridgeField = null,
public null|\Stringable|string|array $bridgeForeignKey = null,
public null|\Stringable|string|array $field = null,
public null|string $entity = null,
public null|string $join = null,
public null|string $function = null,
) {
parent::__construct(
RelationTypeEnum::oneToOne,
$this->key,
$this->generateKey,
$this->foreignKey,
$this->foreignField,
$this->foreignKeys,
$this->bridge,
$this->bridgeKey,
$this->bridgeField,
$this->bridgeForeignKey,
$this->field,
$this->entity,
$this->join,
$this->function,
);
}
}
@@ -0,0 +1,8 @@
<?php
namespace Ulmus\Attribute\Property;
interface ResettablePropertyInterface
{
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\IndexTypeEnum;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Unique {
public function __construct(
public IndexTypeEnum $type = IndexTypeEnum::Unique,
) {}
}
+11 -3
View File
@@ -3,11 +3,19 @@
namespace Ulmus\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Virtual extends Field {
class Virtual extends Field implements ResettablePropertyInterface {
public bool $readonly = true;
public function __construct(
public ? string $method = null,
) {}
public null|string|array $method = null,
public ? \Closure $closure = null,
) {
$this->method ??= [ static::class, 'noop' ];
}
public static function noop() : null
{
return null;
}
}
+4 -1
View File
@@ -13,7 +13,7 @@ class Where {
public string $operator = Query\Where::OPERATOR_EQUAL,
public string $condition = Query\Where::CONDITION_AND,
public string|\Stringable|array|null $fieldValue = null,
public null|array $generateValue = null,
public null|array|\Closure $generateValue = null,
) {
$this->field = Attribute::handleArrayField($field);
$this->fieldValue = Attribute::handleArrayField($fieldValue);
@@ -29,6 +29,9 @@ class Where {
throw new \Exception(sprintf("Could not generate value from non-instanciated entity for field %s.", (string) $this->field));
}
}
elseif ($this->fieldValue && $entity) {
throw new \Exception(sprintf("Field value, from %s, could not be included in query since the entity is already loaded; it is meant to be used with a OneToOne relation loaded within a join.", (string) $this->fieldValue));
}
return $this->fieldValue ?? $this->value;
}
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace Ulmus\Cache;
use Psr\SimpleCache\CacheInterface;
trait CacheEventTrait
{
public function __construct(
protected CacheInterface $cache,
protected string $cacheKey,
) {}
public function purgeCache() : void
{
$keys = $this->cache->get($this->cacheKey);
if ( $keys && is_iterable($keys) ) {
$this->cache->deleteMultiple(array_map(fn($e) => sprintf("%s:%s:", $this->cacheKey, $e), $keys));
}
}
}
+117
View File
@@ -0,0 +1,117 @@
<?php
namespace Ulmus;
use Psr\SimpleCache\CacheInterface;
use Ulmus\Entity\EntityInterface;
use Ulmus\QueryBuilder\QueryBuilderInterface;
use Ulmus\Repository\RepositoryInterface;
trait CacheTrait
{
protected CacheInterface $cache;
public function attachCachingObject(CacheInterface $cache) : self
{
$cacheKey = "";
$this->cache = $cache;
# Reading from cache
$this->eventRegister(new class($cacheKey) implements Event\Repository\CollectionFromQueryDatasetInterface {
public function __construct(
protected string & $cacheKey
) {}
public function execute(RepositoryInterface $repository, array &$data): void
{
$this->cacheKey = $repository->queryBuilder->hashSerializedQuery();
$data = $repository->getFromCache( $this->cacheKey) ?: [];
}
});
# Setting to cache
$this->eventRegister(new class($cacheKey) implements Event\Repository\CollectionFromQueryInterface {
public function __construct(
protected string & $cacheKey
) {}
public function execute(RepositoryInterface $repository, EntityCollection $collection): EntityCollection
{
$repository->setToCache( $this->cacheKey, $collection->map(fn(EntityInterface $e) => $e->entityGetDataset(false, true)));
$this->cacheKey = "";
return $collection;
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Insert {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, object|array $entity, ?array $dataset = null, bool $replace = false): void
{
$this->purgeCache();
}
});
# Cache invalidation
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Update {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, object|array $entity, ?array $dataset = null, bool $replace = false): void
{
$this->purgeCache();
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Delete {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, EntityInterface $entity): void
{
$this->purgeCache();
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Truncate {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, ?string $table = null, ?string $alias = null, ?string $schema = null): void
{
$this->purgeCache();
}
});
return $this;
}
protected function entityCacheKey() : string
{
return sprintf("%s.%s", $this->entityResolver->databaseName(), $this->entityResolver->tableName());
}
public function getFromCache(string $key) : mixed
{
$keys = $this->cache->get($this->entityCacheKey(), []);
if (in_array($key, $keys)) {
return $this->cache->get(sprintf("%s:%s:", $this->entityCacheKey(), $key));
}
return null;
}
public function setToCache(string $key, mixed $value) : void
{
$keys = $this->cache->get($this->entityCacheKey(), []);
if (! in_array($key, $keys)) {
$keys[] = $key;
$this->cache->set($this->entityCacheKey(), $keys);
}
$this->cache->set(sprintf("%s:%s:", $this->entityCacheKey(), $key), $value);
}
}
+2 -4
View File
@@ -2,12 +2,10 @@
namespace Ulmus\Common;
use Ulmus\Annotation\Annotation;
use Ulmus\Attribute;
use Ulmus\Attribute\Property\{ Field };
use Ulmus\Migration\FieldDefinition;
use Ulmus\Ulmus,
Ulmus\Adapter\AdapterInterface,
Ulmus\Annotation\Property\Field,
Ulmus\Query\WhereRawParameter;
class EntityField implements WhereRawParameter
@@ -30,7 +28,7 @@ class EntityField implements WhereRawParameter
public function name($useAlias = true) : string
{
$name = $this->entityResolver->searchFieldAnnotation($this->name, [ Attribute\Property\Field::class, Field::class ] )->name ?? $this->name;
$name = $this->entityResolver->searchFieldAnnotation($this->name, [ Field::class ] )->name ?? $this->name;
$name = $this->entityResolver->databaseAdapter()->adapter()->escapeIdentifier($name, AdapterInterface::IDENTIFIER_FIELD);
+50 -159
View File
@@ -2,15 +2,17 @@
namespace Ulmus\Common;
use Notes\Common\ReflectedClass;
use Notes\Common\ReflectedProperty;
use Psr\SimpleCache\CacheInterface;
use Ulmus\Ulmus,
Ulmus\Annotation\Classes\Table,
Ulmus\Annotation\Property\Field,
Ulmus\Annotation\Property\Virtual,
Ulmus\Annotation\Property\Relation,
Ulmus\Attribute;
Ulmus\Attribute\Obj\Table,
Ulmus\Attribute\Obj\AdapterAttributeInterface,
Ulmus\Attribute\Property\Field,
Ulmus\Attribute\Property\Relation,
Ulmus\Attribute\Property\Virtual;
use Notes\Annotation;
use Notes\Common\ReflectedAttribute;
use Notes\ObjectReflection;
@@ -24,13 +26,7 @@ class EntityResolver {
public string $entityClass;
public array $uses;
public array $class;
public array $properties;
public array $methods;
public ReflectedClass $reflectedClass;
protected array $fieldList = [];
@@ -38,14 +34,10 @@ class EntityResolver {
{
$this->entityClass = $entityClass;
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
ObjectReflection::fromClass($entityClass, $cache)->read()
);
$this->resolveAnnotations();
$this->reflectedClass = ObjectReflection::fromClass($entityClass, $cache)->reflectClass();
}
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? array
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : null|ReflectedProperty
{
try{
return $this->fieldList($fieldKey)[$name] ?? null;
@@ -59,43 +51,33 @@ class EntityResolver {
return null;
}
public function searchField($name) : null|array
public function searchField($name) : null|ReflectedProperty
{
try{
return $this->field($name, self::KEY_ENTITY_NAME, false) ?: $this->field($name, self::KEY_COLUMN_NAME, false);
}
catch(\Throwable $e) {
if ( $throwException) {
throw new \InvalidArgumentException("Can't find entity field's column named `$name` from entity {$this->entityClass}");
}
}
return null;
}
public function fieldList($fieldKey = self::KEY_ENTITY_NAME, bool $skipVirtual = false) : array
{
$fieldList = [];
foreach($this->properties as $item) {
foreach($item['tags'] ?? [] as $tag) {
if ( $tag['object'] instanceof Field or $tag['object'] instanceof Attribute\Property\Field ) {
if ( $skipVirtual && ($tag['object'] instanceof Virtual or $tag['object'] instanceof Attribute\Property\Virtual )) {
foreach($this->reflectedClass->getProperties(true) as $item) {
foreach($item->getAttributes() as $tag) {
if ( $tag->object instanceof Field ) {
if ( $skipVirtual && $tag->object instanceof Virtual ) {
break;
}
switch($fieldKey) {
case static::KEY_LC_ENTITY_NAME:
$key = strtolower($item['name']);
$key = strtolower($item->name);
break;
case static::KEY_ENTITY_NAME:
$key = $item['name'];
$key = $item->name;
break;
case static::KEY_COLUMN_NAME:
$key = strtolower($tag['object']->name ?? $item['name']);
$key = strtolower($tag->object->name ?? $item->name);
break;
default:
@@ -112,13 +94,15 @@ class EntityResolver {
return $fieldList;
}
public function relation(string $name) : ? array
public function relation(string $name) : array
{
$property = $this->reflectedClass->getProperties(true)[$name] ?? false;
try{
if ( null !== ( $this->properties[$name] ?? null ) ) {
foreach($this->properties[$name]['tags'] ?? [] as $tag) {
if ( $tag['object'] instanceof Relation or $tag['object'] instanceof Attribute\Property\Relation ) {
return $this->properties[$name];
if ( $property ) {
foreach($property->getAttributes() as $tag) {
if ( $tag->object instanceof Relation ) {
return $property;
}
}
}
@@ -126,12 +110,8 @@ class EntityResolver {
return [];
}
catch(\Throwable $e) {
# if ( $throwException) {
throw new \InvalidArgumentException("Can't find entity relation's column named `$name` from entity {$this->entityClass}");
# }
}
return null;
}
public function searchFieldAnnotation(string $field, array|object|string $annotationType, bool $caseSensitive = true) : ? object
@@ -139,25 +119,19 @@ class EntityResolver {
return $this->searchFieldAnnotationList($field, $annotationType, $caseSensitive)[0] ?? null;
}
public function searchFieldAnnotationList(string $field, array|object|string $annotationType, bool $caseSensitive = true) : array
public function searchFieldAnnotationList(string $field, array|object|string $attributeType, bool $caseSensitive = true) : false|array
{
$list = [];
$search = $caseSensitive ? $this->properties : array_change_key_case($this->properties, \CASE_LOWER);
$properties = $this->reflectedClass->getProperties(true);
$annotations = is_array($annotationType) ? $annotationType : [ $annotationType ];
$search = $caseSensitive ? $properties : array_change_key_case($properties, \CASE_LOWER);
if ( null !== ( $search[$field] ?? null ) ) {
foreach($search[$field]['tags'] ?? [] as $tag) {
foreach($annotations as $annotation) {
if ( $tag['object'] instanceof $annotation ) {
$list[] = $tag['object'];
}
}
}
return array_map(fn(ReflectedAttribute $e) => $e->object, $search[$field]->getAttributes((array) $attributeType));
}
return $list;
return false;
}
public function tableName($required = false) : string
@@ -173,7 +147,7 @@ class EntityResolver {
return $table->name ?? "";
}
public function tableAnnotation($required = false) : Table|Attribute\Obj\Table
public function tableAnnotation($required = false) : null|Table
{
if ( null === $table = $this->getTableAttribute() ) {
if ($required) {
@@ -184,17 +158,17 @@ class EntityResolver {
return $table;
}
public function databaseName() : ? string
public function databaseName() : null|string
{
return $this->tableAnnotation(false)->database ?? $this->databaseAdapter()->adapter()->databaseName() ?? null;
}
public function sqlAdapter() : \Ulmus\ConnectionAdapter
{
if ( null !== $table = $this->getTableAttribute() ) {
if ( $table->adapter ?? null ) {
if ( null === ( $adapter = \Ulmus\Ulmus::$registeredAdapters[$table->adapter] ?? null ) ) {
throw new \Exception("Requested database adapter `{$table->adapter}` is not registered.");
if ( $adapterObj = $this->getAdapterInterfaceAttribute()) {
if ( false !== $adapterName = $adapterObj->adapter() ) {
if ( null === ( $adapter = \Ulmus\Ulmus::$registeredAdapters[$adapterName] ?? null ) ) {
throw new \Exception("Requested database adapter `$adapterName` is not registered.");
}
else {
return $adapter;
@@ -213,7 +187,7 @@ class EntityResolver {
return $this->sqlAdapter();
}
public function schemaName(bool $required = false) : ? string
public function schemaName(bool $required = false) : null|string
{
if ( null === $table = $this->getTableAttribute() ) {
throw new \LogicException("Your entity {$this->entityClass} seems to be missing a @Table() annotation");
@@ -226,10 +200,10 @@ class EntityResolver {
return $table->schema ?? null;
}
public function getPrimaryKeyField() : ? array
public function getPrimaryKeyField() : null|array
{
foreach($this->fieldList() as $key => $value) {
$field = $this->searchFieldAnnotation($key, [ Attribute\Property\Field::class, Field::class ]);
$field = $this->searchFieldAnnotation($key, [ Field::class ]);
if ( null !== $field ) {
if ( false !== ( $field->attributes['primary_key'] ?? false ) ) {
return [ $key => $field ];
@@ -250,107 +224,24 @@ class EntityResolver {
return null;
}
protected function getAdapterInterfaceAttribute() : null|object
{
return $this->getAttributeImplementing(AdapterAttributeInterface::class);
}
protected function getTableAttribute()
{
return $this->getAnnotationFromClassname(Attribute\Obj\Table::class, false) ?: $this->getAnnotationFromClassname( Table::class );
return $this->getAttributeImplementing(Table::class);
}
/**
* Transform an annotation into it's object's counterpart
*/
public function getAnnotationFromClassname(string $className, bool $throwError = true) : ? object
public function getAttributeImplementing(string $interface) : null|object
{
if ( $name = $this->uses[$className] ?? false ) {
foreach(array_reverse($this->class['tags']) as $item) {
if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item);
foreach ($this->reflectedClass->getAttributes(true) as $item) {
if ($item->object instanceof $interface) {
return $item->object;
}
foreach($this->properties as $item) {
foreach(array_reverse($item['tags']) as $item) {
if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item);
}
}
}
foreach($this->methods as $item) {
foreach(array_reverse($item['tags']) as $item) {
if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item);
}
}
}
}
if ($throwError) {
throw new \TypeError("Annotation `$className` could not be found within your object `{$this->entityClass}`");
}
}
elseif ($throwError) {
throw new \InvalidArgumentException("Class `$className` was not found within {$this->entityClass} uses statement (or it's children / traits)");
}
return null;
}
public function instanciateAnnotationObject(array|\ReflectionAttribute $tagDefinition) : object
{
if ($tagDefinition instanceof \ReflectionAttribute) {
$obj = $tagDefinition->newInstance();
}
else {
$arguments = $this->extractArguments($tagDefinition['arguments']);
if (false === $class = array_search($tagDefinition['tag'], $this->uses)) {
throw new \InvalidArgumentException("Annotation class `{$tagDefinition['tag']}` was not found within {$this->entityClass} uses statement (or it's children / traits)");
}
$obj = new $class(... $arguments['constructor']);
foreach ($arguments['setter'] as $key => $value) {
$obj->$key = $value;
}
}
return $obj;
}
/**
* Extracts arguments from an Annotation definition, easing object's declaration.
*/
protected function extractArguments(array $arguments) : array
{
$list = [
'setter' => [],
'constructor' => [],
];
ksort($arguments);
foreach($arguments as $key => $value) {
$list[ is_int($key) ? 'constructor' : 'setter' ][$key] = $value;
}
return $list;
}
protected function resolveAnnotations()
{
foreach($this->class['tags'] as &$tag) {
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
foreach($this->properties as &$property) {
foreach($property['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
foreach($this->methods as &$method) {
foreach($method['tags'] as &$tag){
$tag['object'] ??= $this->instanciateAnnotationObject($tag);
}
}
}
}
+67 -16
View File
@@ -11,27 +11,37 @@ class PdoObject extends PDO {
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, [ $sql, $parameters ]);
try {
if (false !== ( $statement = $this->prepare($sql) )) {
$statement = $this->execute($statement, $parameters, false);
$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;
}
}
catch (\PDOException $e) {
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
public function __destruct()
{
if ($this->onClose ?? null) {
call_user_func($this->onClose, $this);
}
}
/**
* @deprecated
*/
public function runQuery(string $sql, array $parameters = []): ? PDOStatement
public function runQuery(string $sql, array $parameters = []): ? static
{
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
@@ -40,47 +50,56 @@ class PdoObject extends PDO {
return $this->execute($statement, $parameters, true);
}
}
catch (\PDOException $e) {
catch(\PDOException $pdo) {
if ( substr($pdo->getMessage(), 0, 30) !== 'There is no active transaction' ) {
throw $pdo;
}
}
catch (\Throwable $e) {
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
}
return null;
}
public function runInsertQuery(string $sql, array $parameters = [])
public function runInsertQuery(string $sql, array $parameters = []) : ? static
{
return $this->runQuery($sql, $parameters);
}
public function runUpdateQuery(string $sql, array $parameters = [])
public function runUpdateQuery(string $sql, array $parameters = []) : ? static
{
return $this->runQuery($sql, $parameters);
}
public function runDeleteQuery(string $sql, array $parameters = []): ? PDOStatement
public function runDeleteQuery(string $sql, array $parameters = []) : ? static
{
return $this->runQuery($sql, $parameters);
}
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true): ? PDOStatement
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? static
{
$this->executionStatus = false;
$this->lastInsertId = null;
try {
if ( ! $this->inTransaction() ) {
$this->beginTransaction();
}
$this->executionStatus = empty($parameters) ? $statement->execute() : $statement->execute($parameters);
$this->bindVariables($statement, $parameters);
$this->executionStatus = $statement->execute();
if ( $this->executionStatus ) {
$statement->lastInsertId = $this->lastInsertId();
$this->lastInsertId = $this->lastInsertId();
$this->rowCount = $statement->rowCount();
if ( $commit ) {
$this->commit();
}
return $statement;
return $this;
}
else {
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
@@ -97,4 +116,36 @@ class PdoObject extends PDO {
return null;
}
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) {
switch (strtolower(gettype($value))) {
#$type = Pdo::PARAM_BOOL;
#break;
case "boolean":
case "integer":
$type = Pdo::PARAM_INT;
break;
case "null":
$type = Pdo::PARAM_NULL;
break;
case "string":
default:
$type = Pdo::PARAM_STR;
}
$statement->bindValue($key, $value, $type);
}
}
}
}
}
+40
View File
@@ -0,0 +1,40 @@
<?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 false;
}
public function rollback() : bool
{
if (0 !== $this->openedTransaction) {
return $this->exec('ROLLBACK TO transaction_' . $this->openedTransaction--) !== false;
}
return parent::rollback();
}
protected function openTransaction() : bool
{
return parent::beginTransaction();
}
}
+27
View File
@@ -0,0 +1,27 @@
<?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 false;
}
protected function openTransaction(): bool
{
return $this->exec("BEGIN IMMEDIATE TRANSACTION") !== false;
}
}
+8 -3
View File
@@ -56,7 +56,8 @@ abstract class Sql {
$this->identifier = $identifier;
}
public function __toString() {
public function __toString() : string
{
return $this->identifier;
}
};
@@ -67,7 +68,7 @@ abstract class Sql {
return static::identifier($sql);
}
public static function escape($value)
public static function escape($value) : mixed
{
switch(true) {
case is_object($value):
@@ -83,8 +84,12 @@ abstract class Sql {
return $value;
}
public static function parameter($value) : string
public static function collate(string $name) : string
{
if ( ! preg_match('/^[a-z0-9$_]+$/i',$name) ) {
throw new \InvalidArgumentException(sprintf("Given identifier '%s' should contains supported characters in function name (a-Z, 0-9, $ and _)", $name));
}
return static::raw(sprintf("COLLATE %s", $name));
}
}
+9 -12
View File
@@ -2,26 +2,23 @@
namespace Ulmus;
use Psr\SimpleCache\CacheInterface;
use Ulmus\Adapter\AdapterInterface;
use Ulmus\Common\PdoObject;
class ConnectionAdapter
{
public string $name;
public array $configuration;
protected AdapterInterface $adapter;
protected PdoObject $pdo;
public function __construct(string $name = "default", array $configuration = [], bool $default = false)
{
$this->name = $name;
$this->configuration = $configuration;
public function __construct(
public string $name = "default",
protected array $configuration = [],
public bool $default = false,
public ? CacheInterface $cacheObject = null
) {
Ulmus::registerAdapter($this, $default);
}
@@ -33,7 +30,7 @@ class ConnectionAdapter
$this->adapter = $this->instanciateAdapter($adapterName);
}
else {
throw new \InvalidArgumentException("Adapter not found within your configuration array.");
throw new \InvalidArgumentException(sprintf("Adapter not found within your configuration array. (%s)", json_encode($connection)));
}
$this->adapter->setup($connection);
@@ -77,7 +74,7 @@ class ConnectionAdapter
* @param string $name An Ulmus adapter or full class name implementing AdapterInterface
* @return AdapterInterface
*/
protected function instanciateAdapter($name) : AdapterInterface
protected function instanciateAdapter(string $name) : AdapterInterface
{
$class = substr($name, 0, 2) === "\\" ? $name : sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name);
+153
View File
@@ -0,0 +1,153 @@
<?php
namespace Ulmus\Entity;
use Generator;
use Ulmus\Attribute\Property\Field;
use Ulmus\Attribute\Property\Relation;
use Ulmus\Common\EntityField;
use Ulmus\Common\EntityResolver;
use Ulmus\Ulmus;
class DatasetHandler
{
public function __construct(
protected EntityResolver $entityResolver,
protected bool $entityStrictFieldsDeclaration = false,
) {}
public function pull(object $entity) : Generator
{
foreach($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
$attribute = $this->entityResolver->searchFieldAnnotation($key,[ Field::class ]);
if ( $entity->__isset($key) ) {
yield $attribute->name ?? $key => $entity->$key;
}
elseif ( $field->allowsNull() ) {
yield $attribute->name ?? $key => null;
}
}
}
public function push(iterable $dataset) : Generator|array
{
$unmatched = [];
foreach($dataset as $key => $value) {
$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 ($this->entityStrictFieldsDeclaration ) {
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
}
else {
$unmatched[$key] = $value;
}
continue;
}
$type = $field->getTypes()[0];
if ( is_null($value) ) {
yield $field->name => null;
}
elseif ( $field->expectType('array') ) {
if ( is_string($value)) {
if (substr($value, 0, 1) === "a") {
yield $field->name => unserialize($value);
}
else {
$data = json_decode($value, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s", json_last_error_msg(), $value));
}
yield $field->name => $data;
}
}
elseif ( is_array($value) ) {
yield $field->name => $value;
}
}
elseif ( EntityField::isScalarType($type->type) ) {
if ( $type->type === 'string' ) {
$attribute = $this->entityResolver->searchFieldAnnotation($field->name, [ Field::class ] );
if ( $attribute->length ?? null ) {
$value = mb_substr($value, 0, $attribute->length);
}
}
elseif ( $type->type === 'bool' ) {
$value = (bool) $value;
}
yield $field->name => $value;
}
elseif ( $value instanceof \UnitEnum ) {
yield $field->name => $value;
}
elseif (enum_exists($type->type)) {
yield $field->name => $type->type::from($value);
}
elseif ( ! $type->builtIn ) {
try {
yield $field->name => Ulmus::instanciateObject($type->type, [ $value ]);
}
catch(\Error $e) {
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name));
}
}
}
return $unmatched;
}
public function pullRelation(object $entity) : Generator
{
foreach($this->entityResolver->reflectedClass->getProperties(true) as $name => $field){
$relation = $this->entityResolver->searchFieldAnnotation($name, [ Relation::class ] );
if ($relation) {
$ignore = $this->entityResolver->searchFieldAnnotation($name, [ Relation\Ignore::class ] );
if ($ignore && $ignore->ignoreExport) {
if ( $relation->isOneToOne() ) {
# @TODO TO INCLUDED INTO getTypes() RETURNED CLASS WHEN DONE !
yield $name => ( new \ReflectionClass($field->getTypes()[0]) )->newInstanceWithoutConstructor();
}
else {
# empty collection
yield $name => [];
}
continue;
}
# @TODO Must fix recursive bug.. this last check is way too basic to work
if ( $entity->__isset($name) && ($relation->entity ?? $relation->bridge) !== static::class ) {
if ( null !== $value = $entity->__isset($name) ?? null ) {
if ( is_iterable($value) ) {
$list = [];
foreach($value as $entityObj) {
$list[] = $entityObj->entityGetDataset(false);
}
yield $name => $list;
}
elseif ( is_object($value) ) {
yield $name => $value->entityGetDataset(false);
}
}
}
}
}
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Ulmus\Entity;
use Ulmus\Common\{ EntityField, EntityResolver };
use Ulmus\{ConnectionAdapter,
EntityCollection,
QueryBuilder\QueryBuilderInterface,
Repository,
SearchRequest\SearchableInterface};
interface EntityInterface extends SearchableInterface /* extends \JsonSerializable */
{
public function fromArray(iterable $dataset) : static;
public function entityGetDataset(bool $includeRelations = false, bool $returnSource = false) : array;
public function toArray($includeRelations = false, array $filterFields = null) : array;
public function toCollection() : EntityCollection;
public function isLoaded() : bool;
public function jsonSerialize() : mixed;
public static function resolveEntity() : EntityResolver;
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) : Repository;
public static function entityCollection(...$arguments) : EntityCollection;
public static function queryBuilder() : QueryBuilderInterface;
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField;
public static function fields(array $fields, null|string|false $alias = Repository::DEFAULT_ALIAS, string $separator = ', ') : string;
}
+10 -2
View File
@@ -12,12 +12,20 @@ class Datetime extends \DateTime implements EntityObjectInterface {
{
$value = $arguments[0];
try {
# From Timestamp
if ( is_numeric($value) ) {
return new static("@$value");
$obj = new static("@$value");
}
else {
$obj = new static($value);
}
}
catch(\Throwable $ex) {
throw new \Exception(sprintf("An error occured trying to instanciate from '%s'. %s", $value, $ex->getMessage()), $ex->getCode(), $ex);
}
return new static($value);
return $obj;
}
public function save()
+46 -2
View File
@@ -2,6 +2,7 @@
namespace Ulmus\Entity\InformationSchema;
use Notes\Common\ReflectedProperty;
use Ulmus\Entity\Field\Datetime;
use Ulmus\{Attribute\Obj\Table};
@@ -12,6 +13,9 @@ class Column
{
use \Ulmus\EntityTrait;
#[Field\Id]
public ? id $srs_id;
#[Field(name: "TABLE_CATALOG", length: 512)]
public string $tableCatalog;
@@ -72,10 +76,50 @@ class Column
#[Field(name: "COLUMN_COMMENT", length: 1024)]
public string $comment;
#[Field(name: "IS_GENERATED", length: 6)]
public string $generated;
# #[Field(name: "IS_GENERATED", length: 6)]
# public string $generated;
#[Field(name: "GENERATION_EXPRESSION", type: "longtext")]
public ? string $generationExpression;
public function matchFieldDefinition(ReflectedProperty $definition) : bool
{
$nullable = $this->nullable === 'YES';
if ($nullable !== $definition->allowsNull()) {
return false;
}
if ( isset($definition->value) && $this->canHaveDefaultValue() ) {
if ( $definition->value !== $this->defaultValue()) {
return false;
}
}
elseif (! isset($definition->value)) {
if ( ! $this->defaultValueIsNull() ) {
return false;
}
}
return true;
}
protected function defaultValueIsNull() : bool
{
return $this->defaultValue() === null;
}
protected function defaultValue() : mixed
{
if (is_numeric($this->default)) {
return (int) $this->default;
}
return $this->default;
}
protected function canHaveDefaultValue() : bool
{
return true;
}
}
+8 -2
View File
@@ -5,7 +5,7 @@ namespace Ulmus\Entity\InformationSchema;
use Ulmus\EntityCollection,
Ulmus\Entity\Field\Datetime;
use Ulmus\{Attribute\Obj\Table as TableObj};
use Ulmus\{Attribute\Obj\Table as TableObj, Entity\EntityInterface};
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
#[TableObj(name: "tables", database: "information_schema")]
@@ -83,6 +83,12 @@ class Table
public ? string $temporary;
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
#[Where('TABLE_SCHEMA', [ Column::class, 'tableSchema' ])]
#[Where(field: 'TABLE_SCHEMA', generateValue: [ Table::class, 'getSchema' ])]
public EntityCollection $columns;
# Awaiting PHP 8.5 https://wiki.php.net/rfc/closures_in_const_expr
public static function getSchema(Table $entity) : string
{
return $entity->schema;
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Ulmus\Entity\Mysql;
class Column extends \Ulmus\Entity\InformationSchema\Column
{
# TODO ! Handle FUNCTIONAL default value
protected function canHaveDefaultValue(): bool
{
return ! in_array(strtoupper($this->dataType), [
'BLOB', 'TINYBLOB', 'MEDIUMBLOB', 'LONGBLOB', 'JSON', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'GEOMETRY'
]);
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Ulmus\Entity\Mysql;
use Ulmus\EntityCollection,
Ulmus\Entity\Field\Datetime;
use Ulmus\{Attribute\Obj\Table as TableObj};
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
#[TableObj(name: "tables", database: "information_schema")]
class Table extends \Ulmus\Entity\InformationSchema\Table
{
#[Relation(type: "oneToMany", key: "name", foreignKey: [ Column::class, 'tableName' ], entity: Column::class)]
#[Where('TABLE_SCHEMA', generateValue: [ Table::class, 'getSchema' ])]
public EntityCollection $columns;
}
+26
View File
@@ -2,6 +2,7 @@
namespace Ulmus\Entity\Sqlite;
use Notes\Common\ReflectedProperty;
use Ulmus\EntityCollection;
use Ulmus\{Attribute\Obj\Table};
@@ -32,4 +33,29 @@ class Column
#[Field(name: "pk")]
public bool $primaryKey;
public function matchFieldDefinition(ReflectedProperty $definition) : bool
{
if ($this->notNull === $definition->allowsNull()) {
return false;
}
if (isset($definition->value)) {
if ( $definition->value !== $this->defaultValue) {
return false;
}
}
elseif (! isset($definition->value)) {
if ( ! $this->defaultValueIsNull() ) {
return false;
}
}
return true;
}
protected function defaultValueIsNull() : bool
{
return $this->defaultValue === null || $this->defaultValue === "NULL";
}
}
+5 -3
View File
@@ -4,7 +4,7 @@ namespace Ulmus\Entity\Sqlite;
use Ulmus\EntityCollection;
use Ulmus\Query\{From, Select};
use Ulmus\{Attribute\Obj\Table, Repository};
use Ulmus\{Attribute\Obj\Table, Repository, Ulmus};
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
#[Table(name: "sqlite_master")]
@@ -27,12 +27,14 @@ class Schema
#[Field]
public ? string $sql;
##[Relation("oneToMany", key: "tableName", foreignKey: "tableName", entity: Column::class)]
#[Virtual(method: "filterColumns")]
public EntityCollection $columns;
public function filterColumns() : EntityCollection
{
return ( new Repository\SqliteRepository(Column::class) )->pragma('table_info', $this->tableName)->collectionFromQuery();
$adapter = Ulmus::$registeredAdapters[$this->loadedFromAdapter];
return Column::repository(Repository\SqliteRepository::DEFAULT_ALIAS, $adapter)
->pragma('table_info', $this->tableName)->collectionFromQuery();
}
}
+6 -1
View File
@@ -3,6 +3,7 @@
namespace Ulmus\Entity\Sqlite;
use Ulmus\ConnectionAdapter;
use Ulmus\EntityCollection;
use Ulmus\Repository;
#[\Ulmus\Attribute\Obj\Table(name: "sqlite_master")]
@@ -10,12 +11,16 @@ class Table extends Schema
{
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null): Repository
{
return new class(static::class, $alias, $adapter) extends Repository\SqliteRepository
$repository = new class(static::class, $alias, $adapter) extends Repository\SqliteRepository
{
public function finalizeQuery(): void
{
$this->select(Table::field('tableName'))->groupBy(Table::field('tableName'));
}
};
$repository->entityClass = static::class;
return $repository;
}
}
+51 -3
View File
@@ -4,11 +4,11 @@ namespace Ulmus;
use Generator;
class EntityCollection extends \ArrayObject {
class EntityCollection extends \ArrayObject implements \JsonSerializable {
public ? string $entityClass = null;
public static function instance(array $data, null|string $entityClass) : self
public static function instance(array $data = [], null|string $entityClass = null) : self
{
$instance = new static($data);
$instance->entityClass = $entityClass;
@@ -57,7 +57,7 @@ class EntityCollection extends \ArrayObject {
return $this->filtersCollection($callback, true, false)->toArray();
}
public function filtersCount(Callable $callback) : array
public function filtersCount(Callable $callback) : int
{
return $this->filtersCollection($callback, true, false)->count();
}
@@ -222,6 +222,17 @@ class EntityCollection extends \ArrayObject {
return $list;
}
public function walk(Callable $callback) : self
{
return $this->filtersCollection($callback);
}
public function sum($field) : float|int
{
return array_sum($this->column($field));
}
public function unique(\Stringable|callable|string $field, bool $strict = false) : self
{
$list = [];
@@ -315,6 +326,26 @@ class EntityCollection extends \ArrayObject {
return $list;
}
public function toFilteredArray(Callable $callback) : array {
$list = [];
foreach($this as $entity) {
$list[] = $callback($entity);
}
return $list;
}
public function toJsonArray(bool $includeRelations = false) : array {
$list = [];
foreach($this as $entity) {
$list[] = $entity instanceof \JsonSerializable ? $entity->jsonSerialize() : $entity->toArray($includeRelations);
}
return $list;
}
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
{
foreach($datasets as $dataset) {
@@ -335,6 +366,23 @@ class EntityCollection extends \ArrayObject {
return ( new $className() )->fromArray($dataset);
}
public function arrayToEntities(array $list, ? string /*stringable*/ $entityClass = null) : self
{
$collection = new static();
$collection->entityClass = $entityClass ?? $this->entityClass;
foreach($list as $dataset) {
$collection->append($this->arrayToEntity($dataset, $entityClass));
}
return $collection;
}
public function jsonSerialize(): mixed
{
return $this->toJsonArray(true);
}
public function append($value) : void
{
if ( is_array($value) ) {
+100 -147
View File
@@ -3,15 +3,28 @@
namespace Ulmus;
use Notes\Attribute\Ignore;
use Ulmus\{ Repository, Query, Common\EntityResolver, Common\EntityField };
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
use Ulmus\Annotation\Property\{ Field, Filter, FilterJoin, Relation, OrderBy, Where, OrWhere, Join, Virtual, On, WithJoin, };
use Ulmus\Annotation\Property\Field\{ PrimaryKey, Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob, };
use Ulmus\Annotation\Property\Relation\{ Ignore as RelationIgnore };
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\{Attribute\Property\Join,
Attribute\Property\Relation,
Attribute\Property\ResettablePropertyInterface,
Attribute\Property\Virtual,
Common\EntityResolver,
Common\EntityField,
Entity\DatasetHandler,
Entity\EntityInterface,
QueryBuilder\QueryBuilderInterface};
use Ulmus\SearchRequest\{Attribute\SearchParameter,
SearchMethodEnum,
SearchRequestInterface,
SearchRequestFromRequestTrait,
SearchRequestPaginationTrait};
trait EntityTrait {
use EventTrait;
#[Ignore]
public array $entityLoadedDataset = [];
#[Ignore]
protected bool $entityStrictFieldsDeclaration = false;
@@ -19,10 +32,23 @@ trait EntityTrait {
protected array $entityDatasetUnmatchedFields = [];
#[Ignore]
public array $entityLoadedDataset = [];
protected DatasetHandler $datasetHandler;
#[Ignore]
public function __construct() {
public function __construct(iterable|null $dataset = null)
{
$this->initializeEntity($dataset);
}
#[Ignore]
public function initializeEntity(iterable|null $dataset = null) : void
{
if ($dataset) {
$this->fromArray($dataset);
}
$this->datasetHandler = new DatasetHandler(static::resolveEntity(), $this->entityStrictFieldsDeclaration);
$this->resetVirtualProperties();
}
@@ -31,112 +57,27 @@ trait EntityTrait {
{
$loaded = $this->isLoaded();
$entityResolver = $this->resolveEntity();
$handler = $this->datasetHandler->push($dataset);
foreach($dataset as $key => $value) {
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? null;
$field ??= $entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false);
if ( $field === null ) {
if ($this->entityStrictFieldsDeclaration ) {
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
}
else {
$this->entityDatasetUnmatchedFields[$key] = $value;
}
}
elseif ( is_null($value) ) {
$this->{$field['name']} = null;
}
elseif ( $field['type'] === 'array' ) {
if ( is_string($value)) {
if (substr($value, 0, 1) === "a") {
$this->{$field['name']} = unserialize($value);
}
else {
$data = json_decode($value, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s", json_last_error_msg(), $value));
foreach($handler as $field => $value) {
$this->$field = $value;
}
$this->{$field['name']} = $data;
}
}
elseif ( is_array($value) ) {
$this->{$field['name']} = $value;
}
}
elseif ( EntityField::isScalarType($field['type']) ) {
if ( $field['type'] === 'string' ) {
$annotation = $entityResolver->searchFieldAnnotation($field['name'], [ Attribute\Property\Field::class, Field::class ] );
if ( $annotation->length ?? null ) {
$value = mb_substr($value, 0, $annotation->length);
}
}
elseif ( $field['type'] === 'bool' ) {
$value = (bool) $value;
}
$this->{$field['name']} = $value;
}
elseif ( $value instanceof \UnitEnum ) {
$this->{$field['name']} = $value;
}
elseif (enum_exists($field['type'])) {
$this->{$field['name']} = $field['type']::from($value);
}
elseif ( ! $field['builtin'] ) {
try {
$this->{$field['name']} = Ulmus::instanciateObject($field['type'], [ $value ]);
}
catch(\Error $e) {
$f = $field['type'];
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field['name']));
}
}
$this->entityDatasetUnmatchedFields = $handler->getReturn();
# Keeping original data to diff on UPDATE query
if ( ! $loaded /* || $isLoadedDataset */ ) {
#if ( $field !== null ) {
# $annotation = $entityResolver->searchFieldAnnotation($field['name'], new Field() );
# $this->entityLoadedDataset[$annotation ? $annotation->name : $field['name']] = $dataset; # <--------- THIS TO FIX !!!!!!
#}
$this->entityLoadedDataset = array_change_key_case($dataset, \CASE_LOWER);
if ( ! $loaded ) {
$this->entityLoadedDataset = array_change_key_case(is_array($dataset) ? $dataset : iterator_to_array($dataset), \CASE_LOWER);
}
elseif ($overwriteDataset) {
$this->entityLoadedDataset = array_change_key_case($dataset, \CASE_LOWER) + $this->entityLoadedDataset;
}
$this->entityLoadedDataset = array_change_key_case(is_array($dataset) ? $dataset : iterator_to_array($dataset), \CASE_LOWER) + $this->entityLoadedDataset;
}
return $this;
}
#[Ignore]
public function resetVirtualProperties() : self
{
foreach($this->resolveEntity()->properties as $prop => $property) {
if ( empty($property['builtin']) ) {
foreach($property['tags'] as $tag) {
if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) {
unset($this->$prop);
}
}
}
}
return $this;
}
#[Ignore]
public function fromArray(iterable $dataset) : self
{
return $this->entityFillFromDataset($dataset);
}
public function entityGetDataset(bool $includeRelations = false, bool $returnSource = false) : array
public function entityGetDataset(bool $includeRelations = false, bool $returnSource = false, bool $rewriteValue = true) : array
{
if ( $returnSource ) {
return $this->entityLoadedDataset;
@@ -144,40 +85,13 @@ trait EntityTrait {
$dataset = [];
$entityResolver = $this->resolveEntity();
foreach($entityResolver->fieldList(Common\EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
$annotation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Field::class, Field::class ]);
if ( isset($this->$key) ) {
$dataset[$annotation->name ?? $key] = static::repository()->adapter->adapter()->writableValue($this->$key);
}
elseif ( $field['nullable'] ) {
$dataset[$annotation->name ?? $key] = null;
}
foreach($this->datasetHandler->pull($this) as $field => $value) {
$dataset[$field] = $rewriteValue ? static::repository()->adapter->adapter()->writableValue($value) : $value;
}
# @TODO Must fix recursive bug !
if ($includeRelations) {
foreach($entityResolver->properties as $name => $field){
$relation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Relation::class. Relation::class ] );
if ( $relation && isset($this->$name) && ($relation->entity ?? $relation->bridge) !== static::class ) {
if ( null !== $value = $this->$name ?? null ) {
if ( is_iterable($value) ) {
$list = [];
foreach($value as $entity) {
$list[] = $entity->entityGetDataset(false);
}
$dataset[$name] = $list;
}
elseif ( is_object($value) ) {
$dataset[$name] = $value->entityGetDataset(false);
}
}
}
foreach($this->datasetHandler->pullRelation($this) as $field => $object) {
$dataset[$field] = $object;
}
}
@@ -185,9 +99,34 @@ trait EntityTrait {
}
#[Ignore]
public function toArray($includeRelations = false, array $filterFields = null) : array
public function resetVirtualProperties() : self
{
$dataset = $this->entityGetDataset($includeRelations);
foreach($this->resolveEntity()->reflectedClass->getProperties(true) as $field => $property) {
foreach($property->attributes as $tag) {
if ( $tag->object instanceof ResettablePropertyInterface ) {
unset($this->$field);
}
}
}
return $this;
}
#[Ignore]
public function fromArray(iterable|EntityInterface $dataset) : static
{
if ($dataset instanceof EntityInterface) {
$dataset = $dataset->toArray();
}
return $this->entityFillFromDataset($dataset);
}
#[Ignore]
public function toArray($includeRelations = false, array $filterFields = null, bool $rewriteValue = true) : array
{
$dataset = $this->entityGetDataset($includeRelations, false, $rewriteValue);
return $filterFields ? array_intersect_key($dataset, array_flip($filterFields)) : $dataset;
}
@@ -229,10 +168,7 @@ trait EntityTrait {
#[Ignore]
public function __isset(string $name) : bool
{
#if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
# return isset($this->{$relation->key});
#}
$rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ]);
$rel = static::resolveEntity()->searchFieldAnnotation($name, [ Attribute\Property\Relation::class ]);
if ( $this->isLoaded() && $rel ) {
return true;
@@ -256,10 +192,6 @@ trait EntityTrait {
#[Ignore]
public function __clone()
{
foreach($this as $prop) {
}
if ( null !== $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) {
$key = key($pkField);
@@ -270,7 +202,7 @@ trait EntityTrait {
#[Ignore]
public function jsonSerialize() : mixed
{
return $this->entityGetDataset();
return $this->entityGetDataset(true, false, false);
}
#[Ignore]
@@ -295,22 +227,43 @@ trait EntityTrait {
}
#[Ignore]
public static function queryBuilder() : QueryBuilder
public static function queryBuilder() : QueryBuilderInterface
{
return Ulmus::queryBuilder(static::class);
}
#[Ignore]
public static function field($name, ? string $alias = Repository::DEFAULT_ALIAS) : EntityField
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField
{
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : Repository::DEFAULT_ALIAS, Ulmus::resolveEntity(static::class));
$default = ( $alias === false ? '' : static::repository()::DEFAULT_ALIAS ); # bw compatibility, to be deprecated
$alias = $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default;
return new EntityField(static::class, $name, $alias, Ulmus::resolveEntity(static::class));
}
#[Ignore]
public static function fields(array $fields, ? string $alias = Repository::DEFAULT_ALIAS) : string
public static function fields(array $fields, null|string|false $alias = Repository::DEFAULT_ALIAS, string $separator = ', ') : string
{
return implode(', ', array_map(function($item) use ($alias){
return implode($separator, array_map(function($item) use ($alias){
return static::field($item, $alias);
}, $fields));
}
#[Ignore]
public static function searchRequest(...$arguments) : SearchRequest\SearchRequestInterface
{
return new /* #[SearchRequest\Attribute\SearchRequestParameter(YourEntityClass::class)] */ class(... $arguments) extends SearchRequest\SearchRequest {
# Define searchable properties here, some ex:
# #[SearchParameter(method: SearchMethodEnum::Where)]
# public ? string $username = null;
# #[SearchParameter(method: SearchMethodEnum::Where, toggle: true)]
# public ? bool $hidden = null;
# #[SearchParameter(method: SearchMethodEnum::Like)]
# public ? string $word = null;
};
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Ulmus\Event\Query;
use Ulmus\Repository\RepositoryInterface;
interface Alter {
public function execute(RepositoryInterface $repository, array $fields) : void;
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Ulmus\Event\Query;
use Ulmus\Repository\RepositoryInterface;
interface Create {
public function execute(RepositoryInterface $repository) : void;
}
Loaded 100 of 156 files, more files were not shown because too many files have changed in this diff. Show more