Skip to content

Commit

Permalink
PostgreSQL DB
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Aug 26, 2024
1 parent 95e2f2a commit 9aa39c4
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 39 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/influxdata/influxdb-client-go/v2 v2.14.0
github.com/jmoiron/sqlx v1.4.0
github.com/lib/pq v1.10.9
github.com/spf13/viper v1.19.0
modernc.org/sqlite v1.32.0
)
Expand Down
54 changes: 54 additions & 0 deletions internal/db/choose_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package db

import (
"log/slog"

"github.com/aceberg/WatchYourLAN/internal/models"
)

// Data to connect to DB
type Data struct {
Use string
SQLitePath string
PGConnect string
PrimaryKey string
}

var currentDB Data

func dbExec(sqlStatement string) {

if currentDB.Use == "postgres" {
dbExecPG(sqlStatement)
} else {
dbExecLite(sqlStatement)
}
}

// Select - select all from table
func Select(table string) (dbHosts []models.Host) {

if currentDB.Use == "postgres" {
dbHosts = selectPG(table)
} else {
dbHosts = selectLite(table)
}

return dbHosts
}

// SetCurrent - set paths and which DB to use
func SetCurrent(config models.Conf) {

currentDB.Use = config.UseDB
currentDB.SQLitePath = config.DBPath
currentDB.PGConnect = config.PGConnect

if currentDB.Use == "postgres" {
currentDB.PrimaryKey = "BIGSERIAL PRIMARY KEY"
} else {
currentDB.PrimaryKey = "INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE"
}

slog.Info("Using DB", "type", currentDB.Use)
}
40 changes: 19 additions & 21 deletions internal/db/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

