featurebase/dax/queryer/http/handler.go
Travis Turner 87011e4294
CLI: make it more like psql (#2235)
* Refactor CLI to mimic psql's meta-commands

This PR adds support for meta-commands (also known as "backslash
commands") like those in psql, Postgres's CLI. Only a few meta-commands
are currently implemented, but this was meant to demonstrate how we
could use something like `\i file.csv` to insert local files into SQL
statements.

* Meta-commands: \file and \include

The initial implementation used `\i` as a streaming file handle.

This commit changes that to `\file`, and then implements `\i` (or
`\include`) as handling multiple sql commands.

* Add meta-command "help" (\?)

This is basically a copy of the psql help output, but includes only
those options we currently support.

* Add support for \o [file], and \timing

The \o meta-command writes query output to a file.

The \timing meta-command turns on/off the timing display sent to stdout.

* Add meta-commands: \l (show databases) and \dt (show tables)

* Add meta-command: \watch [period]

* Update meta-command \connect to take database name instead of ID

* Add support for \echo, \qecho, and \warn

This commit contains an known issue in that the `-n` option will exclude
the line feed, but if the output is the terminal, the readline package
clobbers any content on the current line (i.e. anything without a line
feed). That will need to be addressed at some point.

* Add support for \w [FILE] (write query buffer to file)

* Add SchemaAPI no-op implementation

* Refactor query handler to align with /sql and /databases endpoints

We want to standardize on:
/sql
/databases/{databaseID}/sql

* Add CLI support for expanded, border, tuples_only (and pset)

* Add help text for \pset and \t
2023-02-14 09:23:51 -06:00

103 lines
2.5 KiB
Go

package http
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/dax/queryer"
"github.com/gorilla/mux"
)
func Handler(q *queryer.Queryer) http.Handler {
svr := &server{
queryer: q,
}
router := mux.NewRouter()
router.HandleFunc("/health", svr.getHealth).Methods("GET").Name("GetHealth")
router.HandleFunc("/sql", svr.postSQL).Methods("POST").Name("PostSQL")
router.HandleFunc("/databases/{databaseID}/sql", svr.postSQL).Methods("POST").Name("PostDatabaseSQL")
return router
}
type server struct {
queryer *queryer.Queryer
}
// GET /health
func (s *server) getHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// POST /sql
func (s *server) postSQL(w http.ResponseWriter, r *http.Request) {
orgID := getOrganizationID(r)
dbID := dax.DatabaseID(mux.Vars(r)["databaseID"])
contentType := r.Header.Get("Content-Type")
switch contentType {
case "text/plain":
qdbid := dax.NewQualifiedDatabaseID(orgID, dbID)
resp, err := s.queryer.QuerySQL(r.Context(), qdbid, r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
case "application/json":
body := r.Body
defer body.Close()
req := SQLRequest{}
if err := json.NewDecoder(body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx := r.Context()
if orgID == "" {
orgID = req.OrganizationID
}
if dbID == "" {
dbID = req.DatabaseID
}
qdbid := dax.NewQualifiedDatabaseID(orgID, dbID)
resp, err := s.queryer.QuerySQL(ctx, qdbid, strings.NewReader(req.SQL))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
default:
err := fmt.Errorf("unsupported request content-type '%s'", contentType)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func getOrganizationID(r *http.Request) dax.OrganizationID {
return dax.OrganizationID(r.Header.Get("OrganizationID"))
}
type SQLRequest struct {
OrganizationID dax.OrganizationID `json:"org-id"`
DatabaseID dax.DatabaseID `json:"db-id"`
SQL string `json:"sql"`
}