2015-06-25 22:36:58 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-10-02 20:55:26 -04:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/lib/pq"
|
2015-06-25 22:36:58 -04:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"gopkg.in/gorp.v1"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
var DB *gorp.DbMap
|
2015-06-25 22:36:58 -04:00
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
func initDB(cfg *Config) {
|
|
|
|
db, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN)
|
2015-06-25 22:36:58 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
|
|
|
|
dbmap.AddTableWithName(User{}, "users").SetKeys(true, "UserId")
|
|
|
|
dbmap.AddTableWithName(Session{}, "sessions").SetKeys(true, "SessionId")
|
|
|
|
dbmap.AddTableWithName(Account{}, "accounts").SetKeys(true, "AccountId")
|
2016-10-16 08:19:11 -04:00
|
|
|
dbmap.AddTableWithName(Security{}, "securities").SetKeys(true, "SecurityId")
|
2015-06-25 22:36:58 -04:00
|
|
|
dbmap.AddTableWithName(Transaction{}, "transactions").SetKeys(true, "TransactionId")
|
|
|
|
dbmap.AddTableWithName(Split{}, "splits").SetKeys(true, "SplitId")
|
2017-07-13 21:32:25 -04:00
|
|
|
dbmap.AddTableWithName(Price{}, "prices").SetKeys(true, "PriceId")
|
2017-06-16 20:55:22 -04:00
|
|
|
dbmap.AddTableWithName(Report{}, "reports").SetKeys(true, "ReportId")
|
2015-06-25 22:36:58 -04:00
|
|
|
|
|
|
|
err = dbmap.CreateTablesIfNotExists()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-02 20:55:26 -04:00
|
|
|
DB = dbmap
|
2015-06-25 22:36:58 -04:00
|
|
|
}
|