Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DataBase {
- public $pdo;
- protected $prepared_statements = [];
- public function __construct($dsn, $usr=null, $pwd=null, $args=null) {
- try {
- $pdo = new PDO($dsn, $usr, $pwd, $args);
- }
- catch (PDOException $e) {
- throw new RuntimeException('Connection failed: ' . $e->getMessage());
- }
- $this->pdo = $pdo;
- }
- public function quote_name($name) {
- return '`' . str_replace('`', '``', $name) . '`';
- }
- public function execute($query, array $bindings=[]) {
- $t = $this;
- $h = md5($query);
- if (isset($t->prepared_statements[$h])) {
- $stm = $t->prepared_statements[$h];
- }
- else {
- $stm = $t->pdo->prepare($query);
- $t->prepared_statements[$h] = $stm;
- }
- $stm->execute($bindings);
- preg_match('/^\s*(\w+)/', $query, $m);
- $action = strtolower($m[1]);
- if ($action == 'update' or $action == 'delete') {
- return $stm->rowCount();
- }
- if ($action == 'insert') {
- return $t->last_insert_id();
- }
- return $stm;
- }
- public function row($query, array $bindings=[]) {
- $stm = $this->execute($query, $bindings);
- return $stm->fetch(PDO::FETCH_ASSOC);
- }
- public function rows($query, array $bindings=[]) {
- $stm = $this->execute($query, $bindings);
- return $stm->fetchAll(PDO::FETCH_ASSOC);
- }
- public function column($query, array $bindings=[]) {
- $stm = $this->execute($query, $bindings);
- return $stm->fetchColumn();
- }
- public function find($table, $id, $field='id') {
- $t = $this;
- $table = $t->quote_name($table);
- $field = $t->quote_name($field);
- return $t->row("SELECT * FROM $table WHERE $field=? LIMIT 1", [$id]);
- }
- public function count($table, $where=1, array $bindings=[]) {
- $t = $this;
- $table = $t->quote_name($table);
- list($where, $bindings) = $t->get_where($where, $bindings);
- $q = "SELECT COUNT(*) FROM $table WHERE $where";
- return (int) $t->column($q, $bindings);
- }
- public function contains($table, $field, $value) {
- return $this->count($table, [$field => $value]) > 0;
- }
- public function add($table, array $data) {
- $t = $this;
- $table = $t->quote_name($table);
- $q = "INSERT INTO $table ";
- if (is_list($data)) {
- $q .= 'VALUES (' . implode(',', array_fill(0, count($data), '?')) . ')';
- }
- else {
- $columns = [];
- $values = [];
- $keys = array_keys($data);
- foreach ($keys as $v) {
- $columns[] = $t->quote_name($v);
- $values[] = ":$v";
- }
- $columns = implode(',', $columns);
- $values = implode(',', $values);
- $q .= "($columns) VALUES ($values)";
- }
- return $t->execute($q, $data);
- }
- public function update($table, array $data, $where=1, array $bindings=[]) {
- $t = $this;
- $set = [];
- list($where, $bindings) = $t->get_where($where, $bindings);
- $is_list = is_list($bindings);
- $bindings = array_merge($is_list ? array_values($data) : $data, $bindings);
- $keys = array_keys($data);
- foreach ($keys as $v) {
- // подстраитваемся под стиль плейсхолдеров
- $set[] = $t->quote_name($v) . '=' . ($is_list ? '?' : ":$v");
- }
- $set = implode(',', $set);
- $table = $t->quote_name($table);
- return $t->execute("UPDATE $table SET $set WHERE $where", $bindings);
- }
- public function delete($table, $where=1, array $bindings=[]) {
- $t = $this;
- $table = $t->quote_name($table);
- list($where, $bindings) = $t->get_where($where, $bindings);
- return $t->execute("DELETE FROM $table WHERE $where", $bindings);
- }
- protected function get_where($criteria, $bindings) {
- if (is_array($criteria)) {
- $bindings = [];
- $parts = [];
- foreach ($criteria as $k => $v) {
- $parts[] = $this->quote_name($k) . '=?';
- $bindings[] = $v;
- }
- $criteria = implode($parts, ' AND ');
- }
- return [$criteria, $bindings];
- }
- public function query($query) {
- return $this->pdo->query($query);
- }
- public function exec($query) {
- return $this->pdo->exec($query);
- }
- public function quote($s) {
- return $this->pdo->quote($s);
- }
- public function commit() {
- return $this->pdo->commit();
- }
- public function rollback() {
- return $this->pdo->rollback();
- }
- public function last_insert_id() {
- return $this->pdo->lastInsertId();
- }
- public function clear_cache() {
- $this->prepared_statements = [];
- }
- }
- if (!function_exists('is_list')) {
- function is_list($arr) {
- return $arr === array_values($arr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement