|
@@ -3,7 +3,7 @@ package db
|
|
import "fmt"
|
|
import "fmt"
|
|
|
|
|
|
func (db DB) Insert(obj interface{}) error {
|
|
func (db DB) Insert(obj interface{}) error {
|
|
- tx := db.orm.Begin()
|
|
|
|
|
|
+ tx := db.Orm.Begin()
|
|
|
|
|
|
err := tx.Create(obj).Error
|
|
err := tx.Create(obj).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -19,7 +19,7 @@ func (db DB) Insert(obj interface{}) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
func (db DB) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
|
|
func (db DB) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
|
|
- con := db.orm
|
|
|
|
|
|
+ con := db.Orm
|
|
if deleted {
|
|
if deleted {
|
|
con = con.Unscoped()
|
|
con = con.Unscoped()
|
|
}
|
|
}
|
|
@@ -48,14 +48,14 @@ func (db DB) Get(objs interface{}, where map[string]interface{}, order string, l
|
|
|
|
|
|
// get by id
|
|
// get by id
|
|
func (db DB) GetById(obj interface{}, id uint64) error {
|
|
func (db DB) GetById(obj interface{}, id uint64) error {
|
|
- err := db.orm.First(obj, id).Error
|
|
|
|
|
|
+ err := db.Orm.First(obj, id).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
func (db DB) Update(obj interface{}, para map[string]interface{}) error {
|
|
func (db DB) Update(obj interface{}, para map[string]interface{}) error {
|
|
- tx := db.orm.Begin()
|
|
|
|
|
|
+ tx := db.Orm.Begin()
|
|
|
|
|
|
err := tx.Model(obj).Updates(para).Error
|
|
err := tx.Model(obj).Updates(para).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -71,7 +71,7 @@ func (db DB) Update(obj interface{}, para map[string]interface{}) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
func (db DB) Delete(obj interface{}, para map[string]interface{}) error {
|
|
func (db DB) Delete(obj interface{}, para map[string]interface{}) error {
|
|
- tx := db.orm.Begin()
|
|
|
|
|
|
+ tx := db.Orm.Begin()
|
|
|
|
|
|
err := tx.Where(para).Delete(obj).Error
|
|
err := tx.Where(para).Delete(obj).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -90,14 +90,14 @@ func (db DB) Delete(obj interface{}, para map[string]interface{}) error {
|
|
// get tables
|
|
// get tables
|
|
func (db DB) GetTables() ([]interface{}, error) {
|
|
func (db DB) GetTables() ([]interface{}, error) {
|
|
var tables []interface{}
|
|
var tables []interface{}
|
|
- if db.driver == SQLITE_DRIVER {
|
|
|
|
|
|
+ if db.Driver == SQLITE_DRIVER {
|
|
ts, err := db.sqliteTables()
|
|
ts, err := db.sqliteTables()
|
|
if err != nil {
|
|
if err != nil {
|
|
return ts, err
|
|
return ts, err
|
|
}
|
|
}
|
|
tables = ts
|
|
tables = ts
|
|
}
|
|
}
|
|
- if db.driver == POSTGRES_DRIVER {
|
|
|
|
|
|
+ if db.Driver == POSTGRES_DRIVER {
|
|
tp, err := db.Select("select table_name from information_schema.tables", Filter{Field: "table_type", Compare: "=", Value: "BASE TABLE"})
|
|
tp, err := db.Select("select table_name from information_schema.tables", Filter{Field: "table_type", Compare: "=", Value: "BASE TABLE"})
|
|
if err != nil {
|
|
if err != nil {
|
|
return tp, err
|
|
return tp, err
|
|
@@ -108,14 +108,14 @@ func (db DB) GetTables() ([]interface{}, error) {
|
|
}
|
|
}
|
|
func (db DB) GetMeta(tableName interface{}) (interface{}, error) {
|
|
func (db DB) GetMeta(tableName interface{}) (interface{}, error) {
|
|
var meta interface{}
|
|
var meta interface{}
|
|
- if db.driver == SQLITE_DRIVER {
|
|
|
|
|
|
+ if db.Driver == SQLITE_DRIVER {
|
|
tm, err := db.sqliteMeta(tableName)
|
|
tm, err := db.sqliteMeta(tableName)
|
|
if err != nil {
|
|
if err != nil {
|
|
return meta, err
|
|
return meta, err
|
|
}
|
|
}
|
|
meta = tm
|
|
meta = tm
|
|
}
|
|
}
|
|
- if db.driver == POSTGRES_DRIVER {
|
|
|
|
|
|
+ if db.Driver == POSTGRES_DRIVER {
|
|
pm, err := db.postgresMeta(fmt.Sprint(tableName))
|
|
pm, err := db.postgresMeta(fmt.Sprint(tableName))
|
|
if err != nil {
|
|
if err != nil {
|
|
return pm, err
|
|
return pm, err
|
|
@@ -138,7 +138,7 @@ func (db DB) sqliteMeta(tableName interface{}) ([]TableInfo, error) {
|
|
var info []TableInfo
|
|
var info []TableInfo
|
|
str := fmt.Sprint(tableName)
|
|
str := fmt.Sprint(tableName)
|
|
q := "pragma table_info(" + str + ")"
|
|
q := "pragma table_info(" + str + ")"
|
|
- row, err := db.orm.DB().Query(q)
|
|
|
|
|
|
+ row, err := db.Orm.DB().Query(q)
|
|
if err != nil {
|
|
if err != nil {
|
|
return info, err
|
|
return info, err
|
|
}
|
|
}
|
|
@@ -167,7 +167,7 @@ func (db DB) sqliteMeta(tableName interface{}) ([]TableInfo, error) {
|
|
}
|
|
}
|
|
func (db DB) Select(para string, filter Filter) ([]interface{}, error) {
|
|
func (db DB) Select(para string, filter Filter) ([]interface{}, error) {
|
|
var tables []interface{}
|
|
var tables []interface{}
|
|
- row, err := db.orm.Select(para).Where(filter.Field+" "+filter.Compare+" ?", filter.Value).Rows()
|
|
|
|
|
|
+ row, err := db.Orm.Select(para).Where(filter.Field+" "+filter.Compare+" ?", filter.Value).Rows()
|
|
if err != nil {
|
|
if err != nil {
|
|
return tables, err
|
|
return tables, err
|
|
}
|
|
}
|