small stof

e
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-02-27 15:57:15 -06:00
parent c2a7937329
commit 6edc85b731
5 changed files with 63 additions and 10 deletions

View file

@ -2,12 +2,13 @@
namespace AroMVC\Core; namespace AroMVC\Core;
use AroMVC\Core\Configuration as conf; 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_SCALAR", 1);
define("AMVC_DB_FETCH_ROWS", 2); define("AMVC_DB_FETCH_ROWS", 2);
class Database { class Database {
private static $dbConn = null; /** @var \PDO */
private static $dbConn;
private static $queries = []; private static $queries = [];
public static function initialize() { public static function initialize() {
@ -22,15 +23,55 @@ class Database {
return new Selectable($columns); 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); $hash = self::hashQuery($query);
if(key_exists($hash, self::$queries)) 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 { protected static function hashQuery(string $query): string {
$query = trim($query);
while(($pos = strpos($query, ":")) !== false) { 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); return md5($query);

6
AroMVC/Insertable.php Normal file
View file

@ -0,0 +1,6 @@
<?php
namespace AroMVC\Core;
class Insertable extends Queryable {
}

View file

@ -1,5 +1,6 @@
<?php <?php
namespace AroMVC\Core; namespace AroMVC\Core;
use AroMVC\Core\Database as db;
abstract class Queryable { abstract class Queryable {
protected $query = ""; protected $query = "";
@ -14,7 +15,7 @@ abstract class Queryable {
} }
public function execute() { public function execute() {
$this->results = db::rawRowQuery($this->query, $this->params);
} }
/*protected function allowConditionals(bool $allow) { /*protected function allowConditionals(bool $allow) {

View file

@ -27,8 +27,8 @@ class Selectable extends Queryable {
]; ];
public function __construct($selection) { public function __construct($selection) {
if(is_array($selection)) if(!is_array($selection))
$selection = implode(",", $selection); $selection = [$selection];
$this->components[AMVC_QRY_SEL_COLS] = $selection; $this->components[AMVC_QRY_SEL_COLS] = $selection;
} }

View file

@ -1,5 +1,6 @@
<?php <?php
namespace AroMVC; namespace AroMVC;
use AroMVC\Core\Database;
use AroMVC\Core\Selectable; use AroMVC\Core\Selectable;
use AroMVC\Models\Company; use AroMVC\Models\Company;
@ -21,10 +22,14 @@ spl_autoload_register(function($class) {
} }
}); });
$tmp = new Selectable("*"); Database::initialize();
/*$tmp = new Selectable("*");
$tmp->from("Companies") $tmp->from("Companies")
->where("`name` = ?") ->where("`name` = ?")
->or("`id` = ?") ->or("`id` = ?")
->execute() ->execute()
->asModels(new Company); ->asModels(new Company);*/