mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-07-05 10:37:38 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c512acf25 | |||
| 07a402634e |
@@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/common"
|
"github.com/bitechdev/ResolveSpec/pkg/common"
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
||||||
|
"github.com/bitechdev/ResolveSpec/pkg/reflection"
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/restheadspec"
|
"github.com/bitechdev/ResolveSpec/pkg/restheadspec"
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/security"
|
"github.com/bitechdev/ResolveSpec/pkg/security"
|
||||||
)
|
)
|
||||||
@@ -367,13 +368,17 @@ func (h *Handler) SqlQueryList(sqlquery string, options SqlQueryOptions) HTTPFun
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "detail":
|
case "detail":
|
||||||
// Detail format: complex API with metadata
|
// Detail format: { count, fields, items, tablename, tableprefix, total }
|
||||||
|
tableName := r.URL.Path
|
||||||
|
tablePrefix := reflection.ExtractTableNameOnly(tableName)
|
||||||
|
fields := buildDetailFieldsFromRows(dbobjlist)
|
||||||
metaobj := map[string]interface{}{
|
metaobj := map[string]interface{}{
|
||||||
"items": dbobjlist,
|
|
||||||
"count": fmt.Sprintf("%d", len(dbobjlist)),
|
"count": fmt.Sprintf("%d", len(dbobjlist)),
|
||||||
|
"fields": fields,
|
||||||
|
"items": dbobjlist,
|
||||||
|
"tablename": tableName,
|
||||||
|
"tableprefix": tablePrefix,
|
||||||
"total": fmt.Sprintf("%d", total),
|
"total": fmt.Sprintf("%d", total),
|
||||||
"tablename": r.URL.Path,
|
|
||||||
"tableprefix": "gsql",
|
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(metaobj)
|
data, err := json.Marshal(metaobj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1079,6 +1084,49 @@ func getReplacementForBlankParam(sqlquery, param string) string {
|
|||||||
// return result
|
// return result
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// buildDetailFieldsFromRows builds a field metadata list from the column names and value types
|
||||||
|
// of a raw SQL result set. Used when no model struct is available (funcspec raw queries).
|
||||||
|
func buildDetailFieldsFromRows(rows []map[string]interface{}) []reflection.ModelFieldDetail {
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return []reflection.ModelFieldDetail{}
|
||||||
|
}
|
||||||
|
first := rows[0]
|
||||||
|
fields := make([]reflection.ModelFieldDetail, 0, len(first))
|
||||||
|
for colName, val := range first {
|
||||||
|
dataType := inferGoType(val)
|
||||||
|
fields = append(fields, reflection.ModelFieldDetail{
|
||||||
|
Name: colName,
|
||||||
|
DataType: dataType,
|
||||||
|
SQLName: colName,
|
||||||
|
SQLDataType: "",
|
||||||
|
SQLKey: "",
|
||||||
|
Nullable: val == nil,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// inferGoType returns a simple type name for a value, used for detail field metadata.
|
||||||
|
func inferGoType(val interface{}) string {
|
||||||
|
if val == nil {
|
||||||
|
return "interface{}"
|
||||||
|
}
|
||||||
|
switch val.(type) {
|
||||||
|
case bool:
|
||||||
|
return "bool"
|
||||||
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||||
|
return "int64"
|
||||||
|
case float32, float64:
|
||||||
|
return "float64"
|
||||||
|
case string:
|
||||||
|
return "string"
|
||||||
|
case []byte:
|
||||||
|
return "[]byte"
|
||||||
|
default:
|
||||||
|
return "interface{}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getIPAddress extracts the real IP address from the request
|
// getIPAddress extracts the real IP address from the request
|
||||||
func getIPAddress(r *http.Request) string {
|
func getIPAddress(r *http.Request) string {
|
||||||
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
|
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
|
||||||
|
|||||||
@@ -617,6 +617,91 @@ func TestSqlQueryList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "x-detailapi header returns detail format",
|
||||||
|
sqlQuery: "SELECT * FROM myschema.myentity",
|
||||||
|
noCount: false,
|
||||||
|
blankParams: false,
|
||||||
|
allowFilter: false,
|
||||||
|
headers: map[string]string{"x-detailapi": "true"},
|
||||||
|
setupDB: func() *MockDatabase {
|
||||||
|
return &MockDatabase{
|
||||||
|
RunInTransactionFunc: func(ctx context.Context, fn func(common.Database) error) error {
|
||||||
|
db := &MockDatabase{
|
||||||
|
QueryFunc: func(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
if strings.Contains(query, "COUNT") {
|
||||||
|
dest.(*struct{ Count int64 }).Count = 3
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
*dest.(*[]map[string]interface{}) = []map[string]interface{}{
|
||||||
|
{"id": float64(1), "name": "Alice"},
|
||||||
|
{"id": float64(2), "name": "Bob"},
|
||||||
|
{"id": float64(3), "name": "Carol"},
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return fn(db)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
expectedStatus: 200,
|
||||||
|
validateResp: func(t *testing.T, w *httptest.ResponseRecorder) {
|
||||||
|
var resp map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||||
|
t.Fatalf("expected JSON object, got: %s", w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range []string{"count", "fields", "items", "tablename", "tableprefix", "total"} {
|
||||||
|
if _, ok := resp[key]; !ok {
|
||||||
|
t.Errorf("missing key %q in detail response", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var count, total string
|
||||||
|
json.Unmarshal(resp["count"], &count)
|
||||||
|
json.Unmarshal(resp["total"], &total)
|
||||||
|
if count != "3" {
|
||||||
|
t.Errorf("expected count %q, got %q", "3", count)
|
||||||
|
}
|
||||||
|
if total != "3" {
|
||||||
|
t.Errorf("expected total %q, got %q", "3", total)
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []map[string]interface{}
|
||||||
|
if err := json.Unmarshal(resp["items"], &items); err != nil {
|
||||||
|
t.Fatalf("items is not an array: %v", err)
|
||||||
|
}
|
||||||
|
if len(items) != 3 {
|
||||||
|
t.Errorf("expected 3 items, got %d", len(items))
|
||||||
|
}
|
||||||
|
|
||||||
|
var fields []map[string]interface{}
|
||||||
|
if err := json.Unmarshal(resp["fields"], &fields); err != nil {
|
||||||
|
t.Fatalf("fields is not an array: %v", err)
|
||||||
|
}
|
||||||
|
if len(fields) == 0 {
|
||||||
|
t.Error("expected non-empty fields list")
|
||||||
|
}
|
||||||
|
for _, f := range fields {
|
||||||
|
for _, key := range []string{"name", "datatype", "sqlname", "sqldatatype", "sqlkey", "nullable"} {
|
||||||
|
if _, ok := f[key]; !ok {
|
||||||
|
t.Errorf("field %v missing key %q", f, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var tablename, tableprefix string
|
||||||
|
json.Unmarshal(resp["tablename"], &tablename)
|
||||||
|
json.Unmarshal(resp["tableprefix"], &tableprefix)
|
||||||
|
if tablename == "" {
|
||||||
|
t.Error("expected non-empty tablename")
|
||||||
|
}
|
||||||
|
if tableprefix == "" {
|
||||||
|
t.Error("expected non-empty tableprefix")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "List query with noCount",
|
name: "List query with noCount",
|
||||||
sqlQuery: "SELECT * FROM users",
|
sqlQuery: "SELECT * FROM users",
|
||||||
|
|||||||
Reference in New Issue
Block a user