Skip to content

Commit c8e62f3

Browse files
committed
add query endpoint handler
1 parent 7028661 commit c8e62f3

File tree

2 files changed

+378
-1
lines changed

2 files changed

+378
-1
lines changed

catalogd/internal/storage/sqlite.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,92 @@ func (s *SQLiteV1) handleV1All(w http.ResponseWriter, r *http.Request) {
172172
defer rows.Close()
173173

174174
w.Header().Set("Content-Type", "application/jsonl")
175+
s.writeRows(w, rows)
176+
}
177+
178+
func (s *SQLiteV1) handleV1Query(w http.ResponseWriter, r *http.Request) {
179+
if r.Method != http.MethodGet {
180+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
181+
return
182+
}
183+
184+
s.mu.RLock()
185+
defer s.mu.RUnlock()
186+
187+
catalog := r.PathValue("catalog")
188+
if catalog == "" {
189+
http.Error(w, "Not found", http.StatusNotFound)
190+
return
191+
}
192+
193+
// Get query parameters
194+
schema := r.URL.Query().Get("schema")
195+
pkg := r.URL.Query().Get("package")
196+
name := r.URL.Query().Get("name")
197+
198+
// If no parameters are provided, return entire catalog
199+
if schema == "" && pkg == "" && name == "" {
200+
rows, err := s.db.Query(`
201+
SELECT schema, package, name, blob
202+
FROM metas
203+
WHERE catalog_name = ?
204+
ORDER BY schema,
205+
CASE WHEN package IS NULL THEN 1 ELSE 0 END,
206+
package,
207+
name
208+
`, catalog)
209+
if err != nil {
210+
http.Error(w, "Internal server error", http.StatusInternalServerError)
211+
return
212+
}
213+
defer rows.Close()
214+
215+
w.Header().Set("Content-Type", "application/jsonl")
216+
s.writeRows(w, rows)
217+
return
218+
}
219+
220+
// Build query with provided parameters
221+
query := `
222+
SELECT schema, package, name, blob
223+
FROM metas
224+
WHERE catalog_name = ?
225+
`
226+
args := []interface{}{catalog}
227+
228+
if schema != "" {
229+
query += " AND schema = ?"
230+
args = append(args, schema)
231+
}
232+
if pkg != "" {
233+
query += " AND package = ?"
234+
args = append(args, pkg)
235+
}
236+
if name != "" {
237+
query += " AND name = ?"
238+
args = append(args, name)
239+
}
240+
241+
query += `
242+
ORDER BY schema,
243+
CASE WHEN package IS NULL THEN 1 ELSE 0 END,
244+
package,
245+
name
246+
`
247+
248+
rows, err := s.db.Query(query, args...)
249+
if err != nil {
250+
http.Error(w, "Internal server error", http.StatusInternalServerError)
251+
return
252+
}
253+
defer rows.Close()
254+
255+
w.Header().Set("Content-Type", "application/jsonl")
256+
s.writeRows(w, rows)
257+
}
258+
259+
// Helper function to write rows as JSON lines
260+
func (s *SQLiteV1) writeRows(w http.ResponseWriter, rows *sql.Rows) {
175261
encoder := json.NewEncoder(w)
176262

177263
for rows.Next() {
@@ -184,7 +270,6 @@ func (s *SQLiteV1) handleV1All(w http.ResponseWriter, r *http.Request) {
184270
return
185271
}
186272

187-
// Convert NULL package to empty string
188273
if packageVal.Valid {
189274
meta.Package = packageVal.String
190275
}
@@ -200,6 +285,7 @@ func (s *SQLiteV1) handleV1All(w http.ResponseWriter, r *http.Request) {
200285
func (s *SQLiteV1) StorageServerHandler() http.Handler {
201286
mux := http.NewServeMux()
202287
mux.HandleFunc(s.RootURL.JoinPath("{catalog}", "api", "v1", "all").Path, s.handleV1All)
288+
mux.HandleFunc(s.RootURL.JoinPath("{catalog}", "api", "v1", "query").Path, s.handleV1Query)
203289
return mux
204290
}
205291

0 commit comments

Comments
 (0)