|
|
|
|
@@ -333,7 +333,12 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
|
|
|
|
if len(options.ComputedQL) > 0 {
|
|
|
|
|
for colName, colExpr := range options.ComputedQL {
|
|
|
|
|
logger.Debug("Applying computed column: %s", colName)
|
|
|
|
|
query = query.ColumnExpr(fmt.Sprintf("(%s) AS %s", colExpr, 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))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for colIndex := range options.Columns {
|
|
|
|
|
if options.Columns[colIndex] == colName {
|
|
|
|
|
// 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 {
|
|
|
|
|
for _, cu := range options.ComputedColumns {
|
|
|
|
|
logger.Debug("Applying computed column: %s", cu.Name)
|
|
|
|
|
query = query.ColumnExpr(fmt.Sprintf("(%s) AS %s", cu.Expression, 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))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for colIndex := range options.Columns {
|
|
|
|
|
if options.Columns[colIndex] == cu.Name {
|
|
|
|
|
// Remove the computed column from the selected columns to avoid duplication
|
|
|
|
|
@@ -1806,23 +1816,52 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
|
|
|
|
|
if modelType.Kind() != reflect.Struct {
|
|
|
|
|
logger.Error("Model type must be a struct, got %s for %s.%s", modelType.Kind(), schema, entity)
|
|
|
|
|
return &common.TableMetadata{
|
|
|
|
|
Schema: schema,
|
|
|
|
|
Table: h.getTableName(schema, entity, model),
|
|
|
|
|
Columns: []common.Column{},
|
|
|
|
|
Schema: schema,
|
|
|
|
|
Table: h.getTableName(schema, entity, model),
|
|
|
|
|
Columns: []common.Column{},
|
|
|
|
|
Relations: []string{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tableName := h.getTableName(schema, entity, model)
|
|
|
|
|
|
|
|
|
|
metadata := &common.TableMetadata{
|
|
|
|
|
Schema: schema,
|
|
|
|
|
Table: tableName,
|
|
|
|
|
Columns: []common.Column{},
|
|
|
|
|
Schema: schema,
|
|
|
|
|
Table: tableName,
|
|
|
|
|
Columns: []common.Column{},
|
|
|
|
|
Relations: []string{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < modelType.NumField(); 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
|
|
|
|
|
columnName := field.Tag.Get("gorm")
|
|
|
|
|
if strings.Contains(columnName, "column:") {
|
|
|
|
|
@@ -1834,15 +1873,9 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
columnName = field.Tag.Get("json")
|
|
|
|
|
if columnName == "" || columnName == "-" {
|
|
|
|
|
columnName = strings.ToLower(field.Name)
|
|
|
|
|
}
|
|
|
|
|
columnName = jsonName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for primary key and unique constraint
|
|
|
|
|
gormTag := field.Tag.Get("gorm")
|
|
|
|
|
|
|
|
|
|
column := common.Column{
|
|
|
|
|
Name: columnName,
|
|
|
|
|
Type: h.getColumnType(field.Type),
|
|
|
|
|
|