Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- protected $pdo;
- protected function __construct() {
- $info = config('db');
- foreach (array('dsn', 'user', 'password', 'args') as $key) {
- if (!isset($info[$key])) {
- $info[$key] = null;
- }
- }
- try {
- $this->pdo = new PDO($info['dsn'], $info['user'], $info['password'], $info['args']);
- }
- catch (PDOException $e) {
- die('Connection failed: ' . $e->getMessage());
- }
- $this->query('SET NAMES utf8');
- }
- public static function i() {
- static $db;
- if (!$db) {
- $db = new self;
- }
- return $db;
- }
- public function query($sql, $bindings = null) {
- print dump($sql);
- $stmt = $this->pdo->prepare($sql);
- $stmt->execute($bindings);
- return $stmt;
- }
- public function insert($table, $data) {
- $sql = "INSERT INTO $table";
- $values = array_values($data);
- if ($values != $data) {
- $fields = array_keys($data);
- $sql .= ' (' . implode(',', $fields) . ')';
- }
- $sql .= ' VALUES (' . rtrim(str_repeat('?,', count($data)), ',') . ')';
- $this->query($sql, $values);
- $id = $this->pdo->lastInsertId();
- return $id;
- }
- public function select($from, $what = '*', $criteria = 0, $limit = 0, $offset = 0, $order = 0, $bindings = null) {
- $sql = "SELECT $what FROM $from";
- $sql .= $this->where($criteria, $bindings);
- if ($order) {
- $sql .= " ORDER BY $order";
- }
- if ($limit) {
- $sql .= " LIMIT $offset,$limit";
- }
- $stmt = $this->query($sql, $bindings);
- return $stmt;
- }
- public function selectRow($from, $what = '*', $criteria = 0, $order = 0, $bindings = null) {
- return $this->select($from, $what, $criteria, 0, 0, $order, $bindings);
- }
- public function get($from, $what = '*', $criteria = 0, $limit = 0, $offset = 0, $order = 0, $bindings = null) {
- $stmt = $this->select($from, $what, $criteria, $limit, $offset, $order, $bindings);
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- public function getRow($from, $what = '*', $criteria = 0, $order = 0, $bindings = null) {
- $stmt = $this->selectRow($from, $what, $criteria, $order, $bindings);
- return $stmt->fetch(PDO::FETCH_ASSOC);
- }
- public function getValue($from, $field, $criteria = 0, $order = 0, $bindings = null) {
- $stmt = $this->selectRow($from, $field, $criteria, $order, $bindings);
- return $stmt->fetchColumn();
- }
- public function count($table, $criteria = 0, $bindings = null) {
- return (int) $this->getValue($table, 'COUNT(*)', $criteria, 0, $bindings);
- }
- // можно использовать только именованные плейсхолдеры
- public function update($what, $data, $criteria = 0, $bindings = array()) {
- $sql = "UPDATE $what SET ";
- $parts = array();
- foreach ($data as $k => $v) {
- $bindings[":$k"] = $v;
- $parts[] = "$k=:$k";
- }
- $sql .= implode(',', $parts) . $this->where($criteria, $bindings);
- $stmt = $this->query($sql, $bindings);
- return $stmt->rowCount();
- }
- public function delete($from, $criteria, $bindings = null) {
- $sql = "DELETE FROM $from" . $this->where($criteria, $bindings);
- $stmt = $this->query($sql, $bindings);
- return $stmt->rowCount();
- }
- protected function where($criteria, &$bindings) {
- if (is_array($criteria)) {
- // array('foo' => $foo, 'bar' => $bar);
- // WHERE foo=:foo AND bar=:bar
- $bindings = (array) $bindings;
- $parts = array();
- foreach ($criteria as $k => $v) {
- $bindings[":$k"] = $v;
- $parts[] = "$k=:$k";
- }
- $criteria = implode(' AND ', $parts);
- }
- return $criteria ? " WHERE $criteria" : "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment