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