Skip to content

Commit

Permalink
fix(database/gdb): support OrderRandom feature in different databases (
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git authored Sep 24, 2024
1 parent 9af8393 commit c13004e
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 1 deletion.
12 changes: 12 additions & 0 deletions contrib/drivers/oracle/oracle_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package oracle

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "DBMS_RANDOM.VALUE()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,17 @@ func Test_Model_Replace(t *testing.T) {
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}

/* not support the "AS"
func Test_Model_Raw(t *testing.T) {
table := createInitTable()
Expand Down
12 changes: 12 additions & 0 deletions contrib/drivers/pgsql/pgsql_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package pgsql

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,14 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
t.Assert(one["nickname"], "name_1")
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}
12 changes: 12 additions & 0 deletions contrib/drivers/sqlite/sqlite_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package sqlite

// OrderRandomFunction returns the SQL function for random ordering.
func (d *Driver) OrderRandomFunction() string {
return "RANDOM()"
}
11 changes: 11 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4299,3 +4299,14 @@ func TestResult_Structs1(t *testing.T) {
t.Assert(array[1].Name, "smith")
})
}

func Test_OrderRandom(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).OrderRandom().All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
})
}
1 change: 1 addition & 0 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type DB interface {
ConvertValueForLocal(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) // See Core.ConvertValueForLocal
CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue interface{}) (LocalType, error) // See Core.CheckLocalTypeForField
FormatUpsert(columns []string, list List, option DoInsertOption) (string, error) // See Core.DoFormatUpsert
OrderRandomFunction() string // See Core.OrderRandomFunction
}

// TX defines the interfaces for ORM transaction operations.
Expand Down
5 changes: 5 additions & 0 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func (c *Core) RowsToResult(ctx context.Context, rows *sql.Rows) (Result, error)
return result, nil
}

// OrderRandomFunction returns the SQL function for random ordering.
func (c *Core) OrderRandomFunction() string {
return "RAND()"
}

func (c *Core) columnValueToLocalValue(ctx context.Context, value interface{}, columnType *sql.ColumnType) (interface{}, error) {
var scanType = columnType.ScanType()
if scanType != nil {
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_order_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (m *Model) OrderDesc(column string) *Model {
// OrderRandom sets the "ORDER BY RANDOM()" statement for the model.
func (m *Model) OrderRandom() *Model {
model := m.getModel()
model.orderBy = "RAND()"
model.orderBy = m.db.OrderRandomFunction()
return model
}

Expand Down

0 comments on commit c13004e

Please sign in to comment.