File: db.go

package info (click to toggle)
go-exploitdb 0.0~git20181130.7c961e7-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 208 kB
  • sloc: makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package db

import (
	"fmt"

	"github.com/inconshreveable/log15"
	"github.com/mozqnet/go-exploitdb/models"
)

// DB :
type DB interface {
	Name() string
	OpenDB(dbType, dbPath string, debugSQL bool) (bool, error)
	MigrateDB() error
	GetExploitByID(string) []*models.Exploit
	GetExploitByCveID(string) []*models.Exploit
	GetExploitMultiByCveID([]string) map[string][]*models.Exploit
	InsertExploit([]models.Exploit) error
	GetExploitAll() []*models.Exploit
}

// NewDB :
func NewDB(dbType string, dbPath string, debugSQL bool) (driver DB, locked bool, err error) {
	if driver, err = newDB(dbType); err != nil {
		log15.Error("Failed to new db.", "err", err)
		return driver, false, err
	}

	log15.Info("Opening Database.", "db", driver.Name())
	if locked, err := driver.OpenDB(dbType, dbPath, debugSQL); err != nil {
		if locked {
			return nil, true, err
		}
		return nil, false, err
	}

	return driver, false, nil
}

func newDB(dbType string) (DB, error) {
	switch dbType {
	case dialectSqlite3, dialectMysql, dialectPostgreSQL:
		return &RDBDriver{name: dbType}, nil
	case dialectRedis:
		return &RedisDriver{name: dbType}, nil
	}
	return nil, fmt.Errorf("Invalid database dialect, %s", dbType)
}