View difference between Paste ID: 9tW24zDx and XMXS2a06
SHOW: | | - or go back to the newest paste.
1
/**
2
 * Обертка над БД
3
 *
4
 * @author Fragster
5
 */
6
class DB extends PDO {
7
8
    /**
9
     *
10
     * @var DB
11
     */
12
    private static $singleton;
13
    private $engine;
14
    private $host;
15
    private $database;
16
    private $user;
17
    private $pass;
18
19
    public function __construct() {
20
        $this->engine = 'mysql';
21
        $this->host = Config::$db_host;
22
        $this->database = Config::$db_database;
23
        $this->user = Config::$db_user;
24
        $this->pass = Config::$db_password;
25
        $dns = $this->engine . ':dbname=' . $this->database . ";host=" . $this->host;
26
        parent::__construct($dns, $this->user, $this->pass);
27
        $this->exec("SET NAMES utf8");
28
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
29
    }
30
31
    /**
32
     * 
33
     * @return DB
34
     */
35
    static function Init() {
36
        if (!self::$singleton) {
37
            self::$singleton = new DB;
38
        }
39
        return self::$singleton;
40
    }
41
42
    public function arrayPrepare($query, array $input_parameters) {
43
        $query = str_replace("\n", "", $query);
44
45
        $params = array();
46
        foreach ($input_parameters as $key => $value) {
47
            if (is_array($value)) {
48
                $implode = '';
49
                $delim = '';
50
                foreach ($value as $valueKey => $valueValue) {
51
                    $params[$key . '_' . $valueKey] = $valueValue;
52
                    $implode.= $delim . $key . '_' . $valueKey;
53
                    $delim = ', ';
54
                }
55
                $query = str_replace($key, $implode, $query);
56
            } else {
57
                $params[$key] = $value;
58
            }
59
        }
60
61
        try {
62
            $statement = $this->prepare($query);
63
        } catch (Exception $exc) {
64
            throw new Exception($query."\n".$this->errorInfo());
65
        }
66
67-
        //echo "$query\n";
67+
68
            $statement->bindValue($key, $value)."\n";
69-
            //echo "bind $key => $value\n";
69+
70
        return $statement;
71
    }
72
73
    public function multiInsert($query, array $insert_array) {
74
        $query = str_replace("\n", "", $query);
75
        
76
        $query1 = array();
77
        preg_match('/.*values\s*/im', $query, $query1);
78
        $query1 = $query1[0];
79
80
        $placeholders = array();
81
        preg_match_all('/:\w+/im', $query, $placeholders);
82
        $placeholders = $placeholders[0];
83
        $rowDelim = '';
84
        foreach ($insert_array as $key => $value) {
85
            $query1.= $rowDelim . '(';
86
            $rowDelim = ', ';
87
            $fieldDelim = '';
88
            foreach ($placeholders as $placeholder) {
89
                $query1.= $fieldDelim . $placeholder . '_' . $key;
90
                $fieldDelim = ', ';
91
            }
92
            $query1.= ')';
93
        }
94
        
95
        try {
96
            $statement = $this->prepare($query1);
97
        } catch (Exception $exc) {
98
            throw new Exception($query1."\n".$this->errorInfo());
99
        }
100
        
101
        foreach ($insert_array as $key => $value) {
102
            foreach ($placeholders as $placeholder) {
103
                $var = $value[$placeholder];
104
                $statement->bindValue($placeholder . '_' . $key, $var);
105
            }
106
        }
107
        
108
        //throw new Exception(print_r($guids, true));
109
        $statement->execute();
110-
        $guids = array();
110+
111
112-
            $guids[]=$value[':guid'];
112+