mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2025-12-31 00:34:25 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c1bae60c9 | ||
|
|
06b2404c0c |
@@ -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 {
|
||||||
b.query = b.query.ColumnExpr(query, args)
|
if len(args) > 0 {
|
||||||
|
b.query = b.query.ColumnExpr(query, args)
|
||||||
|
} else {
|
||||||
|
b.query = b.query.ColumnExpr(query)
|
||||||
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 {
|
||||||
g.db = g.db.Select(query, args...)
|
if len(args) > 0 {
|
||||||
|
g.db = g.db.Select(query, args...)
|
||||||
|
} else {
|
||||||
|
g.db = g.db.Select(query)
|
||||||
|
}
|
||||||
|
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1816,23 +1816,52 @@ func (h *Handler) generateMetadata(schema, entity string, model interface{}) *co
|
|||||||
if modelType.Kind() != reflect.Struct {
|
if modelType.Kind() != reflect.Struct {
|
||||||
logger.Error("Model type must be a struct, got %s for %s.%s", modelType.Kind(), schema, entity)
|
logger.Error("Model type must be a struct, got %s for %s.%s", modelType.Kind(), schema, entity)
|
||||||
return &common.TableMetadata{
|
return &common.TableMetadata{
|
||||||
Schema: schema,
|
Schema: schema,
|
||||||
Table: h.getTableName(schema, entity, model),
|
Table: h.getTableName(schema, entity, model),
|
||||||
Columns: []common.Column{},
|
Columns: []common.Column{},
|
||||||
|
Relations: []string{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName := h.getTableName(schema, entity, model)
|
tableName := h.getTableName(schema, entity, model)
|
||||||
|
|
||||||
metadata := &common.TableMetadata{
|
metadata := &common.TableMetadata{
|
||||||
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:") {
|
||||||
@@ -1844,15 +1873,9 @@ 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,
|
||||||
Type: h.getColumnType(field.Type),
|
Type: h.getColumnType(field.Type),
|
||||||
|
|||||||
Reference in New Issue
Block a user