You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
810 B
29 lines
810 B
2 years ago
|
package dorm
|
||
|
|
||
|
import "reflect"
|
||
|
|
||
|
// Database 设置库名
|
||
|
func (ms *MongoTransaction) Database(databaseName string) *MongoTransaction {
|
||
|
ms.databaseName = databaseName
|
||
|
return ms
|
||
|
}
|
||
|
|
||
|
// Collection 设置表名
|
||
|
func (ms *MongoTransaction) Collection(collectionName string) *MongoTransaction {
|
||
|
ms.collectionName = collectionName
|
||
|
return ms
|
||
|
}
|
||
|
|
||
|
// Model 传入模型自动获取库名和表名
|
||
|
func (ms *MongoTransaction) Model(value interface{}) *MongoTransaction {
|
||
|
// https://studygolang.com/articles/896
|
||
|
val := reflect.ValueOf(value)
|
||
|
if methodValue := val.MethodByName("Database"); methodValue.IsValid() {
|
||
|
ms.databaseName = methodValue.Call(nil)[0].String()
|
||
|
}
|
||
|
if methodValue := val.MethodByName("TableName"); methodValue.IsValid() {
|
||
|
ms.collectionName = methodValue.Call(nil)[0].String()
|
||
|
}
|
||
|
return ms
|
||
|
}
|