MCPcopy Index your code
hub / github.com/assafmo/SQLiteQueryServer / initQueryHandler

Function initQueryHandler

main.go:70–216  ·  view source on GitHub ↗
(dbPath string, queryString string, serverPort uint)

Source from the content-addressed store, hash-verified

68}
69
70func initQueryHandler(dbPath string, queryString string, serverPort uint) (func(w http.ResponseWriter, r *http.Request), error) {
71 // Init db and query
72
73 if dbPath == "" {
74 return nil, fmt.Errorf("Must provide --db param")
75 }
76 if queryString == "" {
77 return nil, fmt.Errorf("Must provide --query param")
78 }
79 if _, err := os.Stat(dbPath); os.IsNotExist(err) {
80 return nil, fmt.Errorf("Database file '%s' doesn't exist", dbPath)
81 }
82
83 db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?mode=rw&cache=shared&_journal_mode=WAL", dbPath))
84 if err != nil {
85 return nil, err
86 }
87
88 db.SetMaxOpenConns(1)
89
90 queryStmt, err := db.Prepare(queryString)
91 if err != nil {
92 db.Close()
93 return nil, err
94 }
95
96 helpMessage := buildHelpMessage("", queryString, queryStmt, serverPort)
97
98 return func(w http.ResponseWriter, r *http.Request) {
99 w.Header().Set("Server", "SQLiteQueryServer v"+version)
100 w.Header().Set("Access-Control-Allow-Origin", "*")
101 w.Header().Set("X-Content-Type-Options", "nosniff")
102
103 if r.URL.Path != "/query" {
104 http.Error(w, helpMessage, http.StatusNotFound)
105 return
106 }
107 if r.Method != "POST" && r.Method != "GET" {
108 http.Error(w, helpMessage, http.StatusMethodNotAllowed)
109 return
110 }
111
112 // Init fullResponse
113 fullResponse := []queryResult{}
114
115 var reqCsvReader *csv.Reader
116 if r.Method == "GET" {
117 // Static query
118 reqCsvReader = csv.NewReader(strings.NewReader(""))
119 } else {
120 // Parameterized query
121 reqCsvReader = csv.NewReader(r.Body)
122 }
123 reqCsvReader.FieldsPerRecord = -1
124
125 // Iterate over each query
126 for {
127 csvRecord, err := reqCsvReader.Read()

Callers 11

TestResultCountFunction · 0.85
TestAnswersOrderFunction · 0.85
TestAnswersHeadersFunction · 0.85
TestAnswersRowsFunction · 0.85
TestMoreThanOneParamFunction · 0.85
TestZeroParamsFunction · 0.85
TestBadParamsCountFunction · 0.85
TestBadPathRequestFunction · 0.85
TestBadMethodRequestFunction · 0.85
TestBadBodySendRequestFunction · 0.85
cmdFunction · 0.85

Calls 2

buildHelpMessageFunction · 0.85
ReadMethod · 0.80

Tested by 10

TestResultCountFunction · 0.68
TestAnswersOrderFunction · 0.68
TestAnswersHeadersFunction · 0.68
TestAnswersRowsFunction · 0.68
TestMoreThanOneParamFunction · 0.68
TestZeroParamsFunction · 0.68
TestBadParamsCountFunction · 0.68
TestBadPathRequestFunction · 0.68
TestBadMethodRequestFunction · 0.68
TestBadBodySendRequestFunction · 0.68