-
-
Save samayo/6f8a9d924137c45259e7 to your computer and use it in GitHub Desktop.
fastcrud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HTTP request object | |
* | |
* PHP version 5.4 | |
* | |
* @category PitchBlade | |
* @package Network | |
* @subpackage Http | |
* @author Pieter Hordijk <info@pieterhordijk.com> | |
* @copyright Copyright (c) 2013 Pieter Hordijk | |
* @license http://www.opensource.org/licenses/mit-license.html MIT License | |
* @version 1.0.0 | |
*/ | |
namespace FastPress\Database; | |
/** | |
* HTTP request object | |
* | |
* @category PitchBlade | |
* @package Network | |
* @subpackage Http | |
* @author Pieter Hordijk <info@pieterhordijk.com> | |
*/ | |
class Fastcrud extends \PDO | |
{ | |
private $table = null; | |
private $database = null; | |
protected $tables = []; | |
protected $columns = []; | |
protected $storables = []; | |
private $lastInsertId = false; | |
private $defaultPdoAttr = [ | |
\PDO::ATTR_EMULATE_PREPARES => false, | |
\PDO::ATTR_ERRMODE=> \PDO::ERRMODE_EXCEPTION, | |
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC | |
]; | |
public function __construct($dsn, $username, $password, array $driverOptions = []) | |
{ | |
if(!$driverOptions){ | |
$driverOptions = $this->defaultPdoAttr; | |
} | |
parent::__construct($dsn, $username, $password, $driverOptions); | |
$this->getDatabase(); | |
} | |
public function getDatabase(){ | |
return $this->database = parent::query('select database()')->fetchColumn(); | |
} | |
public function getTable(){ | |
return $this->table; | |
} | |
public function getTables() | |
{ | |
$stmt = parent::query("SHOW tables FROM " . $this->database)->fetchAll(); | |
foreach ($stmt as $table) { | |
$this->tables[] = $table['Tables_in_'.$this->database]; | |
} | |
return $this->tables; | |
} | |
public function getColumns() | |
{ | |
$stmt = parent::query(" SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$this->table' ")->fetchAll(); | |
foreach ($stmt as $value) { | |
$this->columns[] = $value['COLUMN_NAME']; | |
} | |
return $this->columns; | |
} | |
function __set($column, $value) | |
{ | |
if (!in_array($column, $this->columns)){ | |
throw new \LogicException("column name \"$column\" does not exist inside table: \"$this->table\" "); | |
} | |
$this->storables[$column] = $value; | |
} | |
function __get($column) | |
{ | |
if(!in_array($column, $this->columns)){ | |
throw new \LogicException("column name {$column} does not exist inside table: \"$this->table\" "); | |
} | |
return $this->storables[$column]; | |
} | |
public function table($value) | |
{ | |
$this->getTables(); | |
if(!in_array($value, $this->tables)){ | |
throw new \Exception("Table \"$value\" not found inside database: \"$this->database\" "); | |
} | |
$this->table = $value; | |
$this->getColumns(); | |
} | |
public function __call($func, $arg) | |
{ | |
$arg = reset($arg); | |
$params = rtrim(str_repeat('?,', count($this->storables)), ','); | |
switch ($func) { | |
case 'save': | |
$columns = array_keys($this->storables); | |
$textColumns = rtrim(implode(', ', $columns), ','); | |
$values = array_values($this->storables); | |
$stmt = parent::prepare("INSERT INTO $this->table ($textColumns) VALUES ($params)"); | |
$stmt->execute($values); | |
return (bool) $this->lastInsertId = parent::lastInsertId(); | |
break; | |
case 'delete': | |
$column = key($arg); $value = reset($arg); | |
$stmt = parent::prepare("DELETE FROM $this->table WHERE {$column[0]} = ? "); | |
$stmt->execute($value); | |
return $stmt->rowCount(); | |
break; | |
case 'read': | |
if(count($arg) > 1) | |
{ | |
$columns = reset($arg); | |
$identifier = array_keys($arg); | |
$value = end($arg); | |
$stmt = parent::prepare("SELECT {$columns} FROM $this->table WHERE {$identifier[1]} = ? "); | |
$stmt->execute([$value]); | |
}else | |
{ | |
$stmt = parent::query("SELECT {$arg[0]} FROM $this->table "); | |
} | |
$result = $stmt->fetchAll(); | |
foreach ($result as $key => $value) { | |
if(in_array($key, $this->columns)){ | |
$this->storables[$key] = $value; | |
} | |
} | |
case 'update': | |
$identifier = key($arg); | |
$col = reset($arg); | |
$rows = implode(' = ?, ', array_keys($this->storables)) . ' = ?'; | |
echo "UPDATE $this->table SET $rows WHERE $identifier"; | |
$stmt = parent::prepare("UPDATE $this->table SET $rows WHERE $identifier = ?"); | |
array_push($this->storables, $col); | |
$stmt->execute(array_values($this->storables)); | |
break; | |
case 'lastInsertId': | |
return $this->lastInsertId; | |
break; | |
default: | |
# code... | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment