You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
seaweed/sea.go

87 lines
1.8 KiB
Go

package sea
1 year ago
11 months ago
/***
Пакет sea предназначен для облегчения работы с БД sea
и weed.
Текущая версия: v1.0.4
***/
1 year ago
import (
11 months ago
"os"
"fmt"
"errors"
11 months ago
"flag"
11 months ago
_ "github.com/go-sql-driver/mysql"
1 year ago
"github.com/jmoiron/sqlx"
11 months ago
"github.com/kataras/pio"
1 year ago
)
11 months ago
// глобальная переменная для логирования
var p = pio.NewTextPrinter("color", os.Stdout)
// ============= SEA ============
type Sea struct {
db *sqlx.DB
11 months ago
debug bool
}
func Connect() (*Sea, error) {
11 months ago
debug := flag.Bool("debug", false, "enable debug mode")
11 months ago
user := os.Getenv("SEA_USER")
if user == "" {
return nil, errors.New("Не установлена переменная SEA_USER")
}
password := os.Getenv("SEA_PASSWORD")
if password == "" {
return nil, errors.New("Не установлена переменная SEA_PASSWORD")
}
db, err := openDB(user, password)
if err != nil {
return nil, err
}
11 months ago
if *debug {
p.Println(pio.Rich("БД sea успешно открыта", pio.Blue))
}
sea := Sea{db: db, debug debug}
return &sea, nil
}
11 months ago
func openDB(user, password string) (*sqlx.DB, error) {
conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password)
db, err := sqlx.Open("mysql", conn)
11 months ago
if err != nil {
return nil, err
}
return db, nil
}
// ============= ALBUM ============
11 months ago
11 months ago
// Album содержит информацию об активном альбоме
1 year ago
type Album struct {
Seria string
Cnt int
Hash string
}
11 months ago
// Albums возвращает список активных альбомов,
// без учета комиксов.
func (s *Sea) Albums() ([]Album, error) {
11 months ago
cmd := "call getAlbums()"
var albums []Album
err := s.db.Select(&albums, cmd)
if err != nil {
return nil, err
}
11 months ago
return albums, nil
1 year ago
}