Skip to content

Commit 65ad6d9

Browse files
committed
some touchups
1 parent 77c42ec commit 65ad6d9

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,14 @@ $ fg
107107
^C
108108
```
109109

110-
## Parameterized Queries
110+
## Parameterized Queries
111+
111112
Must use HTTP POST with content-type=application/json. 'params' element must be an array in request body
113+
112114
```console
113-
$ sqliteproxy --db currenttime.sqlite
114-
$ curl -i -X POST -H "Content-Type: application/json" -d "{\"sql\":\"select DATETIME(?) AS UTC_ISO\",\"params\":[\"now\"]}" http://localhost:2048
115-
$ [{"UTC_ISO":"2020-09-10 02:06:02"}]
115+
$ sqliteproxy --db currenttime.sqlite &
116+
$ curl -H "Content-Type: application/json" -d '{"sql":"select DATETIME(?) AS UTC_ISO","params":["now"]}' http://localhost:2048
117+
[{"UTC_ISO":"2020-09-10 02:06:02"}]
116118
```
117119

118120
## CORS

main.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ function getSqlExecutor(httpRequestFieldName) {
4747
return function (req, res) {
4848
const sql = req[httpRequestFieldName].sql;
4949
let params = [];
50-
if (httpRequestFieldName === "body" && req.is('application/json'))
51-
{
50+
if (httpRequestFieldName === "body" && req.is("application/json")) {
5251
params = req[httpRequestFieldName].params;
53-
if (params == undefined || params == null)
54-
{
52+
if (params == undefined || params == null) {
5553
params = [];
5654
}
5755
}
@@ -61,11 +59,12 @@ function getSqlExecutor(httpRequestFieldName) {
6159

6260
let db;
6361
try {
64-
if (!Array.isArray(params))
65-
{
66-
var err = new Error("'params' element in http request body must be an array!");
67-
err["code"] = 10000;
68-
throw err;
62+
if (!Array.isArray(params)) {
63+
res.status(400);
64+
res.send(
65+
`${err.code}: 'params' element in http request body must be an array!\n`
66+
);
67+
return;
6968
}
7069
const readonly = flags.get("readonly");
7170
db = new Database(flags.get("db"), { readonly });
@@ -82,10 +81,10 @@ function getSqlExecutor(httpRequestFieldName) {
8281
let rows = [];
8382
try {
8483
if (sql.toLowerCase().includes("select")) {
85-
var stmt = db.prepare(sql);
84+
const stmt = db.prepare(sql);
8685
rows = stmt.all(params);
8786
} else {
88-
var stmt = db.prepare(sql);
87+
const stmt = db.prepare(sql);
8988
stmt.run(params);
9089
}
9190
} catch (err) {

0 commit comments

Comments
 (0)