6
6
"strconv"
7
7
"bytes"
8
8
"net/http"
9
+ "net/url"
9
10
"encoding/json"
10
11
"database/sql"
11
12
)
@@ -77,7 +78,7 @@ func getDbRecordsHandler(harg *apiHandlerArg) apiHandlerRet {
77
78
return errorRet (badStat , err , "after fetchParams" )
78
79
}
79
80
80
- return getCommon (params )
81
+ return getCommon (harg . req . URL , params )
81
82
}
82
83
83
84
// getDbRecordHandler() handles GET requests on /db/_table/{table_name}/{id} .
@@ -90,7 +91,7 @@ func getDbRecordHandler(harg *apiHandlerArg) apiHandlerRet {
90
91
params ["limit" ] = strconv .Itoa (1 )
91
92
params ["offset" ] = strconv .Itoa (0 )
92
93
93
- return getCommon (params )
94
+ return getCommon (harg . req . URL , params )
94
95
}
95
96
96
97
// updateDbRecordsHandler() handles PATCH requests on /db/_table/{table_name} .
@@ -180,7 +181,7 @@ func tablesQuery(tabName string,
180
181
181
182
idlist := []interface {}{}
182
183
qstring := fmt .Sprintf ("select %s from %s" , fieldName , tabName )
183
- result , err := runQuery (db , qstring , idlist )
184
+ result , err := runQuery (db , nil , qstring , idlist )
184
185
if err != nil {
185
186
return errorRet (badStat , err , "after runQuery" )
186
187
}
@@ -203,7 +204,7 @@ func schemaQuery(tabName string,
203
204
idlist := []interface {}{}
204
205
qstring := fmt .Sprintf (`select %s from %s where %s = "%s"` ,
205
206
fieldName , tabName , selector , item )
206
- result , err := runQuery (db , qstring , idlist )
207
+ result , err := runQuery (db , nil , qstring , idlist )
207
208
if err != nil {
208
209
return errorRet (badStat , err , "after runQuery" )
209
210
}
@@ -245,9 +246,9 @@ func mkSQLRow(N int) []interface{} {
245
246
246
247
// queryErrorRet() passes thru the first 2 args (ret and err),
247
248
// while logging the third argument (dmsg).
248
- func queryErrorRet (ret []* KVRecord ,
249
+ func queryErrorRet (ret []* KVResponse ,
249
250
err error ,
250
- dmsg string ) ([]* KVRecord , error ) {
251
+ dmsg string ) ([]* KVResponse , error ) {
251
252
if dmsg != "" {
252
253
log .Debugf ("queryErrorRet [%s], %s" , err , dmsg )
253
254
}
@@ -257,12 +258,13 @@ func queryErrorRet(ret []*KVRecord,
257
258
// runQuery() does a select query using the given query string.
258
259
// the return value is a list of the retrieved records.
259
260
func runQuery (db dbType ,
261
+ u * url.URL ,
260
262
qstring string ,
261
- ivals []interface {}) ([]* KVRecord , error ) {
263
+ ivals []interface {}) ([]* KVResponse , error ) {
262
264
log .Debugf ("query = %s" , qstring )
263
265
log .Debugf ("ivals = %s" , ivals )
264
266
265
- ret := make ([]* KVRecord , 0 , 1 )
267
+ ret := make ([]* KVResponse , 0 , 1 )
266
268
267
269
rows , err := db .handle .Query (qstring , ivals ... )
268
270
if err != nil {
@@ -279,6 +281,7 @@ func runQuery(db dbType,
279
281
log .Debugf ("cols = %s" , cols )
280
282
ncols := len (cols )
281
283
284
+ i := 0
282
285
for rows .Next () {
283
286
vals := mkSQLRow (ncols )
284
287
err = rows .Scan (vals ... )
@@ -290,11 +293,16 @@ func runQuery(db dbType,
290
293
if err != nil {
291
294
return queryErrorRet (ret , err , "failure after convValues" )
292
295
}
293
- kvrow := KVRecord {Keys : cols , Values : vals }
296
+ kvrow := KVResponse {Keys : cols ,
297
+ Values : vals ,
298
+ Kind : "KVResponse" ,
299
+ Self : mkSelf (u , ivals , i ),
300
+ }
294
301
ret = append (ret , & kvrow )
295
302
if len (ret ) >= maxRecs { // safety check
296
303
break
297
304
}
305
+ i ++
298
306
}
299
307
300
308
err = rows .Err ()
@@ -526,9 +534,9 @@ func mkSelectString(params map[string]string) (string, []interface{}) {
526
534
}
527
535
528
536
// getCommon() is common code for selection APIs.
529
- func getCommon (params map [string ]string ) apiHandlerRet {
537
+ func getCommon (u * url. URL , params map [string ]string ) apiHandlerRet {
530
538
qstring , idlist := mkSelectString (params )
531
- result , err := runQuery (db , qstring , idlist )
539
+ result , err := runQuery (db , u , qstring , idlist )
532
540
if err != nil {
533
541
return errorRet (badStat , err , "after runQuery" )
534
542
}
@@ -560,7 +568,7 @@ func updateCommon(harg *apiHandlerArg, params map[string]string) apiHandlerRet {
560
568
561
569
// convTableNames() converts the return format from runQuery()
562
570
// into a simple list of names.
563
- func convTableNames (result []* KVRecord ) ([]string , error ) {
571
+ func convTableNames (result []* KVResponse ) ([]string , error ) {
564
572
// convert from query format to simple list of names
565
573
ret := make ([]string , len (result ))
566
574
for i , row := range result {
@@ -689,3 +697,13 @@ func execN(db dbType, cmdList ...*xCmd) error {
689
697
}
690
698
return tx .Commit ()
691
699
}
700
+
701
+ // mkSelf() returns a string for the self field of a KVResponse.
702
+ func mkSelf (u * url.URL , idlist []interface {}, i int ) string {
703
+ if u == nil || i >= len (idlist ) {
704
+ log .Debugf ("mkSelf: idlist=%s, i=%d" , idlist , i )
705
+ return "??"
706
+ }
707
+ id , _ := idlist [i ].(int64 )
708
+ return fmt .Sprintf ("%s://%s%s/%d" , u .Scheme , u .Host , u .Path , id )
709
+ }
0 commit comments