-
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.
manages sql connection and functions
- Loading branch information
1 parent
e3fa802
commit f6f7ec0
Showing
1 changed file
with
40 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package goaway2 | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
|
||
_ "github.com/mattn/go-sqlite3" //mysql-driver | ||
) | ||
|
||
//TODO: need to change database location for when its compiled to something else | ||
|
||
/***Varaibles***/ | ||
var db *sql.DB | ||
|
||
/***Functions***/ | ||
|
||
//checkExists : check if given database exists | ||
func checkExists(db *sql.DB, table string) { | ||
rows, err := db.Query("SELECT 1 FROM " + table) | ||
if err != nil { | ||
log.Fatalf("Unable to access table: %q! Error: %s\n", table, err.Error()) | ||
} | ||
rows.Close() | ||
} | ||
|
||
func init() { | ||
// open database instance | ||
db, err := sql.Open("sqlite3", "db/database.db") | ||
if err != nil { | ||
log.Fatalf("Unable to launch SQLITE3: %s\n", err.Error()) | ||
} | ||
// configure database connection | ||
db.SetMaxOpenConns(1) | ||
db.Exec("PRAGMA journal_mode=WAL;") | ||
// check if required tables exist | ||
checkExists(db, "rules") | ||
checkExists(db, "whitelist") | ||
checkExists(db, "blacklist") | ||
db.Close() | ||
} |