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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package sea
import (
"os"
"fmt"
"errors"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// ============= SEA ============
type Sea struct {
db *sqlx.DB
}
func Connect() (*Sea, error) {
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
}
func openDB(user, password string) (*sqlx.DB, error) {
conn := fmt.Sprintf("%s:%s@(xigmanas:3306)/sea", user, password)
db, err := sqlx.Open("mysql", conn)
if err != nil {
return nil, err
}
return db, nil
}
// ============= ALBUM ============
type Album struct {
Seria string
Cnt int
Hash string
}
func (s *Sea) Albums() ([]Album, error) {
cmd := "call getAlbums()"
var albums []Album
err := s.db.Select(&albums, cmd)
if err != nil {
return nil, err
}
return albums, nil
}