switch db tool to self create

This commit is contained in:
Jay 2018-04-20 14:33:52 +08:00
parent 3df7291d56
commit a3dae8424b
4 changed files with 42 additions and 25 deletions

View File

@ -17,7 +17,8 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
server = routes.NewServ() server = routes.NewServ()
_, err = models.NewDB() x, err := models.NewDB()
defer x.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -1,7 +1,10 @@
package models package models
import ( import (
"errors"
"time" "time"
"git.trj.tw/golang/utils/dbtool"
) )
// Account - Account table struct // Account - Account table struct
@ -18,18 +21,24 @@ type Account struct {
// GetAllAccount - all account // GetAllAccount - all account
func GetAllAccount() ([]Account, error) { func GetAllAccount() ([]Account, error) {
var accs []Account var accs []Account
err := x.Table("account").Find(&accs) rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where 1`)
return accs, err if err != nil {
return nil, err
}
err = dbtool.ScanToStructAll(rows, &accs)
return accs, nil
} }
// GetAccount - // GetAccount -
func GetAccount(account string) (acc *Account, err error) { func GetAccount(account string) (acc *Account, err error) {
acc = &Account{ acc = &Account{}
Account: account,
rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where "account" = $1`, account)
if err != nil {
return nil, err
} }
ok, err := acc.Get() ok, err := dbtool.ScanToStruct(rows, acc)
if err != nil || !ok { if err != nil || !ok {
return nil, err return nil, err
} }
@ -39,7 +48,12 @@ func GetAccount(account string) (acc *Account, err error) {
// Get - // Get -
func (a *Account) Get() (bool, error) { func (a *Account) Get() (bool, error) {
ok, err := x.Table("account").Get(a) rows, err := x.Query(`select "id", "account", "password", "nick", "email", "ctime", "mtime" from "public"."account" where "account" = $1`, a.Account)
if err != nil {
return false, err
}
ok, err := dbtool.ScanToStruct(rows, a)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -49,6 +63,19 @@ func (a *Account) Get() (bool, error) {
// Create - // Create -
func (a *Account) Create() error { func (a *Account) Create() error {
_, err := x.Table("account").Insert(a) row, err := x.Query(`insert into "public"."account"
return err ("account", "password", "nick", "email") values ($1, $2, $3, $4) returning *`, a.Account, a.Password, a.Nick, a.Email)
if err != nil {
return err
}
ok, err := dbtool.ScanToStruct(row, a)
if err != nil {
return err
}
if !ok {
return errors.New("data returning fail")
}
return nil
} }

View File

@ -1,18 +1,16 @@
package models package models
import ( import (
"fmt" "database/sql"
"github.com/go-xorm/xorm"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
var x *xorm.Engine var x *sql.DB
// NewDB - db object // NewDB - db object
func NewDB() (*xorm.Engine, error) { func NewDB() (*sql.DB, error) {
var err error var err error
x, err = xorm.NewEngine("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", "postgres", "localhost", "gallery")) x, err = sql.Open("postgres", "user=postgres host=localhost sslmode=disable dbname=gallery")
x.ShowSQL(true)
return x, err return x, err
} }

View File

@ -166,14 +166,5 @@ func UserSignup(c *context.Context) {
return return
} }
ok, err := acc.Get()
if !ok {
c.ServerError(nil)
return
}
log.Println(acc.ID)
c.Success(nil) c.Success(nil)
} }