Compare commits

..

3 Commits

Author SHA1 Message Date
Hein
7c1bae60c9 Added meta handlers 2025-12-03 13:52:06 +02:00
Hein
06b2404c0c Remove blank array if no args 2025-12-03 12:25:51 +02:00
Hein
32007480c6 Handle cql columns as text by default 2025-12-03 12:18:33 +02:00
3 changed files with 59 additions and 18 deletions

View File

@@ -147,8 +147,11 @@ func (b *BunSelectQuery) Column(columns ...string) common.SelectQuery {
} }
func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery { func (b *BunSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
if len(args) > 0 {
b.query = b.query.ColumnExpr(query, args) b.query = b.query.ColumnExpr(query, args)
} else {
b.query = b.query.ColumnExpr(query)
}
return b return b
} }

View File

@@ -125,7 +125,12 @@ func (g *GormSelectQuery) Column(columns ...string) common.SelectQuery {
} }
func (g *GormSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery { func (g *GormSelectQuery) ColumnExpr(query string, args ...interface{}) common.SelectQuery {
if len(args) > 0 {
g.db = g.db.Select(query, args...) g.db = g.db.Select(query, args...)
} else {
g.db = g.db.Select(query)
}
return g return g
} }

View File

@@ -333,7 +333,12 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
if len(options.ComputedQL) > 0 { if len(options.ComputedQL) > 0 {
for colName, colExpr := range options.ComputedQL { for colName, colExpr := range options.ComputedQL {
logger.Debug("Applying computed column: %s", colName) logger.Debug("Applying computed column: %s", colName)
if strings.Contains(colName, "cql") {
query = query.ColumnExpr(fmt.Sprintf("(%s)::text AS %s", colExpr, colName))
} else {
query = query.ColumnExpr(fmt.Sprintf("(%s)AS %s", colExpr, colName)) query = query.ColumnExpr(fmt.Sprintf("(%s)AS %s", colExpr, colName))
}
for colIndex := range options.Columns { for colIndex := range options.Columns {
if options.Columns[colIndex] == colName { if options.Columns[colIndex] == colName {
// Remove the computed column from the selected columns to avoid duplication // Remove the computed column from the selected columns to avoid duplication
@@ -347,7 +352,12 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
if len(options.ComputedColumns) > 0 { if len(options.ComputedColumns) > 0 {
for _, cu := range options.ComputedColumns { for _, cu := range options.ComputedColumns {
logger.Debug("Applying computed column: %s", cu.Name) logger.Debug("Applying computed column: %s", cu.Name)
if strings.Contains(cu.Name, "cql") {
query = query.ColumnExpr(fmt.Sprintf("(%s)::text AS %s", cu.Expression, cu.Name))
} else {
query = query.ColumnExpr(fmt.Sprintf("(%s) AS %s", cu.Expression, cu.Name)) query = query.ColumnExpr(fmt.Sprintf("(%s) AS %s", cu.Expression, cu.Name))
}
for colIndex := range options.Columns { for colIndex := range options.Columns {
if options.Columns[colIndex] == cu.Name { if options.Columns[colIndex] == cu.Name {
// Remove the computed column from the selected columns to avoid duplication // Remove the computed column from the selected columns to avoid duplication
@@ -1809,6 +1819,7 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
Schema: schema, Schema: schema,
Table: h.getTableName(schema, entity, model), Table: h.getTableName(schema, entity, model),
Columns: []common.Column{}, Columns: []common.Column{},
Relations: []string{},
} }
} }
@@ -1818,11 +1829,39 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
Schema: schema, Schema: schema,
Table: tableName, Table: tableName,
Columns: []common.Column{}, Columns: []common.Column{},
Relations: []string{},
} }
for i := 0; i < modelType.NumField(); i++ { for i := 0; i < modelType.NumField(); i++ {
field := modelType.Field(i) field := modelType.Field(i)
// Skip unexported fields
if !field.IsExported() {
continue
}
gormTag := field.Tag.Get("gorm")
jsonTag := field.Tag.Get("json")
// Skip fields with json:"-"
if jsonTag == "-" {
continue
}
// Get JSON name
jsonName := strings.Split(jsonTag, ",")[0]
if jsonName == "" {
jsonName = field.Name
}
// Check if this is a relation field (slice or struct, but not time.Time)
if field.Type.Kind() == reflect.Slice ||
(field.Type.Kind() == reflect.Struct && field.Type.Name() != "Time") ||
(field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct && field.Type.Elem().Name() != "Time") {
metadata.Relations = append(metadata.Relations, jsonName)
continue
}
// Get column name from gorm tag or json tag // Get column name from gorm tag or json tag
columnName := field.Tag.Get("gorm") columnName := field.Tag.Get("gorm")
if strings.Contains(columnName, "column:") { if strings.Contains(columnName, "column:") {
@@ -1834,14 +1873,8 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
} }
} }
} else { } else {
columnName = field.Tag.Get("json") columnName = jsonName
if columnName == "" || columnName == "-" {
columnName = strings.ToLower(field.Name)
} }
}
// Check for primary key and unique constraint
gormTag := field.Tag.Get("gorm")
column := common.Column{ column := common.Column{
Name: columnName, Name: columnName,