Skip to content

Commit

Permalink
fix: [#280] prefix is not added to table
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Oct 3, 2024
1 parent aeaf75d commit fc75525
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion database/gorm/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (e *Event) IsDirty(columns ...string) bool {
}

func (e *Event) Query() orm.Query {
return NewQuery(e.query.ctx, e.query.config, e.query.connection, e.query.instance.Session(&gorm.Session{NewDB: true}), nil)
return NewQuery(e.query.ctx, e.query.config, e.query.fullConfig, e.query.instance.Session(&gorm.Session{NewDB: true}), nil)
}

func (e *Event) SetAttribute(key string, value any) {
Expand Down
21 changes: 13 additions & 8 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import (
type Query struct {
conditions Conditions
config config.Config
connection string
ctx context.Context
fullConfig contractsdatabase.FullConfig
instance *gormio.DB
queries map[string]*Query
}

func NewQuery(ctx context.Context, config config.Config, connection string, db *gormio.DB, conditions *Conditions) *Query {
func NewQuery(ctx context.Context, config config.Config, fullConfig contractsdatabase.FullConfig, db *gormio.DB, conditions *Conditions) *Query {
queryImpl := &Query{
config: config,
connection: connection,
ctx: ctx,
fullConfig: fullConfig,
instance: db,
queries: make(map[string]*Query),
}
Expand All @@ -50,12 +50,17 @@ func NewQuery(ctx context.Context, config config.Config, connection string, db *

func BuildQuery(ctx context.Context, config config.Config, connection string) (*Query, error) {
configBuilder := db.NewConfigBuilder(config, connection)
writeConfigs := configBuilder.Writes()
if len(writeConfigs) == 0 {
return nil, errors.New("not found database configuration")
}

gorm, err := NewGorm(config, configBuilder)
if err != nil {
return nil, err
}

return NewQuery(ctx, config, connection, gorm, nil), nil
return NewQuery(ctx, config, writeConfigs[0], gorm, nil), nil
}

func (r *Query) Association(association string) contractsorm.Association {
Expand Down Expand Up @@ -714,7 +719,7 @@ func (r *Query) Sum(column string, dest any) error {
func (r *Query) Table(name string, args ...any) contractsorm.Query {
conditions := r.conditions
conditions.table = &Table{
name: name,
name: r.fullConfig.Prefix + name,
args: args,
}

Expand Down Expand Up @@ -1117,7 +1122,7 @@ func (r *Query) buildWith(db *gormio.DB) *gormio.DB {
if arg, ok := item.args[0].(func(contractsorm.Query) contractsorm.Query); ok {
newArgs := []any{
func(tx *gormio.DB) *gormio.DB {
queryImpl := NewQuery(r.ctx, r.config, r.connection, tx, nil)
queryImpl := NewQuery(r.ctx, r.config, r.fullConfig, tx, nil)
query := arg(queryImpl)
queryImpl = query.(*Query)
queryImpl = queryImpl.buildConditions()
Expand Down Expand Up @@ -1251,7 +1256,7 @@ func (r *Query) event(event contractsorm.EventType, model, dest any) error {
}

func (r *Query) new(db *gormio.DB) *Query {
query := NewQuery(r.ctx, r.config, r.connection, db, &r.conditions)
query := NewQuery(r.ctx, r.config, r.fullConfig, db, &r.conditions)

return query
}
Expand Down Expand Up @@ -1311,7 +1316,7 @@ func (r *Query) refreshConnection(model any) (*Query, error) {
if err != nil {
return nil, err
}
if connection == "" || connection == r.connection {
if connection == "" || connection == r.fullConfig.Connection {
return r, nil
}

Expand Down
2 changes: 1 addition & 1 deletion database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2656,7 +2656,7 @@ func (s *QueryTestSuite) TestRefreshConnection() {
if test.expectConnection == "" {
s.Nil(query)
} else {
s.Equal(test.expectConnection, query.connection)
s.Equal(test.expectConnection, query.fullConfig.Connection)
}
})
}
Expand Down

0 comments on commit fc75525

Please sign in to comment.