-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters