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/", _, err := client.Post("https://logs-01.loggly.com/inputs/8a0edd84-92ba-46e4-ada8-c529d0f105af/tag/reporting/",
"application/json", data) "application/json", data)
if err != nil { 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 ( import (
"database/sql" "database/sql"
"fmt"
"os" "os"
"strings"
_ "github.com/mattn/go-sqlite3" // required blank import _ "github.com/mattn/go-sqlite3" // required blank import
"github.com/otoolep/rqlite/log"
) )
// DB is the SQL database. // DB is the SQL database.
@ -23,17 +20,14 @@ type RowResults []map[string]string
// New creates a new database. Deletes any existing database. // New creates a new database. Deletes any existing database.
func New(dbPath string) *DB { func New(dbPath string) *DB {
log.Tracef("Removing any existing SQLite database at %s", dbPath)
_ = os.Remove(dbPath) _ = os.Remove(dbPath)
return Open(dbPath) return Open(dbPath)
} }
// Open an existing database, creating it if it does not exist. // Open an existing database, creating it if it does not exist.
func Open(dbPath string) *DB { func Open(dbPath string) *DB {
log.Tracef("Opening SQLite database path at %s", dbPath)
dbc, err := sql.Open("sqlite3", dbPath) dbc, err := sql.Open("sqlite3", dbPath)
if err != nil { if err != nil {
log.Error(err.Error())
return nil return nil
} }
return &DB{ 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 // Query runs the supplied query against the sqlite database. It returns a slice of
// RowResults. // RowResults.
func (db *DB) Query(query string) (RowResults, error) { 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) rows, err := db.dbConn.Query(query)
if err != nil { if err != nil {
log.Errorf("failed to execute SQLite query: %s", err.Error())
return nil, err return nil, err
} }
defer func() { defer func() {
err = rows.Close() rows.Close()
if err != nil {
log.Errorf("failed to close rows: %s", err.Error())
}
}() }()
results := make(RowResults, 0) results := make(RowResults, 0)
@ -76,7 +63,6 @@ func (db *DB) Query(query string) (RowResults, error) {
for rows.Next() { for rows.Next() {
err = rows.Scan(dest...) err = rows.Scan(dest...)
if err != nil { if err != nil {
log.Errorf("failed to scan SQLite row: %s", err.Error())
return nil, err return nil, err
} }
@ -90,44 +76,24 @@ func (db *DB) Query(query string) (RowResults, error) {
} }
results = append(results, r) results = append(results, r)
} }
log.Debugf("Executed query successfully: %s", query)
return results, nil return results, nil
} }
// Execute executes the given sqlite statement, of a type that doesn't return rows. // Execute executes the given sqlite statement, of a type that doesn't return rows.
func (db *DB) Execute(stmt string) error { func (db *DB) Execute(stmt string) error {
_, err := db.dbConn.Exec(stmt) _, 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 return err
} }
// StartTransaction starts an explicit transaction. // StartTransaction starts an explicit transaction.
func (db *DB) StartTransaction() error { func (db *DB) StartTransaction() error {
_, err := db.dbConn.Exec("BEGIN") _, err := db.dbConn.Exec("BEGIN")
log.Debug(func() string {
if err != nil {
return "Error starting transaction"
}
return "Successfully started transaction"
}())
return err return err
} }
// CommitTransaction commits all changes made since StartTraction was called. // CommitTransaction commits all changes made since StartTraction was called.
func (db *DB) CommitTransaction() error { func (db *DB) CommitTransaction() error {
_, err := db.dbConn.Exec("END") _, err := db.dbConn.Exec("END")
log.Debug(func() string {
if err != nil {
return "Error ending transaction"
}
return "Successfully ended transaction"
}())
return err return err
} }
@ -135,11 +101,5 @@ func (db *DB) CommitTransaction() error {
// StartTransaction was called will take effect. // StartTransaction was called will take effect.
func (db *DB) RollbackTransaction() error { func (db *DB) RollbackTransaction() error {
_, err := db.dbConn.Exec("ROLLBACK") _, err := db.dbConn.Exec("ROLLBACK")
log.Debug(func() string {
if err != nil {
return "Error rolling back transaction"
}
return "Successfully rolled back transaction"
}())
return err return err
} }

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

Loading…
Cancel
Save