- Work done on Repository and QueryBuilder -- fixed some minor bugs and added some delete() functions

- Added a generic SQL query function to PDOObject
This commit is contained in:
Dave Mc Nicoll 2019-12-19 09:54:33 -05:00
parent b589ba3c2b
commit 9df7e65dcf
10 changed files with 139 additions and 31 deletions

View File

@ -2,6 +2,6 @@
namespace Ulmus\Annotation\Classes; namespace Ulmus\Annotation\Classes;
class Function implements \Ulmus\Annotation\Annotation { class Method implements \Ulmus\Annotation\Annotation {
} }

View File

@ -7,7 +7,9 @@ class Field implements \Ulmus\Annotation\Annotation {
public string $type; public string $type;
public string $name; public string $name;
# public string $column;
public int $length; public int $length;
public array $attributes = []; public array $attributes = [];

View File

@ -9,6 +9,7 @@ use Ulmus\Annotation\Annotation,
class EntityResolver { class EntityResolver {
const KEY_ENTITY_NAME = 01; const KEY_ENTITY_NAME = 01;
const KEY_COLUMN_NAME = 02; const KEY_COLUMN_NAME = 02;
public string $entityClass; public string $entityClass;
@ -51,7 +52,7 @@ class EntityResolver {
public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array
{ {
$fieldList = []; $fieldList = [];
foreach($this->properties as $item) { foreach($this->properties as $item) {
foreach($item['tags'] ?? [] as $tag) { foreach($item['tags'] ?? [] as $tag) {
if ( $tag['object'] instanceof Field ) { if ( $tag['object'] instanceof Field ) {
@ -73,7 +74,7 @@ class EntityResolver {
} }
} }
} }
return $fieldList; return $fieldList;
} }
@ -100,15 +101,15 @@ class EntityResolver {
*/ */
public function getAnnotationFromClassname(string $className) : ? object public function getAnnotationFromClassname(string $className) : ? object
{ {
if ( $name = $this->uses[$className] ?? false) { if ( $name = $this->uses[$className] ?? false ) {
foreach($this->class['tags'] as $item) { foreach(array_reverse($this->class['tags']) as $item) {
if ( $item['tag'] === $name ) { if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item); return $this->instanciateAnnotationObject($item);
} }
foreach($this->properties as $item) { foreach($this->properties as $item) {
foreach($item['tags'] as $item) { foreach(array_reverse($item['tags']) as $item) {
if ( $item['tag'] === $name ) { if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item); return $this->instanciateAnnotationObject($item);
} }
@ -116,7 +117,7 @@ class EntityResolver {
} }
foreach($this->methods as $item) { foreach($this->methods as $item) {
foreach($item['tags'] as $item) { foreach(array_reverse($item['tags']) as $item) {
if ( $item['tag'] === $name ) { if ( $item['tag'] === $name ) {
return $this->instanciateAnnotationObject($item); return $this->instanciateAnnotationObject($item);
} }

View File

@ -57,18 +57,18 @@ class ObjectReflection {
if ( $parentClass = $this->classReflection->getParentClass() ) { if ( $parentClass = $this->classReflection->getParentClass() ) {
$class = static::fromClass($parentClass)->gatherClass(true); $class = static::fromClass($parentClass)->gatherClass(true);
} }
$itemName = function($item) { $itemName = function($item) {
return $item->getName(); return $item->getName();
}; };
} }
return [ return array_merge_recursive($class, [
'tags' => array_merge($class, $this->annotationReader->getClass($this->classReflection)) 'tags' => $this->annotationReader->getClass($this->classReflection)
] + ( ! $full ? [] : [ ] + ( ! $full ? [] : [
'traits' => array_map($itemName, $this->classReflection->getTraits()), 'traits' => array_map($itemName, $this->classReflection->getTraits()),
'interfaces' => array_map($itemName, $this->classReflection->getInterfaces()), 'interfaces' => array_map($itemName, $this->classReflection->getInterfaces()),
]); ]));
} }
public function gatherProperties(bool $full = true, int $filter = public function gatherProperties(bool $full = true, int $filter =
@ -86,11 +86,9 @@ class ObjectReflection {
} }
} }
$properties = array_merge($properties, $this->classReflection->getProperties($filter));
$list = []; $list = [];
foreach($properties as $property) { foreach($this->classReflection->getProperties($filter) as $property) {
$current = [ $current = [
'name' => $property->getName() 'name' => $property->getName()
]; ];
@ -111,10 +109,10 @@ class ObjectReflection {
continue; continue;
} }
$list[ $current['name'] ] = $current; $list[ $current['name'] ] = $current;
} }
return $list; return array_merge($properties, $list);
} }
public function gatherMethods(bool $full = true, int $filter = public function gatherMethods(bool $full = true, int $filter =
@ -131,12 +129,10 @@ class ObjectReflection {
$methods = static::fromClass($parentClass)->gatherMethods($full, $filter); $methods = static::fromClass($parentClass)->gatherMethods($full, $filter);
} }
} }
$methods = array_merge($methods, $this->classReflection->getMethods($filter)); foreach($this->classReflection->getMethods($filter) as $method) {
foreach($methods as $method) {
$parameters = []; $parameters = [];
foreach($method->getParameters() as $parameter) { foreach($method->getParameters() as $parameter) {
$parameters[$parameter->getName()] = [ $parameters[$parameter->getName()] = [
'null' => $parameter->allowsNull(), 'null' => $parameter->allowsNull(),
@ -166,7 +162,7 @@ class ObjectReflection {
$list[ $current['name'] ] = $current; $list[ $current['name'] ] = $current;
} }
return $list; return array_merge($methods, $list);
} }
protected function ignoreElementAnnotation($tags) : bool protected function ignoreElementAnnotation($tags) : bool

View File

@ -9,7 +9,7 @@ class PdoObject extends PDO {
public function select(string $sql, array $parameters = []) : PDOStatement public function select(string $sql, array $parameters = []) : PDOStatement
{ {
try { try {
if ( $statement = $this->prepare($sql) ) { if ( false !== ( $statement = $this->prepare($sql) ) ) {
$statement = $this->execute($statement, $parameters, true); $statement = $this->execute($statement, $parameters, true);
$statement->setFetchMode(\PDO::FETCH_ASSOC); $statement->setFetchMode(\PDO::FETCH_ASSOC);
return $statement; return $statement;
@ -17,6 +17,16 @@ class PdoObject extends PDO {
} catch (\PDOException $e) { throw $e; } } catch (\PDOException $e) { throw $e; }
} }
public function runQuery(string $sql, array $parameters = []) : PDOStatement
{
# var_dump($sql, $parameters);die();
try {
if ( false !== ( $statement = $this->prepare($sql) ) ) {
return $this->execute($statement, $parameters, true);
}
} catch (\PDOException $e) { throw $e; }
}
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? PDOStatement public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? PDOStatement
{ {
try { try {

View File

@ -2,7 +2,22 @@
namespace Ulmus; namespace Ulmus;
use Generator;
#class EntityCollection implements \Countable, \ArrayAccess #class EntityCollection implements \Countable, \ArrayAccess
class EntityCollection extends \ArrayObject { class EntityCollection extends \ArrayObject {
# use Common\ArrayObjectTrait; # use Common\ArrayObjectTrait;
}
public function filters(Callable $callback) : Generator
{
$idx = 0;
foreach($this as $key => $item) {
if ( $callback($item, $key, $idx) ) {
$idx++;
yield $item;
}
}
}
}

27
src/Query/Delete.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace Ulmus\Query;
class Delete extends Fragment {
public int $order = -100;
public bool $lowPriority = false;
public bool $quick = false;
public bool $ignore = false;
public string $alias;
public function render() : string
{
return $this->renderSegments([
'DELETE',
( $this->alias ?? false ),
( $this->lowPriority ? 'LOW_PRIORITY' : false ),
( $this->quick ? 'QUICK' : false ),
( $this->ignore ? 'IGNORE' : false ),
]);
}
}

View File

@ -38,6 +38,19 @@ class QueryBuilder
return $this; return $this;
} }
public function delete(string $alias = "") : self
{
if ( ! $this->has(Query\Delete::class) ) {
$delete = new Query\Delete();
$delete->alias = $alias;
$this->push($delete);
}
return $this;
}
public function from($table, $alias = null, $database = null) : self public function from($table, $alias = null, $database = null) : self
{ {

View File

@ -38,6 +38,7 @@ class Repository
public function loadFromPk($value, $primaryKey = "id") : EntityCollection public function loadFromPk($value, $primaryKey = "id") : EntityCollection
{ {
# var_dump("<pre>", $primaryKey);die();
return $this->where($primaryKey, $value)->loadOne(); return $this->where($primaryKey, $value)->loadOne();
} }
@ -46,6 +47,21 @@ class Repository
return $this->where($field, $value)->collectionFromQuery(); return $this->where($field, $value)->collectionFromQuery();
} }
public function deleteOne()
{
return $this->limit(1)->deleteSqlQuery()->runQuery();
}
public function deleteAll()
{
return $this->deleteSqlQuery()->runQuery();
}
public function deleteFromPk($value, $primaryKey = "id")
{
return $this->where($primaryKey, $value)->deleteOne();
}
public function yieldAll() : \Generator public function yieldAll() : \Generator
{ {
@ -57,6 +73,12 @@ class Repository
return $this; return $this;
} }
public function delete() : self
{
$this->queryBuilder->delete($this->alias);
return $this;
}
public function from($table) : self public function from($table) : self
{ {
foreach((array) $table as $alias => $table) { foreach((array) $table as $alias => $table) {
@ -229,6 +251,11 @@ class Repository
return $entityCollection; return $entityCollection;
} }
public function runQuery()
{
return Ulmus::runQuery($this->queryBuilder);
}
protected function selectSqlQuery() : self protected function selectSqlQuery() : self
{ {
if ( ! $this->queryBuilder->has(Query\Select::class) ) { if ( ! $this->queryBuilder->has(Query\Select::class) ) {
@ -242,6 +269,19 @@ class Repository
return $this; return $this;
} }
protected function deleteSqlQuery() : self
{
if ( ! $this->queryBuilder->has(Query\Delete::class) ) {
$this->delete();
}
if ( ! $this->queryBuilder->has(Query\From::class) ) {
$this->from([ $this->alias => $this->entityResolver->tableName() ]);
}
return $this;
}
protected function fromRow($row) : self protected function fromRow($row) : self
{ {

View File

@ -14,12 +14,6 @@ abstract class Ulmus
public static array $resolved = []; public static array $resolved = [];
protected static function fetchQueryBuilder(QueryBuilder $queryBuilder, ?ConnectionAdapter $adapter = null) : array
{
$sql = $queryBuilder->render();
return ( $adapter ?: static::$defaultAdapter )->pdo->select($sql, $queryBuilder->parameters ?? [])->fetchAll();
}
public static function iterateQueryBuilder(QueryBuilder $queryBuilder, ?ConnectionAdapter $adapter = null) : Generator public static function iterateQueryBuilder(QueryBuilder $queryBuilder, ?ConnectionAdapter $adapter = null) : Generator
{ {
$sql = $queryBuilder->render(); $sql = $queryBuilder->render();
@ -35,6 +29,11 @@ abstract class Ulmus
'count' => $statement->rowCount(), 'count' => $statement->rowCount(),
]; ];
} }
public static function runQuery(QueryBuilder $queryBuilder, ?ConnectionAdapter $adapter = null)
{
return ( $adapter ?: static::$defaultAdapter )->pdo->runQuery($queryBuilder->render(), $queryBuilder->parameters ?? []);
}
public static function resolveEntity(string $entityClass) : Common\EntityResolver public static function resolveEntity(string $entityClass) : Common\EntityResolver
{ {
@ -50,4 +49,9 @@ abstract class Ulmus
{ {
return new static::$queryBuilderClass(...$arguments); return new static::$queryBuilderClass(...$arguments);
} }
protected static function fetchQueryBuilder(QueryBuilder $queryBuilder, ?ConnectionAdapter $adapter = null) : array
{
return ( $adapter ?: static::$defaultAdapter )->pdo->select($queryBuilder->render(), $queryBuilder->parameters ?? [])->fetchAll();
}
} }