-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyQuery.php
275 lines (221 loc) · 7.42 KB
/
EasyQuery.php
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
// ini_set("log_errors", 1);
// ini_set("display_errors", 0);
// ini_set("error_log", "easyquery.log");
class config {
public static $SQBAPI_HOST= "http://sqlquerybuilder.com/";
public static $SQBAPI_KEY = "3ea4523c-1d18-4d8c-82f5-c4f998d67daf"; //<-- change to your API key
public static $MODEL_ID = "ModelID"; //<-- change to the ID of your model
public static $MODEL_FILE_JSON = "ModelID.json"; //<-- change to the name of your model file
//Database
//Below we use the connection parameters for our own testing DB.
//You will need to change these parameter if you want to connect to your own database.
public static $DB_NAME='cms';
public static $DB_HOST='localhost';
public static $DB_PORT='3306';
public static $DB_USER='root';
public static $DB_PASSWD='12345678';
}
function getTypeName($type) {
$types[1]='number';
$types[2]='number';
$types[3]='number';
$types[4]='number';
$types[5]='number';
$types[5]='number';
$types[8]='number';
$types[9]='number';
$types[246]='number';
$types[7]='datetime';
$types[10]='datetime';
$types[11]='datetime';
$types[12]='datetime';
if(!isset($types[$type]))return 'string';
return $types[$type];
}
function renderDataTable($recordSet) {
$ret=array();
$columns=$recordSet->fetch_fields();
foreach($columns as $col) {
$columnDescr=array();
$columnDescr['id']=$col->name;
$columnDescr['label']=$col->name;
$columnDescr['type']=getTypeName($col->type);
$ret['cols'][]=$columnDescr;
}
while($array=$recordSet->fetch_array(MYSQLI_ASSOC)) {
$values=array_values($array);
$rowData=array();
$rowData['c']=array();
$col_index = 0;
$colType = '';
foreach($values as $value) {
$colType = $ret['cols'][$col_index]['type'];
if ($colType =='number') {
$rowData['c'][]=array("v"=>$value*1);
}
else if ($colType =='datetime') {
$rowData['c'][]=array("v"=> "Date(".substr($value,0,4).", ".substr($value,5,2).", ".substr($value,8,2).")");
}
else {
$rowData['c'][]=array("v"=>$value);
}
$col_index++;
}
$ret['rows'][]=$rowData;
}
return $ret;
}
function renderRequestedList($recordSet) {
$ret=array();
while($array=$recordSet->fetch_array(MYSQLI_NUM)) {
if(!isset($array[1]))$array[1]=$array[0];
$ret[]=array('id'=>$array[0],'text'=>$array[1]);
}
$ret_json=json_encode($ret);
return $ret_json;
}
function executeSql($sql) {
$mysqli=new mysqli(config::$DB_HOST,config::$DB_USER,config::$DB_PASSWD,config::$DB_NAME,config::$DB_PORT);
if ($mysqli->connect_error) {
return null;
}
return $mysqli->query($sql);
}
function buildSql($query_json) {
/*
//send a request to the REST web-service
$url = config::$SQBAPI_HOST.'api/2.0/SqlQueryBuilder';
$request_data = '{"modelId":"'.config::$MODEL_ID.'", "query":'.$query_json.'}';
//error_log($request_data);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\nSQB-Key: ".config::$SQBAPI_KEY,
'method' => 'POST',
'content' => $request_data,
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
//get a response in JSON format
if ( $response !== FALSE) {
$res = json_decode($response, true);
$sql = "";
//now we get an SQL statement by the query defined on client-side
if ($res != null && array_key_exists("sql", $res) )
$sql = $res["sql"];
return $sql;
}
else {
return 'ERROR';
}
*/
//Create sql
return "SELECT * FROM applications";
}
function getXmlModel($modelId){
//Get XML Model from SimpleQueryBuilder
$url = config::$SQBAPI_HOST.'api/2.0/DataModels/'.$modelId;
$options = array(
'http' => array(
'header' => "SQB-Key: ".config::$SQBAPI_KEY,
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
return $response;
}
$action = $_REQUEST['action'];
if ($action == 'getModel') {
//get model name
$data = json_decode(file_get_contents('php://input'), true);
$MODEL_ID = $data['modelId'];
//read model from a file and return in response
$model = file_get_contents(config::$MODEL_FILE_JSON);
echo $model;
}
else if ($action == 'loadQuery') {
//get query name
$data = json_decode(file_get_contents('php://input'), true);
$query_json = json_encode($data['query']);
$query_name = $data['id'];
//read query from a file and return in response
$query_file_name = $query_name.".json";
$query_json = file_get_contents($query_file_name);
echo $query_json;
}
else if ($action == 'saveQuery') {
//get query in json format
$data = json_decode(file_get_contents('php://input'), true);
//get query name
$query_name = $data['query']['id'];
$query_json = json_encode($data['query']);
//save query to a file
$query_file_name = $query_name.".json";
file_put_contents($query_file_name, $query_json);
echo '{"result":"OK"}';
}
else if ($action == 'syncQuery') {
//return generated SQL to show it on our demo web-page. Not necessary to do in production!
$data = json_decode(file_get_contents('php://input'), true);
$queryJson = json_encode($data['query']);
$sql = buildSql($queryJson);
$result = json_encode(array('statement' => $sql));
echo $result;
}
else if ($action == 'executeQuery') {
//get query in JSON format
$data = json_decode(file_get_contents('php://input'), true);
$query_json = json_encode($data['query']);
$sql = buildSql($query_json);
$result='{}';
$recordSet = executeSql($sql);
if ($recordSet)
{
$resultSet=renderDataTable($recordSet);
$ret=array('statement' => $sql, 'resultSet' => $resultSet);
$result=json_encode($ret);
}
else {
$ret['statement']="DATABASE CONNECTION ERROR!!!";
$result = json_encode($ret);
}
echo $result;
}
else if ($action == 'listRequest') {
//here we need to assemble the requested list based on its name and return it as JSON array
//each item in that array is an object with two properties: "id" and "text"
//get the name of requested list
$data = json_decode(file_get_contents('php://input'), true);
if ($data['listName'] == "SQL") {
//if this is a SQL list request - we need to execute SQL statement and return the result set as a list of of {id, text} items
$modelId = config::$MODEL_ID;
$xmlModel = getXmlModel($modelId);
//Parse xml model to get sql statement for generationg list
$DataModel = simplexml_load_string($xmlModel,'SimpleXMLElement',LIBXML_NOCDATA);
$sql = ' ';
foreach ($DataModel->Editors->Editor as $editor) {
if($editor['id'][0] == $data['editorId']){
$sql = $editor->SQL;
}
}
if($recordSet=executeSql($sql)) {
$result=renderRequestedList($recordSet);
echo $result;
}
//$sql = $_POST['sql'];
//echo '[{"id":"SQL1","text":"SQL List Text 1"},{"id":"SQL2","text":"SQL List Text 1"},{"id":"SQL3","text":"SQL List Text 3"},{"id":"SQL4","text":"SQL List Text 4"}]';
}
else {
//otherwise we return some list based on list name
if ($data['listName'] == "RegionsList") {
echo '[{"id":"11","text":"AAAA"},{"id":"22","text":"BBBB"},{"id":"33","text":"CCCC"},{"id":"44","text":"DDDD"}]';
}
else {
echo '[]';
}
}
}
else
echo '{"result": "OK"}';
?>