Set default transaction mode to read only with --readonly flag

This commit is contained in:
Dan Sosedoff 2016-11-05 17:43:30 -05:00
parent e3e1d7b012
commit 661fed0dbb
2 changed files with 23 additions and 0 deletions

View File

@ -225,7 +225,29 @@ func (client *Client) Query(query string) (*Result, error) {
return res, err
}
func (client *Client) SetReadOnlyMode() error {
var value string
if err := client.db.Get(&value, "SHOW default_transaction_read_only;"); err != nil {
return err
}
if value == "off" {
_, err = client.db.Exec("SET default_transaction_read_only=on;")
return err
}
return nil
}
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
// We're going to force-set transaction mode on every query.
// This is needed so that default mode could not be changed by user.
if command.Opts.ReadOnly {
if err := client.SetReadOnlyMode(); err != nil {
return nil, err
}
}
action := strings.ToLower(strings.Split(query, " ")[0])
if action == "update" || action == "delete" {
res, err := client.db.Exec(query, args...)

View File

@ -24,6 +24,7 @@ type Options struct {
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
Prefix string `long:"prefix" description:"Add a url prefix"`
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
}
var Opts Options