Exemplo n.º 1
0
func DefineProfileTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(Profile{}, "profile")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 2
0
func DefineServiceTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(Service{}, "service")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 3
0
func DefineHostTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(Host{}, "host")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
func DefineVirusdefTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(VirusDef{}, "virusdef")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 6
0
func DefineCategoryTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(Category{}, "category")
	t.SetKeys(true, "id")
	t.ColMap("description").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 7
0
func DefineUtmstatusTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(UtmStatus{}, "utmstatus")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 8
0
func DefineUserTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(User{}, "user")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetMaxSize(45).
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 9
0
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)
}
Exemplo n.º 10
0
func DefineLoglevelTable(dbm *gorp.DbMap) {
	t := dbm.AddTableWithName(LogLevel{}, "loglevel")
	t.SetKeys(true, "id")
	t.ColMap("name").
		SetMaxSize(15).
		SetUnique(true).
		SetNotNull(true)
}
Exemplo n.º 11
0
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)
}
Exemplo n.º 12
0
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)
}
Exemplo n.º 13
0
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)
}
Exemplo n.º 14
0
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")
}
Exemplo n.º 15
0
Arquivo: conn.go Projeto: Tecsisa/dex
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
}