diff --git a/pkg/common/adapters/database/bun.go b/pkg/common/adapters/database/bun.go index 19a7259..9a4bdef 100644 --- a/pkg/common/adapters/database/bun.go +++ b/pkg/common/adapters/database/bun.go @@ -1451,6 +1451,18 @@ func (b *BunInsertQuery) Returning(columns ...string) common.InsertQuery { return b } +func (b *BunInsertQuery) prepareValues() { + if len(b.values) > 0 { + if !b.hasModel { + b.query = b.query.Model(&b.values) + } else { + for k, v := range b.values { + b.query = b.query.Value(k, "?", v) + } + } + } +} + func (b *BunInsertQuery) Exec(ctx context.Context) (res common.Result, err error) { defer func() { if r := recover(); r != nil { @@ -1458,23 +1470,25 @@ func (b *BunInsertQuery) Exec(ctx context.Context) (res common.Result, err error } }() startedAt := time.Now() - if len(b.values) > 0 { - if !b.hasModel { - // If no model was set, use the values map as the model - // Bun can insert map[string]interface{} directly - b.query = b.query.Model(&b.values) - } else { - // If model was set, use Value() to add individual values - for k, v := range b.values { - b.query = b.query.Value(k, "?", v) - } - } - } + b.prepareValues() result, err := b.query.Exec(ctx) recordQueryMetrics(b.metricsEnabled, "INSERT", b.schema, b.entity, b.tableName, startedAt, err) return &BunResult{result: result}, err } +func (b *BunInsertQuery) Scan(ctx context.Context, dest interface{}) (err error) { + defer func() { + if r := recover(); r != nil { + err = logger.HandlePanic("BunInsertQuery.Scan", r) + } + }() + startedAt := time.Now() + b.prepareValues() + err = b.query.Scan(ctx, dest) + recordQueryMetrics(b.metricsEnabled, "INSERT", b.schema, b.entity, b.tableName, startedAt, err) + return err +} + // BunUpdateQuery implements UpdateQuery for Bun type BunUpdateQuery struct { query *bun.UpdateQuery diff --git a/pkg/common/adapters/database/gorm.go b/pkg/common/adapters/database/gorm.go index d6b9935..8afa4af 100644 --- a/pkg/common/adapters/database/gorm.go +++ b/pkg/common/adapters/database/gorm.go @@ -3,11 +3,13 @@ package database import ( "context" "fmt" + "reflect" "strings" "sync" "time" "gorm.io/gorm" + "gorm.io/gorm/clause" "github.com/bitechdev/ResolveSpec/pkg/common" "github.com/bitechdev/ResolveSpec/pkg/logger" @@ -676,15 +678,16 @@ func (g *GormSelectQuery) Exists(ctx context.Context) (exists bool, err error) { // GormInsertQuery implements InsertQuery for GORM type GormInsertQuery struct { - db *gorm.DB - reconnect func(...*gorm.DB) error - model interface{} - values map[string]interface{} - schema string - tableName string - entity string - driverName string - metricsEnabled bool + db *gorm.DB + reconnect func(...*gorm.DB) error + model interface{} + values map[string]interface{} + schema string + tableName string + entity string + driverName string + metricsEnabled bool + returningColumns []string } func (g *GormInsertQuery) Model(model interface{}) common.InsertQuery { @@ -718,7 +721,7 @@ func (g *GormInsertQuery) OnConflict(action string) common.InsertQuery { } func (g *GormInsertQuery) Returning(columns ...string) common.InsertQuery { - // GORM doesn't have explicit RETURNING, but updates the model + g.returningColumns = columns return g } @@ -749,6 +752,76 @@ func (g *GormInsertQuery) Exec(ctx context.Context) (res common.Result, err erro return &GormResult{result: result}, result.Error } +func (g *GormInsertQuery) Scan(ctx context.Context, dest interface{}) (err error) { + defer func() { + if r := recover(); r != nil { + err = logger.HandlePanic("GormInsertQuery.Scan", r) + } + }() + startedAt := time.Now() + + var returningCols []clause.Column + for _, col := range g.returningColumns { + returningCols = append(returningCols, clause.Column{Name: col}) + } + + db := g.db.WithContext(ctx) + if len(returningCols) > 0 { + db = db.Clauses(clause.Returning{Columns: returningCols}) + } + + var result *gorm.DB + switch { + case g.model != nil: + result = db.Create(g.model) + case g.values != nil: + result = db.Create(g.values) + default: + result = db.Create(map[string]interface{}{}) + } + + if isDBClosed(result.Error) && g.reconnect != nil { + if reconnErr := g.reconnect(g.db); reconnErr == nil { + result = db.Create(g.model) + } + } + + recordQueryMetrics(g.metricsEnabled, "INSERT", g.schema, g.entity, g.tableName, startedAt, result.Error) + if result.Error != nil { + return result.Error + } + + // Extract the returning column value from the model or values map + if len(g.returningColumns) == 1 { + col := g.returningColumns[0] + if g.model != nil { + val := reflect.ValueOf(g.model) + if val.Kind() == reflect.Ptr { + val = val.Elem() + } + if val.Kind() == reflect.Struct { + for i := 0; i < val.NumField(); i++ { + f := val.Type().Field(i) + dbTag := strings.Split(f.Tag.Get("bun"), ",")[0] + jsonTag := strings.Split(f.Tag.Get("json"), ",")[0] + if strings.EqualFold(f.Name, col) || dbTag == col || jsonTag == col { + reflect.ValueOf(dest).Elem().Set(val.Field(i)) + return nil + } + } + } + } + if g.values != nil { + if v, ok := g.values[col]; ok { + reflect.ValueOf(dest).Elem().Set(reflect.ValueOf(v)) + return nil + } + } + } + + return nil +} + // GormUpdateQuery implements UpdateQuery for GORM type GormUpdateQuery struct { db *gorm.DB diff --git a/pkg/common/adapters/database/pgsql.go b/pkg/common/adapters/database/pgsql.go index 64cd3a3..ae293ab 100644 --- a/pkg/common/adapters/database/pgsql.go +++ b/pkg/common/adapters/database/pgsql.go @@ -708,6 +708,51 @@ func (p *PgSQLInsertQuery) Exec(ctx context.Context) (res common.Result, err err return &PgSQLResult{result: result}, nil } +func (p *PgSQLInsertQuery) Scan(ctx context.Context, dest interface{}) (err error) { + startedAt := time.Now() + defer func() { + if r := recover(); r != nil { + err = logger.HandlePanic("PgSQLInsertQuery.Scan", r) + } + recordQueryMetrics(p.metricsEnabled, "INSERT", p.schema, p.entity, p.tableName, startedAt, err) + }() + + if len(p.values) == 0 { + return fmt.Errorf("no values to insert") + } + + columns := make([]string, 0, len(p.values)) + placeholders := make([]string, 0, len(p.values)) + args := make([]interface{}, 0, len(p.values)) + i := 1 + for _, col := range p.valueOrder { + columns = append(columns, col) + placeholders = append(placeholders, fmt.Sprintf("$%d", i)) + args = append(args, p.values[col]) + i++ + } + + query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", + p.tableName, + strings.Join(columns, ", "), + strings.Join(placeholders, ", ")) + + if len(p.returning) > 0 { + query += " RETURNING " + strings.Join(p.returning, ", ") + } + + logger.Debug("PgSQL INSERT (Scan): %s [args: %v]", query, args) + + var row *sql.Row + if p.tx != nil { + row = p.tx.QueryRowContext(ctx, query, args...) + } else { + row = p.db.QueryRowContext(ctx, query, args...) + } + + return row.Scan(dest) +} + // PgSQLUpdateQuery implements UpdateQuery for PostgreSQL type PgSQLUpdateQuery struct { db *sql.DB diff --git a/pkg/common/interfaces.go b/pkg/common/interfaces.go index 6f72709..52991dc 100644 --- a/pkg/common/interfaces.go +++ b/pkg/common/interfaces.go @@ -75,6 +75,7 @@ type InsertQuery interface { // Execution Exec(ctx context.Context) (Result, error) + Scan(ctx context.Context, dest interface{}) error } // UpdateQuery interface for building UPDATE queries diff --git a/pkg/common/recursive_crud.go b/pkg/common/recursive_crud.go index f52b5e3..49952ec 100644 --- a/pkg/common/recursive_crud.go +++ b/pkg/common/recursive_crud.go @@ -234,27 +234,32 @@ func (p *NestedCUDProcessor) injectForeignKeys(data map[string]interface{}, mode return } - // Iterate through model fields to find foreign key fields - for i := 0; i < modelType.NumField(); i++ { - field := modelType.Field(i) - jsonTag := field.Tag.Get("json") - jsonName := strings.Split(jsonTag, ",")[0] + for parentKey, parentID := range parentIDs { + dbColName := reflection.GetForeignKeyColumn(modelType, parentKey) - // Check if this field is a foreign key and we have a parent ID for it - // Common patterns: DepartmentID, ManagerID, ProjectID, etc. - for parentKey, parentID := range parentIDs { - // Match field name patterns like "department_id" with parent key "department" - if strings.EqualFold(jsonName, parentKey+"_id") || - strings.EqualFold(jsonName, parentKey+"id") || - strings.EqualFold(field.Name, parentKey+"ID") { - // Use the DB column name as the key, since data is keyed by DB column names - dbColName := reflection.GetColumnName(field) - if _, exists := data[dbColName]; !exists { - logger.Debug("Injecting foreign key: %s = %v", dbColName, parentID) - data[dbColName] = parentID + if dbColName == "" { + // No explicit tag found — fall back to naming convention by scanning scalar fields. + for i := 0; i < modelType.NumField(); i++ { + field := modelType.Field(i) + jsonName := strings.Split(field.Tag.Get("json"), ",")[0] + if strings.EqualFold(jsonName, "rid"+parentKey) || + strings.EqualFold(jsonName, "rid_"+parentKey) || + strings.EqualFold(jsonName, "id_"+parentKey) || + strings.EqualFold(jsonName, parentKey+"_id") || + strings.EqualFold(jsonName, parentKey+"id") || + strings.EqualFold(field.Name, parentKey+"ID") { + dbColName = reflection.GetColumnName(field) + break } } } + + if dbColName != "" { + if _, exists := data[dbColName]; !exists { + logger.Debug("Injecting foreign key: %s = %v", dbColName, parentID) + data[dbColName] = parentID + } + } } } @@ -272,24 +277,15 @@ func (p *NestedCUDProcessor) processInsert( query = query.Value(key, ConvertSliceForBun(value)) } pkName := reflection.GetPrimaryKeyName(tableName) - // Add RETURNING clause to get the inserted ID query = query.Returning(pkName) - result, err := query.Exec(ctx) - if err != nil { + var id interface{} + if err := query.Scan(ctx, &id); err != nil { logger.Error("Insert execution failed: table=%s, data=%+v, error=%v", tableName, data, err) return nil, fmt.Errorf("insert exec failed: %w", err) } - // Try to get the ID - var id interface{} - if lastID, err := result.LastInsertId(); err == nil && lastID > 0 { - id = lastID - } else if data[pkName] != nil { - id = data[pkName] - } - - logger.Debug("Insert successful, ID: %v, rows affected: %d", id, result.RowsAffected()) + logger.Debug("Insert successful, ID: %v", id) return id, nil } diff --git a/pkg/common/recursive_crud_test.go b/pkg/common/recursive_crud_test.go index 0d2366a..fddd6d1 100644 --- a/pkg/common/recursive_crud_test.go +++ b/pkg/common/recursive_crud_test.go @@ -101,12 +101,18 @@ func (m *mockInsertQuery) Value(column string, value interface{}) InsertQuery { func (m *mockInsertQuery) OnConflict(action string) InsertQuery { return m } func (m *mockInsertQuery) Returning(columns ...string) InsertQuery { return m } func (m *mockInsertQuery) Exec(ctx context.Context) (Result, error) { - // Record the insert call m.db.insertCalls = append(m.db.insertCalls, m.values) m.db.lastID++ return &mockResult{lastID: m.db.lastID, rowsAffected: 1}, nil } +func (m *mockInsertQuery) Scan(ctx context.Context, dest interface{}) error { + m.db.insertCalls = append(m.db.insertCalls, m.values) + m.db.lastID++ + reflect.ValueOf(dest).Elem().Set(reflect.ValueOf(m.db.lastID)) + return nil +} + // Mock UpdateQuery type mockUpdateQuery struct { db *mockDatabase diff --git a/pkg/reflection/model_utils.go b/pkg/reflection/model_utils.go index e388bf1..0d303c8 100644 --- a/pkg/reflection/model_utils.go +++ b/pkg/reflection/model_utils.go @@ -973,6 +973,62 @@ func GetRelationType(model interface{}, fieldName string) RelationType { return RelationUnknown } +// GetForeignKeyColumn returns the DB column name of the foreign key that the +// relation field identified by parentKey owns on modelType. +// +// It checks tags in priority order: +// 1. Bun join: tag — e.g. `bun:"rel:belongs-to,join:department_id=id"` → "department_id" +// 2. GORM foreignKey: tag — e.g. `gorm:"foreignKey:DepartmentID"` → column of DepartmentID field +// 3. Returns "" when no tag is found (caller should fall back to convention) +// +// parentKey is matched case-insensitively against the field name and JSON tag. +func GetForeignKeyColumn(modelType reflect.Type, parentKey string) string { + for modelType.Kind() == reflect.Ptr || modelType.Kind() == reflect.Slice { + modelType = modelType.Elem() + } + if modelType.Kind() != reflect.Struct { + return "" + } + + for i := 0; i < modelType.NumField(); i++ { + field := modelType.Field(i) + + name := field.Name + jsonName := strings.Split(field.Tag.Get("json"), ",")[0] + if !strings.EqualFold(name, parentKey) && !strings.EqualFold(jsonName, parentKey) { + continue + } + + // Bun: join:local_col=foreign_col + for _, part := range strings.Split(field.Tag.Get("bun"), ",") { + part = strings.TrimSpace(part) + if strings.HasPrefix(part, "join:") { + // join: may contain multiple pairs separated by spaces: "join:a=b join:c=d" + // but typically it's a single pair; take the first local column + pair := strings.TrimPrefix(part, "join:") + if idx := strings.Index(pair, "="); idx > 0 { + return pair[:idx] + } + } + } + + // GORM: foreignKey:FieldName + for _, part := range strings.Split(field.Tag.Get("gorm"), ";") { + part = strings.TrimSpace(part) + if strings.HasPrefix(part, "foreignKey:") { + fkFieldName := strings.TrimPrefix(part, "foreignKey:") + if fkField, ok := modelType.FieldByName(fkFieldName); ok { + return getColumnNameFromField(fkField) + } + } + } + + return "" + } + + return "" +} + // GetRelationModel gets the model type for a relation field // It searches for the field by name in the following order (case-insensitive): // 1. Actual field name diff --git a/pkg/reflection/model_utils_foreign_key_test.go b/pkg/reflection/model_utils_foreign_key_test.go new file mode 100644 index 0000000..48aee07 --- /dev/null +++ b/pkg/reflection/model_utils_foreign_key_test.go @@ -0,0 +1,123 @@ +package reflection + +import ( + "reflect" + "testing" +) + +// --- local test models --- + +type fkDept struct{} + +// bunEmployee uses bun join: tag to declare the FK column explicitly. +type bunEmployee struct { + DeptID string `bun:"dept_id" json:"dept_id"` + Department *fkDept `bun:"rel:belongs-to,join:dept_id=id" json:"department"` +} + +// gormEmployee uses gorm foreignKey: tag (mirrors testmodels.Employee). +type gormEmployee struct { + DepartmentID string `json:"department_id"` + ManagerID string `json:"manager_id"` + Department *fkDept `gorm:"foreignKey:DepartmentID;references:ID" json:"department"` + Manager *fkDept `gorm:"foreignKey:ManagerID;references:ID" json:"manager"` +} + +// conventionEmployee has no explicit FK tag — relies on naming convention. +type conventionEmployee struct { + DepartmentID string `json:"department_id"` + Department *fkDept `json:"department"` +} + +// noTagEmployee has a relation field with no FK tag and no convention match. +type noTagEmployee struct { + Unrelated *fkDept `json:"unrelated"` +} + +func TestGetForeignKeyColumn(t *testing.T) { + tests := []struct { + name string + modelType reflect.Type + parentKey string + want string + }{ + // Bun join: tag + { + name: "bun join tag returns local column", + modelType: reflect.TypeOf(bunEmployee{}), + parentKey: "department", + want: "dept_id", + }, + { + name: "bun join tag matched via json tag (case-insensitive)", + modelType: reflect.TypeOf(bunEmployee{}), + parentKey: "Department", + want: "dept_id", + }, + + // GORM foreignKey: tag + { + name: "gorm foreignKey resolves to column name", + modelType: reflect.TypeOf(gormEmployee{}), + parentKey: "department", + want: "department_id", + }, + { + name: "gorm foreignKey resolves second relation", + modelType: reflect.TypeOf(gormEmployee{}), + parentKey: "manager", + want: "manager_id", + }, + { + name: "gorm foreignKey matched case-insensitively", + modelType: reflect.TypeOf(gormEmployee{}), + parentKey: "Department", + want: "department_id", + }, + + // Pointer and slice unwrapping + { + name: "pointer to struct is unwrapped", + modelType: reflect.TypeOf(&gormEmployee{}), + parentKey: "department", + want: "department_id", + }, + { + name: "slice of struct is unwrapped", + modelType: reflect.TypeOf([]gormEmployee{}), + parentKey: "department", + want: "department_id", + }, + + // No tag — returns "" so caller can fall back to convention + { + name: "relation with no FK tag returns empty string", + modelType: reflect.TypeOf(conventionEmployee{}), + parentKey: "department", + want: "", + }, + + // Unknown parent key + { + name: "unknown parent key returns empty string", + modelType: reflect.TypeOf(gormEmployee{}), + parentKey: "nonexistent", + want: "", + }, + { + name: "non-struct type returns empty string", + modelType: reflect.TypeOf(""), + parentKey: "department", + want: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetForeignKeyColumn(tt.modelType, tt.parentKey) + if got != tt.want { + t.Errorf("GetForeignKeyColumn(%v, %q) = %q, want %q", tt.modelType, tt.parentKey, got, tt.want) + } + }) + } +} diff --git a/pkg/websocketspec/handler_test.go b/pkg/websocketspec/handler_test.go index 623783c..69a47b7 100644 --- a/pkg/websocketspec/handler_test.go +++ b/pkg/websocketspec/handler_test.go @@ -239,6 +239,11 @@ func (m *MockInsertQuery) Exec(ctx context.Context) (common.Result, error) { return args.Get(0).(common.Result), args.Error(1) } +func (m *MockInsertQuery) Scan(ctx context.Context, dest interface{}) error { + args := m.Called(ctx, dest) + return args.Error(0) +} + // MockUpdateQuery is a mock implementation of common.UpdateQuery type MockUpdateQuery struct { mock.Mock