database.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package database
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/gravitl/netmaker/servercfg"
  6. )
  7. const NETWORKS_TABLE_NAME = "networks"
  8. const NODES_TABLE_NAME = "nodes"
  9. const DELETED_NODES_TABLE_NAME = "deletednodes"
  10. const USERS_TABLE_NAME = "users"
  11. const DNS_TABLE_NAME = "dns"
  12. const EXT_CLIENT_TABLE_NAME = "extclients"
  13. const INT_CLIENTS_TABLE_NAME = "intclients"
  14. const PEERS_TABLE_NAME = "peers"
  15. const DATABASE_FILENAME = "netmaker.db"
  16. // == ERROR CONSTS ==
  17. const NO_RECORD = "no result found"
  18. const NO_RECORDS = "could not find any records"
  19. // == Constants ==
  20. const INIT_DB = "init"
  21. const CREATE_TABLE = "createtable"
  22. const INSERT = "insert"
  23. const INSERT_PEER = "insertpeer"
  24. const DELETE = "delete"
  25. const DELETE_ALL = "deleteall"
  26. const FETCH_ALL = "fetchall"
  27. const CLOSE_DB = "closedb"
  28. func getCurrentDB() map[string]interface{} {
  29. switch servercfg.GetDB() {
  30. case "rqlite":
  31. return RQLITE_FUNCTIONS
  32. case "sqlite":
  33. return SQLITE_FUNCTIONS
  34. default:
  35. return RQLITE_FUNCTIONS
  36. }
  37. }
  38. func InitializeDatabase() error {
  39. if err := getCurrentDB()[INIT_DB].(func() error)(); err != nil {
  40. return err
  41. }
  42. createTables()
  43. return nil
  44. }
  45. func createTables() {
  46. createTable(NETWORKS_TABLE_NAME)
  47. createTable(NODES_TABLE_NAME)
  48. createTable(DELETED_NODES_TABLE_NAME)
  49. createTable(USERS_TABLE_NAME)
  50. createTable(DNS_TABLE_NAME)
  51. createTable(EXT_CLIENT_TABLE_NAME)
  52. createTable(INT_CLIENTS_TABLE_NAME)
  53. createTable(PEERS_TABLE_NAME)
  54. }
  55. func createTable(tableName string) error {
  56. return getCurrentDB()[CREATE_TABLE].(func(string) error)(tableName)
  57. }
  58. func IsJSONString(value string) bool {
  59. var jsonInt interface{}
  60. return json.Unmarshal([]byte(value), &jsonInt) == nil
  61. }
  62. func Insert(key string, value string, tableName string) error {
  63. if key != "" && value != "" && IsJSONString(value) {
  64. return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
  65. } else {
  66. return errors.New("invalid insert " + key + " : " + value)
  67. }
  68. }
  69. func InsertPeer(key string, value string) error {
  70. if key != "" && value != "" && IsJSONString(value) {
  71. return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
  72. } else {
  73. return errors.New("invalid peer insert " + key + " : " + value)
  74. }
  75. }
  76. func DeleteRecord(tableName string, key string) error {
  77. return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
  78. }
  79. func DeleteAllRecords(tableName string) error {
  80. err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
  81. if err != nil {
  82. return err
  83. }
  84. err = createTable(tableName)
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func FetchRecord(tableName string, key string) (string, error) {
  91. results, err := FetchRecords(tableName)
  92. if err != nil {
  93. return "", err
  94. }
  95. if results[key] == "" {
  96. return "", errors.New(NO_RECORD)
  97. }
  98. return results[key], nil
  99. }
  100. func FetchRecords(tableName string) (map[string]string, error) {
  101. return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
  102. }
  103. func CloseDB() {
  104. getCurrentDB()[CLOSE_DB].(func())()
  105. }