From 6edc85b7314bc04528c5d752fdffacce9da423a6 Mon Sep 17 00:00:00 2001 From: Malloc of Kuzkycyziklistan Date: Mon, 27 Feb 2017 15:57:15 -0600 Subject: [PATCH] small stof e --- AroMVC/Database.php | 51 ++++++++++++++++++++++++++++++++++++++----- AroMVC/Insertable.php | 6 +++++ AroMVC/Queryable.php | 3 ++- AroMVC/Selectable.php | 4 ++-- index.php | 9 ++++++-- 5 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 AroMVC/Insertable.php diff --git a/AroMVC/Database.php b/AroMVC/Database.php index a7ea92b..1503ba4 100644 --- a/AroMVC/Database.php +++ b/AroMVC/Database.php @@ -2,12 +2,13 @@ namespace AroMVC\Core; use AroMVC\Core\Configuration as conf; -define("AMVC_DB_FETCH_EMPTY", 0); +define("AMVC_DB_FETCH_VOID", 0); define("AMVC_DB_FETCH_SCALAR", 1); define("AMVC_DB_FETCH_ROWS", 2); class Database { - private static $dbConn = null; + /** @var \PDO */ + private static $dbConn; private static $queries = []; public static function initialize() { @@ -22,15 +23,55 @@ class Database { return new Selectable($columns); } - public static function rawQuery(string $query, $params = null, int $type = AMVC_DB_FETCH_ROWS) { + public static function rawQuery(string $query, $params = null, int $type = AMVC_DB_FETCH_ROWS): array { + $stmt = self::getStatement($query); + foreach($params as $name => $param) + $stmt->bindParam(":$name", $param); + $stmt->execute(); + + switch($type) { + case AMVC_DB_FETCH_ROWS: + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + break; + case AMVC_DB_FETCH_SCALAR: + return [$stmt->fetchColumn(0)]; + break; + case AMVC_DB_FETCH_VOID: + default: + return null; + break; + } + } + + public static function rawVoidQuery(string $query, $params = null): void { + self::rawQuery($query, $params, AMVC_DB_FETCH_VOID); + } + + public static function rawScalarQuery(string $query, $params = null): string { + return self::rawQuery($query, $params, AMVC_DB_FETCH_SCALAR)[0]; + } + + public static function rawRowQuery(string $query, $params = null): array { + return self::rawQuery($query, $params); + } + + protected static function getStatement(string $query): \PDOStatement { $hash = self::hashQuery($query); if(key_exists($hash, self::$queries)) - // TODO finish function + return self::$queries[$hash]; + else + return (self::$queries[$hash] = self::$dbConn->prepare(trim($query))); } protected static function hashQuery(string $query): string { + $query = trim($query); while(($pos = strpos($query, ":")) !== false) { - // TODO finish hasher + for($end = $pos+1;;$end++) { + if(!ctype_alnum($query[$end])) + break; + } + + $query = substr($query, 0, $pos) . "?" . substr($query, $end); } return md5($query); diff --git a/AroMVC/Insertable.php b/AroMVC/Insertable.php new file mode 100644 index 0000000..20b3da2 --- /dev/null +++ b/AroMVC/Insertable.php @@ -0,0 +1,6 @@ +results = db::rawRowQuery($this->query, $this->params); } /*protected function allowConditionals(bool $allow) { diff --git a/AroMVC/Selectable.php b/AroMVC/Selectable.php index 30dc4cf..ac820c8 100644 --- a/AroMVC/Selectable.php +++ b/AroMVC/Selectable.php @@ -27,8 +27,8 @@ class Selectable extends Queryable { ]; public function __construct($selection) { - if(is_array($selection)) - $selection = implode(",", $selection); + if(!is_array($selection)) + $selection = [$selection]; $this->components[AMVC_QRY_SEL_COLS] = $selection; } diff --git a/index.php b/index.php index 4740a14..376e8d7 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ from("Companies") ->where("`name` = ?") ->or("`id` = ?") ->execute() - ->asModels(new Company); + ->asModels(new Company);*/ + +