Skip to content

Commit 28fc331

Browse files
committed
Apply feedback on /query endpoint
Signed-off-by: Carlos Martín <[email protected]>
1 parent e6deab2 commit 28fc331

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

server/handler/query.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,19 @@ func Query(db *sql.DB) RequestProcessFunc {
113113
colData[columnNames[i]] = sqlVal.String
114114
}
115115
case *[]byte:
116-
// TODO (carlosms) this may not always be a JSON array, it could
117-
// be a JSON object
118-
119-
// DatabaseTypeName JSON is used for arrays, but we don't know the
120-
// type of each element. We try with uast node first and text later
116+
// DatabaseTypeName JSON is used for arrays of uast nodes and
117+
// arrays of strings, but we don't know the exact type.
118+
// We try with arry of uast nodes first and any JSON later
121119
nodes, err := unmarshallUAST(val)
122120
if err == nil {
123121
colData[columnNames[i]] = nodes
124122
} else {
125-
var strings []string
126-
if err := json.Unmarshal(*val.(*[]byte), &strings); err != nil {
123+
var data interface{}
124+
125+
if err := json.Unmarshal(*val.(*[]byte), &data); err != nil {
127126
return nil, err
128127
}
129-
colData[columnNames[i]] = strings
128+
colData[columnNames[i]] = data
130129
}
131130
}
132131
}
@@ -185,11 +184,9 @@ func unmarshallUAST(data interface{}) ([]*uast.Node, error) {
185184
// addLimit adds LIMIT to the query, performing basic tests to skip it
186185
// for DESCRIBE TABLE, SHOW TABLES, and avoid '; limit'
187186
func addLimit(query string, limit int) string {
188-
upper := strings.ToUpper(query)
189-
if strings.Contains(upper, "DESCRIBE") || strings.Contains(upper, "SHOW") {
190-
return query
187+
query = strings.TrimRight(strings.TrimSpace(query), ";")
188+
if strings.HasPrefix(strings.ToUpper(query), "SELECT") {
189+
return fmt.Sprintf("%s LIMIT %d", query, limit)
191190
}
192-
193-
trimmed := strings.TrimRight(strings.TrimSpace(query), ";")
194-
return fmt.Sprintf("%s LIMIT %d", trimmed, limit)
191+
return query
195192
}

0 commit comments

Comments
 (0)