File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -107,12 +107,14 @@ $ fg
107107^C
108108```
109109
110- ## Parameterized Queries
110+ ## Parameterized Queries
111+
111112Must 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
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments