Close statements, add default table if the store was not initialised through service.Init() (#1502)

This commit is contained in:
Jake Sanders 2020-04-08 12:08:08 +01:00 committed by GitHub
parent bc0dc2e509
commit cc027d900e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ import (
// will use if no namespace is provided. // will use if no namespace is provided.
var ( var (
DefaultDatabase = "micro" DefaultDatabase = "micro"
DefaultTable = "micro"
) )
type sqlStore struct { type sqlStore struct {
@ -37,6 +38,12 @@ type sqlStore struct {
} }
func (s *sqlStore) Close() error { func (s *sqlStore) Close() error {
closeStmt(s.delete)
closeStmt(s.list)
closeStmt(s.readMany)
closeStmt(s.readOffset)
closeStmt(s.readOne)
closeStmt(s.write)
if s.db != nil { if s.db != nil {
return s.db.Close() return s.db.Close()
} }
@ -248,33 +255,25 @@ func (s *sqlStore) initDB() error {
if err != nil { if err != nil {
return errors.Wrap(err, "List statement couldn't be prepared") return errors.Wrap(err, "List statement couldn't be prepared")
} }
if s.list != nil { closeStmt(s.list)
s.list.Close()
}
s.list = list s.list = list
readOne, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key = $1;", s.database, s.table)) readOne, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key = $1;", s.database, s.table))
if err != nil { if err != nil {
return errors.Wrap(err, "ReadOne statement couldn't be prepared") return errors.Wrap(err, "ReadOne statement couldn't be prepared")
} }
if s.readOne != nil { closeStmt(s.readOne)
s.readOne.Close()
}
s.readOne = readOne s.readOne = readOne
readMany, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key LIKE $1;", s.database, s.table)) readMany, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key LIKE $1;", s.database, s.table))
if err != nil { if err != nil {
return errors.Wrap(err, "ReadMany statement couldn't be prepared") return errors.Wrap(err, "ReadMany statement couldn't be prepared")
} }
if s.readMany != nil { closeStmt(s.readMany)
s.readMany.Close()
}
s.readMany = readMany s.readMany = readMany
readOffset, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key DESC LIMIT $2 OFFSET $3;", s.database, s.table)) readOffset, err := s.db.Prepare(fmt.Sprintf("SELECT key, value, expiry FROM %s.%s WHERE key LIKE $1 ORDER BY key DESC LIMIT $2 OFFSET $3;", s.database, s.table))
if err != nil { if err != nil {
return errors.Wrap(err, "ReadOffset statement couldn't be prepared") return errors.Wrap(err, "ReadOffset statement couldn't be prepared")
} }
if s.readOffset != nil { closeStmt(s.readOffset)
s.readOffset.Close()
}
s.readOffset = readOffset s.readOffset = readOffset
write, err := s.db.Prepare(fmt.Sprintf(`INSERT INTO %s.%s(key, value, expiry) write, err := s.db.Prepare(fmt.Sprintf(`INSERT INTO %s.%s(key, value, expiry)
VALUES ($1, $2::bytea, $3) VALUES ($1, $2::bytea, $3)
@ -284,17 +283,13 @@ func (s *sqlStore) initDB() error {
if err != nil { if err != nil {
return errors.Wrap(err, "Write statement couldn't be prepared") return errors.Wrap(err, "Write statement couldn't be prepared")
} }
if s.write != nil { closeStmt(s.write)
s.write.Close()
}
s.write = write s.write = write
delete, err := s.db.Prepare(fmt.Sprintf("DELETE FROM %s.%s WHERE key = $1;", s.database, s.table)) delete, err := s.db.Prepare(fmt.Sprintf("DELETE FROM %s.%s WHERE key = $1;", s.database, s.table))
if err != nil { if err != nil {
return errors.Wrap(err, "Delete statement couldn't be prepared") return errors.Wrap(err, "Delete statement couldn't be prepared")
} }
if s.delete != nil { closeStmt(s.delete)
s.delete.Close()
}
s.delete = delete s.delete = delete
return nil return nil
@ -302,7 +297,7 @@ func (s *sqlStore) initDB() error {
func (s *sqlStore) configure() error { func (s *sqlStore) configure() error {
if len(s.options.Nodes) == 0 { if len(s.options.Nodes) == 0 {
s.options.Nodes = []string{"postgresql://root@localhost:26257"} s.options.Nodes = []string{"postgresql://root@localhost:26257?sslmode=disable"}
} }
database := s.options.Database database := s.options.Database
@ -310,10 +305,10 @@ func (s *sqlStore) configure() error {
database = DefaultDatabase database = DefaultDatabase
} }
if len(s.options.Table) == 0 {
return errors.New("no table set")
}
table := s.options.Table table := s.options.Table
if len(table) == 0 {
table = DefaultTable
}
// store.namespace must only contain letters, numbers and underscores // store.namespace must only contain letters, numbers and underscores
reg, err := regexp.Compile("[^a-zA-Z0-9]+") reg, err := regexp.Compile("[^a-zA-Z0-9]+")
@ -382,3 +377,9 @@ func NewStore(opts ...store.Option) store.Store {
// return store // return store
return s return s
} }
func closeStmt(s *sql.Stmt) {
if s != nil {
s.Close()
}
}