You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
10 years ago
|
package command
|
||
|
|
||
|
import (
|
||
10 years ago
|
"github.com/otoolep/raft"
|
||
10 years ago
|
"github.com/otoolep/rqlite/db"
|
||
10 years ago
|
|
||
|
log "code.google.com/p/log4go"
|
||
10 years ago
|
)
|
||
|
|
||
|
// This command encapsulates a sqlite statement.
|
||
10 years ago
|
type ExecuteCommand struct {
|
||
10 years ago
|
Stmt string `json:"stmt"`
|
||
|
}
|
||
|
|
||
|
// Creates a new write command.
|
||
10 years ago
|
func NewExecuteCommand(stmt string) *ExecuteCommand {
|
||
|
return &ExecuteCommand{
|
||
10 years ago
|
Stmt: stmt,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// The name of the command in the log.
|
||
10 years ago
|
func (c *ExecuteCommand) CommandName() string {
|
||
10 years ago
|
return "write"
|
||
|
}
|
||
|
|
||
|
// Executes an sqlite statement.
|
||
10 years ago
|
func (c *ExecuteCommand) Apply(server raft.Server) (interface{}, error) {
|
||
|
log.Trace("Applying ExecuteCommand: '%s'", c.Stmt)
|
||
10 years ago
|
db := server.Context().(*db.DB)
|
||
10 years ago
|
return nil, db.Execute(c.Stmt)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
// This command encapsulates a set of sqlite statement, which are executed
|
||
|
// within a transaction.
|
||
10 years ago
|
type TransactionExecuteCommandSet struct {
|
||
10 years ago
|
Stmts []string `json:"stmts"`
|
||
|
}
|
||
|
|
||
|
// Creates a new set of sqlite commands, which execute within a transaction.
|
||
10 years ago
|
func NewTransactionExecuteCommandSet(stmts []string) *TransactionExecuteCommandSet {
|
||
|
return &TransactionExecuteCommandSet{
|
||
10 years ago
|
Stmts: stmts,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// The name of the command in the log.
|
||
10 years ago
|
func (c *TransactionExecuteCommandSet) CommandName() string {
|
||
|
return "transaction_execute"
|
||
10 years ago
|
}
|
||
|
|
||
|
// Executes a set of sqlite statements, within a transaction. All statements
|
||
|
// will take effect, or none.
|
||
10 years ago
|
func (c *TransactionExecuteCommandSet) Apply(server raft.Server) (interface{}, error) {
|
||
|
log.Trace("Applying TransactionExecuteCommandSet of size %d", len(c.Stmts))
|
||
10 years ago
|
|
||
|
commitSuccess := false
|
||
10 years ago
|
db := server.Context().(*db.DB)
|
||
10 years ago
|
defer func() {
|
||
|
if !commitSuccess {
|
||
|
err := db.RollbackTransaction()
|
||
|
if err != nil {
|
||
|
log.Error("Failed to rollback transaction: %s", err.Error)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
10 years ago
|
|
||
|
err := db.StartTransaction()
|
||
|
if err != nil {
|
||
10 years ago
|
log.Error("Failed to start transaction:", err.Error())
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
|
for i := range c.Stmts {
|
||
|
err = db.Execute(c.Stmts[i])
|
||
|
if err != nil {
|
||
10 years ago
|
log.Error("Failed to execute statement within transaction", err.Error())
|
||
10 years ago
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
err = db.CommitTransaction()
|
||
|
if err != nil {
|
||
10 years ago
|
log.Error("Failed to commit transaction:", err.Error())
|
||
10 years ago
|
return nil, err
|
||
10 years ago
|
} else {
|
||
|
commitSuccess = true
|
||
10 years ago
|
}
|
||
|
return nil, nil
|
||
|
}
|