From 2573360320c20388a246597911d753244c18d24c Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Sat, 13 Feb 2016 22:07:48 -0800 Subject: [PATCH] Start integrating actual SQLite support Builds without error. --- cmd/rqlited/main.go | 2 +- db/db.go | 42 +----------------------------------------- store/store.go | 4 ++++ 3 files changed, 6 insertions(+), 42 deletions(-) diff --git a/cmd/rqlited/main.go b/cmd/rqlited/main.go index cfac4ac3..0614d749 100644 --- a/cmd/rqlited/main.go +++ b/cmd/rqlited/main.go @@ -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()) } }() } diff --git a/db/db.go b/db/db.go index d33e49f0..1aadaa93 100644 --- a/db/db.go +++ b/db/db.go @@ -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 } diff --git a/store/store.go b/store/store.go index 9dc3f899..f074e4ef 100644 --- a/store/store.go +++ b/store/store.go @@ -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 }