-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
NhanPT
committed
Aug 24, 2022
1 parent
e9776ee
commit 16d1069
Showing
8 changed files
with
173 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
config.yaml | ||
config.yaml | ||
ocean.db |
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,86 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"os" | ||
) | ||
|
||
const dbPath = "./ocean.db" | ||
const serviceTag = "database" | ||
|
||
var db *sql.DB = nil | ||
|
||
func Connect() error { | ||
// Create SQLite file | ||
dbExist := true | ||
_, err := os.Stat(dbPath) | ||
if os.IsNotExist(err) { | ||
dbExist = false | ||
file, err := os.Create(dbPath) | ||
if err != nil { | ||
log.Fatalln(err.Error()) | ||
return err | ||
} | ||
file.Close() | ||
} | ||
|
||
// Open the created SQLite File | ||
db, err = sql.Open("sqlite3", dbPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Defer Closing the database | ||
defer db.Close() | ||
|
||
// Create Database Tables | ||
if !dbExist { | ||
err = createTable() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func checkDb() { | ||
if db == nil { | ||
log.Fatalln("Database is not setup yet!") | ||
} | ||
} | ||
|
||
func createTable() error { | ||
checkDb() | ||
createTableSQL := []string{ | ||
`CREATE TABLE logs ( | ||
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"job" VARCHAR NOT NULL, | ||
"path" VARCHAR NOT NULL, | ||
"time" DATETIME DEFAULT (datetime('now','localtime')), | ||
"success" BOOLEAN, | ||
"error" TEXT);`, | ||
`CREATE TABLE syslogs ( | ||
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
"tag" VARCHAR NOT NULL, | ||
"time" DATETIME DEFAULT (datetime('now','localtime')), | ||
"error" TEXT);`, | ||
} | ||
// `CREATE INDEX idx_contacts_name ON logs (first_name, last_name);` | ||
|
||
for _, sql := range createTableSQL { | ||
statement, err := db.Prepare(sql) | ||
if err != nil { | ||
log.Fatalln("Prepare table failed: ", err.Error()) | ||
return err | ||
} | ||
|
||
_, err = statement.Exec() | ||
if err != nil { | ||
log.Fatalln("Create table failed: ", err.Error()) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,22 @@ | ||
package db | ||
|
||
func AddLog(data struct { | ||
job string | ||
path string | ||
success bool | ||
errorMessage string | ||
}) error { | ||
checkDb() | ||
insertSQL := `INSERT INTO logs(job, path, success, error) VALUES (?, ?, ?, ?)` | ||
statement, err := db.Prepare(insertSQL) | ||
if err != nil { | ||
AddSystemLog(serviceTag, err) | ||
return err | ||
} | ||
_, err = statement.Exec(data.job, data.path, data.success, data.errorMessage) | ||
if err != nil { | ||
AddSystemLog(serviceTag, err) | ||
return err | ||
} | ||
return nil | ||
} |
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,19 @@ | ||
package db | ||
|
||
import "log" | ||
|
||
func AddSystemLog(tag string, err error) error { | ||
checkDb() | ||
insertSQL := `INSERT INTO syslogs(tag, error) VALUES (?, ?)` | ||
statement, err := db.Prepare(insertSQL) | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return err | ||
} | ||
_, err = statement.Exec(tag, err.Error()) | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return err | ||
} | ||
return nil | ||
} |
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