small stof
e
This commit is contained in:
parent
c2a7937329
commit
6edc85b731
5 changed files with 63 additions and 10 deletions
|
@ -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);
|
||||
|
|
6
AroMVC/Insertable.php
Normal file
6
AroMVC/Insertable.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC\Core;
|
||||
|
||||
class Insertable extends Queryable {
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC\Core;
|
||||
use AroMVC\Core\Database as db;
|
||||
|
||||
abstract class Queryable {
|
||||
protected $query = "";
|
||||
|
@ -14,7 +15,7 @@ abstract class Queryable {
|
|||
}
|
||||
|
||||
public function execute() {
|
||||
|
||||
$this->results = db::rawRowQuery($this->query, $this->params);
|
||||
}
|
||||
|
||||
/*protected function allowConditionals(bool $allow) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
namespace AroMVC;
|
||||
use AroMVC\Core\Database;
|
||||
use AroMVC\Core\Selectable;
|
||||
use AroMVC\Models\Company;
|
||||
|
||||
|
@ -21,10 +22,14 @@ spl_autoload_register(function($class) {
|
|||
}
|
||||
});
|
||||
|
||||
$tmp = new Selectable("*");
|
||||
Database::initialize();
|
||||
|
||||
/*$tmp = new Selectable("*");
|
||||
|
||||
$tmp->from("Companies")
|
||||
->where("`name` = ?")
|
||||
->or("`id` = ?")
|
||||
->execute()
|
||||
->asModels(new Company);
|
||||
->asModels(new Company);*/
|
||||
|
||||
|
||||
|
|
Reference in a new issue