1: <?php
2:
3: namespace nightmare\database;
4:
5: use PDO;
6: use PDOStatement;
7:
8: class database
9: {
10: /**
11: * @var PDO
12: */
13: private $pdo;
14:
15: public function __construct(
16: $dsn,
17: $username = null,
18: $password = null,
19: $options = null
20: ) {
21: if ($dsn instanceof PDO) {
22: $this->pdo = $dsn;
23: } else {
24: $this->pdo = new PDO(
25: $dsn,
26: $username,
27: $password,
28: array_merge([
29: PDO::MYSQL_ATTR_INIT_COMMAND, 'SET sql_mode="ANSI,TRADITIONAL"'
30: ], (array) $options)
31: );
32: }
33:
34: $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
35: $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
36: $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
37: $this->pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [statement::class]);
38:
39: $this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
40: }
41:
42: /**
43: * @param string $sql
44: * @param array|null $params
45: * @return PDOStatement|false
46: */
47: public function query($sql, $params = null)
48: {
49: $stmt = $this->pdo->prepare($sql);
50: $stmt->execute($params);
51:
52: return $stmt;
53: }
54:
55: /**
56: * @param string $table
57: * @param array $params
58: * @return string
59: */
60: public function insert($table, $params)
61: {
62: $sql = 'insert into "' . $table . '"'
63: . ' (' . implode(',', $this->buildName(array_keys($params))) . ')'
64: . ' values (' . implode(',', array_fill(0, count($params), '?')) . ')';
65:
66: $this->query($sql, array_values($params));
67:
68: return $this->pdo->lastInsertId();
69: }
70:
71:
72: /**
73: * @param string $table
74: * @param array $con
75: * @param array $arr
76: * @return void
77: */
78: public function update_or_insert($table, $con, $arr)
79: {
80: $whereConditions = [];
81: $whereParams = [];
82:
83: foreach ($con as $column => $value) {
84: $whereConditions[] = sprintf('"%s" = ?', $column);
85: $whereParams[] = $value;
86: }
87:
88: $whereClause = implode(' AND ', $whereConditions);
89: $checkSql = sprintf('SELECT COUNT(*) FROM "%s" WHERE %s', $table, $whereClause);
90: $count = $this->fetch_column($checkSql, $whereParams);
91:
92: if ($count > 0) {
93: $updateParts = [];
94: $updateParams = [];
95:
96: foreach ($arr as $column => $value) {
97: $updateParts[] = sprintf('"%s" = ?', $column);
98: $updateParams[] = $value;
99: }
100:
101: $updateClause = implode(', ', $updateParts);
102: $updateSql = sprintf('UPDATE "%s" SET %s WHERE %s', $table, $updateClause, $whereClause);
103:
104: $this->query($updateSql, array_merge($updateParams, $whereParams));
105: } else {
106: $this->insert($table, array_merge($con, $arr));
107: }
108: }
109:
110: /**
111: * @param string $sql
112: * @param array|null $params
113: * @return int
114: */
115: public function update($sql, $params = null)
116: {
117: $stmt = $this->query($sql, $params);
118:
119: return $stmt->rowCount();
120: }
121:
122: /**
123: * @param string $sql
124: * @param array|null $params
125: * @return mixed
126: */
127: public function fetch($sql, $params = null)
128: {
129: return $this->query($sql, $params)
130: ->fetch();
131: }
132:
133: public function exec($sql)
134: {
135: return $this->pdo->exec($sql);
136: }
137:
138: /**
139: * @param string $sql
140: * @param array|null $params
141: * @return array
142: */
143: public function fetchAll($sql, $params = null)
144: {
145: return $this->query($sql, $params)
146: ->fetchAll();
147: }
148: /**
149: * @param string $sql
150: * @param array|null $params
151: * @return array
152: */
153: public function fetch_all($sql, $params = null)
154: {
155: return $this->query($sql, $params)
156: ->fetchAll();
157: }
158:
159: /**
160: * @param string $sql
161: * @param array|null $params
162: * @param int $column
163: * @return mixed
164: */
165: public function fetchColumn($sql, $params = null, $column = 0)
166: {
167: $stmt = $this->query($sql, $params);
168: return $stmt->fetchColumn($column);
169: }
170: /**
171: * @param string $sql
172: * @param array|null $params
173: * @param int $column
174: * @return mixed
175: */
176: public function fetch_column($sql, $params = null, $column = 0)
177: {
178: $stmt = $this->query($sql, $params);
179: return $stmt->fetchColumn($column);
180: }
181:
182: /**
183: * @param int $page
184: * @param int $perPage
185: * @return int
186: */
187: public function getOffset($page, $perPage)
188: {
189: return $page * $perPage - $perPage;
190: }
191: /**
192: * @param int $page
193: * @param int $perPage
194: * @return int
195: */
196: public function get_offset($page, $perPage)
197: {
198: return $page * $perPage - $perPage;
199: }
200:
201: /**
202: * @param string $str
203: * @return string
204: */
205: public function quote($str)
206: {
207: return $this->pdo->quote($str);
208: }
209:
210: /**
211: * @param array $arr
212: * @return array
213: */
214: public function buildName($arr)
215: {
216: return array_map(function ($item) {
217: return '"' . $item . '"';
218: }, $arr);
219: }
220: /**
221: * @param array $arr
222: * @return array
223: */
224: public function build_name($arr)
225: {
226: return array_map(function ($item) {
227: return '"' . $item . '"';
228: }, $arr);
229: }
230: }
231: