-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBQuery.php
More file actions
executable file
·151 lines (134 loc) · 5.33 KB
/
Copy pathDBQuery.php
File metadata and controls
executable file
·151 lines (134 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/** Класс запросов в БД на основе PDO */
class DBQuery
{
private string $host;
private string $nameDB;
private string $userDB;
private string $passwordDB;
private string $db_type;
private $dbConnection;
// массив поддерживаемых СУБД
private static $DB_TYPE = ['mysql', 'pgsql', 'mssql', 'sqlite', 'sybase'];
/**
* @param string $host хост
* @param string $nameDB имя бд
* @param string $userDB пользователь
* @param string $passwordDB пароль
* @param string $db_type тип БД: mysql, pgsql, mssql, sqlite, sybase
*/
public function __construct(string $host, string $nameDB, string $userDB, string $passwordDB, string $db_type='mysql')
{
if (!in_array($db_type, DBQuery::$DB_TYPE)) {
throw new \Exception("Указанный тип БД ($db_type) не поддерживается");
}
$this->host = $host;
$this->nameDB = $nameDB;
$this->userDB = $userDB;
$this->passwordDB = $passwordDB;
$this->db_type = $db_type;
$this->dbConnection = new \PDO("{$this->db_type}:dbname={$nameDB}; host={$host}", $userDB, $passwordDB);
}
/** Выполняет подготовленный запрос
*
* @param array $arguments аргументы запроса вида ['arg'=>value], которые вставляются в SQL :arg
* @param bool $is_one_value одно или множество запрашиваемых полей
* @return mixed массив строк или одно значение
*/
public function query_prepared(string $sql, array $arguments=[], bool $is_one_value=true)
{
$stmt = $this->dbConnection->prepare($sql);
if (count($arguments)>0) {
$stmt->execute($arguments);
} else {
$stmt->execute();
}
$mode = \PDO::FETCH_OBJ;
return $is_one_value ? $stmt->fetch($mode) : $stmt->fetchAll($mode);
}
/** Выполняет запрос
* @param string $sql запрос
* @param bool $is_one_value одно или множество запрашиваемых полей
*
* @return mixed массив строк или одно значение
*/
public function query(string $sql, bool $is_one_value = true)
{
$stmt = $this->dbConnection->query($sql);
$mode = \PDO::FETCH_OBJ;
return $is_one_value ? $stmt->fetch($mode) : $stmt->fetchAll($mode);
}
/**
* INSERT
* @param $table_name имя таблицы
* @param $fields_array массив полей строки
* @return id
*/
public function insert(string $table_name, array $fields_array): int
{
if (count($fields_array)===0) {
return 0;
}
$names_str = implode(', ', array_keys($fields_array));
$name_placeholders_str = ':'.implode(', :', array_keys($fields_array));
$stmt = $this->dbConnection->prepare("INSERT INTO {$table_name}($names_str) VALUES($name_placeholders_str)");
$stmt->execute($fields_array);
return $this->dbConnection->lastInsertId();
}
/**
* UPDATE
* @param string $table_name имя таблицы
* @param array $fields_array массив полей строки
* @param integer $id id строки
* @return integer
*/
public function update(string $table_name, array $fields_array, int $id): int
{
if (count($fields_array)===0) {
return false;
}
$values_string = '';
foreach (array_keys($fields_array) as $key) {
$values_string .= "$key = :$key, ";
}
$values_string = mb_substr($values_string, 0, mb_strlen($values_string)-2);
$fields_array['id'] = $id;
$stmt = $this->dbConnection->prepare("UPDATE $table_name SET $values_string WHERE id=:id");
$stmt->execute($fields_array);
return $stmt->rowCount();
}
/**
* DELETE
* @param $table_name имя таблицы
* @param $where_condition условие
* @param $args массив параметров WHERE условия
* @return integer
*/
public function delete(string $table_name, int $id): int
{
$stmt = $this->dbConnection->prepare("DELETE FROM $table_name WHERE id=:id");
$stmt->execute(['id'=>$id]);
return $stmt->rowCount();
}
/** Выполняет изменения данных.
* @param string $sql запрос
*
* @return false|int число измененных строк
*/
public function exec(string $sql)
{
return $this->dbConnection->exec($sql);
}
/** Выполняет процедуру с возвращаемым результатом
* @param mixed $sql выражение
* @param mixed $out выходная переменная, куда будет возвращен результат
*/
public function execute_procedure($sql, $out)
{
$stmt = $this->dbConnection->prepare("call $sql");
$stmt->execute();
$stmt->closeCursor();
$procRst = $this->dbConnection->query("select $out as info");
return $procRst->fetch(\PDO::FETCH_ASSOC)['info'];
}
}