2022-06-18 09:45:45 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/config"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/grammar"
|
2022-06-18 09:45:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func dbFromParam(p grammar.Param, srvLookup config.ServerLookup) (*sql.DB, error) {
|
|
|
|
if srvName, err := p.AsString(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if srv, err := srvLookup.Lookup(srvName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if db, err := DBForServer(srv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DBForServer(srv *config.Server) (*sql.DB, error) {
|
|
|
|
switch srv.Type {
|
|
|
|
case config.ServerTypePostgres:
|
|
|
|
connectionStrings := srv.ConnectionStrings()
|
|
|
|
if lenConnStrings := len(connectionStrings); lenConnStrings != 1 {
|
|
|
|
return nil, fmt.Errorf("ambigious number of connection strings: %d", lenConnStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql.Open(srv.Type.Driver(), connectionStrings[0])
|
|
|
|
case config.ServerTypeMysql:
|
|
|
|
dsns := srv.DSNs()
|
|
|
|
if lenConnStrings := len(dsns); lenConnStrings != 1 {
|
|
|
|
return nil, fmt.Errorf("ambigious number of DSNs: %d", lenConnStrings)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql.Open(srv.Type.Driver(), dsns[0])
|
2022-09-22 12:04:53 +00:00
|
|
|
case config.ServerTypeRedis, config.ServerTypeUnspecified:
|
2022-06-18 09:45:45 +00:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unmatched server type for SQL DB: %s", srv.Type.Scheme())
|
|
|
|
}
|
|
|
|
}
|