289 lines
8.8 KiB
PHP
289 lines
8.8 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 = [];
|
|
public $union = [];
|
|
public $set = [];
|
|
public $join = [];
|
|
public $alter = [];
|
|
public static function name($args)
|
|
{
|
|
return name($args);
|
|
}
|
|
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->alter[] = ["renametable", $from, $to];
|
|
}
|
|
public function renameColumn($from, $to)
|
|
{
|
|
$this->alter[] = ["renamecolumn", $from, $to];
|
|
}
|
|
public function modifyColumn($from, $to)
|
|
{
|
|
$this->alter[] = ["modifyColumn", $from, $to];
|
|
}
|
|
public function addColumn($from)
|
|
{
|
|
$this->alter[] = ["addColumn", $from, $to];
|
|
}
|
|
public function removeColumn($from)
|
|
{
|
|
$this->alter[] = ["addColumn", $from, $to];
|
|
}
|
|
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;
|
|
}
|
|
};
|
|
|
|
|
|
$t = new ORM();
|
|
$t->table("argist");
|
|
$t->whereIn("name",[1,2,3]);
|
|
$t->orWhere([
|
|
"name"=>"value"
|
|
]);
|
|
|
|
dd($t); |