From 67c7b77e439d3921f9518259d344f22edd9032a4 Mon Sep 17 00:00:00 2001
From: Garet Halliday <ghalliday@gfxlabs.io>
Date: Fri, 23 Sep 2022 13:44:15 -0500
Subject: [PATCH] make life less complicated

---
 lib/gat/gatling/client/client.go | 15 ++++-----------
 lib/parse/parse.go               | 13 +++++++++++--
 test/docker-compose.yml          |  2 ++
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/lib/gat/gatling/client/client.go b/lib/gat/gatling/client/client.go
index 88763f86..b6a88869 100644
--- a/lib/gat/gatling/client/client.go
+++ b/lib/gat/gatling/client/client.go
@@ -537,14 +537,7 @@ func (c *Client) handle_query(ctx context.Context, q *protocol.Query) error {
 	}
 
 	transaction := -1
-	for idx, cmd := range parsed {
-		var next int
-		if idx+1 >= len(parsed) {
-			next = len(q.Fields.Query)
-		} else {
-			next = parsed[idx+1].Index
-		}
-
+	for _, cmd := range parsed {
 		cmdUpper := strings.ToUpper(cmd.Command)
 
 		// not in transaction
@@ -556,14 +549,14 @@ func (c *Client) handle_query(ctx context.Context, q *protocol.Query) error {
 				}
 				fallthrough
 			case "BEGIN":
-				transaction = cmd.Index
+				transaction = cmd.Begin
 			}
 		}
 
 		if transaction == -1 {
 			// this is a simple query
 			c.startRequest()
-			err = c.handle_simple_query(ctx, q.Fields.Query[cmd.Index:next])
+			err = c.handle_simple_query(ctx, cmd.SQL)
 			if err != nil {
 				return err
 			}
@@ -572,7 +565,7 @@ func (c *Client) handle_query(ctx context.Context, q *protocol.Query) error {
 			switch cmdUpper {
 			case "END":
 				c.startRequest()
-				err = c.handle_transaction(ctx, q.Fields.Query[transaction:next])
+				err = c.handle_transaction(ctx, q.Fields.Query[transaction:cmd.End])
 				if err != nil {
 					return err
 				}
diff --git a/lib/parse/parse.go b/lib/parse/parse.go
index ab766763..22dad031 100644
--- a/lib/parse/parse.go
+++ b/lib/parse/parse.go
@@ -8,7 +8,12 @@ import (
 )
 
 type Command struct {
-	Index     int
+	// start index in parsed SQL string
+	Begin int
+	// end index in parsed SQL string
+	End int
+	// the subtext SQL. Same as src[Begin:End]
+	SQL       string
 	Command   string
 	Arguments []string
 }
@@ -242,7 +247,11 @@ func (r *reader) nextArgument() (string, error) {
 }
 
 func (r *reader) nextCommand() (cmd Command, err error) {
-	cmd.Index = r.p
+	start := r.p
+	defer func() {
+		cmd.SQL = r.v[start:r.p]
+	}()
+
 	cmd.Command, err = r.nextIdentifier()
 	if err != nil {
 		if err == EndOfStatement {
diff --git a/test/docker-compose.yml b/test/docker-compose.yml
index d10f7f8e..d360903f 100644
--- a/test/docker-compose.yml
+++ b/test/docker-compose.yml
@@ -16,5 +16,7 @@ services:
   pggat:
     build: ../
     restart: always
+    environment:
+      PGGAT_DB_PASS: example
     ports:
       - 6432:6432
-- 
GitLab