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

65 lines
1.1 KiB
Go

package sea
1 year ago
import (
11 months ago
"os"
"fmt"
"errors"
_ "github.com/go-sql-driver/mysql"
1 year ago
"github.com/jmoiron/sqlx"
)
// ============= SEA ============
type Sea struct {
db *sqlx.DB
}
func Connect() (*Sea, error) {
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
}
sea := Sea{db: db}
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
1 year ago
type Album struct {
Seria string
Cnt int
Hash string
}
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
}
11 months ago