From 90953735d99369ecf4ae3b690de5187ba1f20eeb Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 26 Nov 2016 13:05:21 +0000 Subject: [PATCH] DEFINE and REMOVE SQL queries can not be inside transactions --- sql/define.go | 4 ++++ sql/error.go | 8 ++++++++ sql/parser.go | 1 + sql/remove.go | 4 ++++ sql/trans.go | 6 ++++++ 5 files changed, 23 insertions(+) diff --git a/sql/define.go b/sql/define.go index 82211cd7..1f621e22 100644 --- a/sql/define.go +++ b/sql/define.go @@ -16,6 +16,10 @@ package sql func (p *parser) parseDefineStatement() (Statement, error) { + if p.buf.txn { + return nil, &TXError{} + } + // Inspect the next token. tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) diff --git a/sql/error.go b/sql/error.go index d70ee542..aca56fd6 100644 --- a/sql/error.go +++ b/sql/error.go @@ -43,6 +43,14 @@ func (e *BlankError) Error() string { return fmt.Sprint("You need to specify a namespace and a database to use") } +// TXError represents an error that occured when switching access. +type TXError struct{} + +// Error returns the string representation of the error. +func (e *TXError) Error() string { + return fmt.Sprintf("DEFINE and REMOVE statements must be outside of a transaction.") +} + // NSError represents an error that occured when switching access. type NSError struct { NS string diff --git a/sql/parser.go b/sql/parser.go index 7a829ceb..5d24b4c3 100644 --- a/sql/parser.go +++ b/sql/parser.go @@ -30,6 +30,7 @@ type parser struct { v map[string]interface{} buf struct { n int // buffer size + txn bool // inside txn tok Token // last read token lit string // last read literal val interface{} // Last read value diff --git a/sql/remove.go b/sql/remove.go index 7e369b1c..d2398074 100644 --- a/sql/remove.go +++ b/sql/remove.go @@ -16,6 +16,10 @@ package sql func (p *parser) parseRemoveStatement() (Statement, error) { + if p.buf.txn { + return nil, &TXError{} + } + // Inspect the next token. tok, _, err := p.shouldBe(NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, RULES, FIELD, INDEX, VIEW) diff --git a/sql/trans.go b/sql/trans.go index 121f2b48..48f587eb 100644 --- a/sql/trans.go +++ b/sql/trans.go @@ -24,6 +24,8 @@ func (p *parser) parseBeginStatement() (stmt *BeginStatement, err error) { return nil, err } + p.buf.txn = true + return } @@ -38,6 +40,8 @@ func (p *parser) parseCancelStatement() (stmt *CancelStatement, err error) { return nil, err } + p.buf.txn = false + return } @@ -52,6 +56,8 @@ func (p *parser) parseCommitStatement() (stmt *CommitStatement, err error) { return nil, err } + p.buf.txn = false + return }