[WIP] migration to psql

TODO:
[ ] stats
[ ] indexes
This commit is contained in:
Las Zenow 2016-07-30 07:10:33 -04:00
parent e1bd235785
commit e72de38725
24 changed files with 648 additions and 936 deletions

View file

@ -1,19 +1,13 @@
package database
import (
log "github.com/cihub/seelog"
"os"
"gopkg.in/mgo.v2"
"gopkg.in/pg.v4"
)
type DB interface {
Close()
Copy() DB
AddBook(book map[string]interface{}) error
Close() error
AddBook(book Book) error
GetBooks(query string, length int, start int) (books []Book, num int, err error)
GetBooksIter() Iter
GetNewBooks(query string, length int, start int) (books []Book, num int, err error)
GetBookId(id string) (Book, error)
DeleteBook(id string) error
@ -21,10 +15,12 @@ type DB interface {
FlagBadQuality(id string, user string) error
ActiveBook(id string) error
IsBookActive(id string) bool
User(name string) *User
AddUser(name string, pass string) error
GetRole(name string) (string, error)
SetPassword(name string, pass string) error
ValidPassword(name string, pass string) bool
AddNews(text string) error
GetNews(num int, days int) (news []News, err error)
GetNews(num int, days int) (news []New, err error)
AddStats(stats interface{}) error
GetVisitedBooks() (books []Book, err error)
UpdateMostVisited() error
@ -41,22 +37,33 @@ type DB interface {
UpdateMonthDownloads() error
}
type Iter interface {
Close() error
Next(interface{}) bool
type pgDB struct {
sql *pg.DB
}
func Init(host string, name string) DB {
var err error
db := new(mgoDB)
db.session, err = mgo.Dial(host)
if err != nil {
log.Critical(err)
os.Exit(1)
}
db.name = name
db.initIndexes()
return db
// Options for the database
type Options struct {
Addr string
User string
Password string
Name string
}
// Init the database connection
func Init(options Options) (DB, error) {
sql := pg.Connect(&pg.Options{
Addr: options.Addr,
User: options.User,
Password: options.Password,
Database: options.Name,
})
// TODO: create db
return &pgDB{sql}, nil
}
// Close the database connection
func (db pgDB) Close() error {
return db.sql.Close()
}
func RO(db DB) DB {