Compare commits

..

No commits in common. "c3c320112b1fa98d78d3664edcf398345bce84f3" and "84682d1c8e78ec94d6420094d015330fda0036df" have entirely different histories.

11 changed files with 87 additions and 92 deletions

View File

@ -92,11 +92,11 @@ class MsSQL implements AdapterInterface {
$parts[] = "Server={$this->server}" . ( isset($this->port) ? ",{$this->port}" : "" ); $parts[] = "Server={$this->server}" . ( isset($this->port) ? ",{$this->port}" : "" );
$parts[] = "Database={$this->database}"; $parts[] = "Database={$this->database}";
$parts[] = "ConnectionPooling={$this->connectionPooling}"; $parts[] = "ConnectionPooling={$this->connectionPooling}";
if ( $this->app ?? false ) { if ( $this->app ?? false ) {
$parts[] = "APP={$this->app}"; $parts[] = "APP={$this->app}";
} }
if ( $this->encrypt ?? false ) { if ( $this->encrypt ?? false ) {
$parts[] = "Encrypt=1"; $parts[] = "Encrypt=1";
} }
@ -104,7 +104,7 @@ class MsSQL implements AdapterInterface {
$parts[] = "Encrypt=0"; $parts[] = "Encrypt=0";
} }
if ( $this->failoverPartner ?? false ) { if ( $this->failoverPartner ?? false ) {
$parts[] = "Failover_Partner={$this->failoverPartner}"; $parts[] = "Failover_Partner={$this->failoverPartner}";
} }
@ -133,7 +133,7 @@ class MsSQL implements AdapterInterface {
} }
if ( $this->trustServerCertificate ?? false ) { if ( $this->trustServerCertificate ?? false ) {
$parts[] = "TrustServerCertificate=yes"; $parts[] = "TrustServerCertificate=1";
} }
if ( $this->WSID ?? false ) { if ( $this->WSID ?? false ) {
@ -238,8 +238,4 @@ class MsSQL implements AdapterInterface {
return Repository\MssqlRepository::class; return Repository\MssqlRepository::class;
} }
public function queryBuilderClass() : string
{
return QueryBuilder\MssqlQueryBuilder::class;
}
} }

View File

@ -137,14 +137,4 @@ class SQLite implements AdapterInterface {
'unsigned' => "", 'unsigned' => "",
]; ];
} }
public function repositoryClass() : string
{
return Repository\SqliteRepository::class;
}
public function queryBuilderClass() : string
{
return QueryBuilder\SqliteQueryBuilder::class;
}
} }

View File

