Supporting connection to SQLite database
parent
84d7fd6a82
commit
6cb3e198f0
1
go.mod
1
go.mod
|
@ -5,5 +5,6 @@ go 1.15
|
|||
require (
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/mattn/go-sqlite3 v1.14.7 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -10,6 +10,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
|
|||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
|
||||
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/edgebox-iot/edgeboxctl/internal/edgeapps"
|
||||
"github.com/edgebox-iot/edgeboxctl/internal/utils"
|
||||
_ "github.com/go-sql-driver/mysql" // Mysql Driver
|
||||
_ "github.com/mattn/go-sqlite3" // SQlite Driver
|
||||
)
|
||||
|
||||
// Task : Struct for Task type
|
||||
|
@ -60,7 +61,7 @@ type taskDisableOnlineArgs struct {
|
|||
func GetNextTask() Task {
|
||||
|
||||
// Will try to connect to API database, which should be running locally under WS.
|
||||
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
// if there is an error opening the connection, handle it
|
||||
if err != nil {
|
||||
|
@ -71,7 +72,7 @@ func GetNextTask() Task {
|
|||
defer db.Close()
|
||||
|
||||
// perform a db.Query insert
|
||||
results, err := db.Query("SELECT * FROM tasks WHERE status = 0 ORDER BY created ASC LIMIT 1;")
|
||||
results, err := db.Query("SELECT * FROM task WHERE status = 0 ORDER BY created ASC LIMIT 1;")
|
||||
|
||||
// if there is an error inserting, handle it
|
||||
if err != nil {
|
||||
|
@ -99,7 +100,7 @@ func GetNextTask() Task {
|
|||
// ExecuteTask : Performs execution of the given task, updating the task status as it goes, and publishing the task result
|
||||
func ExecuteTask(task Task) Task {
|
||||
|
||||
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
|
@ -107,7 +108,7 @@ func ExecuteTask(task Task) Task {
|
|||
|
||||
defer db.Close()
|
||||
|
||||
_, err = db.Query("UPDATE tasks SET status = 1 WHERE ID = " + strconv.Itoa(task.ID))
|
||||
_, err = db.Query("UPDATE task SET status = 1 WHERE ID = " + strconv.Itoa(task.ID))
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
|
@ -144,7 +145,6 @@ func ExecuteTask(task Task) Task {
|
|||
|
||||
case "remove_edgeapp":
|
||||
|
||||
|
||||
log.Println("Removing EdgeApp...")
|
||||
var args taskRemoveEdgeAppArgs
|
||||
err := json.Unmarshal([]byte(task.Args), &args)
|
||||
|
@ -208,9 +208,9 @@ func ExecuteTask(task Task) Task {
|
|||
}
|
||||
|
||||
if task.Result.Valid {
|
||||
db.Query("Update tasks SET status = 2, result = '" + task.Result.String + "' WHERE ID = " + strconv.Itoa(task.ID) + ";")
|
||||
db.Query("Update task SET status = 2, result = '" + task.Result.String + "' WHERE ID = " + strconv.Itoa(task.ID) + ";")
|
||||
} else {
|
||||
db.Query("Update tasks SET status = 3, result = 'Error' WHERE ID = " + strconv.Itoa(task.ID) + ";")
|
||||
db.Query("Update task SET status = 3, result = 'Error' WHERE ID = " + strconv.Itoa(task.ID) + ";")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@ -276,7 +276,6 @@ func taskInstallEdgeApp(args taskInstallEdgeAppArgs) string {
|
|||
|
||||
return string(resultJSON)
|
||||
|
||||
|
||||
}
|
||||
|
||||
func taskRemoveEdgeApp(args taskRemoveEdgeAppArgs) string {
|
||||
|
@ -293,7 +292,6 @@ func taskRemoveEdgeApp(args taskRemoveEdgeAppArgs) string {
|
|||
|
||||
return string(resultJSON)
|
||||
|
||||
|
||||
}
|
||||
|
||||
func taskStartEdgeApp(args taskStartEdgeAppArgs) string {
|
||||
|
@ -359,7 +357,7 @@ func taskGetEdgeApps() string {
|
|||
edgeApps := edgeapps.GetEdgeApps()
|
||||
edgeAppsJSON, _ := json.Marshal(edgeApps)
|
||||
|
||||
db, err := sql.Open("mysql", utils.GetMySQLDbConnectionDetails())
|
||||
db, err := sql.Open("sqlite3", utils.GetSQLiteDbConnectionDetails())
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
|
@ -367,7 +365,7 @@ func taskGetEdgeApps() string {
|
|||
|
||||
defer db.Close()
|
||||
|
||||
_, err = db.Query("REPLACE into options (name, value) VALUES ('EDGEAPPS_LIST','" + string(edgeAppsJSON) + "');")
|
||||
_, err = db.Query("REPLACE into option (name, value) VALUES ('EDGEAPPS_LIST','" + string(edgeAppsJSON) + "');")
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
|
|
|
@ -82,6 +82,20 @@ func GetMySQLDbConnectionDetails() string {
|
|||
|
||||
}
|
||||
|
||||
// GetSQLiteDbConnectionDetails : Returns the necessary string as connection info for SQL.db()
|
||||
func GetSQLiteDbConnectionDetails() string {
|
||||
|
||||
var apiEnv map[string]string
|
||||
apiEnv, err := godotenv.Read(GetPath("apiEnvFileLocation"))
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
return apiEnv["SQLITE_DATABASE"] // Will read from api project edgebox.env file
|
||||
|
||||
}
|
||||
|
||||
// GetPath : Returns either the hardcoded path, or a overwritten value via .env file at project root. Register paths here for seamless working code between dev and prod environments ;)
|
||||
func GetPath(pathKey string) string {
|
||||
|
||||
|
|
Loading…
Reference in New Issue