microphp/Core/DatabaseORM.php

350 lines
11 KiB
PHP

<?php
include_once "Reflection.php";
include_once "Helpers.php";
function name($name)
{
return (
new tstring($name)
)->wrap(".", ".", function($item){
return "`" . $item->match("/^[\"\']?(.*?)[\"\']?$/")[1] . "`";
});
}
function isString($data)
{
return gettype($data) == "string" || $data instanceof tstring;
}
class ORM
{
public $mode = "select";
public $database = "";
public $table = "";
public $select = [];
public $where = [];
public $groupBy = [];
public $having = [];
public $orderby = [];
public $limit = false;
public $union = [];
public $set = [];
public $join = [];
public $alter = [];
public $israw = false;
public $raw = "";
public static $defaultConnection = null;
public $connection;
public function __construct()
{
$this->connection = ORM::$defaultConnection;
}
public static function name($args)
{
return name($args);
}
public static function raw($sql)
{
$orm = new static();
$orm->raw = $sql;
$orm->israw = true;
return $orm;
}
public static function migrate($class)
{
$orm = new static();
$migration = new ORMMigrate();
$class($migration);
$orm->connection->migrate($migration);
}
public function from($orm)
{
foreach (get_class_vars($orm) as $key => $value) {
$this->{$key} = $value;
}
}
public function table($table)
{
$this->table = $table;
return $this;
}
public function database($database)
{
$this->database = $database;
return $this;
}
public function select(...$args)
{
$this->select = array_merge($this->select, array_map(
function($item){
return new tstring($item);
},
$args
));
return $this;
}
public function update(...$arguments)
{
$this->mode = "update";
$this->set($arguments);
}
public function delete()
{
$this->mode = "delete";
}
public function insert(...$arguments)
{
$this->mode = "insert";
$this->set($arguments);
}
public function renameTable($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["renametable", $from, $to];
}
public function renameColumn($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["renamecolumn", $from, $to];
}
public function modifyColumn($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["modifyColumn", $from, $to];
}
public function addColumn($from)
{
$this->mode = "alter";
$this->alter[] = ["addColumn", $from, $to];
}
public function removeColumn($from)
{
$this->mode = "alter";
$this->alter[] = ["addColumn", $from, $to];
}
public function union(ORM $orm)
{
if($orm->mode == "select")
{
$this->union[] = $orm;
}
}
public function limit($offset = 0, $length = false)
{
$this->limit = [
"offset" => $offset,
"length" => $length
];
}
public function addWhere($statement, $name, $operator, $value)
{
$this->where = array_merge(
$this->where,
[
$statement,
name($name),
$operator,
$value
]
);
}
public function where(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function whereNot(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"!=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function orWhere(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function orWhereNot(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS NOT","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function whereIn($name, $arr)
{
$this->where(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereIn($name, $arr)
{
$this->orwhere(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function whereNotIn($name, $arr)
{
$this->where(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereNotIn($name, $arr)
{
$this->orwhere(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function between($name, $state1, $state2)
{
$this->orwhere(name($name), "BETWEEN", $state1 . " AND " . $state2);
return $this;
}
public function groupBy(...$name)
{
$this->groupBy = array_merge(
$this->groupBy,
...$name
);
return $this;
}
public function having(...$name)
{
$this->having = array_merge(
$this->having,
...$name
);
return $this;
}
public function orderBy($name,$mode = "ASC")
{
$this->orderBy[] = [$name,$mode];
return $this;
}
public function set($name,$value = false)
{
if(is_array($name))
{
foreach ($name as $key => $value) {
$this->set[] = [
name($key),
$value
];
}
}else{
$this->set[] = [
name($name),
$value
];
}
return $this;
}
};
class ORMMigrate
{
use ReflectionHook;
public $table = "";
public $charset = "";
public $columns = [];
public $modifycolumns = [];
public $removecolumns = [];
public function table($name)
{
$this->table = name($name);
}
public function charset($charset)
{
$this->charset = $charset;
}
public function callable($name, $type = "int", $property = "")
{
$this->columns[name($name).""] = [$type,$property];
}
public function setAttribute($name, $value)
{
$this->modifycolumns[name($name).""] = $value;
}
public function removeAttribute($name)
{
$this->removecolumns[] = name($name)."";
}
};