From 20cf28b70805818e8efec6ee2ded252cd79a1238 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 15 Jun 2024 13:58:08 +0200 Subject: [PATCH] stateful queries --- quackpipe.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/quackpipe.go b/quackpipe.go index 656a4ef..745820c 100644 --- a/quackpipe.go +++ b/quackpipe.go @@ -14,6 +14,7 @@ import ( "regexp" "strings" "time" + "crypto/sha256" _ "github.com/marcboeker/go-duckdb" // load duckdb driver ) @@ -56,11 +57,11 @@ func quack(query string, stdin bool, format string, params string) (string, erro if !stdin { check(db.Exec("LOAD httpfs; LOAD json; LOAD parquet;")) } - + if staticAliases != "" { check(db.Exec(staticAliases)) } - + startTime := time.Now() rows, err := db.Query(query) if err != nil { @@ -325,7 +326,7 @@ func main() { default: w.Header().Set("Content-Type", "text/html; charset=utf-8") } - + // format handling if r.URL.Query().Get("default_format") != "" { default_format = r.URL.Query().Get("default_format") @@ -335,15 +336,13 @@ func main() { default_params = r.URL.Query().Get("default_params") } // auth to hash based temp file storage - /* username, password, ok := r.BasicAuth() + hashdb := "" if ok && len(password) > 0 { hash := sha256.Sum256([]byte(username + password)) - hashdb, _ := fmt.Printf("/tmp/%x.db", hash) - repath := regexp.MustCompile(`(.*?)\?`) - default_params = repath.ReplaceAllString(default_params, repath+"?") + hashdb = fmt.Sprintf("/tmp/%x.db", hash) } - */ + // extract FORMAT from query and override the current `default_format` cleanquery, format := extractAndRemoveFormat(query) if len(format) > 0 { @@ -354,11 +353,20 @@ func main() { if len(query) == 0 { _, _ = w.Write([]byte(staticPlay)) } else { - result, err := quack(query, false, default_format, default_params) - if err != nil { - _, _ = w.Write([]byte(err.Error())) + if (len(hashdb) > 0) { + result, err := quack(query, false, default_format, hashdb + "?"+ default_params) + if err != nil { + _, _ = w.Write([]byte(err.Error())) + } else { + _, _ = w.Write([]byte(result)) + } } else { - _, _ = w.Write([]byte(result)) + result, err := quack(query, false, default_format, default_params) + if err != nil { + _, _ = w.Write([]byte(err.Error())) + } else { + _, _ = w.Write([]byte(result)) + } } } })