Stage 3-2
This commit is contained in:
parent
f1a2af9c9d
commit
795074f8fc
|
@ -5,6 +5,7 @@
|
|||
include_once "Helpers.php";
|
||||
include_once "DatabaseConnection.php";
|
||||
include_once "DatabaseORM.php";
|
||||
include_once "DatabaseMySQLEngine.php";
|
||||
|
||||
define("request", new Request(), false);
|
||||
define("response", new Response(), false);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
include_once "Helpers.php";
|
||||
class Connection
|
||||
class DatabaseConnection
|
||||
{
|
||||
public $client = "mysql";
|
||||
public $user = "root";
|
||||
|
@ -11,7 +11,7 @@
|
|||
public $charset = "utf8mb4";
|
||||
public $file = "";
|
||||
public $connectionString = null;
|
||||
public $db = null;
|
||||
public static $db = null;
|
||||
public $ready = false;
|
||||
public function __construct(
|
||||
$client,
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
include_once "Helpers.php";
|
||||
include_once "DatabaseORM.php";
|
||||
include_once "DatabaseConnection.php";
|
||||
|
||||
class MySQLEngine extends DatabaseConnection
|
||||
{
|
||||
public function query(ORM $orm)
|
||||
{
|
||||
|
||||
}
|
||||
public function migrate(ORMMigrate $orm)
|
||||
{
|
||||
$sql = [];
|
||||
$sql[] = "CREATE TABLE IF NOT EXISTS ";
|
||||
$sql[] = $orm->table;
|
||||
$sql[] = " ( ";
|
||||
$columns = [];
|
||||
foreach ($orm->columns as $name => $value) {
|
||||
$type = $value[0];
|
||||
$propety = $value[1] ?? "";
|
||||
$columns[] = "$name $type $propety";
|
||||
}
|
||||
$sql[] = implode(", ",$columns);
|
||||
$sql[] = ") charset=" . $orm->charset;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ORM::$defaultConnection = new MySQLEngine("mysql","root","");
|
||||
|
||||
|
||||
ORM::migrate(function($table){
|
||||
$table->id("int","auto_increment primary key");
|
||||
$table->name("text");
|
||||
$table->value("text");
|
||||
});
|
|
@ -23,15 +23,37 @@
|
|||
public $groupBy = [];
|
||||
public $having = [];
|
||||
public $orderby = [];
|
||||
public $limit = [];
|
||||
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) {
|
||||
|
@ -74,24 +96,43 @@
|
|||
}
|
||||
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(
|
||||
|
@ -278,12 +319,32 @@
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
$t = new ORM();
|
||||
$t->table("argist");
|
||||
$t->whereIn("name",[1,2,3]);
|
||||
$t->orWhere([
|
||||
"name"=>"value"
|
||||
]);
|
||||
|
||||
dd($t);
|
||||
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)."";
|
||||
}
|
||||
};
|
|
@ -22,11 +22,15 @@
|
|||
return $this->{$formarlyA}(...$arguments);
|
||||
}else if(method_exists(__CLASS__, $formarlyB))
|
||||
{
|
||||
return (new static)->{$formarlyB}(...$arguments);
|
||||
return $this->{$formarlyB}(...$arguments);
|
||||
}
|
||||
else if(method_exists(__CLASS__, "call$name"))
|
||||
{
|
||||
return (new static)->{"call$name"}($name, ...$arguments);
|
||||
return $this->{"call$name"}($name, ...$arguments);
|
||||
}
|
||||
else if(method_exists(__CLASS__, "callable"))
|
||||
{
|
||||
return $this->callable($name, ...$arguments);
|
||||
}
|
||||
}
|
||||
public function __get($name)
|
||||
|
@ -80,14 +84,14 @@
|
|||
{
|
||||
$this->{"set" . $formarly . "Attribute"}();
|
||||
}
|
||||
else if(method_exists($this, "removeAttribute"))
|
||||
{
|
||||
return $this->removeAttribute($name);
|
||||
}
|
||||
else if(method_exists($this, "setAttribute"))
|
||||
{
|
||||
return $this->setAttribute($name, null);
|
||||
}
|
||||
else if(method_exists($this, "remoteAttribute"))
|
||||
{
|
||||
return $this->remoteAttribute($name);
|
||||
}
|
||||
else if(property_exists($this, "_attributes") && isset($this->_attributes[$name]))
|
||||
{
|
||||
unset($this->_attributes[$name]);
|
||||
|
@ -99,17 +103,6 @@
|
|||
{
|
||||
return $this->reflectionCall(...$args);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(property_exists(__CLASS__, "reflectionCallException"))
|
||||
{
|
||||
if(!(new static)->reflectionCallException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Exception("\"".__CLASS__."()\" not callable");
|
||||
}
|
||||
}
|
||||
public function __toString()
|
||||
{
|
||||
|
@ -117,17 +110,6 @@
|
|||
{
|
||||
return $this->toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(property_exists(__CLASS__, "reflectionCallException"))
|
||||
{
|
||||
if(!(new static)->reflectionCallException)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
throw new Exception("\"".__CLASS__."()\" not callable");
|
||||
}
|
||||
}
|
||||
public function __serialize()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue