diff --git a/app/Model/Job.php b/app/Model/Job.php deleted file mode 100644 index 911d4b7..0000000 --- a/app/Model/Job.php +++ /dev/null @@ -1,22 +0,0 @@ -GetLast10()); - } -} \ No newline at end of file diff --git a/composer.json b/composer.json index 13d2057..64a22b3 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "Fufle/SiBase2", + "name": "fufle/micro-orm", "description": "MicroORM for php8", "type": "orm", "require": { @@ -16,7 +16,8 @@ "minimum-stability": "dev", "autoload": { "psr-4": { - "Fufle\\": "src/Fufle/" + "Fufle\\": "src/Fufle/", + "Fufle\\Demo\\": "demo/" } } } diff --git a/demo/Model/Job.php b/demo/Model/Job.php new file mode 100644 index 0000000..f3cb9ca --- /dev/null +++ b/demo/Model/Job.php @@ -0,0 +1,20 @@ +GetLast10()); + } +} diff --git a/app/Query/IJobQuery.php b/demo/Query/IJobQuery.php similarity index 81% rename from app/Query/IJobQuery.php rename to demo/Query/IJobQuery.php index ea0fb0b..6b44286 100644 --- a/app/Query/IJobQuery.php +++ b/demo/Query/IJobQuery.php @@ -1,6 +1,6 @@ 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 deleted file mode 100644 index e69de29..0000000 diff --git a/src/Fufle/Database/Connector/PostgresConnector.php b/src/Fufle/Database/Connector/PostgresConnector.php deleted file mode 100644 index e69de29..0000000 diff --git a/src/Fufle/ORM/Database/ConnectionManager.php b/src/Fufle/ORM/Database/ConnectionManager.php new file mode 100644 index 0000000..54935ef --- /dev/null +++ b/src/Fufle/ORM/Database/ConnectionManager.php @@ -0,0 +1,52 @@ +driverEquals($driver)) { + $credentials = new Credentials($host, $port, $database, $user, $password); + self::$connectionString = $c->getConnectionString($credentials); + self::ConnectWithString(self::$connectionString); + return; + } + } + throw new Exception('Driver not found'); + } + + public static function ConnectWithString(string $connectionString) { + self::$connection = new PDO($connectionString); + } + + public static function Query(string $query, array|object $params = [], string $output = 'stdObject') { + $data = new QueryData($query, $params); + $data->normalize(); + $stmt = self::$connection->prepare($data->query); + $stmt->execute($data->params); + $stmt->setFetchMode(PDO::FETCH_CLASS, $output); + return $stmt->fetch(); + } + + public static function QueryAll(string $query, array|object $params = [], string $output = 'stdObject') { + $data = new QueryData($query, $params); + $data->normalize(); + $stmt = self::$connection->prepare($data->query); + $stmt->execute($data->params); + $stmt->setFetchMode(PDO::FETCH_CLASS, $output); + return $stmt->fetchAll(); + } +} \ No newline at end of file diff --git a/src/Fufle/ORM/Database/Connectors/IConnector.php b/src/Fufle/ORM/Database/Connectors/IConnector.php new file mode 100644 index 0000000..02b34e5 --- /dev/null +++ b/src/Fufle/ORM/Database/Connectors/IConnector.php @@ -0,0 +1,11 @@ +host};port={$c->port};dbname={$c->database};user={$c->user};password={$c->password}"; + } + public function driverEquals(string $driver) { + return $driver === 'pgsql'; + } + + public static function Connect(string $host, int $port, string $database, string $user, string $password) { + ConnectionManager::RegisterConnector(new PostgresConnector()); + ConnectionManager::Connect('pgsql', $host, $port, $database, $user, $password); + } +} \ No newline at end of file diff --git a/src/Fufle/ORM/Database/Credentials.php b/src/Fufle/ORM/Database/Credentials.php new file mode 100644 index 0000000..47493ac --- /dev/null +++ b/src/Fufle/ORM/Database/Credentials.php @@ -0,0 +1,15 @@ +params; + $replaces = []; + foreach($params as $name => $param) { + if (is_array($param)) { + $replaces2 = []; + foreach($param as $i => $p) { + $params[$name.$i.'x'] = $p; + $replaces[] = ':'.$name.$i.'x'; + } + unset($params[$name]); + $this->query = str_replace(':'.$name, implode(', ', $replaces), $this->query); + } + } + $this->params = $params; + } +} \ No newline at end of file diff --git a/src/Fufle/ORM/Model.php b/src/Fufle/ORM/Model.php index 4ceb41d..1698375 100644 --- a/src/Fufle/ORM/Model.php +++ b/src/Fufle/ORM/Model.php @@ -2,50 +2,63 @@ namespace Fufle\ORM; -use Fufle\Database\ConnectionManager; +use Fufle\ORM\Database\ConnectionManager; abstract class Model { protected static string $primaryKey = 'id'; protected static ?string $table = null; - protected static ?IQuery $query = null; - - public static function getTableName() { + private static ?IQuery $query = null; + + protected static array $fields = []; + + public function toArray(): array { + $array = []; + $fields = static::$fields; + foreach($fields as $field) { + if ($this->{$field} ?? null !== null) { + $array[$field] = $this->{$field}; + } + } + return $array; + } + + protected static function QueryModel() { + if (self::$query === null && self::class !== static::class) { + self::$query = static::SetQueryModel(); + } + return self::$query; + } + + public static function GetTableName() { + if (static::$table === null) { + static::$table = basename(str_replace('\\', '/', get_called_class())) . 's'; + } return static::$table; } - - public static function getPrimaryKey() { + + public static function GetPrimaryKey() { return static::$primaryKey; } - + public static function Get(int $id) { - self::Check(); return self::Query( - static::$query->Get(), + static::QueryModel()->Get(), [$id]); } - + protected static function Query(string $sql, array|object $params = []) { - self::Check(); return ConnectionManager::Query( $sql, $params, get_called_class()); } - + protected static function QueryAll(string $sql, array|object $params = []) { - self::Check(); return ConnectionManager::QueryAll( $sql, $params, get_called_class()); } - - public static function Check() { - if (static::$table === null) { - static::$table = basename(str_replace('\\', '/', get_called_class())) . 's'; - } - static::Initialize(); - } - abstract public static function Initialize(); + abstract public static function SetQueryModel(): IQuery; } \ No newline at end of file diff --git a/src/Fufle/ORM/Query.php b/src/Fufle/ORM/Query.php index 2052a56..b0e0950 100644 --- a/src/Fufle/ORM/Query.php +++ b/src/Fufle/ORM/Query.php @@ -1,13 +1,15 @@ table = $model::getTableName(); - $this->primaryKey = $model::getPrimaryKey(); + $this->table = $model::GetTableName(); + $this->primaryKey = $model::GetPrimaryKey(); } + public function Get(): string { return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?'; } diff --git a/tests/ModelTests.php b/tests/ModelTests.php index 7510169..9028c9d 100644 --- a/tests/ModelTests.php +++ b/tests/ModelTests.php @@ -1,19 +1,30 @@