Set default transaction mode to read only with --readonly flag
This commit is contained in:
@@ -225,7 +225,29 @@ func (client *Client) Query(query string) (*Result, error) {
|
|||||||
return res, err
|
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) {
|
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])
|
action := strings.ToLower(strings.Split(query, " ")[0])
|
||||||
if action == "update" || action == "delete" {
|
if action == "update" || action == "delete" {
|
||||||
res, err := client.db.Exec(query, args...)
|
res, err := client.db.Exec(query, args...)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type Options struct {
|
|||||||
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
|
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
|
||||||
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
|
Sessions bool `long:"sessions" description:"Enable multiple database sessions" default:"false"`
|
||||||
Prefix string `long:"prefix" description:"Add a url prefix"`
|
Prefix string `long:"prefix" description:"Add a url prefix"`
|
||||||
|
ReadOnly bool `long:"readonly" description:"Run database connection in readonly mode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Opts Options
|
var Opts Options
|
||||||
|
|||||||
Reference in New Issue
Block a user