Compare commits
150
Commits
d4a9fc8463
..
v2.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75f4b1d5f8 | ||
|
|
caf5bd9664 | ||
|
|
69dcfe9324 | ||
|
|
a18b7dba6c | ||
|
|
9062a73ee6 | ||
|
|
447430b7bb | ||
|
|
00093b0398 | ||
|
|
e5a842a812 | ||
|
|
1dcf32e0da | ||
|
|
13b7cf06f1 | ||
|
|
709ed63323 | ||
|
|
5d32d9f635 | ||
|
|
1e4a528d39 | ||
|
|
2d86adc3e1 | ||
|
|
2a03789e7c | ||
|
|
1af0063203 | ||
|
|
7b35de3258 | ||
|
|
f938e18572 | ||
|
|
b394b9348f | ||
|
|
009e30eb7e | ||
|
|
fd5d31fa17 | ||
|
|
b0b3aca6d9 | ||
|
|
36162c6486 | ||
|
|
11e436236c | ||
|
|
a9b9c2cce9 | ||
|
|
cb125545c7 | ||
|
|
2271b0b034 | ||
|
|
2e45eba520 | ||
|
|
033a183269 | ||
|
|
0192975f10 | ||
|
|
b33c67b594 | ||
|
|
2f482324b2 | ||
|
|
a5d65025f8 | ||
|
|
b53327a6ed | ||
|
|
fb644b0b0a | ||
|
|
c719e4a6d1 | ||
|
|
fb4985160a | ||
|
|
c54de30069 | ||
|
|
8fff21ecb0 | ||
|
|
cc6048d4ee | ||
|
|
761e0401b0 | ||
|
|
1ec582d1ac | ||
|
|
9556dea849 | ||
|
|
bfddf84564 | ||
|
|
e0ec140661 | ||
|
|
0fd6889c6f | ||
|
|
bd0d928f35 | ||
|
|
5a5d326d70 | ||
|
|
b5e96eb7e0 | ||
|
|
7c589ab7b4 | ||
|
|
589524ca3a | ||
|
|
1fac12c928 | ||
|
|
9eed2fe1a8 | ||
|
|
b601939459 | ||
|
|
e3314f40ac | ||
|
|
4ad44f73d7 | ||
|
|
68a6636c06 | ||
|
|
70c6cde603 | ||
|
|
079a3fd110 | ||
|
|
ec4d02b9d9 | ||
|
|
35ceb97241 | ||
|
|
83f673b37f | ||
|
|
411992c7a8 | ||
|
|
5f4f23a8e4 | ||
|
|
2fda7e82d7 | ||
|
|
573d4cc06b | ||
|
|
18313fd7f5 | ||
|
|
9503b24467 | ||
|
|
3c2ae86653 | ||
|
|
9977a25cf5 | ||
|
|
3ba8a2a5d0 | ||
|
|
4106c8db91 | ||
|
|
d5cca085bc | ||
|
|
c56b584140 | ||
|
|
8b1c11676d | ||
|
|
aa07c728fe | ||
|
|
d4181065cf | ||
|
|
62998faf40 | ||
|
|
feee26cd26 | ||
|
|
e37944c63a | ||
|
|
1d38ae8245 | ||
|
|
0c8591f238 | ||
|
|
bbfd7c02b4 | ||
|
|
bd9078230d | ||
|
|
f3be11a590 | ||
|
|
2de3139c80 | ||
|
|
e37eeb85f1 | ||
|
|
7de0ecb028 | ||
|
|
75231f32b3 | ||
|
|
1dd9da6eb6 | ||
|
|
b38b81d03c | ||
|
|
950df5eb99 | ||
|
|
9fdebf454d | ||
|
|
4571517dc8 | ||
|
|
15be1597b8 | ||
|
|
fa4e686f35 | ||
|
|
d9fa51fa18 | ||
|
|
d4d68a029d | ||
|
|
ea667db552 | ||
|
|
afe5144dc7 | ||
|
|
62792e8325 | ||
|
|
3a80fee9c3 | ||
|
|
7f8780d328 | ||
|
|
9a9f473f3f | ||
|
|
fab70aec6a | ||
|
|
c1755d250b | ||
|
|
acab934f6f | ||
|
|
2e377374d4 | ||
|
|
c2cccc0222 | ||
|
|
ab582c9318 | ||
|
|
a4b81f1932 | ||
|
|
ab1d7e4f5b | ||
|
|
bfc1e1cf93 | ||
|
|
531908b04c | ||
|
|
07ae8faaac | ||
|
|
756474d460 | ||
|
|
d297fd9e86 | ||
|
|
fed7d2e302 | ||
|
|
3736fbe0f6 | ||
|
|
667c1fe9ab | ||
|
|
d2bd6c2f58 | ||
|
|
953fc35680 | ||
|
|
6c6733b503 | ||
|
|
e641bc321d | ||
|
|
da5d203768 | ||
|
|
f77c93ad20 | ||
|
|
c9d9281c63 | ||
|
|
8e0dce3e71 | ||
|
|
1927b43999 | ||
|
|
cc741566fb | ||
|
|
8489cb4841 | ||
|
|
9ebf7b05d7 | ||
|
|
338b599d39 | ||
|
|
d01624b8aa | ||
|
|
01ab6e82d4 | ||
|
|
a4743b471c | ||
|
|
bd61522a07 | ||
|
|
f3712c9cc7 | ||
|
|
cfaebbecc4 | ||
|
|
eb6434bb72 | ||
|
|
2ad52aba85 | ||
|
|
3c5aa51850 | ||
|
|
9514a46ae7 | ||
|
|
cee978ecfd | ||
|
|
43880eb428 | ||
|
|
58bdce0ea8 | ||
|
|
fa8adcace1 | ||
|
|
36cddf8f78 | ||
|
|
c6bfaa05f2 | ||
|
|
c9c6a11ebd |
No files matched your search
+4
-4
@@ -6,16 +6,16 @@
|
|||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Dave Mc Nicoll",
|
"name": "Dave Mc Nicoll",
|
||||||
"email": "mcndave@gmail.com"
|
"email": "info@mcnd.ca"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"mcnd/notes": "dev-master"
|
"mcnd/notes": "^2.1.1"
|
||||||
},
|
},
|
||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
"type": "vcs",
|
"type": "composer",
|
||||||
"url": "https://github.com/mcNdave/notes.git"
|
"url": "https://git.mcnd.ca/api/packages/mcndave/composer"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
+6
-16
@@ -4,7 +4,7 @@ Welcome to the official Ulmus documentation.
|
|||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
Creating a simple user entity:
|
Creating a simple user exemple entity:
|
||||||
|
|
||||||
*/app/entity/user.php*
|
*/app/entity/user.php*
|
||||||
```php
|
```php
|
||||||
@@ -21,29 +21,19 @@ class User
|
|||||||
{
|
{
|
||||||
use \Ulmus\EntityTrait;
|
use \Ulmus\EntityTrait;
|
||||||
|
|
||||||
/**
|
#[Field\Id]
|
||||||
* @Id
|
|
||||||
*/
|
|
||||||
public int $id;
|
public int $id;
|
||||||
|
|
||||||
/**
|
#[Field]
|
||||||
* @Field
|
|
||||||
*/
|
|
||||||
public string $fullname;
|
public string $fullname;
|
||||||
|
|
||||||
/**
|
#[Field]
|
||||||
* @Field
|
|
||||||
*/
|
|
||||||
public ? string $email;
|
public ? string $email;
|
||||||
|
|
||||||
/**
|
#[Field]
|
||||||
* @Field('name' => 'is_admin')
|
|
||||||
*/
|
|
||||||
public bool $isAdmin = false;
|
public bool $isAdmin = false;
|
||||||
|
|
||||||
/**
|
#[Field\Datetime]
|
||||||
* @DateTime
|
|
||||||
*/
|
|
||||||
public Datetime $birthday;
|
public Datetime $birthday;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -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:
|
||||||
Whitespace-only changes.
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Ulmus\Adapter;
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
use Ulmus\Common\PdoObject;
|
|
||||||
use Ulmus\Migration\FieldDefinition;
|
use Ulmus\Migration\FieldDefinition;
|
||||||
|
|
||||||
interface AdapterInterface {
|
interface AdapterInterface {
|
||||||
@@ -12,20 +11,10 @@ interface AdapterInterface {
|
|||||||
public const IDENTIFIER_SCHEMA = 4;
|
public const IDENTIFIER_SCHEMA = 4;
|
||||||
public const IDENTIFIER_VALUE = 5;
|
public const IDENTIFIER_VALUE = 5;
|
||||||
|
|
||||||
public function connect() : object /* | PdoObject|mixed */;
|
public function connect() : object;
|
||||||
public function buildDataSourceName() : string;
|
public function buildDataSourceName() : string;
|
||||||
public function setup(array $configuration) : void;
|
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 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 repositoryClass() : string;
|
||||||
public function queryBuilderClass() : string;
|
public function queryBuilderClass() : string;
|
||||||
public function tableSyntax() : array;
|
|
||||||
}
|
}
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Adapter;
|
|
||||||
|
|
||||||
use Ulmus\{ConnectionAdapter, Entity\InformationSchema\Table, Migration\FieldDefinition, Repository, QueryBuilder};
|
|
||||||
|
|
||||||
trait DefaultAdapterTrait
|
|
||||||
{
|
|
||||||
public function repositoryClass() : string
|
|
||||||
{
|
|
||||||
return Repository::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function queryBuilderClass() : string
|
|
||||||
{
|
|
||||||
return QueryBuilder::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 $parent, $databaseName, string $tableName) /* : ? object */
|
|
||||||
{
|
|
||||||
return Table::repository(Repository::DEFAULT_ALIAS, $parent)->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
|
|
||||||
{
|
|
||||||
$type = $field->type;
|
|
||||||
|
|
||||||
$length = $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";
|
|
||||||
}
|
|
||||||
|
|
||||||
switch($type) {
|
|
||||||
case "bool":
|
|
||||||
$type = "TINYINT";
|
|
||||||
$length = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "array":
|
|
||||||
case "string":
|
|
||||||
if ($length && $length <= 255) {
|
|
||||||
$type = "VARCHAR";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
elseif (! $length || ( $length <= 65535 ) ) {
|
|
||||||
$type = "TEXT";
|
|
||||||
}
|
|
||||||
elseif ( $length <= 16777215 ) {
|
|
||||||
$type = "MEDIUMTEXT";
|
|
||||||
}
|
|
||||||
elseif ($length <= 4294967295) {
|
|
||||||
$type = "LONGTEXT";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new \Exception("A column with size bigger than 4GB cannot be created.");
|
|
||||||
}
|
|
||||||
|
|
||||||
# Length is unnecessary on TEXT fields
|
|
||||||
unset($length);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "float":
|
|
||||||
$type = "DOUBLE";
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
$type = strtoupper($type);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $typeOnly ? $type : $type . ( isset($length) ? "($length" . ( ! empty($precision) ? ",$precision" : "" ) . ")" : "" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+39
-31
@@ -2,17 +2,26 @@
|
|||||||
|
|
||||||
namespace Ulmus\Adapter;
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
use Ulmus\Common\PdoObject;
|
use Ulmus\Pdo;
|
||||||
|
|
||||||
use Ulmus\Exception\AdapterConfigurationException;
|
use Ulmus\Exception\AdapterConfigurationException;
|
||||||
|
|
||||||
use Ulmus\Ulmus;
|
use Ulmus\Ulmus;
|
||||||
|
|
||||||
use Ulmus\Migration\FieldDefinition;
|
use Ulmus\Migration\FieldDefinition;
|
||||||
use Ulmus\{Entity\InformationSchema\Table, Repository, QueryBuilder};
|
use Ulmus\{ConnectionAdapter,
|
||||||
|
Entity\InformationSchema\Table,
|
||||||
|
Migration\MigrateInterface,
|
||||||
|
Migration\SqlMigrationTrait,
|
||||||
|
Repository,
|
||||||
|
QueryBuilder};
|
||||||
|
|
||||||
class MsSQL implements AdapterInterface {
|
class MsSQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
|
||||||
use DefaultAdapterTrait;
|
use SqlAdapterTrait, SqlMigrationTrait;
|
||||||
|
|
||||||
|
const ALLOWED_ATTRIBUTES = [
|
||||||
|
'default', 'primary_key', 'auto_increment',
|
||||||
|
];
|
||||||
|
|
||||||
const DSN_PREFIX = "sqlsrv";
|
const DSN_PREFIX = "sqlsrv";
|
||||||
|
|
||||||
@@ -35,7 +44,7 @@ class MsSQL implements AdapterInterface {
|
|||||||
public string $database;
|
public string $database;
|
||||||
|
|
||||||
public string $username;
|
public string $username;
|
||||||
|
|
||||||
public string $password;
|
public string $password;
|
||||||
|
|
||||||
public string $traceFile;
|
public string $traceFile;
|
||||||
@@ -47,7 +56,9 @@ class MsSQL implements AdapterInterface {
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
?string $server = null,
|
?string $server = null,
|
||||||
?string $database = null,
|
?string $database = null,
|
||||||
|
#[\SensitiveParameter]
|
||||||
?string $username = null,
|
?string $username = null,
|
||||||
|
#[\SensitiveParameter]
|
||||||
?string $password = null,
|
?string $password = null,
|
||||||
?int $port = null
|
?int $port = null
|
||||||
) {
|
) {
|
||||||
@@ -72,15 +83,15 @@ class MsSQL implements AdapterInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect() : PdoObject
|
public function connect() : \PDO
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$pdo = new PdoObject($this->buildDataSourceName(), $this->username, $this->password);
|
$pdo = new Pdo\Mssql($this->buildDataSourceName(), $this->username, $this->password);
|
||||||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
||||||
$pdo->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_UTF8);
|
$pdo->setAttribute(\PDO::SQLSRV_ATTR_ENCODING, \PDO::SQLSRV_ENCODING_UTF8);
|
||||||
}
|
}
|
||||||
catch(PDOException $ex){
|
catch(\PDOException $ex){
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
@@ -199,10 +210,18 @@ class MsSQL implements AdapterInterface {
|
|||||||
$this->traceOn = $configuration['trace_on'];
|
$this->traceOn = $configuration['trace_on'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function escapeIdentifier(string $segment, int $type) : string
|
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
|
||||||
|
{
|
||||||
|
$mapper = new MsSQLFieldMapper($field);
|
||||||
|
|
||||||
|
return $typeOnly ? $mapper->type : $mapper->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function escapeIdentifier(string $segment, int $type) : string
|
||||||
{
|
{
|
||||||
switch($type) {
|
switch($type) {
|
||||||
|
default:
|
||||||
case static::IDENTIFIER_SCHEMA:
|
case static::IDENTIFIER_SCHEMA:
|
||||||
case static::IDENTIFIER_DATABASE:
|
case static::IDENTIFIER_DATABASE:
|
||||||
case static::IDENTIFIER_TABLE:
|
case static::IDENTIFIER_TABLE:
|
||||||
@@ -214,26 +233,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
|
public function defaultEngine(): ? string
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
@@ -246,6 +245,15 @@ class MsSQL implements AdapterInterface {
|
|||||||
|
|
||||||
public function queryBuilderClass() : string
|
public function queryBuilderClass() : string
|
||||||
{
|
{
|
||||||
return QueryBuilder\MssqlQueryBuilder::class;
|
return QueryBuilder\Sql\MssqlQueryBuilder::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function schemaTable(ConnectionAdapter $adapter, $databaseName, string $tableName) : null|object
|
||||||
|
{
|
||||||
|
return \Ulmus\Entity\Mssql\Table::repository(Repository::DEFAULT_ALIAS, $adapter)
|
||||||
|
->select(\Ulmus\Common\Sql::raw('this.*'))
|
||||||
|
->where($this->escapeIdentifier('table_schema', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
|
||||||
|
->or($this->escapeIdentifier('table_catalog', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
|
||||||
|
->loadOneFromField($this->escapeIdentifier('table_name', AdapterInterface::IDENTIFIER_FIELD), $tableName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
|
use Ulmus\Migration\FieldDefinition;
|
||||||
|
|
||||||
|
class MsSQLFieldMapper extends SqlFieldMapper
|
||||||
|
{
|
||||||
|
public function map() : void
|
||||||
|
{
|
||||||
|
parent::map();
|
||||||
|
|
||||||
|
if (in_array($this->type, [ 'CHAR', 'VARCHAR', 'TEXT', ])) {
|
||||||
|
$this->type = "N" . $this->type;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @TODO !
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
* */
|
||||||
|
}
|
||||||
+69
-37
@@ -2,28 +2,34 @@
|
|||||||
|
|
||||||
namespace Ulmus\Adapter;
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
use Ulmus\Entity\InformationSchema\Table;
|
use Ulmus\ConnectionAdapter;
|
||||||
use Ulmus\QueryBuilder;
|
use Ulmus\Entity\Mysql\Table;
|
||||||
use Ulmus\Repository;
|
use Ulmus\Migration\FieldDefinition;
|
||||||
use Ulmus\Common\PdoObject;
|
use Ulmus\Migration\MigrateInterface;
|
||||||
|
use Ulmus\Migration\SqlMigrationTrait;
|
||||||
|
use Ulmus\QueryBuilder\Sql;
|
||||||
|
use Ulmus\Pdo;
|
||||||
|
|
||||||
use Ulmus\Exception\AdapterConfigurationException;
|
use Ulmus\Exception\AdapterConfigurationException;
|
||||||
use Ulmus\Ulmus;
|
use Ulmus\Repository;
|
||||||
use Ulmus\Migration\FieldDefinition;
|
|
||||||
|
|
||||||
class MySQL implements AdapterInterface {
|
class MySQL implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
|
||||||
use DefaultAdapterTrait;
|
use SqlAdapterTrait, SqlMigrationTrait;
|
||||||
|
|
||||||
|
const ALLOWED_ATTRIBUTES = [
|
||||||
|
'default', 'primary_key', 'auto_increment', 'update',
|
||||||
|
];
|
||||||
|
|
||||||
const DSN_PREFIX = "mysql";
|
const DSN_PREFIX = "mysql";
|
||||||
|
|
||||||
public string $hostname;
|
public string $hostname;
|
||||||
|
|
||||||
public string $database;
|
public string $database;
|
||||||
|
|
||||||
public string $username;
|
protected string $username;
|
||||||
|
|
||||||
public string $password;
|
protected string $password;
|
||||||
|
|
||||||
public string $charset = "utf8mb4";
|
public string $charset = "utf8mb4";
|
||||||
|
|
||||||
public ? string $socket;
|
public ? string $socket;
|
||||||
@@ -33,7 +39,9 @@ class MySQL implements AdapterInterface {
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
?string $hostname = null,
|
?string $hostname = null,
|
||||||
?string $database = null,
|
?string $database = null,
|
||||||
|
#[\SensitiveParameter]
|
||||||
?string $username = null,
|
?string $username = null,
|
||||||
|
#[\SensitiveParameter]
|
||||||
?string $password = null,
|
?string $password = null,
|
||||||
?int $port = null,
|
?int $port = null,
|
||||||
?string $charset = null
|
?string $charset = null
|
||||||
@@ -63,17 +71,17 @@ class MySQL implements AdapterInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect() : PdoObject
|
public function connect() : \PDO
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$pdo = new PdoObject($this->buildDataSourceName(), $this->username, $this->password);
|
$pdo = new Pdo\Mysql($this->buildDataSourceName(), $this->username, $this->password);
|
||||||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
||||||
$pdo->setAttribute(\PDO::ATTR_AUTOCOMMIT, false);
|
$pdo->setAttribute(\PDO::ATTR_AUTOCOMMIT, false);
|
||||||
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
||||||
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
|
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
|
||||||
}
|
}
|
||||||
catch(PDOException $ex){
|
catch(\PDOException $ex){
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
@@ -135,9 +143,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) {
|
switch($type) {
|
||||||
|
default:
|
||||||
case static::IDENTIFIER_DATABASE:
|
case static::IDENTIFIER_DATABASE:
|
||||||
case static::IDENTIFIER_TABLE:
|
case static::IDENTIFIER_TABLE:
|
||||||
case static::IDENTIFIER_FIELD:
|
case static::IDENTIFIER_FIELD:
|
||||||
@@ -148,27 +164,43 @@ 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
|
public function defaultEngine(): ? string
|
||||||
{
|
{
|
||||||
return "InnoDB";
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
|
||||||
|
{
|
||||||
|
if ($field['action'] === 'add') {
|
||||||
|
if (! empty($field['previous'])) {
|
||||||
|
$position = sprintf('AFTER %s', $this->escapeIdentifier($field['previous']->field, AdapterInterface::IDENTIFIER_FIELD));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$position = "FIRST";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(" ", array_filter([
|
||||||
|
strtoupper($field['action']),
|
||||||
|
$this->escapeIdentifier($definition->getSqlName(), AdapterInterface::IDENTIFIER_FIELD),
|
||||||
|
$definition->getSqlType(),
|
||||||
|
$definition->getSqlParams(),
|
||||||
|
$position ?? null,
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
|
use Ulmus\Migration\FieldDefinition;
|
||||||
|
|
||||||
|
class MySQLFieldMapper extends SqlFieldMapper
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+134
-66
@@ -2,46 +2,45 @@
|
|||||||
|
|
||||||
namespace Ulmus\Adapter;
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
use Ulmus\Common\PdoObject;
|
use Collator;
|
||||||
|
use Ulmus\Pdo;
|
||||||
use Ulmus\Entity\Sqlite\Table;
|
use Ulmus\ConnectionAdapter;
|
||||||
use Ulmus\Exception\AdapterConfigurationException;
|
use Ulmus\Entity;
|
||||||
use Ulmus\Migration\FieldDefinition;
|
use Ulmus\Migration\FieldDefinition;
|
||||||
use Ulmus\{Repository, QueryBuilder, Ulmus};
|
use Ulmus\{Migration\MigrateInterface, Migration\SqlMigrationTrait, Repository, QueryBuilder};
|
||||||
|
|
||||||
class SQLite implements AdapterInterface {
|
class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface {
|
||||||
use DefaultAdapterTrait;
|
use SqlAdapterTrait, SqlMigrationTrait;
|
||||||
|
|
||||||
|
const ALLOWED_ATTRIBUTES = [
|
||||||
|
'default', 'primary_key', 'auto_increment', 'collate nocase', 'collate binary', 'collate rtrim',
|
||||||
|
];
|
||||||
|
|
||||||
const DSN_PREFIX = "sqlite";
|
const DSN_PREFIX = "sqlite";
|
||||||
|
|
||||||
public string $path;
|
|
||||||
|
|
||||||
public array $pragma;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
? string $path = null,
|
public null|string $path = null,
|
||||||
? array $pragma = null
|
public null|array $pragmaBegin = null,
|
||||||
) {
|
public null|array $pragmaClose = null,
|
||||||
if ($path !== null) {
|
) { }
|
||||||
$this->path = $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($pragma !== null) {
|
public function connect() : \Pdo
|
||||||
$this->pragma = $pragma;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function connect() : PdoObject
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$pdo = new PdoObject($this->buildDataSourceName(), null, null);
|
$pdo = new Pdo\Sqlite($this->buildDataSourceName(), null, null);
|
||||||
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
||||||
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
$pdo->onClose = function(\PDO $obj) {
|
||||||
|
static::registerPragma($obj, $this->pragmaClose);
|
||||||
|
};
|
||||||
|
|
||||||
$this->exportFunctions($pdo);
|
$this->exportFunctions($pdo);
|
||||||
|
$this->exportCollations($pdo);
|
||||||
|
$this->registerPragma($pdo, $this->pragmaBegin);
|
||||||
}
|
}
|
||||||
catch(PDOException $ex){
|
catch(\PDOException $ex){
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,13 +57,15 @@ class SQLite implements AdapterInterface {
|
|||||||
public function setup(array $configuration) : void
|
public function setup(array $configuration) : void
|
||||||
{
|
{
|
||||||
$this->path = $configuration['path'] ?? "";
|
$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
|
# 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) {
|
switch($type) {
|
||||||
|
default:
|
||||||
case static::IDENTIFIER_DATABASE:
|
case static::IDENTIFIER_DATABASE:
|
||||||
case static::IDENTIFIER_TABLE:
|
case static::IDENTIFIER_TABLE:
|
||||||
case static::IDENTIFIER_FIELD:
|
case static::IDENTIFIER_FIELD:
|
||||||
@@ -87,20 +88,22 @@ class SQLite implements AdapterInterface {
|
|||||||
return substr($base, 0, strrpos($base, '.') ?: strlen($base));
|
return substr($base, 0, strrpos($base, '.') ?: strlen($base));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function schemaTable(string $databaseName, string $tableName) : null|object
|
public function schemaTable(ConnectionAdapter $adapter, string $databaseName, string $tableName) : null|object
|
||||||
{
|
{
|
||||||
return Table::repository()->loadOneFromField(Table::field('tableName'), $tableName);
|
return Entity\Sqlite\Table::repository(Repository::DEFAULT_ALIAS, $adapter)
|
||||||
|
->select(\Ulmus\Common\Sql::raw('this.*'))
|
||||||
|
->loadOneFromField(Entity\Sqlite\Table::field('tableName'), $tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
|
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
|
||||||
{
|
{
|
||||||
$type = $field->type;
|
$type = $field->type;
|
||||||
|
|
||||||
$length = $field->length;
|
$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) ) {
|
if ( is_a($type, Entity\Field\Date::class, true) || is_a($type, Entity\Field\Time::class, true) || is_a($type, \DateTime::class, true) ) {
|
||||||
$type = "TEXT";
|
$type = "TEXT";
|
||||||
$length = strlen((string) $type);
|
|
||||||
|
#$length = strlen((string) $type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
switch($type) {
|
switch($type) {
|
||||||
@@ -113,6 +116,10 @@ class SQLite implements AdapterInterface {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "array":
|
case "array":
|
||||||
|
$type = "JSON";
|
||||||
|
$length = null;
|
||||||
|
break;
|
||||||
|
|
||||||
case "string":
|
case "string":
|
||||||
$type = "TEXT";
|
$type = "TEXT";
|
||||||
$length = null;
|
$length = null;
|
||||||
@@ -128,26 +135,7 @@ class SQLite implements AdapterInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $typeOnly ? $type : $type . ( $length ? "($length" . ( isset($precision) ? ",$precision" : "" ) . ")" : "" );
|
return $typeOnly ? $type : $type . ( $length ? "($length" . ")" : "" );
|
||||||
}
|
|
||||||
|
|
||||||
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 tableSyntax() : array
|
public function tableSyntax() : array
|
||||||
@@ -166,23 +154,31 @@ class SQLite implements AdapterInterface {
|
|||||||
|
|
||||||
public function queryBuilderClass() : string
|
public function queryBuilderClass() : string
|
||||||
{
|
{
|
||||||
return QueryBuilder\SqliteQueryBuilder::class;
|
return QueryBuilder\Sql\SqliteQueryBuilder::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportFunctions(PdoObject $pdo) : void
|
public function exportFunctions(\PDO $pdo) : void
|
||||||
{
|
{
|
||||||
$pdo->sqliteCreateFunction('if', fn($comparison, $yes, $no) => $comparison ? $yes : $no, 3);
|
$pdo->createFunction('if', fn($comparison, $yes, $no) => $comparison ? $yes : $no, 3);
|
||||||
$pdo->sqliteCreateFunction('length', fn($string) => strlen($string), 1);
|
$pdo->createFunction('length', fn($string) => strlen($string), 1);
|
||||||
$pdo->sqliteCreateFunction('lcase', fn($string) => strtolower($string), 1);
|
$pdo->createFunction('lcase', fn($string) => strtolower($string), 1);
|
||||||
$pdo->sqliteCreateFunction('ucase', fn($string) => strtoupper($string), 1);
|
$pdo->createFunction('ucase', fn($string) => strtoupper($string), 1);
|
||||||
$pdo->sqliteCreateFunction('left', fn($string, $length) => substr($string, 0, $length), 2);
|
$pdo->createFunction('md5', fn($string) => md5($string), 1);
|
||||||
$pdo->sqliteCreateFunction('right', fn($string, $length) => substr($string, -$length), 2);
|
$pdo->createFunction('sha1', fn($string) => sha1($string), 1);
|
||||||
$pdo->sqliteCreateFunction('strcmp', fn($s1, $s2) => strcmp($s1, $s2), 2);
|
$pdo->createFunction('sha256', fn($string) => hash('sha256', $string), 1);
|
||||||
$pdo->sqliteCreateFunction('lpad', fn($string, $length, $pad) => str_pad($string, $length, $pad, STR_PAD_LEFT), 3);
|
$pdo->createFunction('sha512', fn($string) => hash('sha512', $string), 1);
|
||||||
$pdo->sqliteCreateFunction('rpad', fn($string, $length, $pad) => str_pad($string, $length, $pad, STR_PAD_RIGHT), 3);
|
$pdo->createFunction('whirlpool', fn($string) => hash('whirlpool', $string), 1);
|
||||||
$pdo->sqliteCreateFunction('concat', fn(...$args) => implode('', $args), -1);
|
$pdo->createFunction('murmur3a', fn($string) => hash('murmur3a', $string), 1);
|
||||||
$pdo->sqliteCreateFunction('concat_ws', fn($separator, ...$args) => implode($separator, $args), -1);
|
$pdo->createFunction('murmur3c', fn($string) => hash('murmur3c', $string), 1);
|
||||||
$pdo->sqliteCreateFunction('find_in_set', function($string, $string_list) {
|
$pdo->createFunction('murmur3f', fn($string) => hash('murmur3f', $string), 1);
|
||||||
|
$pdo->createFunction('left', fn($string, $length) => substr($string, 0, $length), 2);
|
||||||
|
$pdo->createFunction('right', fn($string, $length) => substr($string, -$length), 2);
|
||||||
|
$pdo->createFunction('strcmp', fn($s1, $s2) => strcmp($s1, $s2), 2);
|
||||||
|
$pdo->createFunction('lpad', fn($string, $length, $pad) => str_pad($string, $length, $pad, STR_PAD_LEFT), 3);
|
||||||
|
$pdo->createFunction('rpad', fn($string, $length, $pad) => str_pad($string, $length, $pad, STR_PAD_RIGHT), 3);
|
||||||
|
$pdo->createFunction('concat', fn(...$args) => implode('', $args), -1);
|
||||||
|
$pdo->createFunction('concat_ws', fn($separator, ...$args) => implode($separator, $args), -1);
|
||||||
|
$pdo->createFunction('find_in_set', function($string, $string_list) {
|
||||||
if ( $string === null || $string_list === null ) {
|
if ( $string === null || $string_list === null ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -193,5 +189,77 @@ class SQLite implements AdapterInterface {
|
|||||||
|
|
||||||
return (int) in_array($string, explode(',', $string_list));
|
return (int) in_array($string, explode(',', $string_list));
|
||||||
}, 2);
|
}, 2);
|
||||||
|
$pdo->createFunction('day', fn($date) => ( new \DateTime($date) )->format('j'), 1);
|
||||||
|
$pdo->createFunction('month', fn($date) => ( new \DateTime($date) )->format('n'), 1);
|
||||||
|
$pdo->createFunction('year', fn($date) => ( new \DateTime($date) )->format('Y'), 1);
|
||||||
|
$pdo->createFunction('str_to_date', fn($value, $format) => ($f = \DateTime::createFromFormat($format, $value)) ? $f->format('Y-m-d H:i:s') : false);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public function exportCollations(\PDO $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->createCollation('locale_cmp', fn($e1, $e2) => $collator->compare($e1, $e2));
|
||||||
|
$pdo->createCollation('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->createCollation('locale_cmp_ci', fn($e1, $e2) => $collatorCi->compare($e1, $e2));
|
||||||
|
$pdo->createCollation('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->createCollation('locale_cmp', fn($e1, $e2) => $comp);
|
||||||
|
$pdo->createCollation('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->createCollation('locale_cmp_ci', fn($e1, $e2) => $compCi);
|
||||||
|
$pdo->createCollation('locale_cmp_ci_desc', fn($e2, $e1) => $compCi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function registerPragma(\PDO $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(" ", [
|
||||||
|
strtoupper($field['action']),
|
||||||
|
$this->escapeIdentifier($definition->getSqlName(), static::IDENTIFIER_FIELD),
|
||||||
|
$definition->getSqlType(),
|
||||||
|
$definition->getSqlParams(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportAlterColumnAction(string $action)
|
||||||
|
{
|
||||||
|
return in_array(strtolower($action), [ 'add' ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function splitAlterQuery() : bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
|
interface SqlAdapterInterface
|
||||||
|
{
|
||||||
|
public static function escapeIdentifier(string $segment, int $type) : string;
|
||||||
|
public function defaultEngine() : ? string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?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 whitelistAttributes(array &$parameters) : void
|
||||||
|
{
|
||||||
|
$parameters = array_intersect_key($parameters, array_flip(static::ALLOWED_ATTRIBUTES));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Adapter;
|
||||||
|
|
||||||
|
use Ulmus\Migration\FieldDefinition;
|
||||||
|
use Ulmus\Entity;
|
||||||
|
|
||||||
|
class SqlFieldMapper
|
||||||
|
{
|
||||||
|
public string $type;
|
||||||
|
|
||||||
|
public 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":
|
||||||
|
# Was set at JSON, but for now LONGTEXT is an alias of it (with additional constraints)
|
||||||
|
$this->type = "LONGTEXT";
|
||||||
|
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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Annotation\Classes;
|
|
||||||
|
|
||||||
class Collation implements \Notes\Annotation {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Annotation\Classes;
|
|
||||||
|
|
||||||
class Method implements \Notes\Annotation {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Annotation\Property;
|
|
||||||
|
|
||||||
class Having extends Where {}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\Annotation\Property;
|
|
||||||
|
|
||||||
class On extends Where {}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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 {}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,13 +3,26 @@
|
|||||||
namespace Ulmus\Attribute;
|
namespace Ulmus\Attribute;
|
||||||
|
|
||||||
use Ulmus\Common\EntityField;
|
use Ulmus\Common\EntityField;
|
||||||
|
use Ulmus\Repository;
|
||||||
|
|
||||||
class Attribute
|
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) ) {
|
if ( is_array($field) ) {
|
||||||
|
if (count($field) < 2) {
|
||||||
|
throw new \RuntimeException(
|
||||||
|
sprintf("Array field must be formed of at least two things, a class name, and a property. (received %s)", json_encode($field))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$class = array_shift($field);
|
$class = array_shift($field);
|
||||||
|
$field[1] ??= $alias;
|
||||||
|
|
||||||
|
if (is_array($field[0])) {
|
||||||
|
$field[] = $separator;
|
||||||
|
return $class::fields(...$field);
|
||||||
|
}
|
||||||
|
|
||||||
return $class::field(...$field);
|
return $class::field(...$field);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute;
|
||||||
|
|
||||||
|
enum ConstrainActionEnum : string
|
||||||
|
{
|
||||||
|
case Cascade = 'cascade';
|
||||||
|
|
||||||
|
case NoAction = 'noaction';
|
||||||
|
|
||||||
|
case Restrict = 'restrict';
|
||||||
|
|
||||||
|
case SetNull = 'setnull';
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -4,5 +4,7 @@ namespace Ulmus\Attribute\Obj;
|
|||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_CLASS)]
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
||||||
class Collation {
|
class Collation {
|
||||||
|
public function __construct(
|
||||||
|
public string $name = ""
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Obj;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
||||||
|
class JsonSerialize {
|
||||||
|
public function __construct(
|
||||||
|
public bool $includeRelations = true,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -3,12 +3,18 @@
|
|||||||
namespace Ulmus\Attribute\Obj;
|
namespace Ulmus\Attribute\Obj;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_CLASS)]
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
||||||
class Table {
|
class Table implements AdapterAttributeInterface {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ? string $name = null,
|
public ? string $name = null,
|
||||||
public ? string $database = null,
|
public ? string $database = null,
|
||||||
public ? string $schema = null,
|
public ? string $schema = null,
|
||||||
public ? string $adapter = null,
|
public ? string $adapter = null,
|
||||||
public ? string $engine = null,
|
public ? string $engine = null,
|
||||||
|
public string $description = "",
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public function adapter() : false|string
|
||||||
|
{
|
||||||
|
return $this->adapter ?: false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
|
use Notes\Common\ReflectedProperty;
|
||||||
|
use Ulmus\Entity\EntityInterface;
|
||||||
|
use Ulmus\Entity\JsonUnserializable;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class ArrayOf implements \Ulmus\Entity\EntityValueModifier
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $type,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function push(mixed $value, ReflectedProperty $property): mixed
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
foreach($value as $index => $item) {
|
||||||
|
if (! is_object($item)) {
|
||||||
|
if (enum_exists($this->type)) {
|
||||||
|
if ($this->type::tryFrom($item) === null) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf("Given item '%s' is not a valid enum for type %s ; try one of theses instead : %s", $item, $this->type, implode("', '", array_map(fn($e) => $e->value, $this->type::cases())))
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$obj = $this->type::from($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (class_exists($this->type)) {
|
||||||
|
if (is_subclass_of($this->type, JsonUnserializable::class)) {
|
||||||
|
$obj = (new \ReflectionClass($this->type))->newInstanceWithoutConstructor();
|
||||||
|
$obj->jsonUnserialize($item);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$obj = new $this->type($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new \InvalidArgumentException("Given type for ArrayOf is incompatible with it's use. An object should be provided");
|
||||||
|
}
|
||||||
|
|
||||||
|
$return[$index] = $obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $property->type->builtIn) {
|
||||||
|
$cls = $property->type->type;
|
||||||
|
|
||||||
|
return new $cls($return);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pull(mixed $value, ReflectedProperty $property) : mixed
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
foreach($value as $index => $item) {
|
||||||
|
if (is_object($item)) {
|
||||||
|
if (enum_exists($this->type)) {
|
||||||
|
$value = $item->value;
|
||||||
|
} elseif (class_exists($this->type)) {
|
||||||
|
$value = (string) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$return[$index] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Collation {
|
||||||
|
public function __construct(
|
||||||
|
public string $name = ""
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace Ulmus\Attribute\Property\Field;
|
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
|
* Since we need consistancy between the declaration of our ID and FK fields, it
|
||||||
* was decided to extend the PK class instead of Field for this case.
|
* was decided to extend the PK class instead of Field for this case.
|
||||||
@@ -10,7 +13,7 @@ namespace Ulmus\Attribute\Property\Field;
|
|||||||
class ForeignKey extends PrimaryKey {
|
class ForeignKey extends PrimaryKey {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ? string $name = null,
|
public ? string $name = null,
|
||||||
public ? string $type = 'bigint',
|
public ? string $type = null,
|
||||||
public null|int|string $length = null,
|
public null|int|string $length = null,
|
||||||
public ? int $precision = null,
|
public ? int $precision = null,
|
||||||
public array $attributes = [
|
public array $attributes = [
|
||||||
@@ -20,5 +23,10 @@ class ForeignKey extends PrimaryKey {
|
|||||||
public bool $nullable = false,
|
public bool $nullable = false,
|
||||||
public mixed $default = null,
|
public mixed $default = null,
|
||||||
public bool $readonly = false,
|
public bool $readonly = false,
|
||||||
) {}
|
public null|string $relation = null,
|
||||||
|
public ConstrainActionEnum $onDelete = ConstrainActionEnum::NoAction,
|
||||||
|
public ConstrainActionEnum $onUpdate = ConstrainActionEnum::NoAction,
|
||||||
|
) {
|
||||||
|
#$this->references = Attribute::handleArrayField($this->references, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,6 @@ class Id extends \Ulmus\Attribute\Property\Field {
|
|||||||
],
|
],
|
||||||
public bool $nullable = false,
|
public bool $nullable = false,
|
||||||
public mixed $default = null,
|
public mixed $default = null,
|
||||||
public bool $readonly = false,
|
public bool $readonly = true,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property\Field;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Select {
|
||||||
|
public function __construct(
|
||||||
|
public string $sql,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -5,8 +5,12 @@ namespace Ulmus\Attribute\Property\Field;
|
|||||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
class Time extends \Ulmus\Attribute\Property\Field
|
class Time extends \Ulmus\Attribute\Property\Field
|
||||||
{
|
{
|
||||||
public function __construct(? string $type = "time", ? int $length = null)
|
public function __construct(
|
||||||
{
|
public ? string $name = null,
|
||||||
parent::__construct($type, $length);
|
public ? string $type = "time",
|
||||||
}
|
public array $attributes = [],
|
||||||
|
public bool $nullable = false,
|
||||||
|
public mixed $default = null,
|
||||||
|
public bool $readonly = false,
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,12 @@ namespace Ulmus\Attribute\Property\Field;
|
|||||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
class Timestamp extends \Ulmus\Attribute\Property\Field
|
class Timestamp extends \Ulmus\Attribute\Property\Field
|
||||||
{
|
{
|
||||||
public function __construct(? string $type = "timestamp", ? int $length = null)
|
public function __construct(
|
||||||
{
|
public ? string $name = null,
|
||||||
parent::__construct($type, $length);
|
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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -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 = "",
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -6,6 +6,5 @@ namespace Ulmus\Attribute\Property;
|
|||||||
class Filter {
|
class Filter {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $method = ""
|
public string $method = ""
|
||||||
)
|
) {}
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -3,17 +3,49 @@
|
|||||||
namespace Ulmus\Attribute\Property;
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
use Ulmus\Attribute\Attribute;
|
use Ulmus\Attribute\Attribute;
|
||||||
|
use Ulmus\Repository;
|
||||||
|
|
||||||
#[\Attribute]
|
#[\Attribute]
|
||||||
class Join {
|
class Join implements ResettablePropertyInterface {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $type,
|
public string $type,
|
||||||
public null|string|\Stringable|array $key = null,
|
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 $entity = null,
|
||||||
public null|string $alias = null,
|
public null|string $alias = null,
|
||||||
) {
|
) {
|
||||||
$this->key = Attribute::handleArrayField($this->key);
|
$this->key = Attribute::handleArrayField($this->key);
|
||||||
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
|
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## AWAITING THE Closures in constant expression from PHP 8.5 !
|
||||||
|
public function foreignKey(? Repository $repository = null) : mixed
|
||||||
|
{
|
||||||
|
if (is_string($this->foreignKey)) {
|
||||||
|
if (str_starts_with($this->foreignKey, 'fn(') && str_contains($this->foreignKey, '=>')) {
|
||||||
|
$closure = eval("return {$this->foreignKey};");
|
||||||
|
|
||||||
|
return $repository ? $closure($repository) : $closure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->entity::field($this->foreignKey, $this->alias);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->foreignKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isOneToOne() : bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isOneToMany() : bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isManyToMany() : bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class JsonSerialize {
|
||||||
|
public function __construct(
|
||||||
|
public bool $ignoreField = false,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Limit {
|
||||||
|
public function __construct(
|
||||||
|
public int $value,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Offset {
|
||||||
|
public function __construct(
|
||||||
|
public int $value,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -10,10 +10,11 @@ class OrWhere extends Where {
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
public string|\Stringable|array $field,
|
public string|\Stringable|array $field,
|
||||||
public mixed $value = null,
|
public mixed $value = null,
|
||||||
public null|string $operator = null,
|
public string $operator = Query\Where::OPERATOR_EQUAL,
|
||||||
public null|string $condition = Query\Where::CONDITION_OR,
|
public string $condition = Query\Where::CONDITION_OR,
|
||||||
|
public string|\Stringable|array|null $fieldValue = null,
|
||||||
|
public null|array|\Closure $generateValue = null,
|
||||||
) {
|
) {
|
||||||
|
parent::__construct($field, $value, $operator, $condition, $fieldValue, $generateValue);
|
||||||
$this->key = Attribute::handleArrayField($this->key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,14 @@
|
|||||||
namespace Ulmus\Attribute\Property;
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
use Ulmus\Attribute\Attribute;
|
use Ulmus\Attribute\Attribute;
|
||||||
|
use Ulmus\Repository;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
class Relation {
|
class Relation implements ResettablePropertyInterface {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public Relation\RelationTypeEnum|string $type,
|
public Relation\RelationTypeEnum|string $type,
|
||||||
public \Stringable|string|array $key = "",
|
public \Stringable|string|array $key = "",
|
||||||
public null|\Closure $generateKey = null,
|
public null|\Closure|array $generateKey = null,
|
||||||
public null|\Stringable|string|array $foreignKey = null,
|
public null|\Stringable|string|array $foreignKey = null,
|
||||||
public null|\Stringable|string|array $foreignField = null,
|
public null|\Stringable|string|array $foreignField = null,
|
||||||
public array $foreignKeys = [],
|
public array $foreignKeys = [],
|
||||||
@@ -20,7 +21,8 @@ class Relation {
|
|||||||
public null|\Stringable|string|array $field = null,
|
public null|\Stringable|string|array $field = null,
|
||||||
public null|string $entity = null,
|
public null|string $entity = null,
|
||||||
public null|string $join = null,
|
public null|string $join = null,
|
||||||
public string $function = "loadAll",
|
public null|string $function = null,
|
||||||
|
public null|string $alias = null,
|
||||||
) {
|
) {
|
||||||
$this->key = Attribute::handleArrayField($this->key);
|
$this->key = Attribute::handleArrayField($this->key);
|
||||||
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
|
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
|
||||||
@@ -35,7 +37,7 @@ class Relation {
|
|||||||
try {
|
try {
|
||||||
$e = $this->entity;
|
$e = $this->entity;
|
||||||
} catch (\Throwable $ex) {
|
} 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();
|
return new $e();
|
||||||
@@ -54,17 +56,17 @@ class Relation {
|
|||||||
|
|
||||||
public function isOneToOne() : bool
|
public function isOneToOne() : bool
|
||||||
{
|
{
|
||||||
return $this->type === Relation\RelationTypeEnum::oneToOne || $this->normalizeType() === 'onetoone';
|
return $this->type instanceof Relation\RelationTypeEnum ? $this->type === Relation\RelationTypeEnum::oneToOne : $this->normalizeType() === 'onetoone';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isOneToMany() : bool
|
public function isOneToMany() : bool
|
||||||
{
|
{
|
||||||
return $this->type === Relation\RelationTypeEnum::oneToMany || $this->normalizeType() === 'onetomany';
|
return $this->type instanceof Relation\RelationTypeEnum ? $this->type === Relation\RelationTypeEnum::oneToMany : $this->normalizeType() === 'onetomany';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isManyToMany() : bool
|
public function isManyToMany() : bool
|
||||||
{
|
{
|
||||||
return $this->type === Relation\RelationTypeEnum::manyToMany || $this->normalizeType() === 'manytomany';
|
return $this->type instanceof Relation\RelationTypeEnum ? $this->type === Relation\RelationTypeEnum::manyToMany : $this->normalizeType() === 'manytomany';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function function() : string
|
public function function() : string
|
||||||
@@ -73,7 +75,7 @@ class Relation {
|
|||||||
return $this->function;
|
return $this->function;
|
||||||
}
|
}
|
||||||
elseif ($this->isOneToOne()) {
|
elseif ($this->isOneToOne()) {
|
||||||
return 'load';
|
return 'loadOne';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'loadAll';
|
return 'loadAll';
|
||||||
@@ -83,4 +85,20 @@ class Relation {
|
|||||||
{
|
{
|
||||||
return (bool) $this->bridge;
|
return (bool) $this->bridge;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## AWAITING THE Closures in constant expression from PHP 8.5 !
|
||||||
|
public function foreignKey(? Repository $repository = null) : mixed
|
||||||
|
{
|
||||||
|
if (is_string($this->foreignKey)) {
|
||||||
|
if (str_starts_with($this->foreignKey, 'fn(') && str_contains($this->foreignKey, '=>')) {
|
||||||
|
$closure = eval("return {$this->foreignKey};");
|
||||||
|
|
||||||
|
return $repository ? $closure($repository) : $closure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->entity::field($this->foreignKey, $this->alias);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->foreignKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,4 +3,10 @@
|
|||||||
namespace Ulmus\Attribute\Property\Relation;
|
namespace Ulmus\Attribute\Property\Relation;
|
||||||
|
|
||||||
#[\Attribute]
|
#[\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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -3,11 +3,20 @@
|
|||||||
namespace Ulmus\Attribute\Property;
|
namespace Ulmus\Attribute\Property;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
class Virtual extends Field {
|
class Virtual extends Field implements ResettablePropertyInterface {
|
||||||
|
|
||||||
public bool $readonly = true;
|
public bool $readonly = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ? string $method = null,
|
public null|string|array $method = null,
|
||||||
) {}
|
public ? \Closure $closure = null,
|
||||||
|
public null|string $name = null,
|
||||||
|
) {
|
||||||
|
$this->method ??= [ static::class, 'noop' ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function noop() : null
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,17 +7,38 @@ use Ulmus\Query;
|
|||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
||||||
class Where {
|
class Where {
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string|\Stringable|array $field,
|
public string|\Stringable|array $field,
|
||||||
public mixed $value = null,
|
public mixed $value = null,
|
||||||
public string $operator = Query\Where::OPERATOR_EQUAL,
|
public string $operator = Query\Where::OPERATOR_EQUAL,
|
||||||
public string $condition = Query\Where::CONDITION_AND,
|
public string $condition = Query\Where::CONDITION_AND,
|
||||||
public null|\Stringable|array $foreignField = null,
|
public string|\Stringable|array|null $fieldValue = null,
|
||||||
|
public null|array|\Closure $generateValue = null,
|
||||||
) {
|
) {
|
||||||
$this->field = Attribute::handleArrayField($field);
|
$this->field = Attribute::handleArrayField($field);
|
||||||
|
$this->fieldValue = Attribute::handleArrayField($fieldValue);
|
||||||
|
}
|
||||||
|
|
||||||
if ($foreignField) {
|
public function getValue(/* null|EntityInterface */ $entity = null) : mixed
|
||||||
$this->value = Attribute::handleArrayField($foreignField);
|
{
|
||||||
|
if ($entity) {
|
||||||
|
if ($this->generateValue) {
|
||||||
|
return call_user_func_array($this->generateValue, [ $entity ]);
|
||||||
|
}
|
||||||
|
elseif ($this->fieldValue) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if ($this->fieldValue) {
|
||||||
|
return $this->fieldValue;
|
||||||
|
}
|
||||||
|
elseif ($this->generateValue) {
|
||||||
|
throw new \Exception(sprintf("Could not generate value from non-instanciated entity for field %s.", (string) $this->field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,6 @@ namespace Ulmus\Attribute\Property;
|
|||||||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
|
||||||
class WithJoin {
|
class WithJoin {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
array $joins = []
|
public string|array $joins = []
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Attribute;
|
||||||
|
|
||||||
|
use Ulmus\Entity\EntityInterface;
|
||||||
|
|
||||||
|
interface RegisterEntityEventInterface
|
||||||
|
{
|
||||||
|
public function attach(EntityInterface $entity, string $field);
|
||||||
|
}
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
-22
@@ -2,39 +2,39 @@
|
|||||||
|
|
||||||
namespace Ulmus\Common;
|
namespace Ulmus\Common;
|
||||||
|
|
||||||
use Ulmus\Annotation\Annotation;
|
use Ulmus\Attribute\Property\{ Field };
|
||||||
use Ulmus\Attribute;
|
|
||||||
use Ulmus\Migration\FieldDefinition;
|
use Ulmus\Migration\FieldDefinition;
|
||||||
use Ulmus\Ulmus,
|
use Ulmus\Ulmus,
|
||||||
Ulmus\Adapter\AdapterInterface,
|
Ulmus\Adapter\AdapterInterface,
|
||||||
Ulmus\Annotation\Property\Field,
|
|
||||||
Ulmus\Query\WhereRawParameter;
|
Ulmus\Query\WhereRawParameter;
|
||||||
|
|
||||||
class EntityField implements WhereRawParameter
|
class EntityField implements WhereRawParameter
|
||||||
{
|
{
|
||||||
public string $name;
|
public function __construct(
|
||||||
|
public string $name,
|
||||||
public string $entityClass;
|
public string $entityClass,
|
||||||
|
public ? string $alias,
|
||||||
public ? string $alias;
|
protected EntityResolver $entityResolver,
|
||||||
|
protected bool $select = false
|
||||||
protected EntityResolver $entityResolver;
|
) {}
|
||||||
|
|
||||||
public function __construct(string $entityClass, string $name, ? string $alias, EntityResolver $resolver)
|
|
||||||
{
|
|
||||||
$this->entityClass = $entityClass;
|
|
||||||
$this->name = $name;
|
|
||||||
$this->alias = $alias;
|
|
||||||
$this->entityResolver = $resolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function name($useAlias = true) : string
|
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);
|
$name = $this->entityResolver->databaseAdapter()->adapter()->escapeIdentifier($name, AdapterInterface::IDENTIFIER_FIELD);
|
||||||
|
|
||||||
return $useAlias && $this->alias ? "{$this->alias}.$name" : $name;
|
$fullname = $useAlias && $this->alias ? "{$this->alias}.$name" : $name;
|
||||||
|
|
||||||
|
if ($this->select) {
|
||||||
|
$select = $this->entityResolver->searchFieldAnnotation($this->name, [Field\Select::class]);
|
||||||
|
|
||||||
|
if ($select) {
|
||||||
|
return "(" . str_replace(['%NAME%'], [$fullname], $select->sql) . ") AS code";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fullname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isScalarType($type) : bool
|
public static function isScalarType($type) : bool
|
||||||
@@ -45,6 +45,7 @@ class EntityField implements WhereRawParameter
|
|||||||
case 'string':
|
case 'string':
|
||||||
case 'float':
|
case 'float':
|
||||||
case 'double':
|
case 'double':
|
||||||
|
case 'mixed':
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,12 +57,19 @@ class EntityField implements WhereRawParameter
|
|||||||
$definition = new FieldDefinition($adapter, $field);
|
$definition = new FieldDefinition($adapter, $field);
|
||||||
|
|
||||||
return implode(" ", [
|
return implode(" ", [
|
||||||
$definition->getSqlName(),
|
$adapter->escapeIdentifier($definition->getSqlName(), AdapterInterface::IDENTIFIER_FIELD),
|
||||||
$definition->getSqlType(),
|
$definition->getSqlType(),
|
||||||
$definition->getSqlParams(),
|
$definition->getSqlParams(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function generateAlterColumn(AdapterInterface $adapter, array $field) : string
|
||||||
|
{
|
||||||
|
$definition = new FieldDefinition($adapter, $field['definition']);
|
||||||
|
|
||||||
|
return $adapter->generateAlterColumn($definition, $field);
|
||||||
|
}
|
||||||
|
|
||||||
public static function isObjectType($type) : bool
|
public static function isObjectType($type) : bool
|
||||||
{
|
{
|
||||||
# @Should be fixed with isBuiltIn() instead, it won't be correct based only on name
|
# @Should be fixed with isBuiltIn() instead, it won't be correct based only on name
|
||||||
|
|||||||
+72
-166
@@ -2,15 +2,18 @@
|
|||||||
|
|
||||||
namespace Ulmus\Common;
|
namespace Ulmus\Common;
|
||||||
|
|
||||||
|
use Notes\Common\ReflectedClass;
|
||||||
|
use Notes\Common\ReflectedProperty;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use Ulmus\Attribute\Obj\Index;
|
||||||
use Ulmus\Ulmus,
|
use Ulmus\Ulmus,
|
||||||
Ulmus\Annotation\Classes\Table,
|
Ulmus\Attribute\Obj\Table,
|
||||||
Ulmus\Annotation\Property\Field,
|
Ulmus\Attribute\Obj\AdapterAttributeInterface,
|
||||||
Ulmus\Annotation\Property\Virtual,
|
Ulmus\Attribute\Property\Field,
|
||||||
Ulmus\Annotation\Property\Relation,
|
Ulmus\Attribute\Property\Relation,
|
||||||
Ulmus\Attribute;
|
Ulmus\Attribute\Property\Virtual;
|
||||||
|
|
||||||
use Notes\Annotation;
|
use Notes\Common\ReflectedAttribute;
|
||||||
|
|
||||||
use Notes\ObjectReflection;
|
use Notes\ObjectReflection;
|
||||||
|
|
||||||
@@ -24,13 +27,7 @@ class EntityResolver {
|
|||||||
|
|
||||||
public string $entityClass;
|
public string $entityClass;
|
||||||
|
|
||||||
public array $uses;
|
public ReflectedClass $reflectedClass;
|
||||||
|
|
||||||
public array $class;
|
|
||||||
|
|
||||||
public array $properties;
|
|
||||||
|
|
||||||
public array $methods;
|
|
||||||
|
|
||||||
protected array $fieldList = [];
|
protected array $fieldList = [];
|
||||||
|
|
||||||
@@ -38,14 +35,10 @@ class EntityResolver {
|
|||||||
{
|
{
|
||||||
$this->entityClass = $entityClass;
|
$this->entityClass = $entityClass;
|
||||||
|
|
||||||
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
|
$this->reflectedClass = ObjectReflection::fromClass($entityClass, $cache)->reflectClass();
|
||||||
ObjectReflection::fromClass($entityClass, $cache)->read()
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->resolveAnnotations();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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{
|
try{
|
||||||
return $this->fieldList($fieldKey)[$name] ?? null;
|
return $this->fieldList($fieldKey)[$name] ?? null;
|
||||||
@@ -59,43 +52,33 @@ class EntityResolver {
|
|||||||
return null;
|
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);
|
||||||
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
|
public function fieldList($fieldKey = self::KEY_ENTITY_NAME, bool $skipVirtual = false) : array
|
||||||
{
|
{
|
||||||
$fieldList = [];
|
$fieldList = [];
|
||||||
|
|
||||||
foreach($this->properties as $item) {
|
foreach($this->reflectedClass->getProperties(true) as $item) {
|
||||||
foreach($item['tags'] ?? [] as $tag) {
|
foreach($item->getAttributes() as $tag) {
|
||||||
if ( $tag['object'] instanceof Field or $tag['object'] instanceof Attribute\Property\Field ) {
|
if ( $tag->object instanceof Field ) {
|
||||||
if ( $skipVirtual && ($tag['object'] instanceof Virtual or $tag['object'] instanceof Attribute\Property\Virtual )) {
|
if ( $skipVirtual && $tag->object instanceof Virtual ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch($fieldKey) {
|
switch($fieldKey) {
|
||||||
case static::KEY_LC_ENTITY_NAME:
|
case static::KEY_LC_ENTITY_NAME:
|
||||||
$key = strtolower($item['name']);
|
$key = strtolower($item->name);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case static::KEY_ENTITY_NAME:
|
case static::KEY_ENTITY_NAME:
|
||||||
$key = $item['name'];
|
$key = $item->name;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case static::KEY_COLUMN_NAME:
|
case static::KEY_COLUMN_NAME:
|
||||||
$key = strtolower($tag['object']->name ?? $item['name']);
|
$key = strtolower($tag->object->name ?? $item->name);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -112,52 +95,58 @@ class EntityResolver {
|
|||||||
return $fieldList;
|
return $fieldList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function relation(string $name) : ? array
|
public function relation(string $name, bool $throwOnError = true) : ? array
|
||||||
{
|
{
|
||||||
|
$property = $this->reflectedClass->getProperties(true)[$name] ?? false;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
if ( null !== ( $this->properties[$name] ?? null ) ) {
|
if ( $property ) {
|
||||||
foreach($this->properties[$name]['tags'] ?? [] as $tag) {
|
foreach($property->getAttributes() as $tag) {
|
||||||
if ( $tag['object'] instanceof Relation or $tag['object'] instanceof Attribute\Property\Relation ) {
|
if ( $tag->object instanceof Relation ) {
|
||||||
return $this->properties[$name];
|
return $property;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
catch(\Throwable $e) {
|
catch(\Throwable $e) {
|
||||||
# if ( $throwException) {
|
$throwOnError && throw new \InvalidArgumentException("Can't find entity relation's column named `$name` from entity {$this->entityClass}");
|
||||||
throw new \InvalidArgumentException("Can't find entity relation's column named `$name` from entity {$this->entityClass}");
|
|
||||||
# }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPropertyHavingAttribute(array|object|string $type) : array
|
||||||
|
{
|
||||||
|
return array_filter(array_map(
|
||||||
|
fn($e) => $e->getAttributes(is_object($type) ? $type::class : $type), $this->reflectedClass->getProperties(true)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPropertyEntityType(string $name) : false|string
|
||||||
|
{
|
||||||
|
return $this->reflectedClass->getProperties(true)[$name]->getTypes()[0]->type ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
public function searchFieldAnnotation(string $field, array|object|string $annotationType, bool $caseSensitive = true) : ? object
|
public function searchFieldAnnotation(string $field, array|object|string $annotationType, bool $caseSensitive = true) : ? object
|
||||||
{
|
{
|
||||||
return $this->searchFieldAnnotationList($field, $annotationType, $caseSensitive)[0] ?? null;
|
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 = [];
|
$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 ) ) {
|
if ( null !== ( $search[$field] ?? null ) ) {
|
||||||
foreach($search[$field]['tags'] ?? [] as $tag) {
|
return array_map(fn(ReflectedAttribute $e) => $e->object, $search[$field]->getAttributes((array) $attributeType));
|
||||||
foreach($annotations as $annotation) {
|
|
||||||
if ( $tag['object'] instanceof $annotation ) {
|
|
||||||
$list[] = $tag['object'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $list;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tableName($required = false) : string
|
public function tableName($required = false) : string
|
||||||
@@ -173,7 +162,7 @@ class EntityResolver {
|
|||||||
return $table->name ?? "";
|
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 ( null === $table = $this->getTableAttribute() ) {
|
||||||
if ($required) {
|
if ($required) {
|
||||||
@@ -184,17 +173,17 @@ class EntityResolver {
|
|||||||
return $table;
|
return $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function databaseName() : ? string
|
public function databaseName() : null|string
|
||||||
{
|
{
|
||||||
return $this->tableAnnotation(false)->database ?? $this->databaseAdapter()->adapter()->databaseName() ?? null;
|
return $this->tableAnnotation(false)->database ?? $this->databaseAdapter()->adapter()->databaseName() ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sqlAdapter() : \Ulmus\ConnectionAdapter
|
public function sqlAdapter() : \Ulmus\ConnectionAdapter
|
||||||
{
|
{
|
||||||
if ( null !== $table = $this->getTableAttribute() ) {
|
if ( $adapterObj = $this->getAdapterInterfaceAttribute()) {
|
||||||
if ( $table->adapter ?? null ) {
|
if ( false !== $adapterName = $adapterObj->adapter() ) {
|
||||||
if ( null === ( $adapter = \Ulmus\Ulmus::$registeredAdapters[$table->adapter] ?? null ) ) {
|
if ( null === ( $adapter = \Ulmus\Ulmus::$registeredAdapters[$adapterName] ?? null ) ) {
|
||||||
throw new \Exception("Requested database adapter `{$table->adapter}` is not registered.");
|
throw new \Exception("Requested database adapter `$adapterName` is not registered.");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return $adapter;
|
return $adapter;
|
||||||
@@ -213,7 +202,7 @@ class EntityResolver {
|
|||||||
return $this->sqlAdapter();
|
return $this->sqlAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function schemaName(bool $required = false) : ? string
|
public function schemaName(bool $required = false) : null|string
|
||||||
{
|
{
|
||||||
if ( null === $table = $this->getTableAttribute() ) {
|
if ( null === $table = $this->getTableAttribute() ) {
|
||||||
throw new \LogicException("Your entity {$this->entityClass} seems to be missing a @Table() annotation");
|
throw new \LogicException("Your entity {$this->entityClass} seems to be missing a @Table() annotation");
|
||||||
@@ -226,10 +215,10 @@ class EntityResolver {
|
|||||||
return $table->schema ?? null;
|
return $table->schema ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPrimaryKeyField() : ? array
|
public function getPrimaryKeyField() : null|array
|
||||||
{
|
{
|
||||||
foreach($this->fieldList() as $key => $value) {
|
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 ( null !== $field ) {
|
||||||
if ( false !== ( $field->attributes['primary_key'] ?? false ) ) {
|
if ( false !== ( $field->attributes['primary_key'] ?? false ) ) {
|
||||||
return [ $key => $field ];
|
return [ $key => $field ];
|
||||||
@@ -240,9 +229,9 @@ class EntityResolver {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCompoundKeyFields() : ? array
|
public function getCompoundKeyFields() : ? Index
|
||||||
{
|
{
|
||||||
return null;
|
return $this->getAttributeImplementing(Index::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUniqueFields() : ? array
|
public function getUniqueFields() : ? array
|
||||||
@@ -250,107 +239,24 @@ class EntityResolver {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTableAttribute()
|
protected function getAdapterInterfaceAttribute() : null|object
|
||||||
{
|
{
|
||||||
return $this->getAnnotationFromClassname(Attribute\Obj\Table::class, false) ?: $this->getAnnotationFromClassname( Table::class );
|
return $this->getAttributeImplementing(AdapterAttributeInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function getTableAttribute()
|
||||||
* Transform an annotation into it's object's counterpart
|
|
||||||
*/
|
|
||||||
public function getAnnotationFromClassname(string $className, bool $throwError = true) : ? object
|
|
||||||
{
|
{
|
||||||
if ( $name = $this->uses[$className] ?? false ) {
|
return $this->getAttributeImplementing(Table::class);
|
||||||
foreach(array_reverse($this->class['tags']) as $item) {
|
}
|
||||||
if ( $item['tag'] === $name ) {
|
|
||||||
return $this->instanciateAnnotationObject($item);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($this->properties as $item) {
|
public function getAttributeImplementing(string $interface) : null|object
|
||||||
foreach(array_reverse($item['tags']) as $item) {
|
{
|
||||||
if ( $item['tag'] === $name ) {
|
foreach ($this->reflectedClass->getAttributes(true) as $item) {
|
||||||
return $this->instanciateAnnotationObject($item);
|
if ($item->object instanceof $interface) {
|
||||||
}
|
return $item->object;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+70
-31
@@ -11,76 +11,95 @@ class PdoObject extends PDO {
|
|||||||
|
|
||||||
public bool $executionStatus;
|
public bool $executionStatus;
|
||||||
|
|
||||||
public function select(string $sql, array $parameters = []): PDOStatement
|
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 ]);
|
static::$dump && call_user_func_array(static::$dump, [ $this->fillDebugSqlParameters($sql, $parameters) ]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||||
$statement = $this->execute($statement, $parameters, false);
|
|
||||||
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||||||
|
$this->execute($statement, $parameters, false);
|
||||||
return $statement;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (\PDOException $e) {
|
catch (\Throwable $e) {
|
||||||
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
if ($this->onClose ?? null) {
|
||||||
|
call_user_func($this->onClose, $this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function runQuery(string $sql, array $parameters = []): ? static
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
public function runQuery(string $sql, array $parameters = []): ? PDOStatement
|
|
||||||
{
|
{
|
||||||
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
|
static::$dump && call_user_func_array(static::$dump, [ $this->fillDebugSqlParameters($sql, $parameters) ]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||||
return $this->execute($statement, $parameters, true);
|
return $this->execute($statement, $parameters, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (\PDOException $e) {
|
catch(\PDOException $pdo) {
|
||||||
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
if (!str_starts_with($pdo->getMessage(), 'There is no active transaction')) {
|
||||||
|
throw $pdo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (\Throwable $e) {
|
||||||
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters), (int) $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function runInsertQuery(string $sql, array $parameters = [])
|
public function runInsertQuery(string $sql, array $parameters = []) : ? static
|
||||||
{
|
{
|
||||||
return $this->runQuery($sql, $parameters);
|
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);
|
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);
|
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->executionStatus = false;
|
||||||
|
$this->lastInsertId = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if ( ! $this->inTransaction() ) {
|
if (! $this->inTransaction()) {
|
||||||
$this->beginTransaction();
|
$this->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->executionStatus = empty($parameters) ? $statement->execute() : $statement->execute($parameters);
|
$this->bindVariables($statement, $parameters);
|
||||||
|
|
||||||
|
$this->executionStatus = $statement->execute();
|
||||||
|
|
||||||
if ( $this->executionStatus ) {
|
if ( $this->executionStatus ) {
|
||||||
$statement->lastInsertId = $this->lastInsertId();
|
$this->lastInsertId = $this->lastInsertId();
|
||||||
|
$this->rowCount = $statement->rowCount();
|
||||||
|
|
||||||
if ( $commit ) {
|
if ( $commit ) {
|
||||||
$this->commit();
|
$this->commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $statement;
|
return $this;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
|
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
|
||||||
@@ -91,14 +110,34 @@ class PdoObject extends PDO {
|
|||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
catch (\Throwable $e) {
|
}
|
||||||
if ( function_exists("debogueur") ) {
|
|
||||||
debogueur($statement, $parameters, $commit);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw $e;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
|
$type = match (strtolower(gettype($value))) {
|
||||||
|
"boolean", "integer" => Pdo::PARAM_INT,
|
||||||
|
"null" => Pdo::PARAM_NULL,
|
||||||
|
default => Pdo::PARAM_STR,
|
||||||
|
};
|
||||||
|
|
||||||
|
$statement->bindValue($key, $value, $type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fillDebugSqlParameters(string $sql, array $parameters) : string
|
||||||
|
{
|
||||||
|
$parameters = array_reverse($parameters);
|
||||||
|
|
||||||
|
return str_replace(array_keys($parameters), array_map(fn($e) => is_numeric($e) ? $e : (
|
||||||
|
is_bool($e) ? (int) $e : $this->quote($e)
|
||||||
|
), array_values($parameters)), $sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Common\PdoObject;
|
||||||
|
|
||||||
|
class SqlPdoObject extends \Ulmus\Common\PdoObject
|
||||||
|
{
|
||||||
|
protected int $openedTransaction = 0;
|
||||||
|
|
||||||
|
public function beginTransaction(): bool
|
||||||
|
{
|
||||||
|
if ( 0 === $this->openedTransaction++ ) {
|
||||||
|
return $this->openTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->exec("SAVEPOINT transaction_{$this->openedTransaction}") !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function commit() : bool
|
||||||
|
{
|
||||||
|
if ( 0 === --$this->openedTransaction) {
|
||||||
|
return parent::commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->exec("RELEASE SAVEPOINT transaction_{$this->openedTransaction}") !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rollback() : bool
|
||||||
|
{
|
||||||
|
if ($this->openedTransaction > 1) {
|
||||||
|
return $this->exec('ROLLBACK TO transaction_' . $this->openedTransaction--) !== false;
|
||||||
|
}
|
||||||
|
elseif ($this->openedTransaction-- === 1) {
|
||||||
|
return parent::rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function openTransaction() : bool
|
||||||
|
{
|
||||||
|
return parent::beginTransaction();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Common\PdoObject;
|
||||||
|
|
||||||
|
class SqlitePdoObject extends SqlPdoObject
|
||||||
|
{
|
||||||
|
public bool $inTransaction = false;
|
||||||
|
|
||||||
|
public function inTransaction(): bool
|
||||||
|
{
|
||||||
|
return $this->openedTransaction > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function commit(): bool
|
||||||
|
{
|
||||||
|
if ( 0 === --$this->openedTransaction) {
|
||||||
|
return $this->exec("COMMIT") !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->exec("RELEASE SAVEPOINT transaction_{$this->openedTransaction}") !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rollback() : bool
|
||||||
|
{
|
||||||
|
if ($this->openedTransaction > 1) {
|
||||||
|
return $this->exec('ROLLBACK TO transaction_' . $this->openedTransaction--) !== false;
|
||||||
|
}
|
||||||
|
elseif ($this->openedTransaction-- === 1) {
|
||||||
|
# We must do it manually since opening a transaction manually stucks PDO into thinking we've got no transaction opened
|
||||||
|
return $this->exec('ROLLBACK') !== false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function openTransaction(): bool
|
||||||
|
{
|
||||||
|
return $this->exec("BEGIN IMMEDIATE TRANSACTION") !== false;
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-5
@@ -56,18 +56,19 @@ abstract class Sql {
|
|||||||
$this->identifier = $identifier;
|
$this->identifier = $identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString() {
|
public function __toString() : string
|
||||||
|
{
|
||||||
return $this->identifier;
|
return $this->identifier;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function raw(string $sql) : object
|
public static function raw(string $sql) : object
|
||||||
{
|
{
|
||||||
return static::identifier($sql);
|
return static::identifier($sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function escape($value)
|
public static function escape($value) : mixed
|
||||||
{
|
{
|
||||||
switch(true) {
|
switch(true) {
|
||||||
case is_object($value):
|
case is_object($value):
|
||||||
@@ -83,8 +84,12 @@ abstract class Sql {
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parameter($value) : string
|
public static function collate(string $name) : object
|
||||||
{
|
{
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+16
-17
@@ -2,26 +2,21 @@
|
|||||||
|
|
||||||
namespace Ulmus;
|
namespace Ulmus;
|
||||||
|
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use Ulmus\Adapter\AdapterInterface;
|
use Ulmus\Adapter\AdapterInterface;
|
||||||
|
|
||||||
use Ulmus\Common\PdoObject;
|
|
||||||
|
|
||||||
class ConnectionAdapter
|
class ConnectionAdapter
|
||||||
{
|
{
|
||||||
public string $name;
|
|
||||||
|
|
||||||
public array $configuration;
|
|
||||||
|
|
||||||
protected AdapterInterface $adapter;
|
protected AdapterInterface $adapter;
|
||||||
|
|
||||||
protected PdoObject $pdo;
|
protected \PDO $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);
|
Ulmus::registerAdapter($this, $default);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,10 +25,14 @@ class ConnectionAdapter
|
|||||||
$connection = $this->configuration['connections'][$this->name] ?? [];
|
$connection = $this->configuration['connections'][$this->name] ?? [];
|
||||||
|
|
||||||
if ( false !== ( $adapterName = $connection['adapter'] ?? false ) ) {
|
if ( false !== ( $adapterName = $connection['adapter'] ?? false ) ) {
|
||||||
|
if (is_array($adapterName)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf("Given adapter (%s) name is an array, not a string.. received : %s.", $this->name, json_encode($adapterName)));
|
||||||
|
}
|
||||||
|
|
||||||
$this->adapter = $this->instanciateAdapter($adapterName);
|
$this->adapter = $this->instanciateAdapter($adapterName);
|
||||||
}
|
}
|
||||||
else {
|
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);
|
$this->adapter->setup($connection);
|
||||||
@@ -53,7 +52,7 @@ class ConnectionAdapter
|
|||||||
public function connect() : self
|
public function connect() : self
|
||||||
{
|
{
|
||||||
$this->pdo = $this->adapter->connect();
|
$this->pdo = $this->adapter->connect();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +61,7 @@ class ConnectionAdapter
|
|||||||
return $this->adapter;
|
return $this->adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function pdo() : PdoObject
|
public function pdo() : \PDO
|
||||||
{
|
{
|
||||||
return $this->pdo ?? $this->connect()->pdo;
|
return $this->pdo ?? $this->connect()->pdo;
|
||||||
}
|
}
|
||||||
@@ -77,7 +76,7 @@ class ConnectionAdapter
|
|||||||
* @param string $name An Ulmus adapter or full class name implementing AdapterInterface
|
* @param string $name An Ulmus adapter or full class name implementing AdapterInterface
|
||||||
* @return 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);
|
$class = substr($name, 0, 2) === "\\" ? $name : sprintf("\\%s\\Adapter\\%s", __NAMESPACE__, $name);
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,27 @@
|
|||||||
|
|
||||||
namespace Ulmus\Container;
|
namespace Ulmus\Container;
|
||||||
|
|
||||||
|
use Ulmus\ConnectionAdapter;
|
||||||
|
|
||||||
class AdapterProxy
|
class AdapterProxy
|
||||||
{
|
{
|
||||||
public array $connections = [];
|
public array $connections = [];
|
||||||
|
|
||||||
public function __construct(...$arguments)
|
public function __construct(...$arguments)
|
||||||
{
|
{
|
||||||
$this->connections = $arguments;
|
$this->push(array_filter($arguments));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function push(array|ConnectionAdapter $connection) : void
|
||||||
|
{
|
||||||
|
foreach((array) $connection as $adapter) {
|
||||||
|
foreach($this->connections as $existing) {
|
||||||
|
if ($adapter === $existing) {
|
||||||
|
continue 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->connections[] = $adapter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Entity;
|
||||||
|
|
||||||
|
use Ulmus\EntityCollection;
|
||||||
|
|
||||||
|
class ArrayCollection extends EntityCollection implements JsonUnserializable
|
||||||
|
{
|
||||||
|
public function jsonUnserialize(array $json): void
|
||||||
|
{
|
||||||
|
$this->fromArray($json);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): mixed
|
||||||
|
{
|
||||||
|
return $this->getArrayCopy();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
|
||||||
|
{
|
||||||
|
foreach($datasets as $dataset) {
|
||||||
|
\ArrayObject::append($dataset);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 203 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user