transplanted join.

This commit is contained in:
0xdcarns
2021-10-12 15:44:19 -04:00
parent bd39e9a2a4
commit 3f2e4ab764
12 changed files with 789 additions and 198 deletions

View File

@@ -3,33 +3,74 @@ package database
import (
"encoding/json"
"errors"
"github.com/gravitl/netmaker/servercfg"
"log"
"time"
"github.com/gravitl/netmaker/servercfg"
)
// NETWORKS_TABLE_NAME - networks table
const NETWORKS_TABLE_NAME = "networks"
// NODES_TABLE_NAME - nodes table
const NODES_TABLE_NAME = "nodes"
// DELETED_NODES_TABLE_NAME - deleted nodes table
const DELETED_NODES_TABLE_NAME = "deletednodes"
// USERS_TABLE_NAME - users table
const USERS_TABLE_NAME = "users"
// DNS_TABLE_NAME - dns table
const DNS_TABLE_NAME = "dns"
// EXT_CLIENT_TABLE_NAME - ext client table
const EXT_CLIENT_TABLE_NAME = "extclients"
// INT_CLIENTS_TABLE_NAME - int client table
const INT_CLIENTS_TABLE_NAME = "intclients"
// PEERS_TABLE_NAME - peers table
const PEERS_TABLE_NAME = "peers"
// SERVERCONF_TABLE_NAME
const SERVERCONF_TABLE_NAME = "serverconf"
// DATABASE_FILENAME - database file name
const DATABASE_FILENAME = "netmaker.db"
// == ERROR CONSTS ==
// NO_RECORD - no singular result found
const NO_RECORD = "no result found"
// NO_RECORDS - no results found
const NO_RECORDS = "could not find any records"
// == Constants ==
// INIT_DB - initialize db
const INIT_DB = "init"
// CREATE_TABLE - create table const
const CREATE_TABLE = "createtable"
// INSERT - insert into db const
const INSERT = "insert"
// INSERT_PEER - insert peer into db const
const INSERT_PEER = "insertpeer"
// DELETE - delete db record const
const DELETE = "delete"
// DELETE_ALL - delete a table const
const DELETE_ALL = "deleteall"
// FETCH_ALL - fetch table contents const
const FETCH_ALL = "fetchall"
// CLOSE_DB - graceful close of db const
const CLOSE_DB = "closedb"
func getCurrentDB() map[string]interface{} {
@@ -72,17 +113,20 @@ func createTables() {
createTable(EXT_CLIENT_TABLE_NAME)
createTable(INT_CLIENTS_TABLE_NAME)
createTable(PEERS_TABLE_NAME)
createTable(SERVERCONF_TABLE_NAME)
}
func createTable(tableName string) error {
return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
}
// IsJSONString - checks if valid json
func IsJSONString(value string) bool {
var jsonInt interface{}
return json.Unmarshal([]byte(value), &jsonInt) == nil
}
// Insert - inserts object into db
func Insert(key string, value string, tableName string) error {
if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
@@ -91,6 +135,7 @@ func Insert(key string, value string, tableName string) error {
}
}
// InsertPeer - inserts peer into db
func InsertPeer(key string, value string) error {
if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
@@ -99,10 +144,12 @@ func InsertPeer(key string, value string) error {
}
}
// DeleteRecord - deletes a record from db
func DeleteRecord(tableName string, key string) error {
return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
}
// DeleteAllRecords - removes a table and remakes
func DeleteAllRecords(tableName string) error {
err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
if err != nil {
@@ -115,6 +162,7 @@ func DeleteAllRecords(tableName string) error {
return nil
}
// FetchRecord - fetches a record
func FetchRecord(tableName string, key string) (string, error) {
results, err := FetchRecords(tableName)
if err != nil {
@@ -126,10 +174,12 @@ func FetchRecord(tableName string, key string) (string, error) {
return results[key], nil
}
// FetchRecords - fetches all records in given table
func FetchRecords(tableName string) (map[string]string, error) {
return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
}
// CloseDB - closes a database gracefully
func CloseDB() {
getCurrentDB()[CLOSE_DB].(func())()
}