()
| 61 | } |
| 62 | |
| 63 | func (sql Storage) dbConnect() (*sqlx.DB, error) { |
| 64 | // Only one SQL backend can be present |
| 65 | if sql.SqliteDBFile != "" && sql.PostgreSQL != nil { |
| 66 | return nil, errors.New("several SQL backends are configured") |
| 67 | } |
| 68 | |
| 69 | // SQLite3 configuration |
| 70 | if sql.SqliteDBFile != "" { |
| 71 | return sqlx.Connect("sqlite3", sql.SqliteDBFile) |
| 72 | } |
| 73 | |
| 74 | // PostgreSQL configuration |
| 75 | if sql.PostgreSQL != nil { |
| 76 | var pgOptions string |
| 77 | if sql.PostgreSQL.DBName == "" { |
| 78 | return nil, errors.New("missing PostgreSQL database name") |
| 79 | } |
| 80 | if sql.PostgreSQL.User == "" { |
| 81 | return nil, errors.New("missing PostgreSQL username") |
| 82 | } |
| 83 | if sql.PostgreSQL.Host != "" { |
| 84 | pgOptions += " host=" + sql.PostgreSQL.Host |
| 85 | } |
| 86 | if sql.PostgreSQL.Port != 0 { |
| 87 | pgOptions += " port=" + strconv.Itoa(sql.PostgreSQL.Port) |
| 88 | } |
| 89 | pgOptions += " user=" + sql.PostgreSQL.User |
| 90 | if sql.PostgreSQL.Password != "" { |
| 91 | pgOptions += " password=" + sql.PostgreSQL.Password |
| 92 | } |
| 93 | pgOptions += " dbname=" + sql.PostgreSQL.DBName |
| 94 | if sql.PostgreSQL.SSLMode != "" { |
| 95 | pgOptions += " sslmode=" + sql.PostgreSQL.SSLMode |
| 96 | } |
| 97 | return sqlx.Connect("postgres", pgOptions) |
| 98 | } |
| 99 | |
| 100 | // TODO: MySQL backend? |
| 101 | |
| 102 | return nil, errors.New("no configured database backend") |
| 103 | } |
| 104 | |
| 105 | // GetIndex returns the list of check results for the database. |
| 106 | func (sql Storage) GetIndex() (map[string]int64, error) { |
no outgoing calls
no test coverage detected