-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_db_abstraction.php
More file actions
160 lines (134 loc) · 4.14 KB
/
query_db_abstraction.php
File metadata and controls
160 lines (134 loc) · 4.14 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
152
153
154
155
156
157
158
159
160
<?php
/**
* Class DBConnection
*
* Abstract the database connection and low level database
* library calls for the query builder layer. If one decides to change the
* database type, this should be the only place to change.
*
* @author Fabio Andreozzi Godoy <fabio.godoy@oldpocket.com>
* @copyright 2019-2020 Fabio Godoy
* @link https://github.com/oldpocket/ARAIS
* @license https://github.com/oldpocket/ARAIS/blob/master/LICENSE
* @version 0.5.0
* @package ARAIS
*/
class QueryDBAbstraction
{
/**
* @var DB
*/
private $db = null;
/**
* @return DB
*/
protected function connect()
{
if (!$this->db) {
// Create a new database, if the file doesn't exist and open it for reading/writing.
// The extension of the file is arbitrary.
$this->db = new SQLite3(getenv('SQLITE_FILE_NAME'), SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
/*
// ToDo : load external SQL file with all SQL create statements
$this->db->query('CREATE TABLE IF NOT EXISTS "data" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"device_uid" VARCHAR,
"sensor_uid" VARCHAR,
"value" INTEGER,
"timestamp" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
)'); */
}
return $this->db;
}
/**
* @param string $sql
* @param array $values
* @return string with the id of the last inserted row
*/
protected final function executeInsert($sql, array $values)
{
$statement = $this->statement($sql);
foreach ($values as $key => $value) {
$statement->bindValue($key+1, $value);
}
if ($statement && $statement->execute()) {
return $this->connect()->lastInsertRowID();
}
return null;
}
/**
* @param string $sql
* @param array $values
* @return array with the found rows
*/
protected final function executeSelect($sql, array $values)
{
$statement = $this->statement($sql);
foreach ($values as $key => $value) {
$statement->bindValue($key+1, $value);
}
$results = $statement->execute();
if (!$results) return null;
$resultArray = array();
// Get all the row results
while($entry = $results->fetchArray(SQLITE3_ASSOC)) {
$resultArray[] = $entry;
};
$result['request_date'] = date("Y-m-d H:i:s");
$result['values'] = $resultArray;
return json_decode(json_encode($result));
}
/**
* @param string $sql
* @param array $values
* @return int with the number of rows affected
*/
protected final function executeUpdate($sql, array $values)
{
$statement = $this->statement($sql);
foreach ($values as $key => $value) {
$statement->bindValue($key+1, $value);
}
if ($statement && $statement->execute()) {
return $this->connect()->changes();
}
return null;
}
/**
* @param string $sql
* @param array $values
* @return int with the number of rows deleted
*/
protected final function executeDelete($sql, array $values)
{
$statement = $this->statement($sql);
foreach ($values as $key => $value) {
$statement->bindValue($key+1, $value);
}
if ($statement && $statement->execute()) {
return $this->connect()->changes();
}
return null;
}
/**
* @param $sql
* @param array $values
* @return int|null
*/
protected final function execute($sql, array $values)
{
$statement = $this->statement($sql);
if ($statement && $statement->execute(array_values($values))) {
return $statement->rowCount();
}
return null;
}
/**
* @param $sql
* @return PDOStatement
*/
private final function statement($sql)
{
return $this->connect()->prepare($sql);
}
}