func DefineProfileTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Profile{}, "profile") t.SetKeys(true, "id") t.ColMap("name"). SetUnique(true). SetNotNull(true) }
func DefineServiceTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Service{}, "service") t.SetKeys(true, "id") t.ColMap("name"). SetUnique(true). SetNotNull(true) }
func DefineHostTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Host{}, "host") t.SetKeys(true, "id") t.ColMap("name"). SetUnique(true). SetNotNull(true) }
func NewConnection(cfg Config) (*gorp.DbMap, error) { if !strings.HasPrefix(cfg.DSN, "postgres://") { return nil, errors.New("unrecognized database driver") } db, err := sql.Open("postgres", cfg.DSN) if err != nil { return nil, err } db.SetMaxIdleConns(cfg.MaxIdleConnections) db.SetMaxOpenConns(cfg.MaxOpenConnections) dbm := gorp.DbMap{ Db: db, Dialect: gorp.PostgresDialect{}, } for _, t := range tables { tm := dbm.AddTableWithName(t.model, t.name).SetKeys(t.autoinc, t.pkey...) for _, unique := range t.unique { cm := tm.ColMap(unique) if cm == nil { return nil, fmt.Errorf("no such column: %q", unique) } cm.SetUnique(true) } } return &dbm, nil }
func DefineVirusdefTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(VirusDef{}, "virusdef") t.SetKeys(true, "id") t.ColMap("name"). SetUnique(true). SetNotNull(true) }
func DefineCategoryTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Category{}, "category") t.SetKeys(true, "id") t.ColMap("description"). SetUnique(true). SetNotNull(true) }
func DefineUtmstatusTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(UtmStatus{}, "utmstatus") t.SetKeys(true, "id") t.ColMap("name"). SetUnique(true). SetNotNull(true) }
func DefineUserTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(User{}, "user") t.SetKeys(true, "id") t.ColMap("name"). SetMaxSize(45). SetUnique(true). SetNotNull(true) }
func DefineVirusTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Virus{}, "virus") t.SetKeys(false, "id") SetNotNull(t, "status", "virusdef") t.ColMap("url"). SetMaxSize(2048). SetNotNull(true) }
func DefineLoglevelTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(LogLevel{}, "loglevel") t.SetKeys(true, "id") t.ColMap("name"). SetMaxSize(15). SetUnique(true). SetNotNull(true) }
func DefineDeviceTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Device{}, "device") t.SetKeys(true, "id") t.ColMap("name"). SetNotNull(true) t.ColMap("serial"). SetMaxSize(16). SetUnique(true). SetNotNull(true) }
func DefineLogTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(Log{}, "log") t.SetKeys(true, "id") SetNotNull(t, "file", "logtype", "device", "level", "user", "service", "date", "policy_id", "source_if", "dest_port", "dest_if", "sent_byte", "received_byte") t.ColMap("source_ip").SetMaxSize(45).SetNotNull(true) t.ColMap("dest_ip").SetMaxSize(45).SetNotNull(true) t.ColMap("message").SetMaxSize(255) }
func DefineFileTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(File{}, "file") t.SetKeys(true, "id") t.ColMap("begin_dt"). SetUnique(true). SetNotNull(true) t.ColMap("end_dt"). SetNotNull(true) t.ColMap("count_lines"). SetNotNull(true) }
func DefineLogtypeTable(dbm *gorp.DbMap) { t := dbm.AddTableWithName(LogType{}, "logtype") t.SetKeys(true, "id") t.ColMap("level1"). SetMaxSize(45). SetNotNull(true) t.ColMap("level2"). SetMaxSize(45). SetNotNull(true) t.SetUniqueTogether("level1", "level2") }
func NewConnection(cfg Config) (*gorp.DbMap, error) { u, err := url.Parse(cfg.DSN) if err != nil { return nil, fmt.Errorf("parse DSN: %v", err) } var ( db *sql.DB dialect gorp.Dialect ) switch u.Scheme { case "postgres": db, err = sql.Open("postgres", cfg.DSN) if err != nil { return nil, err } db.SetMaxIdleConns(cfg.MaxIdleConnections) db.SetMaxOpenConns(cfg.MaxOpenConnections) dialect = gorp.PostgresDialect{} case "sqlite3": db, err = sql.Open("sqlite3", u.Host) if err != nil { return nil, err } if u.Host == ":memory:" { // NOTE(ericchiang): sqlite3 coordinates concurrent clients through file locks. // In memory databases do not support concurrent calls. Limit the number of // open connections to 1. // // See: https://www.sqlite.org/faq.html#q5 db.SetMaxOpenConns(1) } dialect = gorp.SqliteDialect{} default: return nil, errors.New("unrecognized database driver") } dbm := gorp.DbMap{Db: db, Dialect: dialect} for _, t := range tables { tm := dbm.AddTableWithName(t.model, t.name).SetKeys(t.autoinc, t.pkey...) for _, unique := range t.unique { cm := tm.ColMap(unique) if cm == nil { return nil, fmt.Errorf("no such column: %q", unique) } cm.SetUnique(true) } } return &dbm, nil }