File: sqlparse_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (194 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sqlparse

import (
	"testing"

	newrelic "github.com/newrelic/go-agent"
	"github.com/newrelic/go-agent/internal/crossagent"
)

type sqlTestcase struct {
	Input     string `json:"input"`
	Operation string `json:"operation"`
	Table     string `json:"table"`
}

func (tc sqlTestcase) test(t *testing.T) {
	var segment newrelic.DatastoreSegment
	ParseQuery(&segment, tc.Input)
	if tc.Operation == "other" {
		// Allow for matching of Operation "other" to ""
		if segment.Operation != "" {
			t.Errorf("operation mismatch query='%s' wanted='%s' got='%s'",
				tc.Input, tc.Operation, segment.Operation)
		}
	} else if segment.Operation != tc.Operation {
		t.Errorf("operation mismatch query='%s' wanted='%s' got='%s'",
			tc.Input, tc.Operation, segment.Operation)
	}
	// The Go agent subquery behavior does not match the PHP Agent.
	if tc.Table == "(subquery)" {
		return
	}
	if tc.Table != segment.Collection {
		t.Errorf("table mismatch query='%s' wanted='%s' got='%s'",
			tc.Input, tc.Table, segment.Collection)
	}
}

func TestParseSQLCrossAgent(t *testing.T) {
	var tcs []sqlTestcase
	err := crossagent.ReadJSON("sql_parsing.json", &tcs)
	if err != nil {
		t.Fatal(err)
	}

	for _, tc := range tcs {
		tc.test(t)
	}
}

func TestParseSQLSubQuery(t *testing.T) {
	for _, tc := range []sqlTestcase{
		{Input: "SELECT * FROM (SELECT * FROM foobar)", Operation: "select", Table: "foobar"},
		{Input: "SELECT * FROM (SELECT * FROM foobar) WHERE x > y", Operation: "select", Table: "foobar"},
		{Input: "SELECT * FROM(SELECT * FROM foobar) WHERE x > y", Operation: "select", Table: "foobar"},
	} {
		tc.test(t)
	}
}

func TestParseSQLOther(t *testing.T) {
	for _, tc := range []sqlTestcase{
		// Test that we handle table names enclosed in brackets.
		{Input: "SELECT * FROM [foo]", Operation: "select", Table: "foo"},
		{Input: "SELECT * FROM[foo]", Operation: "select", Table: "foo"},
		{Input: "SELECT * FROM [ foo ]", Operation: "select", Table: "foo"},
		{Input: "SELECT * FROM [ 'foo' ]", Operation: "select", Table: "foo"},
		{Input: "SELECT * FROM[ `something`.'foo' ]", Operation: "select", Table: "foo"},
		// Test that we handle the cheese.
		{Input: "SELECT fromage FROM fromagier", Operation: "select", Table: "fromagier"},
	} {
		tc.test(t)
	}
}

func TestParseSQLUpdateExtraKeywords(t *testing.T) {
	for _, tc := range []sqlTestcase{
		{Input: "update or rollback foo", Operation: "update", Table: "foo"},
		{Input: "update only foo", Operation: "update", Table: "foo"},
		{Input: "update low_priority ignore{foo}", Operation: "update", Table: "foo"},
	} {
		tc.test(t)
	}
}

func TestLineComment(t *testing.T) {
	for _, tc := range []sqlTestcase{
		{
			Input: `SELECT -- * FROM tricky
			* FROM foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input: `SELECT # * FROM tricky
			* FROM foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input: `    -- SELECT * FROM tricky
			SELECT * FROM foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input: `    # SELECT * FROM tricky
			SELECT * FROM foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input: `SELECT * FROM -- tricky
			foo`,
			Operation: "select",
			Table:     "foo",
		},
	} {
		tc.test(t)
	}
}

func TestSemicolonPrefix(t *testing.T) {
	for _, tc := range []sqlTestcase{
		{
			Input:     `;select * from foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input:     `  ;;  ; select * from foo`,
			Operation: "select",
			Table:     "foo",
		},
		{
			Input: ` ;
			SELECT * FROM foo`,
			Operation: "select",
			Table:     "foo",
		},
	} {
		tc.test(t)
	}
}

func TestDollarSignTable(t *testing.T) {
	for _, tc := range []sqlTestcase{
		{
			Input:     `select * from $dollar_100_$`,
			Operation: "select",
			Table:     "$dollar_100_$",
		},
	} {
		tc.test(t)
	}
}

func TestPriorityQuery(t *testing.T) {
	// Test that we handle:
	// https://dev.mysql.com/doc/refman/8.0/en/insert.html
	//     INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name
	for _, tc := range []sqlTestcase{
		{
			Input:     `INSERT HIGH_PRIORITY INTO employee VALUES('Tom',12345,'Sales',100)`,
			Operation: "insert",
			Table:     "employee",
		},
	} {
		tc.test(t)
	}
}

func TestExtractTable(t *testing.T) {
	for idx, tc := range []string{
		"table",
		"`table`",
		`"table"`,
		"`database.table`",
		"`database`.table",
		"database.`table`",
		"`database`.`table`",
		"  { table }",
		"\n[table]",
		"\t    ( 'database'.`table`  ) ",
	} {
		table := extractTable(tc)
		if table != "table" {
			t.Error(idx, table)
		}
	}
}