@ -44,11 +44,6 @@ class EntityCollection extends \ArrayObject {
} }
} }
public function filtersArray(Callable $callback) : array
{
return $this->filtersCollection($callback, true, false)->toArray();
}
public function filtersOne(Callable $callback) : ? object public function filtersOne(Callable $callback) : ? object
{ {
foreach($this->filters($callback, true) as $item) { foreach($this->filters($callback, true) as $item) {

View File

@ -204,10 +204,6 @@ trait EntityTrait {
*/ */
public function isLoaded() : bool public function isLoaded() : bool
{ {
if (empty($this->entityLoadedDataset)) {
return false;
}
if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) { if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) {
throw new Exception\EntityPrimaryKeyUnknown(sprintf("Entity %s has no field containing attributes 'primary_key'", static::class)); throw new Exception\EntityPrimaryKeyUnknown(sprintf("Entity %s has no field containing attributes 'primary_key'", static::class));
} }
@ -319,7 +315,7 @@ trait EntityTrait {
*/ */
public static function field($name, ? string $alias = null) : EntityField public static function field($name, ? string $alias = null) : EntityField
{ {
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : Repository::DEFAULT_ALIAS, Ulmus::resolveEntity(static::class)); return new EntityField(static::class, $name, $alias ?: Repository::DEFAULT_ALIAS, Ulmus::resolveEntity(static::class));
} }
/** /**

View File

@ -247,10 +247,6 @@ class QueryBuilder implements Query\QueryBuilderInterface
$limit->set($value); $limit->set($value);
if ($value === 0) {
$this->removeFragment(Query\Limit::class);
}
return $this; return $this;
} }

View File

@ -2,9 +2,11 @@
namespace Ulmus\QueryBuilder; namespace Ulmus\QueryBuilder;
use Ulmus\{Query, QueryBuilder }; use Ulmus\QueryBuilder;
class MssqlQueryBuilder extends QueryBuilder implements Query\QueryBuilderInterface use Ulmus\Query;
class MssqlQueryBuilder extends QueryBuilder implements Ulmus\Query\QueryBuilderInterface
{ {
public function limit(int $value) : self public function limit(int $value) : self
{ {
@ -15,10 +17,6 @@ class MssqlQueryBuilder extends QueryBuilder implements Query\QueryBuilderInterf
$offset->limit = $value; $offset->limit = $value;
if ($value === 0) {
$this->removeFragment(Query\MsSQL\Offset::class);
}
return $this; return $this;
} }

View File

@ -68,7 +68,7 @@ class Repository
public function count() : int public function count() : int
{ {
$this->removeQueryFragment([ Query\Select::class, Query\OrderBy::class, ]); $this->removeQueryFragment(Query\Select::class);
if ( $this->queryBuilder->getFragment(Query\GroupBy::class) ) { if ( $this->queryBuilder->getFragment(Query\GroupBy::class) ) {
$this->select( "DISTINCT COUNT(*) OVER ()" ); $this->select( "DISTINCT COUNT(*) OVER ()" );
@ -149,20 +149,16 @@ class Repository
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runInsertQuery(); $statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runInsertQuery();
if ( null !== $primaryKeyDefinition ) { if ( ( 0 !== $statement->lastInsertId ) &&
$pkField = key($primaryKeyDefinition); ( null !== $primaryKeyDefinition )) {
if ($statement->lastInsertId ) { $pkField = key($primaryKeyDefinition);
$dataset[$pkField] = $statement->lastInsertId; $dataset[$pkField] = $statement->lastInsertId;
}
elseif ($replace) {
$pkValue = $dataset[$pkField];
}
} }
$entity->entityFillFromDataset($dataset, true); $entity->entityFillFromDataset($dataset, true);
return (bool) ($pkValue ?? $statement->lastInsertId); return (bool) $statement->lastInsertId;
} }
else { else {
if ( $primaryKeyDefinition === null ) { if ( $primaryKeyDefinition === null ) {
@ -274,7 +270,7 @@ class Repository
{ {
$schema = $schema ?: $this->entityResolver->schemaName(); $schema = $schema ?: $this->entityResolver->schemaName();
$this->queryBuilder->truncate($this->escapeTable($table ?: $this->entityResolver->tableName()), $this->escapeIdentifier($alias ?: $this->alias), $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null); $this->queryBuilder->truncate($this->escapeTable($table ?: $this->entityResolver->tableName()), $alias ?: $this->alias, $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null);
$this->finalizeQuery(); $this->finalizeQuery();
@ -340,7 +336,7 @@ class Repository
foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) { foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) { if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) {
$this->select(sprintf("%s.$key as {$prependField}{$field['name']}", $this->escapeIdentifier($alias))); $this->select("$alias.$key as {$prependField}{$field['name']}");
} }
} }
@ -354,7 +350,7 @@ class Repository
foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) { foreach ($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) { if (null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore)) {
$fieldlist[] = $key; $fieldlist[] = $key;
$fieldlist[] = $entity::field($field['name'], $this->escapeIdentifier($alias)); $fieldlist[] = $entity::field($field['name'], $alias);
} }
} }
@ -384,7 +380,7 @@ class Repository
public function insert(array $fieldlist, string $table, string $alias, ? string $schema, bool $replace = false) : self public function insert(array $fieldlist, string $table, string $alias, ? string $schema, bool $replace = false) : self
{ {
$this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $this->escapeIdentifier($alias), $this->escapedDatabase(), $schema, $replace); $this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema, $replace);
return $this; return $this;
} }
@ -398,7 +394,7 @@ class Repository
public function update(string $table, string $alias, ? string $schema) : self public function update(string $table, string $alias, ? string $schema) : self
{ {
$this->queryBuilder->update($this->escapeTable($table), $this->escapeIdentifier($alias), $this->escapedDatabase(), $schema); $this->queryBuilder->update($this->escapeTable($table), $alias, $this->escapedDatabase(), $schema);
return $this; return $this;
} }
@ -422,13 +418,13 @@ class Repository
public function from(string $table, ? string $alias, ? string $schema) : self public function from(string $table, ? string $alias, ? string $schema) : self
{ {
$this->queryBuilder->from($this->escapeTable($table), $this->escapeIdentifier($alias), $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null); $this->queryBuilder->from($this->escapeTable($table), $alias, $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null);
return $this; return $this;
} }
public function join(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self public function join(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self
{ {
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, false, $this->escapeIdentifier($alias)); $join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, false, $alias);
if ( $callback ) { if ( $callback ) {
$callback($join); $callback($join);
@ -439,7 +435,7 @@ class Repository
public function outerJoin(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self public function outerJoin(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self
{ {
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, true, $this->escapeIdentifier($alias)); $join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, true, $alias);
if ( $callback ) { if ( $callback ) {
$callback($join); $callback($join);
@ -567,9 +563,7 @@ class Repository
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) { foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
if ( null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore) ) { if ( null === $entity::resolveEntity()->searchFieldAnnotation($field['name'], new RelationIgnore) ) {
$escAlias = $this->escapeIdentifier($alias); $this->select("$alias.$key as {$alias}\${$field['name']}");
$this->select("$escAlias.$key as $alias\${$field['name']}");
} }
} }
@ -632,7 +626,7 @@ class Repository
} }
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) { if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
$this->select($this->escapeIdentifier($this->alias) . ".*"); $this->select("{$this->alias}.*");
} }
# Apply FILTER annotation to this too ! # Apply FILTER annotation to this too !

View File

@ -7,11 +7,6 @@ use Ulmus\Annotation\Property\Field;
trait EscapeTrait trait EscapeTrait
{ {
public function escapeIdentifier(string $identifier) : string
{
return $this->escapeField($identifier);
}
public function escapeField(string $identifier) : string public function escapeField(string $identifier) : string
{ {
return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_FIELD); return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_FIELD);

View File

@ -5,11 +5,66 @@ namespace Ulmus\Repository;
use Ulmus\{Repository, Query, Ulmus, Common}; use Ulmus\{Repository, Query, Ulmus, Common};
class MssqlRepository extends Repository { class MssqlRepository extends Repository {
/*
protected function finalizeQuery() : void
{
if ( null !== $offset = $this->queryBuilder->getFragment(Query\Offset::class) ) {
if ( null === $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
throw new \Exception("Your offset query fragment is missing a LIMIT value.");
}
# an order by is mandatory for mssql offset/limit
if ( null === $order = $this->queryBuilder->getFragment(Query\OrderBy::class) ) {
$this->orderBy("(SELECT 0)");
}
$mssqlOffset = new \Ulmus\Query\MsSQL\Offset();
$mssqlOffset->set($offset->offset, $limit->limit);
$this->queryBuilder->removeFragment($offset);
$this->queryBuilder->removeFragment($limit);
$this->queryBuilder->push($mssqlOffset);
}
elseif ( null !== $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
$select->top = $limit->limit;
}
elseif ( null !== $delete = $this->queryBuilder->getFragment(Query\Delete::class) ) {
$delete->top = $limit->limit;
}
$this->queryBuilder->removeFragment($limit);
}
}
*/
protected function finalizeQuery() : void protected function finalizeQuery() : void
{ {
if ( null === $order = $this->queryBuilder->getFragment(Query\OrderBy::class) ) { if ( null !== $offset = $this->queryBuilder->getFragment(Query\Offset::class) ) {
$this->orderBy("(SELECT 0)"); # an order by is mandatory for mssql offset/limit
if ( null === $order = $this->queryBuilder->getFragment(Query\OrderBy::class) ) {
$this->orderBy("(SELECT 0)");
}
if ( empty ($offset->offset ) ) {
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
$select->top = $offset->limit;
}
elseif ( null !== $delete = $this->queryBuilder->getFragment(Query\Delete::class) ) {
$delete->top = $offset->limit;
}
$this->queryBuilder->removeFragment($offset);
}
elseif ( empty($offset->limit) ) {
throw new \Exception("Your offset query fragment is missing a LIMIT value.");
}
}
if ( null !== $limit = $this->queryBuilder->getFragment(Query\Limit::class) ) {
$this->queryBuilder->removeFragment($limit);
} }
} }

View File

@ -47,11 +47,9 @@ class RelationBuilder
return $this->resolveRelation($name) ?: $this->resolveVirtual($name); return $this->resolveRelation($name) ?: $this->resolveVirtual($name);
} }
elseif ( $relation = $this->resolver->searchFieldAnnotation($name, new Relation() ) ) { else {
return $this->instanciateEmptyObject($name, $relation); return $this->instanciateEmptyObject($name, $this->resolver->searchFieldAnnotation($name, new Relation() ));
} }
return false;
} }
protected function resolveVirtual(string $name) /* : bool|object|EntityCollection */ protected function resolveVirtual(string $name) /* : bool|object|EntityCollection */

