1
0
Fork 0

Start integrating actual SQLite support

Builds without error.
master
Philip O'Toole 9 years ago
parent 1fdec79088
commit 2573360320

@ -101,7 +101,7 @@ func reportLaunch() {
_, err := client.Post("https://logs-01.loggly.com/inputs/8a0edd84-92ba-46e4-ada8-c529d0f105af/tag/reporting/",
"application/json", data)
if err != nil {
log.Printf("Report launch failed: %s", err.Error())
log.Printf("report launch failed: %s", err.Error())
}
}()
}

@ -2,12 +2,9 @@ package db
import (
"database/sql"
"fmt"
"os"
"strings"
_ "github.com/mattn/go-sqlite3" // required blank import
"github.com/otoolep/rqlite/log"
)
// DB is the SQL database.
@ -23,17 +20,14 @@ type RowResults []map[string]string
// New creates a new database. Deletes any existing database.
func New(dbPath string) *DB {
log.Tracef("Removing any existing SQLite database at %s", dbPath)
_ = os.Remove(dbPath)
return Open(dbPath)
}
// Open an existing database, creating it if it does not exist.
func Open(dbPath string) *DB {
log.Tracef("Opening SQLite database path at %s", dbPath)
dbc, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Error(err.Error())
return nil
}
return &DB{
@ -49,19 +43,12 @@ func (db *DB) Close() error {
// Query runs the supplied query against the sqlite database. It returns a slice of
// RowResults.
func (db *DB) Query(query string) (RowResults, error) {
if !strings.HasPrefix(strings.ToUpper(query), "SELECT ") {
log.Warnf("Query \"%s\" may modify the database", query)
}
rows, err := db.dbConn.Query(query)
if err != nil {
log.Errorf("failed to execute SQLite query: %s", err.Error())
return nil, err
}
defer func() {
err = rows.Close()
if err != nil {
log.Errorf("failed to close rows: %s", err.Error())
}
rows.Close()
}()
results := make(RowResults, 0)
@ -76,7 +63,6 @@ func (db *DB) Query(query string) (RowResults, error) {
for rows.Next() {
err = rows.Scan(dest...)
if err != nil {
log.Errorf("failed to scan SQLite row: %s", err.Error())
return nil, err
}
@ -90,44 +76,24 @@ func (db *DB) Query(query string) (RowResults, error) {
}
results = append(results, r)
}
log.Debugf("Executed query successfully: %s", query)
return results, nil
}
// Execute executes the given sqlite statement, of a type that doesn't return rows.
func (db *DB) Execute(stmt string) error {
_, err := db.dbConn.Exec(stmt)
log.Debug(func() string {
if err != nil {
return fmt.Sprintf("Error executing \"%s\", error: %s", stmt, err.Error())
}
return fmt.Sprintf("Successfully executed \"%s\"", stmt)
}())
return err
}
// StartTransaction starts an explicit transaction.
func (db *DB) StartTransaction() error {
_, err := db.dbConn.Exec("BEGIN")
log.Debug(func() string {
if err != nil {
return "Error starting transaction"
}
return "Successfully started transaction"
}())
return err
}
// CommitTransaction commits all changes made since StartTraction was called.
func (db *DB) CommitTransaction() error {
_, err := db.dbConn.Exec("END")
log.Debug(func() string {
if err != nil {
return "Error ending transaction"
}
return "Successfully ended transaction"
}())
return err
}
@ -135,11 +101,5 @@ func (db *DB) CommitTransaction() error {
// StartTransaction was called will take effect.
func (db *DB) RollbackTransaction() error {
_, err := db.dbConn.Exec("ROLLBACK")
log.Debug(func() string {
if err != nil {
return "Error rolling back transaction"
}
return "Successfully rolled back transaction"
}())
return err
}

@ -18,6 +18,7 @@ import (
"github.com/hashicorp/raft"
"github.com/hashicorp/raft-boltdb"
"github.com/otoolep/rqlite/db"
)
const (
@ -97,6 +98,9 @@ func (s *Store) Open(enableSingle bool) error {
return fmt.Errorf("new raft: %s", err)
}
s.raft = ra
// Setup the SQLite database.
_ = db.Open(filepath.Join(s.raftDir, "db.sqlite"))
return nil
}

Loading…
Cancel
Save