/
index.php
90 lines (76 loc) · 2.14 KB
/
index.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
<?php
require_once(__DIR__.'/config.php');
//
// connect to odbc
//
$handle = false;
if ($_ENV['USE_PERSISTENT_ODBC']) {
$handle = odbc_pconnect($_ENV['DSN'], $_ENV['DSN_USER'], $_ENV['DSN_PASSWORD']);
} else {
$handle = odbc_connect($_ENV['DSN'], $_ENV['DSN_USER'], $_ENV['DSN_PASSWORD']);
}
if ($handle === false) {
http_response_code(500);
die('Could not connect to ODBC DSN: '.$_ENV['DSN'].', error was: '.odbc_errormsg());
}
debug('Connected to ODBC '.$_ENV['DSN']);
//
// read the JSON post body
//
$body = file_get_contents('php://input');
if (strlen($body) == 0) {
http_response_code(405);
die('POST was empty. Expected: POST of JSON data, SQL queries in an array.');
}
//
// decode the JSON
//
$json = @json_decode($body);
if ($json === false) {
http_response_code(400);
die('Could not decode JSON. Check your encoding.');
}
//
// make sure we've been given an array of SQL queries
//
if (!is_array($json)) {
http_response_code(400);
die('Expected JSON array of SQL queries.');
}
debug('Got '.count($json).' queries to execute');
//
// execute the queries, one by one
//
$response = array();
foreach ($json as $sql) {
debug('Doing query: '.$sql);
// we'll populate this array with our rows from ODBC
$query_response_rows = array();
$result = @odbc_exec($handle, $sql);
if ($result === false) {
die('ODBC error: '.odbc_errormsg());
} else if ($result) {
while ($row = odbc_fetch_array($result)) {
$query_response_rows[] = $row;
}
debug('Query returned '.count($query_response_rows).' rows');
} else {
http_response_code(502);
$error = odbc_errormsg($result);
die('Could not execute query: "'.$sql.'". The error (if any) was: '.$error);
}
// add an inner array to our overall response
$response[] = force_encoding($query_response_rows);
// frees memory associated with the query
if ($result) {
odbc_free_result($result);
}
}
header('Content-Type: application/json');
echo json_encode($response);
//
// disconnect from odbc
//
if ($handle && !$_ENV['USE_PERSISTENT_ODBC']) {
odbc_close($handle);
}