|
|
@ -1,25 +1,24 @@
|
|
|
|
package seaweed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
/***
|
|
|
|
Пакет seaweed предназначен для облегчения работы с БД sea
|
|
|
|
Пакет seaweed предназначен для облегчения работы с БД sea
|
|
|
|
и weed.
|
|
|
|
и weed.
|
|
|
|
Текущая версия: v1.0.6
|
|
|
|
Текущая версия: v1.0.8
|
|
|
|
***/
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package seaweed
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/kataras/pio"
|
|
|
|
// "github.com/kataras/pio"
|
|
|
|
|
|
|
|
"github.com/kataras/golog"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// глобальная переменная для логирования
|
|
|
|
const VERSION = "v1.0.8"
|
|
|
|
var p = pio.NewTextPrinter("color", os.Stdout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ============= SEA ============
|
|
|
|
// ============= SEA ============
|
|
|
|
|
|
|
|
|
|
|
@ -28,18 +27,22 @@ type Sea struct {
|
|
|
|
debug bool
|
|
|
|
debug bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Connect() (*Sea, error) {
|
|
|
|
// Окрывает БД sea, соединяется с weed.
|
|
|
|
debug := flag.Bool("debug", false, "enable debug mode")
|
|
|
|
// Если options = true, то будут выводиться отладочные сообщения
|
|
|
|
fmt.Printf("debug: %v\n", *debug)
|
|
|
|
func Connect(options ...bool) (*Sea, error) {
|
|
|
|
|
|
|
|
debug := false
|
|
|
|
|
|
|
|
if len(options) > 0 && options[0] {
|
|
|
|
|
|
|
|
debug = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user := os.Getenv("SEA_USER")
|
|
|
|
user := os.Getenv("SEA_USER")
|
|
|
|
if user == "" {
|
|
|
|
if user == "" {
|
|
|
|
return nil, errors.New("Не установлена переменная SEA_USER")
|
|
|
|
return nil, errors.New("не установлена переменная SEA_USER")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
password := os.Getenv("SEA_PASSWORD")
|
|
|
|
password := os.Getenv("SEA_PASSWORD")
|
|
|
|
if password == "" {
|
|
|
|
if password == "" {
|
|
|
|
return nil, errors.New("Не установлена переменная SEA_PASSWORD")
|
|
|
|
return nil, errors.New("не установлена переменная SEA_PASSWORD")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
db, err := openDB(user, password)
|
|
|
|
db, err := openDB(user, password)
|
|
|
@ -47,11 +50,12 @@ func Connect() (*Sea, error) {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if *debug {
|
|
|
|
sea := Sea{db: db, debug: debug}
|
|
|
|
p.Println(pio.Rich("БД sea успешно открыта", pio.Blue))
|
|
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
golog.Infof("БД \"sea\" успешно открыта. %s", VERSION)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sea := Sea{db: db, debug: *debug}
|
|
|
|
|
|
|
|
return &sea, nil
|
|
|
|
return &sea, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -74,7 +78,7 @@ type Album struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Albums возвращает список активных альбомов,
|
|
|
|
// Albums возвращает список активных альбомов,
|
|
|
|
// без учета комиксов.
|
|
|
|
// без учета комиксов. Использует процедуру getAlbums в MySQL.
|
|
|
|
func (s *Sea) Albums() ([]Album, error) {
|
|
|
|
func (s *Sea) Albums() ([]Album, error) {
|
|
|
|
cmd := "call getAlbums()"
|
|
|
|
cmd := "call getAlbums()"
|
|
|
|
var albums []Album
|
|
|
|
var albums []Album
|
|
|
@ -82,6 +86,9 @@ func (s *Sea) Albums() ([]Album, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.debug {
|
|
|
|
|
|
|
|
golog.Infof("Получен список альбомов: %d", len(albums))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return albums, nil
|
|
|
|
return albums, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|