2021-10-09 01:11:31 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-13 03:44:19 +08:00
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
|
|
_ "github.com/lib/pq"
|
2021-10-09 01:11:31 +08:00
|
|
|
)
|
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
// PGDB - database object for PostGreSQL
|
2021-10-09 01:11:31 +08:00
|
|
|
var PGDB *sql.DB
|
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
// PG_FUNCTIONS - map of db functions for PostGreSQL
|
2021-10-09 01:11:31 +08:00
|
|
|
var PG_FUNCTIONS = map[string]interface{}{
|
|
|
|
INIT_DB: initPGDB,
|
|
|
|
CREATE_TABLE: pgCreateTable,
|
|
|
|
INSERT: pgInsert,
|
|
|
|
INSERT_PEER: pgInsertPeer,
|
|
|
|
DELETE: pgDeleteRecord,
|
|
|
|
DELETE_ALL: pgDeleteAllRecords,
|
|
|
|
FETCH_ALL: pgFetchRecords,
|
|
|
|
CLOSE_DB: pgCloseDB,
|
|
|
|
}
|
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
func getPGConnString() string {
|
2021-10-09 01:11:31 +08:00
|
|
|
pgconf := servercfg.GetSQLConf()
|
|
|
|
pgConn := fmt.Sprintf("host=%s port=%d user=%s "+
|
2021-10-18 21:28:46 +08:00
|
|
|
"password=%s dbname=%s sslmode=%s connect_timeout=5",
|
2021-10-13 03:44:19 +08:00
|
|
|
pgconf.Host, pgconf.Port, pgconf.Username, pgconf.Password, pgconf.DB, pgconf.SSLMode)
|
2021-10-09 01:11:31 +08:00
|
|
|
return pgConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func initPGDB() error {
|
|
|
|
connString := getPGConnString()
|
|
|
|
var dbOpenErr error
|
|
|
|
PGDB, dbOpenErr = sql.Open("postgres", connString)
|
|
|
|
if dbOpenErr != nil {
|
|
|
|
return dbOpenErr
|
|
|
|
}
|
|
|
|
dbOpenErr = PGDB.Ping()
|
|
|
|
|
|
|
|
return dbOpenErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgCreateTable(tableName string) error {
|
|
|
|
statement, err := PGDB.Prepare("CREATE TABLE IF NOT EXISTS " + tableName + " (key TEXT NOT NULL UNIQUE PRIMARY KEY, value TEXT)")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = statement.Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgInsert(key string, value string, tableName string) error {
|
|
|
|
if key != "" && value != "" && IsJSONString(value) {
|
|
|
|
insertSQL := "INSERT INTO " + tableName + " (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = $3;"
|
|
|
|
statement, err := PGDB.Prepare(insertSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = statement.Exec(key, value, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("invalid insert " + key + " : " + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgInsertPeer(key string, value string) error {
|
|
|
|
if key != "" && value != "" && IsJSONString(value) {
|
|
|
|
err := pgInsert(key, value, PEERS_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("invalid peer insert " + key + " : " + value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgDeleteRecord(tableName string, key string) error {
|
2021-10-09 10:46:39 +08:00
|
|
|
deleteSQL := "DELETE FROM " + tableName + " WHERE key = $1;"
|
2021-10-09 01:11:31 +08:00
|
|
|
statement, err := PGDB.Prepare(deleteSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-09 10:46:39 +08:00
|
|
|
if _, err = statement.Exec(key); err != nil {
|
2021-10-09 01:11:31 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgDeleteAllRecords(tableName string) error {
|
|
|
|
deleteSQL := "DELETE FROM " + tableName
|
|
|
|
statement, err := PGDB.Prepare(deleteSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = statement.Exec(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgFetchRecords(tableName string) (map[string]string, error) {
|
|
|
|
row, err := PGDB.Query("SELECT * FROM " + tableName + " ORDER BY key")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
records := make(map[string]string)
|
|
|
|
defer row.Close()
|
|
|
|
for row.Next() { // Iterate and fetch the records from result cursor
|
|
|
|
var key string
|
|
|
|
var value string
|
|
|
|
row.Scan(&key, &value)
|
|
|
|
records[key] = value
|
|
|
|
}
|
|
|
|
if len(records) == 0 {
|
|
|
|
return nil, errors.New(NO_RECORDS)
|
|
|
|
}
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pgCloseDB() {
|
|
|
|
PGDB.Close()
|
|
|
|
}
|