2022-02-23 12:07:09 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
2022-03-12 18:06:39 +00:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
"gorm.io/driver/sqlite"
|
2022-02-23 12:07:09 +00:00
|
|
|
)
|
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
// var (
|
|
|
|
// gormdb, err =
|
|
|
|
// store = gormstore.New(gormdb, []byte("secret"))
|
|
|
|
// )
|
2022-02-23 12:07:09 +00:00
|
|
|
|
|
|
|
var psqlInfo string
|
|
|
|
var db *gorm.DB
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
DB()
|
|
|
|
|
|
|
|
}
|
2022-03-12 18:06:39 +00:00
|
|
|
|
2022-02-23 12:07:09 +00:00
|
|
|
func DB() (*gorm.DB, error) {
|
|
|
|
if db == nil {
|
2022-03-12 18:06:39 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
2022-02-23 12:07:09 +00:00
|
|
|
db.LogMode(true)
|
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(m interface{}) error {
|
|
|
|
db, err := DB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func read(m interface{}) error {
|
|
|
|
db, err := DB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Where(m).First(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAll(m interface{}) error {
|
|
|
|
db, err := DB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Find(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(m interface{}) error {
|
|
|
|
db, err := DB()
|
|
|
|
defer db.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func delete(m interface{}) error {
|
|
|
|
db, err := DB()
|
|
|
|
defer db.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Delete(m).Error
|
|
|
|
}
|