@@ -58,11 +58,11 @@ type SchemaTableResponse struct {
5858
5959type SchemaColumnResponse struct {
6060 Name string `json:"name"`
61- Table string `json:"table"`
62- Schema string `json:"schema"`
63- Type string `json:"type"`
64- Primary bool `json:"primary"`
65- Nullable bool `json:"nullable"`
61+ Table string `json:"table,omitempty "`
62+ Schema string `json:"schema,omitempty "`
63+ Type string `json:"type,omitempty "`
64+ Primary bool `json:"primary,omitempty "`
65+ Nullable bool `json:"nullable,omitempty "`
6666}
6767
6868type SchemaIndexResponse struct {
@@ -76,12 +76,13 @@ type SqlRequest struct {
7676}
7777
7878type SqlResultResponse struct {
79- Schema string `json:"schema,omitempty"`
80- Table string `json:"table,omitempty"`
81- Sql string `json:"sql"`
82- LastInsertId int64 `json:"last_insert_id,omitempty"`
83- RowsAffected int `json:"rows_affected,omitempty"`
84- Result []interface {} `json:"result,omitempty"`
79+ Schema string `json:"schema,omitempty"`
80+ Table string `json:"table,omitempty"`
81+ Sql string `json:"sql"`
82+ LastInsertId int64 `json:"last_insert_id,omitempty"`
83+ RowsAffected int `json:"rows_affected,omitempty"`
84+ Columns []SchemaColumnResponse `json:"columns,omitempty"`
85+ Results []interface {} `json:"results,omitempty"`
8586}
8687
8788type TokenizerResponse struct {
@@ -213,16 +214,7 @@ func (p *plugin) ServeSchema(w http.ResponseWriter, req *http.Request) {
213214 })
214215 }
215216 for _ , column := range conn .ColumnsForTable (params [0 ], name ) {
216- col := SchemaColumnResponse {
217- Name : column .Name (),
218- Table : name ,
219- Schema : params [0 ],
220- Type : column .Type (),
221- }
222- if column .Primary () != "" {
223- col .Primary = true
224- }
225- table .Columns = append (table .Columns , col )
217+ table .Columns = append (table .Columns , schemaColumn (params [0 ], name , column ))
226218 }
227219 response .Tables = append (response .Tables , table )
228220 }
@@ -265,6 +257,9 @@ func (p *plugin) ServeTable(w http.ResponseWriter, req *http.Request) {
265257 return
266258 }
267259
260+ // Fix limit to ensure we only steam up to 1K results
261+ q .Limit = uintMin (q .Limit , maxResultLimit )
262+
268263 // Populate response
269264 var response SqlResultResponse
270265 if err := conn .Do (req .Context (), SQLITE_TXN_DEFAULT , func (txn SQTransaction ) error {
@@ -372,23 +367,44 @@ func (p *plugin) ServeQuery(w http.ResponseWriter, req *http.Request) {
372367///////////////////////////////////////////////////////////////////////////////
373368// PRIVATE METHODS
374369
370+ func schemaColumn (schema , table string , column SQColumn ) SchemaColumnResponse {
371+ result := SchemaColumnResponse {
372+ Name : column .Name (),
373+ Table : table ,
374+ Schema : schema ,
375+ Type : column .Type (),
376+ }
377+ if column .Primary () != "" {
378+ result .Primary = true
379+ }
380+ return result
381+ }
382+
375383func results (r SQResults ) (SqlResultResponse , error ) {
376384 result := SqlResultResponse {
377385 Sql : r .ExpandedSQL (),
378386 LastInsertId : r .LastInsertId (),
379387 RowsAffected : r .RowsAffected (),
388+ Columns : []SchemaColumnResponse {},
389+ }
390+
391+ // Set the columns
392+ for i , column := range r .Columns () {
393+ schema , table , _ := r .ColumnSource (i )
394+ result .Columns = append (result .Columns , schemaColumn (schema , table , column ))
380395 }
381396
382397 // Iterate through the rows, break when maximum number of results is reached
383398 for {
384- if row , err := r .Next (); errors .Is (err , io .EOF ) {
399+ row , err := r .Next ()
400+ if errors .Is (err , io .EOF ) {
385401 break
386402 } else if err != nil {
387403 return result , err
388404 } else {
389- result .Result = append (result .Result , row )
405+ result .Results = append (result .Results , interfaceSliceCopy ( row ) )
390406 }
391- if len (result .Result ) >= maxResultLimit {
407+ if len (result .Results ) >= maxResultLimit {
392408 break
393409 }
394410 }
@@ -397,6 +413,12 @@ func results(r SQResults) (SqlResultResponse, error) {
397413 return result , nil
398414}
399415
416+ func interfaceSliceCopy (v []interface {}) []interface {} {
417+ result := make ([]interface {}, len (v ))
418+ copy (result , v )
419+ return result
420+ }
421+
400422func stringSliceContainsElement (v []string , elem string ) bool {
401423 for _ , v := range v {
402424 if v == elem {
0 commit comments