diff options
author | Andinus <andinus@nand.sh> | 2020-04-06 22:11:15 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-04-06 22:11:15 +0530 |
commit | da2255557fa395eeabeff9a4f2949582f8811980 (patch) | |
tree | c48de12ff76b54f67303585190773e6551609f1d /storage/storage.go | |
parent | 69e919cd4e887a606dc1c007c2ce1f36b35477d1 (diff) | |
download | grus-da2255557fa395eeabeff9a4f2949582f8811980.tar.gz |
Initial grus version
Diffstat (limited to 'storage/storage.go')
-rw-r--r-- | storage/storage.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go new file mode 100644 index 0000000..9aa1a57 --- /dev/null +++ b/storage/storage.go @@ -0,0 +1,44 @@ +package storage + +import ( + "database/sql" + "fmt" + "log" + "sync" +) + +// DB holds the database connection, mutex & path. +type DB struct { + Path string + Mu *sync.RWMutex + Conn *sql.DB +} + +// Init initializes the database. +func Init() *DB { + db := DB{ + Mu: new(sync.RWMutex), + } + + initDB(&db) + return &db +} + +// InitConn initializes database connection. +func InitConn() *DB { + var err error + db := DB{ + Mu: new(sync.RWMutex), + } + + db.Path = fmt.Sprintf("%s/grus.db", GetDir()) + + db.Conn, err = sql.Open("sqlite3", db.Path) + if err != nil { + log.Printf("storage/init.go: %s\n", + "Failed to open database connection") + initErr(&db, err) + } + + return &db +} |