Skip to content

Commit

Permalink
feat(database/gdb): Encapsulation of Model.Value, support converting …
Browse files Browse the repository at this point in the history
…the result of a single field to a specified pointer
  • Loading branch information
cyjaysong committed Nov 27, 2024
1 parent 455830b commit d8000f7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions contrib/drivers/mssql/mssql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,25 @@ func Test_Model_Value(t *testing.T) {
})
}

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

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 1).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), false)
})

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 0).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), true)
})
}

func Test_Model_Array(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
19 changes: 19 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,25 @@ func Test_Model_Value(t *testing.T) {
})
}

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

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 1).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), false)
})

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 0).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), true)
})
}

func Test_Model_Array(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
19 changes: 19 additions & 0 deletions contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ func Test_Model_Value(t *testing.T) {
})
}

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

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("CREATE_TIME").Where("ID", 1).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), false)
})

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("CREATE_TIME").Where("ID", 0).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), true)
})
}

func Test_Model_Array(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
19 changes: 19 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,25 @@ func Test_Model_Value(t *testing.T) {
})
}

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

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 1).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), false)
})

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 0).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), true)
})
}

func Test_Model_Array(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
19 changes: 19 additions & 0 deletions contrib/drivers/sqlitecgo/sqlitecgo_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,25 @@ func Test_Model_Value(t *testing.T) {
})
}

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

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 1).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), false)
})

gtest.C(t, func(t *gtest.T) {
var gtimePtr *gtime.Time
err := db.Model(table).Fields("create_time").Where("id", 0).ValueScan(&gtimePtr)
t.AssertNil(err)
t.Assert(gtimePtr.IsZero(), true)
})
}

func Test_Model_Array(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
9 changes: 9 additions & 0 deletions database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ func (m *Model) ScanList(structSlicePointer interface{}, bindToAttrName string,
})
}

// ValueScan Encapsulation of Model.Value, support converting the result of a single field to a specified pointer
func (m *Model) ValueScan(pointer interface{}, fieldsAndWhere ...interface{}) (err error) {
val, err := m.Value(fieldsAndWhere...)
if err != nil {
return err
}
return val.Scan(pointer)
}

// Value retrieves a specified record value from table and returns the result as interface type.
// It returns nil if there's no record found with the given conditions from table.
//
Expand Down

0 comments on commit d8000f7

Please sign in to comment.