PR2: 0.2.3
Co-Authored-By: Sieciech <sieciech@noreply.fufle.net> Co-Committed-By: Sieciech <sieciech@noreply.fufle.net>
This commit is contained in:
parent
e79ee148dd
commit
248006a830
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use App\Query\JobQuery;
|
||||
use Fufle\ORM\Model as BaseModel;
|
||||
|
||||
class Job extends BaseModel {
|
||||
//private string $table = 'jobs';
|
||||
//protected static ?IJobQuery $query;
|
||||
public int $id;
|
||||
public float $budget;
|
||||
public static function Initialize() {
|
||||
if (static::$query === null) {
|
||||
static::$query = new JobQuery(self::class);
|
||||
}
|
||||
}
|
||||
public static function GetLast10() {
|
||||
return self::QueryAll(self::$query->GetLast10());
|
||||
}
|
||||
}
|
|
@ -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/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\Demo\Model;
|
||||
|
||||
use Fufle\Demo\Query\IJobQuery;
|
||||
use Fufle\Demo\Query\JobQuery;
|
||||
use Fufle\ORM\Model as BaseModel;
|
||||
use Fufle\ORM\IQuery;
|
||||
|
||||
class Job extends BaseModel {
|
||||
public int $id;
|
||||
public float $budget;
|
||||
public static function SetQueryModel(): IQuery {
|
||||
return new JobQuery( static::class );
|
||||
}
|
||||
|
||||
public static function GetLast10() {
|
||||
return self::QueryAll(self::QueryModel()->GetLast10());
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
namespace Fufle\Demo\Query;
|
||||
|
||||
use Fufle\ORM\IQuery as IBaseQuery;
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
namespace Fufle\Demo\Query;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use Fufle\Demo\Query\IJobQuery;
|
||||
use Fufle\ORM\Query as BaseQuery;
|
||||
|
||||
class JobQuery extends BaseQuery implements IJobQuery {
|
||||
public function GetLast10(): string {
|
||||
return "select * from jobs order by id desc limit 10";
|
||||
return "SELECT * from jobs order by id desc limit 10";
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
class ConnectionManager {
|
||||
private static $connection;
|
||||
|
||||
public static function connect(string $connectionString) {
|
||||
self::$connection = new PDO($connectionString);
|
||||
}
|
||||
|
||||
public static function Query(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$stmt = self::$connection->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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
use PDO;
|
||||
use Fufle\ORM\Database\QueryData;
|
||||
use Fufle\ORM\Database\Connectors\IConnector;
|
||||
|
||||
class ConnectionManager {
|
||||
private static $connection;
|
||||
private static $connectors = [];
|
||||
private static string $connectionString;
|
||||
private static Credentials $credentials;
|
||||
|
||||
public static function RegisterConnector(IConnector $connector) {
|
||||
self::$connectors[] = $connector;
|
||||
}
|
||||
|
||||
public static function Connect(string $driver, $host, $port = null, $database = null, $user = null, $password = null) {
|
||||
foreach (self::$connectors as $c) {
|
||||
if ($c->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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database\Connectors;
|
||||
|
||||
use Fufle\ORM\Database\Credentials;
|
||||
|
||||
interface IConnector {
|
||||
public function getConnectionString(Credentials $credentials);
|
||||
public function driverEquals(string $driver);
|
||||
public static function Connect(string $host, int $port, string $database, string $user, string $password);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database\Connectors;
|
||||
|
||||
use Fufle\ORM\Database\Credentials;
|
||||
use Fufle\ORM\Database\ConnectionManager;
|
||||
|
||||
class PostgresConnector implements IConnector {
|
||||
public function getConnectionString(Credentials $c) {
|
||||
return "pgsql:host={$c->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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
class Credentials {
|
||||
|
||||
public function __construct(
|
||||
public string $host,
|
||||
public ?int $port = null,
|
||||
public ?string $database = null,
|
||||
public ?string $user = null,
|
||||
public ?string $password = null) {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
class QueryData {
|
||||
public function __construct(
|
||||
public string $query,
|
||||
public array|object $params
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public function normalize() {
|
||||
$params = (array)$this->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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -1,13 +1,15 @@
|
|||
<?php
|
||||
namespace Fufle\ORM;
|
||||
|
||||
class Query {
|
||||
private string $table;
|
||||
private string $primaryKey;
|
||||
class Query implements IQuery {
|
||||
protected string $table;
|
||||
protected string $primaryKey;
|
||||
|
||||
public function __construct(string $model) {
|
||||
$this->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.' = ?';
|
||||
}
|
||||
|
|
|
@ -1,19 +1,30 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use App\Model\Job;
|
||||
use Fufle\Database\ConnectionManager;
|
||||
use Fufle\Demo\Model\Job;
|
||||
//use Fufle\ORM\Database\ConnectionManager;
|
||||
use Fufle\ORM\Database\Connectors\PostgresConnector;
|
||||
|
||||
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
||||
ConnectionManager::Connect($dns);
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::Get(10));
|
||||
//$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic;user=dynamic;password=password123';
|
||||
$params = [
|
||||
// 'pgsql',
|
||||
'localhost',
|
||||
5432,
|
||||
'dynamic_dev',
|
||||
'dynamic',
|
||||
'Pass@Dyna21',
|
||||
];
|
||||
PostgresConnector::Connect(...$params);
|
||||
//ConnectionManager::RegisterConnector(new PostgresConnector());
|
||||
//ConnectionManager::Connect(...$params);
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::GetLast10());
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::Get(10));
|
||||
echo "
|
||||
|
||||
";
|
Loading…
Reference in New Issue