diff --git a/src/Annotation/Classes/Method.php b/src/Annotation/Classes/Method.php index 589aba6..523d2bb 100644 --- a/src/Annotation/Classes/Method.php +++ b/src/Annotation/Classes/Method.php @@ -2,6 +2,6 @@ namespace Ulmus\Annotation\Classes; -class Function implements \Ulmus\Annotation\Annotation { +class Method implements \Ulmus\Annotation\Annotation { } diff --git a/src/Annotation/Property/Field.php b/src/Annotation/Property/Field.php index c6671a5..7887111 100644 --- a/src/Annotation/Property/Field.php +++ b/src/Annotation/Property/Field.php @@ -7,7 +7,9 @@ class Field implements \Ulmus\Annotation\Annotation { public string $type; public string $name; - + + # public string $column; + public int $length; public array $attributes = []; diff --git a/src/Common/EntityResolver.php b/src/Common/EntityResolver.php index 0f0281e..42a95b9 100644 --- a/src/Common/EntityResolver.php +++ b/src/Common/EntityResolver.php @@ -9,6 +9,7 @@ use Ulmus\Annotation\Annotation, class EntityResolver { const KEY_ENTITY_NAME = 01; + const KEY_COLUMN_NAME = 02; public string $entityClass; @@ -51,7 +52,7 @@ class EntityResolver { public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array { $fieldList = []; - + foreach($this->properties as $item) { foreach($item['tags'] ?? [] as $tag) { if ( $tag['object'] instanceof Field ) { @@ -73,7 +74,7 @@ class EntityResolver { } } } - + return $fieldList; } @@ -100,15 +101,15 @@ class EntityResolver { */ 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 ) { return $this->instanciateAnnotationObject($item); } foreach($this->properties as $item) { - foreach($item['tags'] as $item) { + foreach(array_reverse($item['tags']) as $item) { if ( $item['tag'] === $name ) { return $this->instanciateAnnotationObject($item); } @@ -116,7 +117,7 @@ class EntityResolver { } foreach($this->methods as $item) { - foreach($item['tags'] as $item) { + foreach(array_reverse($item['tags']) as $item) { if ( $item['tag'] === $name ) { return $this->instanciateAnnotationObject($item); } diff --git a/src/Common/ObjectReflection.php b/src/Common/ObjectReflection.php index 7108c13..65be665 100644 --- a/src/Common/ObjectReflection.php +++ b/src/Common/ObjectReflection.php @@ -57,18 +57,18 @@ class ObjectReflection { if ( $parentClass = $this->classReflection->getParentClass() ) { $class = static::fromClass($parentClass)->gatherClass(true); } - + $itemName = function($item) { return $item->getName(); }; } - return [ - 'tags' => array_merge($class, $this->annotationReader->getClass($this->classReflection)) + return array_merge_recursive($class, [ + 'tags' => $this->annotationReader->getClass($this->classReflection) ] + ( ! $full ? [] : [ 'traits' => array_map($itemName, $this->classReflection->getTraits()), 'interfaces' => array_map($itemName, $this->classReflection->getInterfaces()), - ]); + ])); } public function gatherProperties(bool $full = true, int $filter = @@ -86,11 +86,9 @@ class ObjectReflection { } } - $properties = array_merge($properties, $this->classReflection->getProperties($filter)); - $list = []; - foreach($properties as $property) { + foreach($this->classReflection->getProperties($filter) as $property) { $current = [ 'name' => $property->getName() ]; @@ -111,10 +109,10 @@ class ObjectReflection { continue; } - $list[ $current['name'] ] = $current; + $list[ $current['name'] ] = $current; } - return $list; + return array_merge($properties, $list); } public function gatherMethods(bool $full = true, int $filter = @@ -131,12 +129,10 @@ class ObjectReflection { $methods = static::fromClass($parentClass)->gatherMethods($full, $filter); } } - - $methods = array_merge($methods, $this->classReflection->getMethods($filter)); - - foreach($methods as $method) { + + foreach($this->classReflection->getMethods($filter) as $method) { $parameters = []; - + foreach($method->getParameters() as $parameter) { $parameters[$parameter->getName()] = [ 'null' => $parameter->allowsNull(), @@ -166,7 +162,7 @@ class ObjectReflection { $list[ $current['name'] ] = $current; } - return $list; + return array_merge($methods, $list); } protected function ignoreElementAnnotation($tags) : bool diff --git a/src/Common/PdoObject.php b/src/Common/PdoObject.php index cc1274c..5af04dd 100644 --- a/src/Common/PdoObject.php +++ b/src/Common/PdoObject.php @@ -9,7 +9,7 @@ class PdoObject extends PDO { public function select(string $sql, array $parameters = []) : PDOStatement { try { - if ( $statement = $this->prepare($sql) ) { + if ( false !== ( $statement = $this->prepare($sql) ) ) { $statement = $this->execute($statement, $parameters, true); $statement->setFetchMode(\PDO::FETCH_ASSOC); return $statement; @@ -17,6 +17,16 @@ class PdoObject extends PDO { } 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 { try { diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 27b1ade..e9d6f3f 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -2,7 +2,22 @@ namespace Ulmus; +use Generator; + #class EntityCollection implements \Countable, \ArrayAccess class EntityCollection extends \ArrayObject { # use Common\ArrayObjectTrait; -} + + public function filters(Callable $callback) : Generator + { + $idx = 0; + + foreach($this as $key => $item) { + if ( $callback($item, $key, $idx) ) { + $idx++; + + yield $item; + } + } + } +} \ No newline at end of file diff --git a/src/Query/Delete.php b/src/Query/Delete.php new file mode 100644 index 0000000..083088d --- /dev/null +++ b/src/Query/Delete.php @@ -0,0 +1,27 @@ +renderSegments([ + 'DELETE', + ( $this->alias ?? false ), + ( $this->lowPriority ? 'LOW_PRIORITY' : false ), + ( $this->quick ? 'QUICK' : false ), + ( $this->ignore ? 'IGNORE' : false ), + ]); + } +} diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index ac0f37a..ba12135 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -38,6 +38,19 @@ class QueryBuilder 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 { diff --git a/src/Repository.php b/src/Repository.php index de8cc9d..0fc00f8 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -38,6 +38,7 @@ class Repository public function loadFromPk($value, $primaryKey = "id") : EntityCollection { + # var_dump("
", $primaryKey);die(); return $this->where($primaryKey, $value)->loadOne(); } @@ -46,6 +47,21 @@ class Repository 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 { @@ -57,6 +73,12 @@ class Repository return $this; } + public function delete() : self + { + $this->queryBuilder->delete($this->alias); + return $this; + } + public function from($table) : self { foreach((array) $table as $alias => $table) { @@ -229,6 +251,11 @@ class Repository return $entityCollection; } + public function runQuery() + { + return Ulmus::runQuery($this->queryBuilder); + } + protected function selectSqlQuery() : self { if ( ! $this->queryBuilder->has(Query\Select::class) ) { @@ -242,6 +269,19 @@ class Repository 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 { diff --git a/src/Ulmus.php b/src/Ulmus.php index f7f322e..0869103 100644 --- a/src/Ulmus.php +++ b/src/Ulmus.php @@ -14,12 +14,6 @@ abstract class Ulmus 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 { $sql = $queryBuilder->render(); @@ -35,6 +29,11 @@ abstract class Ulmus '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 { @@ -50,4 +49,9 @@ abstract class Ulmus { 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(); + } }