From c2a79373299f9c2047b450bffbf0fffc492b2ece Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Fri, 24 Feb 2017 16:01:32 -0600 Subject: [PATCH] bug touch don't --- AroMVC/ConfigSection.php | 27 ++++++++++++++ AroMVC/Configuration.php | 29 +++++++++++++++ AroMVC/Database.php | 33 +++++++++++++++-- AroMVC/{AroModel.php => Model.php} | 6 +++- AroMVC/Queryable.php | 51 ++++++++++++++------------ AroMVC/Selectable.php | 57 ++++++++++++++++-------------- Models/Company.php | 18 ++++++++-- Models/State.php | 20 +++++++++-- auto.php | 18 ---------- conf.ini | 4 +++ index.php | 21 +++++++++-- 11 files changed, 205 insertions(+), 79 deletions(-) create mode 100644 AroMVC/ConfigSection.php create mode 100644 AroMVC/Configuration.php rename AroMVC/{AroModel.php => Model.php} (94%) delete mode 100644 auto.php create mode 100644 conf.ini diff --git a/AroMVC/ConfigSection.php b/AroMVC/ConfigSection.php new file mode 100644 index 0000000..13af851 --- /dev/null +++ b/AroMVC/ConfigSection.php @@ -0,0 +1,27 @@ +section = $section; + $this->name = $name; + } + + public function hasKey(string $key): bool { + return key_exists($key, $this->section); + } + + public function value(string $key): string { + if($this->hasKey($key)) + return $this->section[$key]; + else + throw new \Exception("Section '$this->section' in configuration does not contain key '$key'"); + } + + public function getSectionName(): string { + return $this->name; + } +} \ No newline at end of file diff --git a/AroMVC/Configuration.php b/AroMVC/Configuration.php new file mode 100644 index 0000000..3f92843 --- /dev/null +++ b/AroMVC/Configuration.php @@ -0,0 +1,29 @@ +value(AMVC_CNF_DB_DSN), + conf::section(AMVC_CNF_DB)->value(AMVC_CNF_DB_USER), + conf::section(AMVC_CNF_DB)->value(AMVC_CNF_DB_PASS) + ); + } + public static function select($columns): Selectable { + return new Selectable($columns); + } + + public static function rawQuery(string $query, $params = null, int $type = AMVC_DB_FETCH_ROWS) { + $hash = self::hashQuery($query); + if(key_exists($hash, self::$queries)) + // TODO finish function + } + + protected static function hashQuery(string $query): string { + while(($pos = strpos($query, ":")) !== false) { + // TODO finish hasher + } + + return md5($query); + } } \ No newline at end of file diff --git a/AroMVC/AroModel.php b/AroMVC/Model.php similarity index 94% rename from AroMVC/AroModel.php rename to AroMVC/Model.php index 937b50a..df1046d 100644 --- a/AroMVC/AroModel.php +++ b/AroMVC/Model.php @@ -1,7 +1,7 @@ rawData[$name]; + } + public function __get(string $name) { $name = strtolower($name); diff --git a/AroMVC/Queryable.php b/AroMVC/Queryable.php index 70ad3be..9402eb9 100644 --- a/AroMVC/Queryable.php +++ b/AroMVC/Queryable.php @@ -1,38 +1,27 @@ allowConditionals) - throw new \Exception("Query type does not allow AND/OR subclauses"); - - array_push($this->query, "AND", $condition); + public function params(array $params) { + $this->params = array_merge($this->params, $params); + return $this; } - public function or(string $condition) { - if(!$this->allowConditionals) - throw new \Exception("Query type does not allow AND/OR subclauses"); + public function execute() { - array_push($this->query, "OR", $condition); } - public function execute(array $params) { - // TODO execution logic - var_dump($this->query); + /*protected function allowConditionals(bool $allow) { + if($allow) + $this->setFlag(AMVC_QRY_CNDLS); + else + $this->clearFlag(AMVC_QRY_CNDLS); } protected function setFlag(int $flag) { @@ -53,5 +42,21 @@ abstract class Queryable { protected function checkForOrPast(int $flag): bool { return $this->checkFlag($flag) || $this->checkPast($flag); + }*/ + + protected function isLocked() { + return $this->locks != 0; + } + + protected function checkLock(int $lock) { + return ($this->locks & $lock) != 0; + } + + protected function lock(int $lock) { + $this->locks |= $lock; + } + + protected function unlock(int $lock) { + $this->locks &= ~$lock; } } \ No newline at end of file diff --git a/AroMVC/Selectable.php b/AroMVC/Selectable.php index 674fff3..30dc4cf 100644 --- a/AroMVC/Selectable.php +++ b/AroMVC/Selectable.php @@ -1,17 +1,41 @@ [], + AMVC_QRY_SEL_FROM => null, + AMVC_QRY_SEL_JOINS => [], + AMVC_QRY_SEL_WHERE => [], + AMVC_QRY_SEL_GROUP => [], + AMVC_QRY_SEL_HAVING => [], + AMVC_QRY_SEL_ORDER => [], + AMVC_QRY_SEL_LIMIT => null + ]; + public function __construct($selection) { if(is_array($selection)) $selection = implode(",", $selection); - $this->query = ["SELECT", $selection]; + $this->components[AMVC_QRY_SEL_COLS] = $selection; } - public function execute(array $params): Selectable { + public function execute(): Selectable { if($this->checkFlag(AMVC_QRY_SEL_NEEDON)) - throw new \Exception("JOIN clause declared in query with no matching ON clause"); + throw new \Exception("JOIN clause declared in query with no matching ON or USING clause"); if($this->checkFlag(AMVC_QRY_SEL_GROUP) && !$this->checkFlag(AMVC_QRY_SEL_ORDER)) { if($this->checkFlag(AMVC_QRY_SEL_HAVING)) { @@ -23,26 +47,19 @@ class Selectable extends Queryable { } } - parent::execute($params); + parent::execute(); return $this; } - protected function checkFrom() { - if(!$this->checkFlag(AMVC_QRY_SEL_FROM)) - throw new \Exception("FROM clause must come first in a SELECT query"); - } - public function from(string $where): Selectable { - if($this->checkFlag(AMVC_QRY_SEL_FROM)) - throw new \Exception("Cannot declare second FROM clause on a SELECT query"); + if($this->components[AMVC_QRY_SEL_FROM] != null) + throw new \Exception("Cannot redefine FROM clause after first definition"); - $this->setFlag(AMVC_QRY_SEL_FROM); array_push($this->query, "FROM", $where); return $this; } public function join(string $type, string $table): Selectable { - $this->checkFrom(); if($this->checkPast(AMVC_QRY_SEL_JOINS)) throw new \Exception("Invalid JOIN clause, must proceed FROM clause"); @@ -83,20 +100,6 @@ class Selectable extends Queryable { return $this; } - public function and(string $condition): Selectable { - if(!$this->checkFlag(AMVC_QRY_SEL_FROM)) - throw new \Exception("Cannot use AND subclause until FROM clause has been declared in SELECT query"); - - parent::and($condition); - return $this; - } - - public function or(string $condition): Selectable { - $this->checkFrom(); - parent::or($condition); - return $this; - } - public function groupBy($columns): Selectable { $this->checkFrom(); if($this->checkForOrPast(AMVC_QRY_SEL_GROUP)) diff --git a/Models/Company.php b/Models/Company.php index 9b84bac..f997004 100644 --- a/Models/Company.php +++ b/Models/Company.php @@ -1,7 +1,19 @@ addHook("states", function() { + return db::select("*") + ->from("States") + ->where("`cid` = ?") + ->params([$this->get("id")]); + }); + } } \ No newline at end of file diff --git a/Models/State.php b/Models/State.php index d238fa4..ebebfe3 100644 --- a/Models/State.php +++ b/Models/State.php @@ -1,7 +1,21 @@ addHook("company", function() { + + }); + + $this->addAssociation("companyId", "cid"); + } } \ No newline at end of file diff --git a/auto.php b/auto.php deleted file mode 100644 index 9ae0305..0000000 --- a/auto.php +++ /dev/null @@ -1,18 +0,0 @@ -from("Companies") ->where("`name` = ?") ->or("`id` = ?") - ->execute(["dekko", 12]) + ->execute() ->asModels(new Company);