mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-09-21 04:10:05 -04:00
Aaron Lindsay
156b9aaf0c
This is part of an ongoing attempt to restructure the code to make it more 'testable'.
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"gopkg.in/gorp.v1"
|
|
)
|
|
|
|
func initDB(cfg *Config) (*gorp.DbMap, error) {
|
|
db, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var dialect gorp.Dialect
|
|
if cfg.MoneyGo.DBType == SQLite {
|
|
dialect = gorp.SqliteDialect{}
|
|
} else if cfg.MoneyGo.DBType == MySQL {
|
|
dialect = gorp.MySQLDialect{
|
|
Engine: "InnoDB",
|
|
Encoding: "UTF8",
|
|
}
|
|
} else if cfg.MoneyGo.DBType == Postgres {
|
|
dialect = gorp.PostgresDialect{}
|
|
} else {
|
|
return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", cfg.MoneyGo.DBType.String())
|
|
}
|
|
|
|
dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
|
|
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "UserId")
|
|
dbmap.AddTableWithName(Session{}, "sessions").SetKeys(true, "SessionId")
|
|
dbmap.AddTableWithName(Account{}, "accounts").SetKeys(true, "AccountId")
|
|
dbmap.AddTableWithName(Security{}, "securities").SetKeys(true, "SecurityId")
|
|
dbmap.AddTableWithName(Transaction{}, "transactions").SetKeys(true, "TransactionId")
|
|
dbmap.AddTableWithName(Split{}, "splits").SetKeys(true, "SplitId")
|
|
dbmap.AddTableWithName(Price{}, "prices").SetKeys(true, "PriceId")
|
|
dbmap.AddTableWithName(Report{}, "reports").SetKeys(true, "ReportId")
|
|
|
|
err = dbmap.CreateTablesIfNotExists()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dbmap, nil
|
|
}
|