Compare commits
20 Commits
Author | SHA1 | Date |
---|---|---|
Dariusz Jaski | e14198e262 | |
Dariusz Jaski | 1be8bc5511 | |
Dariusz Jaski | 37599e8cf6 | |
Dariusz Jaski | fbf2aaae07 | |
Dariusz Jaski | 1920374639 | |
Dariusz Jaski | 1c886fb385 | |
Dariusz Jaski | b9d1d1601a | |
Dariusz Jaski | 3f9758c403 | |
Dariusz Jaski | 661406db67 | |
Dariusz Jaski | 9c6c34f870 | |
Dariusz Jaski | a026facb7c | |
Dariusz Jaski | 3c43612503 | |
Dariusz Jaski | d32c7a0b92 | |
Dariusz Jaski | d9da5559c5 | |
Dariusz Jaski | afc2b5a261 | |
Dariusz Jaski | 54113fffc0 | |
Michal Sieciechowicz | 9204f70e50 | |
Michal Sieciechowicz | a3d8bb6131 | |
Michal Sieciechowicz | a3d71c7bc5 | |
Michal Sieciechowicz | a3c38a27c7 |
|
@ -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",
|
"description": "MicroORM for php8",
|
||||||
"type": "orm",
|
"type": "orm",
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -16,7 +16,8 @@
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"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
|
<?php
|
||||||
|
|
||||||
namespace App\Query;
|
namespace Fufle\Demo\Query;
|
||||||
|
|
||||||
use Fufle\ORM\IQuery as IBaseQuery;
|
use Fufle\ORM\IQuery as IBaseQuery;
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Query;
|
namespace Fufle\Demo\Query;
|
||||||
|
|
||||||
use App\Query\IJobQuery;
|
use Fufle\Demo\Query\IJobQuery;
|
||||||
use Fufle\ORM\Query as BaseQuery;
|
use Fufle\ORM\Query as BaseQuery;
|
||||||
|
|
||||||
class JobQuery extends BaseQuery implements IJobQuery {
|
class JobQuery extends BaseQuery implements IJobQuery {
|
||||||
public function GetLast10(): string {
|
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;
|
namespace Fufle\ORM;
|
||||||
|
|
||||||
use Fufle\Database\ConnectionManager;
|
use Fufle\ORM\Database\ConnectionManager;
|
||||||
|
|
||||||
abstract class Model {
|
abstract class Model {
|
||||||
protected static string $primaryKey = 'id';
|
protected static string $primaryKey = 'id';
|
||||||
protected static ?string $table = null;
|
protected static ?string $table = null;
|
||||||
protected static ?IQuery $query = null;
|
private static ?IQuery $query = null;
|
||||||
|
|
||||||
public static function getTableName() {
|
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 (true || (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;
|
return static::$table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getPrimaryKey() {
|
public static function GetPrimaryKey() {
|
||||||
return static::$primaryKey;
|
return static::$primaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function Get(int $id) {
|
public static function Get(int $id) {
|
||||||
self::Check();
|
|
||||||
return self::Query(
|
return self::Query(
|
||||||
static::$query->Get(),
|
static::QueryModel()->Get(),
|
||||||
[$id]);
|
[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function Query(string $sql, array|object $params = []) {
|
protected static function Query(string $sql, array|object $params = []) {
|
||||||
self::Check();
|
|
||||||
return ConnectionManager::Query(
|
return ConnectionManager::Query(
|
||||||
$sql,
|
$sql,
|
||||||
$params,
|
$params,
|
||||||
get_called_class());
|
get_called_class());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function QueryAll(string $sql, array|object $params = []) {
|
protected static function QueryAll(string $sql, array|object $params = []) {
|
||||||
self::Check();
|
|
||||||
return ConnectionManager::QueryAll(
|
return ConnectionManager::QueryAll(
|
||||||
$sql,
|
$sql,
|
||||||
$params,
|
$params,
|
||||||
get_called_class());
|
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
|
<?php
|
||||||
namespace Fufle\ORM;
|
namespace Fufle\ORM;
|
||||||
|
|
||||||
class Query {
|
class Query implements IQuery {
|
||||||
private string $table;
|
protected string $table;
|
||||||
private string $primaryKey;
|
protected string $primaryKey;
|
||||||
|
|
||||||
public function __construct(string $model) {
|
public function __construct(string $model) {
|
||||||
$this->table = $model::getTableName();
|
$this->table = $model::GetTableName();
|
||||||
$this->primaryKey = $model::getPrimaryKey();
|
$this->primaryKey = $model::GetPrimaryKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Get(): string {
|
public function Get(): string {
|
||||||
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,30 @@
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use App\Model\Job;
|
use Fufle\Demo\Model\Job;
|
||||||
use Fufle\Database\ConnectionManager;
|
//use Fufle\ORM\Database\ConnectionManager;
|
||||||
|
use Fufle\ORM\Database\Connectors\PostgresConnector;
|
||||||
|
|
||||||
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
//$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic;user=dynamic;password=password123';
|
||||||
ConnectionManager::Connect($dns);
|
$params = [
|
||||||
echo "
|
// 'pgsql',
|
||||||
|
'localhost',
|
||||||
";
|
5432,
|
||||||
var_dump(Job::Get(10));
|
'dynamic_dev',
|
||||||
|
'dynamic',
|
||||||
|
'Pass@Dyna21',
|
||||||
|
];
|
||||||
|
PostgresConnector::Connect(...$params);
|
||||||
|
//ConnectionManager::RegisterConnector(new PostgresConnector());
|
||||||
|
//ConnectionManager::Connect(...$params);
|
||||||
echo "
|
echo "
|
||||||
|
|
||||||
";
|
";
|
||||||
var_dump(Job::GetLast10());
|
var_dump(Job::GetLast10());
|
||||||
echo "
|
echo "
|
||||||
|
|
||||||
|
";
|
||||||
|
var_dump(Job::Get(10));
|
||||||
|
echo "
|
||||||
|
|
||||||
";
|
";
|
Loading…
Reference in New Issue