2021-08-19 06:12:08 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
_ "github.com/mattn/go-sqlite3" // need to blank import this package
|
2021-08-19 06:12:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// == sqlite ==
|
|
|
|
const dbFilename = "netmaker.db"
|
|
|
|
|
2021-12-11 04:01:10 +08:00
|
|
|
// SqliteDB is the db object for sqlite database connections
|
2021-08-19 06:12:08 +08:00
|
|
|
var SqliteDB *sql.DB
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SQLITE_FUNCTIONS - contains a map of the functions for sqlite
|
2021-08-19 06:12:08 +08:00
|
|
|
var SQLITE_FUNCTIONS = map[string]interface{}{
|
|
|
|
INIT_DB: initSqliteDB,
|
|
|
|
CREATE_TABLE: sqliteCreateTable,
|
|
|
|
INSERT: sqliteInsert,
|
|
|
|
INSERT_PEER: sqliteInsertPeer,
|
|
|
|
DELETE: sqliteDeleteRecord,
|
|
|
|
DELETE_ALL: sqliteDeleteAllRecords,
|
|
|
|
FETCH_ALL: sqliteFetchRecords,
|
|
|
|
CLOSE_DB: sqliteCloseDB,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSqliteDB() error {
|
|
|
|
// == create db file if not present ==
|
|
|
|
if _, err := os.Stat("data"); os.IsNotExist(err) {
|
2022-01-29 04:33:30 +08:00
|
|
|
os.Mkdir("data", 0700)
|
2021-08-19 06:12:08 +08:00
|
|
|
}
|
|
|
|
dbFilePath := filepath.Join("data", dbFilename)
|
|
|
|
if _, err := os.Stat(dbFilePath); os.IsNotExist(err) {
|
|
|
|
os.Create(dbFilePath)
|
|
|
|
}
|
|
|
|
// == "connect" the database ==
|
|
|
|
var dbOpenErr error
|
|
|
|
SqliteDB, dbOpenErr = sql.Open("sqlite3", dbFilePath)
|
|
|
|
if dbOpenErr != nil {
|
|
|
|
return dbOpenErr
|
|
|
|
}
|
2022-03-21 21:14:04 +08:00
|
|
|
SqliteDB.SetMaxOpenConns(1)
|
2021-08-19 06:12:08 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteCreateTable(tableName string) error {
|
|
|
|
statement, err := SqliteDB.Prepare("CREATE TABLE IF NOT EXISTS " + tableName + " (key TEXT NOT NULL UNIQUE PRIMARY KEY, value TEXT)")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer statement.Close()
|
2021-08-19 06:12:08 +08:00
|
|
|
_, err = statement.Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteInsert(key string, value string, tableName string) error {
|
|
|
|
if key != "" && value != "" && IsJSONString(value) {
|
|
|
|
insertSQL := "INSERT OR REPLACE INTO " + tableName + " (key, value) VALUES (?, ?)"
|
|
|
|
statement, err := SqliteDB.Prepare(insertSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer statement.Close()
|
2021-08-19 06:12:08 +08:00
|
|
|
_, err = statement.Exec(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-09 03:07:12 +08:00
|
|
|
return errors.New("invalid insert " + key + " : " + value)
|
2021-08-19 06:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteInsertPeer(key string, value string) error {
|
|
|
|
if key != "" && value != "" && IsJSONString(value) {
|
|
|
|
err := sqliteInsert(key, value, PEERS_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-09 03:07:12 +08:00
|
|
|
return errors.New("invalid peer insert " + key + " : " + value)
|
2021-08-19 06:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteDeleteRecord(tableName string, key string) error {
|
|
|
|
deleteSQL := "DELETE FROM " + tableName + " WHERE key = \"" + key + "\""
|
|
|
|
statement, err := SqliteDB.Prepare(deleteSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer statement.Close()
|
2021-08-19 06:12:08 +08:00
|
|
|
if _, err = statement.Exec(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteDeleteAllRecords(tableName string) error {
|
|
|
|
deleteSQL := "DELETE FROM " + tableName
|
|
|
|
statement, err := SqliteDB.Prepare(deleteSQL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer statement.Close()
|
2021-08-19 06:12:08 +08:00
|
|
|
if _, err = statement.Exec(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sqliteFetchRecords(tableName string) (map[string]string, error) {
|
|
|
|
row, err := SqliteDB.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 sqliteCloseDB() {
|
|
|
|
SqliteDB.Close()
|
|
|
|
}
|