~roger-booth/mysql-proxy/laminator

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
--[[ $%BEGINLICENSE%$
 Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 $%ENDLICENSE%$ --]]


module("proxy.parser", package.seeall)

function tablename_expand(tblname, db_name)
	if not db_name then db_name = proxy.connection.client.default_db end
	if db_name then
		tblname = db_name .. "." .. tblname
	end

	return tblname
end

---
-- extract the table-names from tokenized SQL token stream
--
-- @see proxy.tokenize
function get_tables(tokens)
	local sql_stmt = nil
	local in_tablelist = false
	local next_is_tblname = false
	local in_braces = 0
	local db_name = nil
	local ddl_type = nil

	local tables = {}

	for i = 1, #tokens do
		local token = tokens[i]

		-- print(i .. " token: " .. token["token_name"])
		if token["token_name"] == "TK_COMMENT" or
		   token["token_name"] == "TK_UNKNOWN" then

		elseif not sql_stmt then
			-- try to get the SQL stmt
			sql_stmt = token.text:upper()
			-- print(i .. " sql-stmt: " .. sql_stmt)
			
			if sql_stmt == "UPDATE" or
			   sql_stmt == "INSERT" then
				in_tablelist = true
			end
		elseif sql_stmt == "SELECT" or 
		       sql_stmt == "DELETE" then
			-- we have sql_stmt token already, try to get the table names
			--
			-- SELECT ... FROM tbl AS alias, ... 
			-- DELETE FROM ... 
		
			if in_tablelist then
				if token["token_name"] == "TK_COMMA" then
					next_is_tblname = true
				elseif in_braces > 0 then
					-- ignore sub-queries
				elseif token["token_name"] == "TK_SQL_AS" then
					-- FROM tbl AS alias ...
					next_is_tblname = false
				elseif token["token_name"] == "TK_SQL_JOIN" then
					-- FROM tbl JOIN tbl ...
					next_is_tblname = true
				elseif token["token_name"] == "TK_SQL_LEFT" or
				       token["token_name"] == "TK_SQL_RIGHT" or
				       token["token_name"] == "TK_SQL_OUTER" or
				       token["token_name"] == "TK_SQL_USING" or
				       token["token_name"] == "TK_SQL_ON" or
				       token["token_name"] == "TK_SQL_AND" or
				       token["token_name"] == "TK_SQL_OR" then
					-- ignore me
				elseif token["token_name"] == "TK_LITERAL" and next_is_tblname then
					-- we have to handle <tbl> and <db>.<tbl>
					if not db_name and tokens[i + 1] and tokens[i + 1].token_name == "TK_DOT" then
						db_name = token.text
					else
						tables[tablename_expand(token.text, db_name)] = (sql_stmt == "SELECT" and "read" or "write")

						db_name = nil
					end

					next_is_tblname = false
				elseif token["token_name"] == "TK_OBRACE" then
					in_braces = in_braces + 1
				elseif token["token_name"] == "TK_CBRACE" then
					in_braces = in_braces - 1
				elseif token["token_name"] == "TK_SQL_WHERE" or
				       token["token_name"] == "TK_SQL_GROUP" or
				       token["token_name"] == "TK_SQL_ORDER" or
				       token["token_name"] == "TK_SQL_LIMIT" or
				       token["token_name"] == "TK_CBRACE" then
					in_tablelist = false
				elseif token["token_name"] == "TK_DOT" then
					-- FROM db.tbl
					next_is_tblname = true
				else
					print("(parser) unknown, found token: " .. token["token_name"] .. " -> " .. token.text)
					-- in_tablelist = false
				end
			elseif token["token_name"] == "TK_SQL_FROM" then
				in_tablelist = true
				next_is_tblname = true
			end
			
			-- print(i .. " in-from: " .. (in_from and "true" or "false"))
			-- print(i .. " next-is-tblname: " .. (next_is_tblname and "true" or "false"))
		elseif sql_stmt == "CREATE" or 
		       sql_stmt == "DROP" or 
		       sql_stmt == "ALTER" or 
		       sql_stmt == "RENAME" then
			-- CREATE TABLE <tblname>
			if not ddl_type then
				ddl_type = token.text:upper()
				in_tablelist = true
			elseif ddl_type == "TABLE" then
				if in_tablelist and 
				   token["token_name"] == "TK_LITERAL" then
				   if not db_name and tokens[i + 1] and tokens[i + 1].token_name == "TK_DOT" then
					db_name = token.text
					else
						tables[tablename_expand(token.text, db_name)] = (sql_stmt == "SELECT" and "read" or "write")
						db_name = nil
					end
				else
					in_tablelist = false
					break
				end
			end
		elseif sql_stmt == "INSERT" then
			-- INSERT INTO ...
			if in_tablelist then
				if token["token_name"] == "TK_LITERAL" then
					if not db_name and tokens[i + 1] and tokens[i + 1].token_name == "TK_DOT" then
						db_name = token.text
					else
						tables[tablename_expand(token.text, db_name)] = (sql_stmt == "SELECT" and "read" or "write")
						db_name = nil
					end
				elseif token["token_name"] == "TK_SQL_INTO" then
				elseif token["token_name"] == "TK_DOT" then
				else
					in_tablelist = false
				end
			end
		elseif sql_stmt == "UPDATE" then
			-- UPDATE <tbl> SET ..
			if in_tablelist then
				if token["token_name"] == "TK_LITERAL" then
					if not db_name and tokens[i + 1] and tokens[i + 1].token_name == "TK_DOT" then
						db_name = token.text
					else
						tables[tablename_expand(token.text, db_name)] = (sql_stmt == "SELECT" and "read" or "write")
						db_name = nil
					end
				elseif token["token_name"] == "TK_SQL_SET" then
					in_tablelist = false
					break
				end
			end
		end
	end

	return tables
end