From c91ccb94809693a1f8a09c22fbf9e239b52b67cd Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sun, 28 Feb 2021 00:04:16 +0100 Subject: [PATCH] package --- app/Model/JobModel.php | 22 +++++++++ app/Query/IJobQuery.php | 9 ++++ app/Query/JobQuery.php | 12 +++++ src/Fufle/Database/ConnectionManager.php | 27 +++++++++++ src/Fufle/Database/Connector/IConnector.php | 0 .../Database/Connector/PostgresConnector.php | 0 src/Fufle/ORM/IQuery.php | 10 ++++ src/Fufle/ORM/Model.php | 48 +++++++++++++++++++ src/Fufle/ORM/Query.php | 20 ++++++++ tests/ModelTests.php | 16 +++++++ 10 files changed, 164 insertions(+) create mode 100644 app/Model/JobModel.php create mode 100644 app/Query/IJobQuery.php create mode 100644 app/Query/JobQuery.php create mode 100644 src/Fufle/Database/ConnectionManager.php create mode 100644 src/Fufle/Database/Connector/IConnector.php create mode 100644 src/Fufle/Database/Connector/PostgresConnector.php create mode 100644 src/Fufle/ORM/IQuery.php create mode 100644 src/Fufle/ORM/Model.php create mode 100644 src/Fufle/ORM/Query.php create mode 100644 tests/ModelTests.php diff --git a/app/Model/JobModel.php b/app/Model/JobModel.php new file mode 100644 index 0000000..73413ad --- /dev/null +++ b/app/Model/JobModel.php @@ -0,0 +1,22 @@ +GetLast10()); + } +} \ No newline at end of file diff --git a/app/Query/IJobQuery.php b/app/Query/IJobQuery.php new file mode 100644 index 0000000..ea0fb0b --- /dev/null +++ b/app/Query/IJobQuery.php @@ -0,0 +1,9 @@ +prepare($query); + $stmt->execute($params); + $stmt->setFetchMode(PDO::FETCH_CLASS, $output); + return $stmt->fetch(); + } + + public static function QueryAll(string $query, array|object $params = [], string $output = 'stdObject') { + $stmt = self::$connection->prepare($query); + $stmt->execute($params); + $stmt->setFetchMode(PDO::FETCH_CLASS, $output); + return $stmt->fetchAll(); + } +} \ No newline at end of file diff --git a/src/Fufle/Database/Connector/IConnector.php b/src/Fufle/Database/Connector/IConnector.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Fufle/Database/Connector/PostgresConnector.php b/src/Fufle/Database/Connector/PostgresConnector.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Fufle/ORM/IQuery.php b/src/Fufle/ORM/IQuery.php new file mode 100644 index 0000000..797bb54 --- /dev/null +++ b/src/Fufle/ORM/IQuery.php @@ -0,0 +1,10 @@ +Get(), + [$id]); + } + + protected static function Query($sql, $params) { + self::Initialize(); + return ConnectionManager::Query( + $sql, + $params, + get_called_class()); + } + + protected static function QueryAll($sql, $params) { + self::Initialize(); + return ConnectionManager::QueryAll( + $sql, + $params, + get_called_class()); + } + + abstract public static function Initialize() { + if (static::$table === null) { + static::$table = basename(get_called_class()) . 's'; + } + static::Initialize(); + } +} \ No newline at end of file diff --git a/src/Fufle/ORM/Query.php b/src/Fufle/ORM/Query.php new file mode 100644 index 0000000..6a9720d --- /dev/null +++ b/src/Fufle/ORM/Query.php @@ -0,0 +1,20 @@ +table = $model->getTableName(); + $this->primaryKey = $model->getPrimaryKey(); + } + public function Get(): string { + return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?'; + } + + //public function Insert(): string; + //public function Update(): string; + public function Delete(): string { + return 'DELETE FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?'; + } +} \ No newline at end of file diff --git a/tests/ModelTests.php b/tests/ModelTests.php new file mode 100644 index 0000000..84e3794 --- /dev/null +++ b/tests/ModelTests.php @@ -0,0 +1,16 @@ +