mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-05-16 08:45:18 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49639b6c19 | ||
|
|
8733176cba | ||
|
|
bce27f7ed2 | ||
|
|
987a2a7faf | ||
| 157788b73b |
@@ -1529,7 +1529,7 @@ func (b *BunUpdateQuery) SetMap(values map[string]interface{}) common.UpdateQuer
|
||||
// Skip primary key updates
|
||||
continue
|
||||
}
|
||||
b.query = b.query.Set(column+" = ?", value)
|
||||
b.query = b.query.Set(column+" = ?", common.ConvertSliceForBun(value))
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package common
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
||||
@@ -261,3 +262,48 @@ func GetTableNameFromModel(model interface{}) string {
|
||||
// This handles cases like "MasterTaskItem" -> "mastertaskitem"
|
||||
return strings.ToLower(modelType.Name())
|
||||
}
|
||||
|
||||
// ConvertSliceForBun converts []interface{} values to PostgreSQL array literal strings.
|
||||
// BUN's fallback appender for []interface{} is JSON encoding, which produces "[]" —
|
||||
// invalid PostgreSQL array syntax. PostgreSQL expects "{}" for empty arrays and
|
||||
// "{elem1,elem2}" for non-empty ones. All other value types are returned unchanged.
|
||||
func ConvertSliceForBun(value interface{}) interface{} {
|
||||
arr, ok := value.([]interface{})
|
||||
if !ok {
|
||||
return value
|
||||
}
|
||||
if len(arr) == 0 {
|
||||
return "{}"
|
||||
}
|
||||
parts := make([]string, len(arr))
|
||||
for i, elem := range arr {
|
||||
switch e := elem.(type) {
|
||||
case string:
|
||||
needsQuote := e == "" || strings.ContainsAny(e, `,"\\{}`+"\t\n\r ")
|
||||
if needsQuote {
|
||||
e = strings.ReplaceAll(e, `\`, `\\`)
|
||||
e = strings.ReplaceAll(e, `"`, `""`)
|
||||
parts[i] = `"` + e + `"`
|
||||
} else {
|
||||
parts[i] = e
|
||||
}
|
||||
case float64:
|
||||
if e == float64(int64(e)) {
|
||||
parts[i] = strconv.FormatInt(int64(e), 10)
|
||||
} else {
|
||||
parts[i] = strconv.FormatFloat(e, 'f', -1, 64)
|
||||
}
|
||||
case bool:
|
||||
if e {
|
||||
parts[i] = "t"
|
||||
} else {
|
||||
parts[i] = "f"
|
||||
}
|
||||
case nil:
|
||||
parts[i] = "NULL"
|
||||
default:
|
||||
parts[i] = fmt.Sprintf("%v", e)
|
||||
}
|
||||
}
|
||||
return "{" + strings.Join(parts, ",") + "}"
|
||||
}
|
||||
|
||||
@@ -106,3 +106,66 @@ func TestExtractTagValue(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertSliceForBun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
expected interface{}
|
||||
}{
|
||||
{
|
||||
name: "empty slice produces empty pg array",
|
||||
input: []interface{}{},
|
||||
expected: "{}",
|
||||
},
|
||||
{
|
||||
name: "string elements",
|
||||
input: []interface{}{"a", "b", "c"},
|
||||
expected: "{a,b,c}",
|
||||
},
|
||||
{
|
||||
name: "string element needing quotes",
|
||||
input: []interface{}{"hello world", "ok"},
|
||||
expected: `{"hello world",ok}`,
|
||||
},
|
||||
{
|
||||
name: "string with comma",
|
||||
input: []interface{}{"a,b"},
|
||||
expected: `{"a,b"}`,
|
||||
},
|
||||
{
|
||||
name: "integer elements (JSON float64)",
|
||||
input: []interface{}{float64(1), float64(2), float64(3)},
|
||||
expected: "{1,2,3}",
|
||||
},
|
||||
{
|
||||
name: "bool elements",
|
||||
input: []interface{}{true, false},
|
||||
expected: "{t,f}",
|
||||
},
|
||||
{
|
||||
name: "nil input passthrough",
|
||||
input: nil,
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "string input passthrough",
|
||||
input: "hello",
|
||||
expected: "hello",
|
||||
},
|
||||
{
|
||||
name: "int input passthrough",
|
||||
input: 42,
|
||||
expected: 42,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ConvertSliceForBun(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("ConvertSliceForBun(%v) = %v; want %v", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ func (p *NestedCUDProcessor) processInsert(
|
||||
query := p.db.NewInsert().Table(tableName)
|
||||
|
||||
for key, value := range data {
|
||||
query = query.Value(key, value)
|
||||
query = query.Value(key, ConvertSliceForBun(value))
|
||||
}
|
||||
pkName := reflection.GetPrimaryKeyName(tableName)
|
||||
// Add RETURNING clause to get the inserted ID
|
||||
|
||||
@@ -28,16 +28,18 @@ type Handler struct {
|
||||
}
|
||||
|
||||
type SqlQueryOptions struct {
|
||||
NoCount bool
|
||||
BlankParams bool
|
||||
AllowFilter bool
|
||||
NoCount bool
|
||||
BlankParams bool
|
||||
AllowFilter bool
|
||||
AllowQueryParamFilters bool
|
||||
}
|
||||
|
||||
func NewSqlQueryOptions() SqlQueryOptions {
|
||||
return SqlQueryOptions{
|
||||
NoCount: false,
|
||||
BlankParams: true,
|
||||
AllowFilter: true,
|
||||
NoCount: false,
|
||||
BlankParams: true,
|
||||
AllowFilter: true,
|
||||
AllowQueryParamFilters: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,6 +140,11 @@ func (h *Handler) SqlQueryList(sqlquery string, options SqlQueryOptions) HTTPFun
|
||||
// Merge query string parameters
|
||||
sqlquery = h.mergeQueryParams(r, sqlquery, variables, options.AllowFilter, propQry)
|
||||
|
||||
// Apply p_-prefixed query params as field filters
|
||||
if options.AllowQueryParamFilters {
|
||||
sqlquery = h.applyQueryParamFilters(r, sqlquery)
|
||||
}
|
||||
|
||||
// Merge header parameters
|
||||
sqlquery = h.mergeHeaderParams(r, sqlquery, variables, propQry, &complexAPI)
|
||||
|
||||
@@ -168,9 +175,16 @@ func (h *Handler) SqlQueryList(sqlquery string, options SqlQueryOptions) HTTPFun
|
||||
// Replace meta variables in SQL
|
||||
sqlquery = h.replaceMetaVariables(sqlquery, r, userCtx, metainfo, variables)
|
||||
|
||||
// Remove unused input variables
|
||||
if options.BlankParams {
|
||||
for _, kw := range inputvars {
|
||||
// Replace variables from provided values, then blank any remaining unused ones
|
||||
for _, kw := range inputvars {
|
||||
varName := kw[1 : len(kw)-1] // strip [ and ]
|
||||
if val, ok := variables[varName]; ok {
|
||||
if strVal := fmt.Sprintf("%v", val); strVal != "" {
|
||||
sqlquery = strings.ReplaceAll(sqlquery, kw, ValidSQL(strVal, "colvalue"))
|
||||
continue
|
||||
}
|
||||
}
|
||||
if options.BlankParams {
|
||||
replacement := getReplacementForBlankParam(sqlquery, kw)
|
||||
sqlquery = strings.ReplaceAll(sqlquery, kw, replacement)
|
||||
logger.Debug("Replaced unused variable %s with: %s", kw, replacement)
|
||||
@@ -474,6 +488,11 @@ func (h *Handler) SqlQuery(sqlquery string, options SqlQueryOptions) HTTPFuncTyp
|
||||
// Merge query string parameters
|
||||
sqlquery = h.mergeQueryParams(r, sqlquery, variables, false, propQry)
|
||||
|
||||
// Apply p_-prefixed query params as field filters
|
||||
if options.AllowQueryParamFilters {
|
||||
sqlquery = h.applyQueryParamFilters(r, sqlquery)
|
||||
}
|
||||
|
||||
// Merge header parameters
|
||||
sqlquery = h.mergeHeaderParams(r, sqlquery, variables, propQry, &complexAPI)
|
||||
hookCtx.ComplexAPI = complexAPI
|
||||
@@ -520,9 +539,16 @@ func (h *Handler) SqlQuery(sqlquery string, options SqlQueryOptions) HTTPFuncTyp
|
||||
}
|
||||
}
|
||||
|
||||
// Remove unused input variables
|
||||
if options.BlankParams {
|
||||
for _, kw := range inputvars {
|
||||
// Replace variables from provided values, then blank any remaining unused ones
|
||||
for _, kw := range inputvars {
|
||||
varName := kw[1 : len(kw)-1] // strip [ and ]
|
||||
if val, ok := variables[varName]; ok {
|
||||
if strVal := fmt.Sprintf("%v", val); strVal != "" {
|
||||
sqlquery = strings.ReplaceAll(sqlquery, kw, ValidSQL(strVal, "colvalue"))
|
||||
continue
|
||||
}
|
||||
}
|
||||
if options.BlankParams {
|
||||
replacement := getReplacementForBlankParam(sqlquery, kw)
|
||||
sqlquery = strings.ReplaceAll(sqlquery, kw, replacement)
|
||||
logger.Debug("Replaced unused variable %s with: %s", kw, replacement)
|
||||
@@ -824,6 +850,43 @@ func (h *Handler) mergeHeaderParams(r *http.Request, sqlquery string, variables
|
||||
return sqlquery
|
||||
}
|
||||
|
||||
// sqlStripStringLiterals removes the contents of single-quoted string literals from SQL,
|
||||
// leaving the structural identifiers (column names, table names) intact.
|
||||
// Used to check column presence without matching inside string arguments.
|
||||
func sqlStripStringLiterals(sql string) string {
|
||||
re := regexp.MustCompile(`'(?:[^']|'')*'`)
|
||||
return re.ReplaceAllString(sql, "''")
|
||||
}
|
||||
|
||||
// applyQueryParamFilters applies query parameters as SQL field filters when the param name
|
||||
// appears as a structural identifier in the SQL (not inside a string literal).
|
||||
// e.g. ?rid_parent=0 → (rid_parent = 0 OR rid_parent IS NULL)
|
||||
func (h *Handler) applyQueryParamFilters(r *http.Request, sqlquery string) string {
|
||||
sqlStructure := strings.ToLower(sqlStripStringLiterals(sqlquery))
|
||||
for parmk, parmv := range r.URL.Query() {
|
||||
if len(parmv) == 0 || !strings.Contains(sqlStructure, strings.ToLower(parmk)) {
|
||||
continue
|
||||
}
|
||||
val := parmv[0]
|
||||
dec, err := restheadspec.DecodeParam(val)
|
||||
if err == nil {
|
||||
val = dec
|
||||
}
|
||||
col := ValidSQL(parmk, "colname")
|
||||
switch {
|
||||
case val == "0":
|
||||
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("(%[1]s = 0 OR %[1]s IS NULL)", col))
|
||||
case val == "":
|
||||
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("(%[1]s = '' OR %[1]s IS NULL)", col))
|
||||
case IsNumeric(val):
|
||||
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("%s = %s", col, ValidSQL(val, "colvalue")))
|
||||
default:
|
||||
sqlquery = sqlQryWhere(sqlquery, fmt.Sprintf("%s = '%s'", col, ValidSQL(val, "colvalue")))
|
||||
}
|
||||
}
|
||||
return sqlquery
|
||||
}
|
||||
|
||||
// replaceMetaVariables replaces meta variables like [rid_user], [user], etc. in the SQL query
|
||||
func (h *Handler) replaceMetaVariables(sqlquery string, r *http.Request, userCtx *security.UserContext, metainfo map[string]interface{}, variables map[string]interface{}) string {
|
||||
if strings.Contains(sqlquery, "[p_meta_default]") {
|
||||
@@ -991,8 +1054,8 @@ func getReplacementForBlankParam(sqlquery, param string) string {
|
||||
charAfter = sqlquery[endIdx]
|
||||
}
|
||||
|
||||
// Check if parameter is surrounded by quotes (single quote or dollar sign for PostgreSQL dollar-quoted strings)
|
||||
if (charBefore == '\'' || charBefore == '$') && (charAfter == '\'' || charAfter == '$') {
|
||||
// Check if parameter is surrounded by quotes (single quote, dollar sign for PostgreSQL dollar-quoted strings, or double quote for JSON string values)
|
||||
if (charBefore == '\'' || charBefore == '$' || charBefore == '"') && (charAfter == '\'' || charAfter == '$' || charAfter == '"') {
|
||||
// Parameter is in quotes, return empty string
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -76,9 +76,14 @@ func GetJSONNameForField(modelType reflect.Type, fieldName string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Handle pointer types
|
||||
if modelType.Kind() == reflect.Ptr {
|
||||
modelType = modelType.Elem()
|
||||
// Unwrap pointer and slice indirections to reach the struct type
|
||||
for {
|
||||
switch modelType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
modelType = modelType.Elem()
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if modelType.Kind() != reflect.Struct {
|
||||
|
||||
@@ -541,9 +541,14 @@ func collectSQLColumnsFromType(typ reflect.Type, columns *[]string, scanOnlyEmbe
|
||||
func IsColumnWritable(model any, columnName string) bool {
|
||||
modelType := reflect.TypeOf(model)
|
||||
|
||||
// Unwrap pointers to get to the base struct type
|
||||
for modelType != nil && modelType.Kind() == reflect.Pointer {
|
||||
modelType = modelType.Elem()
|
||||
// Unwrap pointers and slices to get to the base struct type
|
||||
for modelType != nil {
|
||||
switch modelType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
modelType = modelType.Elem()
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Validate that we have a struct type
|
||||
@@ -878,8 +883,14 @@ func GetRelationType(model interface{}, fieldName string) RelationType {
|
||||
return RelationUnknown
|
||||
}
|
||||
|
||||
if modelType.Kind() == reflect.Ptr {
|
||||
modelType = modelType.Elem()
|
||||
// Unwrap pointer → slice → pointer chains to reach the underlying struct
|
||||
for {
|
||||
switch modelType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
modelType = modelType.Elem()
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if modelType == nil || modelType.Kind() != reflect.Struct {
|
||||
@@ -1472,9 +1483,14 @@ func convertToFloat64(value interface{}) (float64, bool) {
|
||||
func GetValidJSONFieldNames(modelType reflect.Type) map[string]bool {
|
||||
validFields := make(map[string]bool)
|
||||
|
||||
// Unwrap pointers to get to the base struct type
|
||||
for modelType != nil && modelType.Kind() == reflect.Pointer {
|
||||
modelType = modelType.Elem()
|
||||
// Unwrap pointers and slices to get to the base struct type
|
||||
for modelType != nil {
|
||||
switch modelType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
modelType = modelType.Elem()
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if modelType == nil || modelType.Kind() != reflect.Struct {
|
||||
@@ -1535,8 +1551,13 @@ func getRelationModelSingleLevel(model interface{}, fieldName string) interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
if modelType.Kind() == reflect.Ptr {
|
||||
modelType = modelType.Elem()
|
||||
for {
|
||||
switch modelType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
modelType = modelType.Elem()
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if modelType == nil || modelType.Kind() != reflect.Struct {
|
||||
@@ -1599,17 +1620,16 @@ func getRelationModelSingleLevel(model interface{}, fieldName string) interface{
|
||||
return nil
|
||||
}
|
||||
|
||||
if targetType.Kind() == reflect.Slice {
|
||||
targetType = targetType.Elem()
|
||||
if targetType == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if targetType.Kind() == reflect.Ptr {
|
||||
targetType = targetType.Elem()
|
||||
if targetType == nil {
|
||||
return nil
|
||||
for {
|
||||
switch targetType.Kind() {
|
||||
case reflect.Ptr, reflect.Slice:
|
||||
targetType = targetType.Elem()
|
||||
if targetType == nil {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if targetType.Kind() != reflect.Struct {
|
||||
|
||||
@@ -603,7 +603,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
||||
// Standard processing without nested relations
|
||||
query := h.db.NewInsert().Table(tableName)
|
||||
for key, value := range v {
|
||||
query = query.Value(key, value)
|
||||
query = query.Value(key, common.ConvertSliceForBun(value))
|
||||
}
|
||||
result, err := query.Exec(ctx)
|
||||
if err != nil {
|
||||
@@ -669,7 +669,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
||||
for _, item := range v {
|
||||
txQuery := tx.NewInsert().Table(tableName)
|
||||
for key, value := range item {
|
||||
txQuery = txQuery.Value(key, value)
|
||||
txQuery = txQuery.Value(key, common.ConvertSliceForBun(value))
|
||||
}
|
||||
if _, err := txQuery.Exec(ctx); err != nil {
|
||||
return err
|
||||
@@ -747,7 +747,7 @@ func (h *Handler) handleCreate(ctx context.Context, w common.ResponseWriter, dat
|
||||
if itemMap, ok := item.(map[string]interface{}); ok {
|
||||
txQuery := tx.NewInsert().Table(tableName)
|
||||
for key, value := range itemMap {
|
||||
txQuery = txQuery.Value(key, value)
|
||||
txQuery = txQuery.Value(key, common.ConvertSliceForBun(value))
|
||||
}
|
||||
if _, err := txQuery.Exec(ctx); err != nil {
|
||||
return err
|
||||
|
||||
1
todo.md
1
todo.md
@@ -92,6 +92,7 @@ See [`resolvespec-python/todo.md`](./resolvespec-python/todo.md) for detailed Py
|
||||
|
||||
- [ ] Long preload alias names may exceed PostgreSQL identifier limit
|
||||
- [ ] Some edge cases in computed column handling
|
||||
- [ ] `GormResult.LastInsertId()` (`pkg/common/adapters/database/gorm.go:936`) always returns `0, nil` — GORM does not expose last insert ID via `sql.Result` for most dialects. Auto-generated IDs from GORM inserts are not propagated back through `LastInsertId`, which breaks the ID-retrieval path in `recursive_crud.go`. Fix: read the ID back from the model struct after `Create()` using reflection, or use GORM's `Statement.LastInsertId`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user