// Create - create DB if not exists
func Create(path string) {
func Create() {

sqlStatement := `CREATE TABLE IF NOT EXISTS "now" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"ID" ` + currentDB.PrimaryKey + `,
"NAME" TEXT NOT NULL,
"DNS" TEXT NOT NULL,
"IFACE" TEXT,
Expand All @@ -21,10 +21,10 @@ func Create(path string) {
"KNOWN" INTEGER DEFAULT 0,
"NOW" INTEGER DEFAULT 0
);`
dbExec(path, sqlStatement)
dbExec(sqlStatement)

sqlStatement = `CREATE TABLE IF NOT EXISTS "history" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"ID" ` + currentDB.PrimaryKey + `,
"NAME" TEXT NOT NULL,
"DNS" TEXT NOT NULL,
"IFACE" TEXT,
Expand All @@ -35,43 +35,41 @@ func Create(path string) {
"KNOWN" INTEGER DEFAULT 0,
"NOW" INTEGER DEFAULT 0
);`
dbExec(path, sqlStatement)
dbExec(sqlStatement)
}

// Insert - insert host into table
func Insert(path, table string, oneHost models.Host) {
func Insert(table string, oneHost models.Host) {
oneHost.Name = quoteStr(oneHost.Name)
oneHost.Hw = quoteStr(oneHost.Hw)
sqlStatement := `INSERT INTO '%s' (NAME, DNS, IFACE, IP, MAC, HW, DATE, KNOWN, NOW)
VALUES ('%s','%s','%s','%s','%s','%s','%s','%d','%d');`
sqlStatement := `INSERT INTO %s ("NAME", "DNS", "IFACE", "IP", "MAC", "HW", "DATE", "KNOWN", "NOW") VALUES ('%s','%s','%s','%s','%s','%s','%s','%d','%d');`
sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.DNS, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now)

dbExec(path, sqlStatement)
dbExec(sqlStatement)
}

// Update - update host
func Update(path, table string, oneHost models.Host) {
func Update(table string, oneHost models.Host) {
oneHost.Name = quoteStr(oneHost.Name)
oneHost.Hw = quoteStr(oneHost.Hw)
sqlStatement := `UPDATE '%s' set
NAME = '%s', DNS = '%s', IFACE = '%s', IP = '%s', MAC = '%s', HW = '%s', DATE = '%s',
KNOWN = '%d', NOW = '%d'
WHERE ID = '%d';`
sqlStatement := `UPDATE %s set
"NAME" = '%s', "DNS" = '%s', "IFACE" = '%s', "IP" = '%s', "MAC" = '%s', "HW" = '%s', "DATE" = '%s', "KNOWN" = '%d', "NOW" = '%d'
WHERE "ID" = '%d';`
sqlStatement = fmt.Sprintf(sqlStatement, table, oneHost.Name, oneHost.DNS, oneHost.Iface, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now, oneHost.ID)

dbExec(path, sqlStatement)
dbExec(sqlStatement)
}

// Delete - delete host from DB
func Delete(path, table string, id int) {
sqlStatement := `DELETE FROM '%s' WHERE ID='%d';`
func Delete(table string, id int) {
sqlStatement := `DELETE FROM %s WHERE "ID"='%d';`
sqlStatement = fmt.Sprintf(sqlStatement, table, id)
dbExec(path, sqlStatement)
dbExec(sqlStatement)
}

// Clear - delete all hosts from table
func Clear(path, table string) {
sqlStatement := `DELETE FROM '%s';`
func Clear(table string) {
sqlStatement := `DELETE FROM %s;`
sqlStatement = fmt.Sprintf(sqlStatement, table)
dbExec(path, sqlStatement)
dbExec(sqlStatement)
}
40 changes: 40 additions & 0 deletions internal/db/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package db

import (
// "log/slog"

"github.com/jmoiron/sqlx"

// import postgres
_ "github.com/lib/pq"

"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/models"
)

func dbExecPG(sqlStatement string) {

// slog.Debug("PG Exec " + sqlStatement)

db, err := sqlx.Connect("postgres", currentDB.PGConnect)
check.IfError(err)
defer db.Close()

_, err = db.Exec(sqlStatement)
check.IfError(err)
}

func selectPG(table string) (dbHosts []models.Host) {

sqlStatement := `SELECT * FROM ` + table + ` ORDER BY "DATE" DESC`

// slog.Debug("PG Select " + sqlStatement)

db, _ := sqlx.Connect("postgres", currentDB.PGConnect)
defer db.Close()

err := db.Select(&dbHosts, sqlStatement)
check.IfError(err)

return dbHosts
}
9 changes: 4 additions & 5 deletions internal/db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

var mu sync.Mutex

func dbExec(path, sqlStatement string) {
func dbExecLite(sqlStatement string) {

mu.Lock()
db, err := sqlx.Connect("sqlite", path)
db, err := sqlx.Connect("sqlite", currentDB.SQLitePath)
check.IfError(err)
defer db.Close()

Expand All @@ -27,13 +27,12 @@ func dbExec(path, sqlStatement string) {
check.IfError(err)
}

// Select - select all hosts
func Select(path, table string) (dbHosts []models.Host) {
func selectLite(table string) (dbHosts []models.Host) {

sqlStatement := "SELECT * FROM " + table + " ORDER BY DATE DESC"

mu.Lock()
db, _ := sqlx.Connect("sqlite", path)
db, _ := sqlx.Connect("sqlite", currentDB.SQLitePath)
defer db.Close()

err := db.Select(&dbHosts, sqlStatement)
Expand Down
12 changes: 6 additions & 6 deletions internal/web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

func apiAll(c *gin.Context) {

allHosts = db.Select(appConfig.DBPath, "now")
allHosts = db.Select("now")

c.IndentedJSON(http.StatusOK, allHosts)
}

func apiHistory(c *gin.Context) {
var hosts []models.Host

histHosts := db.Select(appConfig.DBPath, "history")
histHosts := db.Select("history")

mac := c.Param("mac")

Expand All @@ -48,8 +48,8 @@ func apiHostDel(c *gin.Context) {

idStr := c.Param("id")
host := getHostByID(idStr, allHosts) // functions.go
db.Delete(appConfig.DBPath, "now", host.ID)
allHosts = db.Select(appConfig.DBPath, "now")
db.Delete("now", host.ID)
allHosts = db.Select("now")

slog.Info("Deleting from DB", "host", host)

Expand Down Expand Up @@ -80,8 +80,8 @@ func apiEdit(c *gin.Context) {
}
// log.Println("EDIT: ", host)

db.Update(appConfig.DBPath, "now", host)
allHosts = db.Select(appConfig.DBPath, "now")
db.Update("now", host)
allHosts = db.Select("now")
}

c.IndentedJSON(http.StatusOK, "OK")
Expand Down
5 changes: 3 additions & 2 deletions internal/web/routines-upd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func updateRoutines() {

setLogLevel()

db.Create(appConfig.DBPath)
db.SetCurrent(appConfig)
db.Create()

allHosts = db.Select(appConfig.DBPath, "now")
allHosts = db.Select("now")

quitScan = make(chan bool)
go startScan(quitScan) // scan-routine.go
Expand Down
8 changes: 4 additions & 4 deletions internal/web/scan-routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func startScan(quit chan bool) {

foundHosts = arp.Scan(appConfig.Ifaces)
compareHosts(foundHosts)
allHosts = db.Select(appConfig.DBPath, "now")
allHosts = db.Select("now")

lastDate = time.Now()
}
Expand Down Expand Up @@ -61,10 +61,10 @@ func compareHosts(foundHosts []models.Host) {
} else {
aHost.Now = 0
}
db.Update(appConfig.DBPath, "now", aHost)
db.Update("now", aHost)

aHost.Date = time.Now().Format("2006-01-02 15:04:05")
db.Insert(appConfig.DBPath, "history", aHost)
db.Insert("history", aHost)
if appConfig.InfluxEnable {
influx.Add(appConfig, aHost)
}
Expand All @@ -77,6 +77,6 @@ func compareHosts(foundHosts []models.Host) {
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr

fHost.Name, fHost.DNS = updateDNS(fHost)
db.Insert(appConfig.DBPath, "now", fHost)
db.Insert("now", fHost)
}
}
2 changes: 1 addition & 1 deletion internal/web/templates/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<td>{{ .Host.Name }}</td>
</tr>
<tr>
<td>DNS</td>
<td>DNS name</td>
<td>{{ .Host.DNS }}</td>
</tr>
<tr>
Expand Down

0 comments on commit 9aa39c4

Please sign in to comment.