View File

@ -15,17 +15,7 @@ trait SearchRequestPaginationTrait {
public bool $skipCount = false; public bool $skipCount = false;
public ? array $columns = null; public ? array $columns = null;
public function count(): int
{
return $this->count;
}
public function page(): int
{
return $this->page;
}
public function limit(): int public function limit(): int
{ {
return $this->limit; return $this->limit;
@ -33,7 +23,7 @@ trait SearchRequestPaginationTrait {
public function offset(): int public function offset(): int
{ {
return (int) ( abs( ( $this->page() - 1 ) * $this->limit() ) ); return (int) ( abs( ( $this->page - 1 ) * $this->limit() ) );
} }
public function pagination(int $page, int $itemCount) : void public function pagination(int $page, int $itemCount) : void
@ -44,25 +34,17 @@ trait SearchRequestPaginationTrait {
public function pageCount() : int public function pageCount() : int
{ {
return ceil($this->count() / $this->limit()); return ceil($this->count / $this->limit());
} }
public function hasPagination() : int public function hasPagination() : int
{ {
return $this->pageCount() > 1; return $this->pageCount() > 1;
} }
public function skipCount(bool $value) : self public function skipCount(bool $value) : self
{ {
$this->skipCount = $value; $this->skipCount = $value;
return $this; return $this;
} }
public function resultShown() : int
{
$total = $this->page() * $this->limit();
return $total <= $this->count() ? $total : $this->count();
}
} }