Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class dal {
- public $pdo;
- protected $queryCache = [];
- const FETCH_ALL = 1;
- const FETCH_ROW = 2;
- const FETCH_COLUMN = 4;
- // const D = 8;
- // const E = 16;
- // const F = 32;
- // const G = 64;
- // const H = 128;
- // const I = 256;
- // const J = 512;
- // const K = 1024;
- // const L = 2048;
- const FETCH_NUM = 4096;
- const FETCH_ASSOC = 8192;
- // const O = 16384;
- // const P = 32768;
- // const Q = 65536;
- // const R = 131072;
- // const S = 262144;
- // const T = 524288;
- // const U = 1048576;
- // const V = 2097152;
- // const W = 4194304;
- // const X = 8388608;
- // const Y = 16777216;
- // const Z = 33554432;
- const FETCH_BOTH = 12288; // 4096 | 8192
- public function __construct($dsn, $usr=null, $pswd=null, $args=null,
- $charset='utf8')
- {
- try {
- $this->pdo = new PDO($dsn, $usr, $pswd, $args);
- }
- catch (PDOException $e) {
- die('Connection failed: ' . $e->getMessage());
- }
- $this->query("SET NAMES $charset");
- }
- public function query($query, array $params=null)
- {
- echo dump($query);
- $h = md5($query);
- // кешируем подготовленные выражения
- if (isset($this->queryCache[$h])) {
- echo 'Используем подготовленное выражение из кэша.<br>';
- $s = $this->queryCache[$h];
- }
- else {
- $s = $this->pdo->prepare($query);
- $this->queryCache[$h] = $s;
- }
- $s->execute($params);
- preg_match('/^\s*(\w+)/', $query, $m);
- $action = strtolower($m[1]);
- // var_dump($action);
- if ($action == 'insert') {
- return $this->lastInsertId();
- }
- if ($action == 'update' or $action == 'delete') {
- return $s->rowCount();
- }
- return $s;
- }
- public function get($query, array $params=null, $flags=self::FETCH_ALL)
- {
- $r = $this->query($query, $params);
- if ($flags & self::FETCH_ROW) {
- return $r->fetch();
- }
- if ($flags & self::FETCH_COLUMN) {
- return $r->fetchColumn();
- }
- return $r->fetchAll();
- }
- public function row($query, array $params=null, $add_flags=0)
- {
- return $this->get($query, $params, self::FETCH_ROW | $add_flagsPar);
- }
- public function column($query, array $params=null)
- {
- return $this->get($query, $params, self::FETCH_COLUMN);
- }
- public function count($table, $where=1, array $params=null)
- {
- return $this->column('SELECT COUNT(*) FROM ' .
- $this->quoteid($table) . " WHERE $where",
- $params);
- }
- public function insert($table, array $data)
- {
- $q = 'INSERT INTO ' . $this->quoteid($table);
- $fields = array_keys($data);
- $q .= ' (' .
- implode( ',', array_map([$this, 'quoteid'], $fields) ) .
- ') VALUES (';
- for ($i = 0, $c = count($fields); $i < $c; ++$i) {
- $q .= ($i ? ',' : '') . ':' . $fields[$i];
- }
- $q .= ')';
- return $this->query($q, $data);
- }
- public function update($table, array $data, $where=1, array $params=null)
- {
- is_null($params) and $params = [];
- $is_list_params = array_values($params) == $params;
- // var_dump($is_list_params);
- $params = array_merge($is_list_params ? array_values($data) : $data,
- $params);
- $s = '';
- foreach ($data as $k => $v) {
- $s .= (strlen($s) ? ',' : '') . $this->quoteid($k) . '=' .
- ($is_list_params ? '?' : ":$k");
- }
- // echo dump($params);
- return $this->query('UPDATE ' . $this->quoteid($table) .
- " SET $s WHERE $where", $params);
- }
- public function delete($table, $where=1, array $params=null) {
- return $this->query('DELETE FROM ' . $this->quoteid($table) .
- " WHERE $where", $params);
- }
- public function describe($table)
- {
- return $this->get( 'DESCRIBE ' . $this->quoteid($table) );
- }
- public function quoteid($s)
- {
- return '`' . str_replace('`', '``', $s) . '`';
- }
- public function lastInsertId()
- {
- return $this->pdo->lastInsertId();
- }
- }
- /* ---------------------------- 78 characters ----------------------------- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement