-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLHandler.php
More file actions
192 lines (155 loc) · 4.23 KB
/
SQLHandler.php
File metadata and controls
192 lines (155 loc) · 4.23 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
SQL-Editor - An SQl table editor
Copyright (C) 2016 Andrew S
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class SQLResponse {
private static $response;
private static $array_cache;
private static $length;
private static $index;
/**
* Creates a SQL response object
* @param {Object} r - Result of query
*/
function __construct($r) {
self::$response = $r;
$this->initialise();
}
/**
* Initialise and fill the array_cache;
*/
function initialise() {
$array = [];
$length = 0;
while ($row = mysqli_fetch_array(self::$response)) {
array_push($array,$row);
$length ++;
}
self::$array_cache = $array;
self::$length = $length;
self::$index = 0;
}
/**
* Gets an array of rows
* @returns {Array} rows
*/
function toArray() {
return self::$array_cache;
}
/**
* Gets raw response data
* @returns {Object} response
*/
function getResponse() {
return self::$response;
}
/**
* Gets next row
* @returns {Object} Row
*/
function next() {
$ind = self::$index ++;
if (isset(self::$array_cache[$ind])) {
return self::$array_cache[$ind];
} else {
return false;
}
}
/**
* Resets the pointer for nextItem();
*/
function reset() {
self::$index = 0;
}
/**
* Gets the length of rows
* @returns {Number} length of rows
*/
function getLength() {
return self::$length;
}
}
/** Connector to sql server*/
class SQLConnector {
private static $db;
/**
* Creates a SQL connector object
* @param {Array} options - Options for connection
*/
function __construct($a,$b,$c,$d,$e) {
self::$db = mysqli_connect($a,$b,$c,$d,$e);
}
/**
* Gets the error if there is one
* @returns {Error} Error
*/
function getError() {
return mysqli_error(self::$db);
}
/**
* Sends a query to the mysql server, and returns a SQL response object
* @param {String} query - Query to send
* @returns {SQLResponse} Response object
*/
function query($query,$giveResult = false) {
$r = mysqli_query(self::$db,$query);
if (!$r) {
return false;
}
if (!$giveResult) {
return new SQLResponse($r);
}
}
/**
* Inserts an object into the sql database
* @param {String} table - Table to insert object
* @param {Object} object - Object to insert
* @returns {SQLResponse} Response object
*/
function insert($table,$object) {
$keys = array_keys($object);
$query = "INSERT INTO `".$table."` (";
$comma = "";
foreach ($keys as $key) {
$query .= $comma . "`". $key ."`";
$comma = ",";
}
$query .= ") VALUES (";
$comma = "";
foreach ($object as $item) {
$colon = "'";
if (is_int($item)) {
$colon = "";
}
$query .= $comma . $colon . $item . $colon;
$comma = ",";
}
$query .= ")";
return $this->query($query);
}
/**
* Gets the database connection
* @returns {SQLDatabase}
*/
function getDB() {
return self::$db;
}
/**
* Closes the database connection
*/
function close() {
mysqli_close(self::$db);
}
}
?>