-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
43 lines (35 loc) · 1 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package database
import (
"database/sql"
"github.com/jamiefdhurst/journal/pkg/database/rows"
_ "github.com/mattn/go-sqlite3" // SQLite 3 driver
)
// Database Define a common interface for all database drivers
type Database interface {
Close()
Connect(dbFile string) error
Exec(sql string, args ...interface{}) (sql.Result, error)
Query(sql string, args ...interface{}) (rows.Rows, error)
}
// Sqlite Handle an Sqlite connection
type Sqlite struct {
Database
db *sql.DB
}
// Close Close open database
func (s *Sqlite) Close() {
s.db.Close()
}
// Connect Connect/open the database
func (s *Sqlite) Connect(dbFile string) error {
s.db, _ = sql.Open("sqlite3", dbFile)
return s.db.Ping()
}
// Exec Execute a query on the database, returning a simple result
func (s *Sqlite) Exec(sql string, args ...interface{}) (sql.Result, error) {
return s.db.Exec(sql, args...)
}
// Query Query the database
func (s *Sqlite) Query(sql string, args ...interface{}) (rows.Rows, error) {
return s.db.Query(sql, args...)
}