|
|
@@ -361,26 +361,35 @@ func (c *Conn) prepareStatement(stmt string, trace Tracer) (*queryInfo, error) {
|
|
|
return flight.info, flight.err
|
|
|
}
|
|
|
|
|
|
+func shouldPrepare(stmt string) bool {
|
|
|
+ stmt = strings.TrimLeftFunc(strings.TrimRightFunc(stmt, func(r rune) bool {
|
|
|
+ return unicode.IsSpace(r) || r == ';'
|
|
|
+ }), unicode.IsSpace)
|
|
|
+
|
|
|
+ var stmtType string
|
|
|
+ if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
|
|
|
+ stmtType = strings.ToLower(stmt[:n])
|
|
|
+ }
|
|
|
+ if stmtType == "begin" {
|
|
|
+ if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
|
|
|
+ stmtType = strings.ToLower(stmt[n+1:])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ switch stmtType {
|
|
|
+ case "select", "insert", "update", "delete", "batch":
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
func (c *Conn) executeQuery(qry *Query) *Iter {
|
|
|
op := &queryFrame{
|
|
|
- Stmt: strings.TrimSpace(qry.stmt),
|
|
|
+ Stmt: qry.stmt,
|
|
|
Cons: qry.cons,
|
|
|
PageSize: qry.pageSize,
|
|
|
PageState: qry.pageState,
|
|
|
}
|
|
|
- stmtType := op.Stmt
|
|
|
- if n := strings.IndexFunc(stmtType, unicode.IsSpace); n >= 0 {
|
|
|
- stmtType = strings.ToLower(stmtType[:n])
|
|
|
- switch stmtType {
|
|
|
- case "begin":
|
|
|
- stmtTail := strings.TrimSpace(op.Stmt[n:])
|
|
|
- if n := strings.IndexFunc(stmtTail, unicode.IsSpace); n >= 0 {
|
|
|
- stmtType = stmtType + " " + strings.ToLower(stmtTail[:n])
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- switch stmtType {
|
|
|
- case "select", "insert", "update", "delete", "begin batch":
|
|
|
+ if shouldPrepare(op.Stmt) {
|
|
|
// Prepare all DML queries. Other queries can not be prepared.
|
|
|
info, err := c.prepareStatement(qry.stmt, qry.trace)
|
|
|
if err != nil {
|