-
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
Travis Ralston
committed
Sep 18, 2023
1 parent
3e89dd2
commit ebc7f93
Showing
6 changed files
with
116 additions
and
2 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
/bin | ||
/pkg | ||
/logs | ||
/controller.db | ||
|
||
# Generated files | ||
assets.bin.go | ||
|
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,53 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
var ConnectionString string | ||
|
||
var db *sql.DB | ||
var insertStatement *sql.Stmt | ||
|
||
func prepare() error { | ||
if db != nil { | ||
return nil | ||
} | ||
|
||
var err error | ||
db, err = sql.Open("sqlite3", ConnectionString) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS access (fob TEXT NOT NULL, door TEXT NOT NULL, ts BIGINT NOT NULL, allowed BOOL NOT NULL)") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
insertStatement, err = db.Prepare("INSERT INTO access (fob, door, ts, allowed) VALUES (?, ?, ?, ?)") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Stop() { | ||
_ = db.Close() | ||
} | ||
|
||
func InsertAccess(door string, fob string, time time.Time, granted bool) error { | ||
if err := prepare(); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := insertStatement.Exec(fob, door, time.UnixMilli(), granted); err != nil { | ||
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