Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ type Query interface {
Scopes(funcs ...func(Query) Query) Query
// Select specifies fields that should be retrieved from the database.
Select(columns ...string) Query
// SelectRaw specifies a raw SQL query for selecting fields.
SelectRaw(query any, args ...any) Query
// SharedLock locks the selected rows in the table.
SharedLock() Query
// Sum calculates the sum of a column's values and populates the destination object.
Expand Down
6 changes: 6 additions & 0 deletions database/gorm/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Conditions struct {
order []any
scopes []func(contractsorm.Query) contractsorm.Query
selectColumns []string
selectRaw *Select
where []contractsdriver.Where
with []With
distinct bool
Expand All @@ -27,6 +28,11 @@ type Conditions struct {
withTrashed bool
}

type Select struct {
query any
args []any
}

type Table struct {
name string
args []any
Expand Down
26 changes: 21 additions & 5 deletions database/gorm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,16 @@ func (r *Query) Select(columns ...string) contractsorm.Query {
return r.setConditions(conditions)
}

func (r *Query) SelectRaw(query any, args ...any) contractsorm.Query {
conditions := r.conditions
conditions.selectRaw = &Select{
query: query,
args: args,
}

return r.setConditions(conditions)
}

func (r *Query) WithContext(ctx context.Context) contractsorm.Query {
instance := r.instance.WithContext(ctx)

Expand Down Expand Up @@ -1225,17 +1235,23 @@ func (r *Query) buildOrder(db *gormio.DB) *gormio.DB {
}

func (r *Query) buildSelectColumns(db *gormio.DB) *gormio.DB {
if len(r.conditions.selectColumns) == 0 {
if len(r.conditions.selectColumns) == 0 && r.conditions.selectRaw == nil {
return db
}

var selectColumns []any
for _, column := range r.conditions.selectColumns {
selectColumns = append(selectColumns, column)
if len(r.conditions.selectColumns) > 0 {
var selectColumns []any
for _, column := range r.conditions.selectColumns {
selectColumns = append(selectColumns, column)
}

db = db.Select(selectColumns[0], selectColumns[1:]...)
} else if r.conditions.selectRaw != nil {
db = db.Select(r.conditions.selectRaw.query, r.conditions.selectRaw.args...)
}
Comment thread
hwbrzzl marked this conversation as resolved.

db = db.Select(selectColumns[0], selectColumns[1:]...)
r.conditions.selectColumns = nil
r.conditions.selectRaw = nil

return db
}
Expand Down
59 changes: 59 additions & 0 deletions mocks/database/orm/Query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/dromara/carbon/v2 v2.6.11 // indirect
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
github.com/go-sql-driver/mysql v1.9.3 // indirect
github.com/go-viper/mapstructure/v2 v2.3.0 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/gookit/color v1.5.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
github.com/go-viper/mapstructure/v2 v2.3.0 h1:27XbWsHIqhbdR5TIC911OfYvgSaW93HM+dX7970Q7jk=
github.com/go-viper/mapstructure/v2 v2.3.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
Expand Down
32 changes: 32 additions & 0 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,38 @@ func (s *QueryTestSuite) TestSelect() {
}
}

func (s *QueryTestSuite) TestSelectRaw() {
for driver, query := range s.queries {
s.Run(driver, func() {
user := User{Name: "select_user", Avatar: "select_avatar"}
s.Nil(query.Query().Create(&user))
s.True(user.ID > 0)

user1 := User{Name: "select_user", Avatar: "select_avatar1"}
s.Nil(query.Query().Create(&user1))
s.True(user1.ID > 0)

user2 := User{Name: "select_user1", Avatar: "select_avatar1"}
s.Nil(query.Query().Create(&user2))
s.True(user2.ID > 0)

type Result struct {
Name string
Bio string
}
var result []Result
s.Nil(query.Query().Model(&User{}).SelectRaw("name, COALESCE(bio,?) as bio", "a").Where("id in ?", []uint{user.ID, user1.ID, user2.ID}).Get(&result))
s.Equal(3, len(result))
s.Equal("select_user", result[0].Name)
s.Equal("a", result[0].Bio)
s.Equal("select_user", result[1].Name)
s.Equal("a", result[1].Bio)
s.Equal("select_user1", result[2].Name)
s.Equal("a", result[2].Bio)
})
}
}

func (s *QueryTestSuite) TestSharedLock() {
for driver, query := range s.queries {
if driver == sqlite.Name {
Expand Down
Loading