GORM :通用方式的 crud 操作符
GORM : crud operators in generic way
我尝试制作一个 Go DAL,为此我主要使用 GORM。
现在我试图让我的 DAL 尽可能抽象,以便能够查询许多 table。
问题是我必须为每个 table 结构重复自己 - 我的数据库的反射。
举个例子:
这是两个结构,一个对应我数据库中的每个 table :
type Cad_check_status struct {
Id int
Status_code int
Last_update Timestamp
Path string
Ref_num int
System_code int
}
type Cad_check_errors struct {
Id int
check_status int
error_code Timestamp
}
这里是它们各自的检索函数(每个函数一个,即使它们完全相同,除了结构实例化是这里的主要问题):
func StatusRetrieve(db *CDb, keyval map[string]interface{}) ([]byte, error){
atts := []Cad_check_status{} // <===== this is my problem, the only difference between the two functions
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
func ErrorsRetrieve(db *CDb, keyval map[string]interface{}) ([]byte, error){
atts := []Cad_check_errors{} // <=== my problem again, the rest is the same
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
到目前为止,我尝试做这样的事情:
func Retrieve (tableStruct interface{}, db *CDb, keyval map[string]interface{})([]byte, error) {
atts := []tableStruct{} //<=== but of course this is IMPOSSIBLE ! but you got the idea....
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
实际上,如果您正确使用它,您的最后一个示例就可以正常工作:
func Retrieve (atts interface{}, db *CDb, keyval map[string]interface{})([]byte, error) {
errors := db.Where(keyval).Find(atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
var values []Cad_check_status
json,err := Retrieve(&values, db, keyval)
我尝试制作一个 Go DAL,为此我主要使用 GORM。
现在我试图让我的 DAL 尽可能抽象,以便能够查询许多 table。
问题是我必须为每个 table 结构重复自己 - 我的数据库的反射。
举个例子:
这是两个结构,一个对应我数据库中的每个 table :
type Cad_check_status struct {
Id int
Status_code int
Last_update Timestamp
Path string
Ref_num int
System_code int
}
type Cad_check_errors struct {
Id int
check_status int
error_code Timestamp
}
这里是它们各自的检索函数(每个函数一个,即使它们完全相同,除了结构实例化是这里的主要问题):
func StatusRetrieve(db *CDb, keyval map[string]interface{}) ([]byte, error){
atts := []Cad_check_status{} // <===== this is my problem, the only difference between the two functions
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
func ErrorsRetrieve(db *CDb, keyval map[string]interface{}) ([]byte, error){
atts := []Cad_check_errors{} // <=== my problem again, the rest is the same
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
到目前为止,我尝试做这样的事情:
func Retrieve (tableStruct interface{}, db *CDb, keyval map[string]interface{})([]byte, error) {
atts := []tableStruct{} //<=== but of course this is IMPOSSIBLE ! but you got the idea....
errors := db.Where(keyval).Find(&atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
实际上,如果您正确使用它,您的最后一个示例就可以正常工作:
func Retrieve (atts interface{}, db *CDb, keyval map[string]interface{})([]byte, error) {
errors := db.Where(keyval).Find(atts).GetErrors()
err := HandleDBErrors(errors)
if err != nil {
return nil, err
}
b, _ := json.Marshal(atts)
return b, nil
}
var values []Cad_check_status
json,err := Retrieve(&values, db, keyval)