-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_mysql_example.cpp
More file actions
214 lines (179 loc) · 10.4 KB
/
main_mysql_example.cpp
File metadata and controls
214 lines (179 loc) · 10.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "mysql_parser/mysql_parser.h" // Ensure this path is correct for your include setup
#include <iostream>
#include <vector>
#include <string>
#include <memory> // Required for std::unique_ptr
bool verbose_print = true;
unsigned long int queries = 0;
// Function to parse a query and print the AST or errors
void parse_and_print(MySQLParser::Parser& parser, const std::string& query_type, const std::string& query) {
queries++;
if (verbose_print) {
std::cout << "------------------------------------------\n";
std::cout << "Parsing MySQL " << query_type << " query: " << query << std::endl;
}
//std::cout << "AAA" << std::endl;
parser.clear_errors(); // Clear any errors from previous parses
//std::cout << "BBB" << std::endl;
std::unique_ptr<MySQLParser::AstNode> ast = parser.parse(query);
//std::cout << "CCC" << std::endl;
if (verbose_print == false) {
return;
}
if (ast) {
std::cout << "Parsing successful!" << std::endl;
MySQLParser::print_ast(ast.get()); // Print the AST
} else {
std::cout << "Parsing failed." << std::endl;
const auto& errors = parser.get_errors();
if (errors.empty()) {
std::cout << " (No specific error messages, check parser logic or mysql_yyerror implementation)" << std::endl;
} else {
for (const auto& error : errors) {
std::cout << " Error: " << error << std::endl;
}
}
}
std::cout << "------------------------------------------\n\n";
}
int main() {
MySQLParser::Parser parser;
std::vector<std::string> select_queries = {
// Basic SELECTs (from original)
"SELECT name FROM users;",
"SELECT * FROM `orders`;",
"SELECT `col1`, `col2` FROM tablenameB", // No semicolon
// SELECT with ALIAS
"SELECT column1 AS first_column, column2 AS second_column FROM my_table;",
"SELECT column1 first_column, column2 second_column FROM my_table;", // Implicit AS
// SELECT with WHERE
"SELECT product_name, price FROM products WHERE category = 'Electronics';",
"SELECT * FROM employees WHERE salary > 50000 AND department_id = 3;",
// SELECT with ORDER BY
"SELECT student_name, score FROM results ORDER BY score DESC;",
"SELECT item, quantity FROM inventory ORDER BY item ASC, quantity DESC;",
// SELECT with LIMIT
"SELECT event_name FROM event_log LIMIT 10;",
"SELECT message FROM messages ORDER BY created_at DESC LIMIT 5, 10;", // LIMIT offset, count
// SELECT with GROUP BY
"SELECT department, COUNT(*) AS num_employees FROM employees GROUP BY department;",
"SELECT product_category, AVG(price) AS avg_price FROM products GROUP BY product_category;",
// SELECT with GROUP BY and HAVING
"SELECT department, COUNT(*) AS num_employees FROM employees GROUP BY department HAVING COUNT(*) > 10;",
"SELECT product_category, AVG(price) AS avg_price FROM products GROUP BY product_category HAVING AVG(price) > 100.00;",
// Basic JOIN clauses
"SELECT c.customer_name, o.order_id FROM customers c JOIN orders o ON c.customer_id = o.customer_id;",
"SELECT s.name, p.product_name FROM suppliers s INNER JOIN products p ON s.supplier_id = p.supplier_id;",
"SELECT e.name, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;",
"SELECT e.name, p.project_name FROM employees e RIGHT OUTER JOIN projects p ON e.employee_id = p.lead_employee_id;",
"SELECT c1.name, c2.name AS city_pair FROM cities c1 CROSS JOIN cities c2 WHERE c1.id <> c2.id;", // Comma join will be CROSS JOIN
"SELECT c1.name, c2.name AS city_pair_comma FROM cities c1, cities c2 WHERE c1.id <> c2.id;",
"SELECT e.name, d.name FROM employees e NATURAL JOIN departments d;", // Natural Join
"SELECT s.student_name, c.course_name FROM students s NATURAL LEFT JOIN courses c;",
// JOIN with USING
"SELECT c.customer_name, o.order_date FROM customers c JOIN orders o USING (customer_id);",
"SELECT a.val, b.val FROM tableA a LEFT JOIN tableB b USING (id, common_column);",
// More complex JOINs (multiple joins)
"SELECT c.name, o.order_date, p.product_name, oi.quantity "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"JOIN order_items oi ON o.order_id = oi.order_id "
"JOIN products p ON oi.product_id = p.product_id "
"WHERE c.country = 'USA' ORDER BY o.order_date DESC;",
// Subquery in FROM clause (Derived Table)
"SELECT dt.category_name, dt.total_sales "
"FROM (SELECT category, SUM(sales_amount) AS total_sales FROM sales GROUP BY category) AS dt "
"WHERE dt.total_sales > 10000;",
"SELECT emp_details.name, emp_details.dept "
"FROM (SELECT e.name, d.department_name AS dept FROM employees e JOIN departments d ON e.dept_id = d.id) emp_details "
"ORDER BY emp_details.name;",
"SELECT * FROM (SELECT id FROM t1) AS derived_t1 JOIN (SELECT id FROM t2) AS derived_t2 ON derived_t1.id = derived_t2.id;",
// SELECT ... INTO OUTFILE / DUMPFILE
"SELECT user_id, username, email INTO OUTFILE '/tmp/users.txt' FROM user_accounts WHERE is_active = 1;",
"SELECT * INTO OUTFILE '/tmp/products_export.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' FROM products;",
"SELECT data_column INTO DUMPFILE '/tmp/data_blob.dat' FROM large_objects WHERE id = 42;",
"SELECT col1, col2 INTO OUTFILE 'test_char_set.txt' CHARACTER SET 'utf8mb4' FIELDS ENCLOSED BY '`' FROM my_data;",
// SELECT ... INTO variables
"SELECT COUNT(*), MAX(salary) INTO @user_count, @max_sal FROM employees;",
"SELECT name, email INTO @emp_name, @emp_email FROM employees WHERE id = 101;",
// SELECT ... FOR UPDATE / FOR SHARE
"SELECT account_balance FROM accounts WHERE account_id = 123 FOR UPDATE;",
"SELECT product_name, quantity FROM inventory WHERE product_id = 789 FOR SHARE;",
"SELECT c.name, o.status FROM customers c JOIN orders o ON c.id = o.customer_id WHERE o.id = 500 FOR UPDATE OF c, o;",
"SELECT item_name FROM stock_items WHERE category = 'electronics' FOR SHARE NOWAIT;",
"SELECT * FROM pending_tasks FOR UPDATE SKIP LOCKED;",
"SELECT id FROM users WHERE status = 'pending' FOR SHARE OF users SKIP LOCKED;",
// Combined query
"SELECT c.name AS customer_name, COUNT(o.order_id) AS total_orders, SUM(oi.price * oi.quantity) AS total_spent "
"FROM customers AS c "
"LEFT JOIN orders AS o ON c.customer_id = o.customer_id "
"JOIN order_items AS oi ON o.order_id = oi.order_id "
"WHERE c.registration_date > '2022-01-01' "
"GROUP BY c.customer_id, c.name "
"HAVING COUNT(o.order_id) > 2 AND SUM(oi.price * oi.quantity) > 500 "
"ORDER BY total_spent DESC, customer_name ASC "
"LIMIT 10, 5 "
"FOR UPDATE OF c, o NOWAIT;"
// MATCH-AGAINST
"SELECT MATCH customer_name,total_orders AGAINST ('foo') FROM customers;"
"SELECT MATCH customer_name,total_orders AGAINST ('foo' IN NATURAL LANGUAGE MODE) FROM customers;"
"SELECT MATCH customer_name,total_orders AGAINST ('foo' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION) FROM customers;"
"SELECT MATCH customer_name,total_orders AGAINST ('foo' IN BOOLEAN MODE) FROM customers;"
"SELECT MATCH customer_name,total_orders AGAINST ('foo' WITH QUERY EXPANSION) FROM customers;"
};
std::vector<std::string> insert_queries = {
"INSERT INTO products VALUES ('a new gadget');",
"INSERT INTO logs VALUES (\"Error message with double quotes\")",
"INSERT INTO `special-table` VALUES ('escaped value \\'single quote\\' and \\\\ backslash');"
};
std::vector<std::string> set_queries = {
"SET @my_user_var = 'hello world';",
"SET @anotherVar = 12345;",
"SET global max_connections = 1000",
"SET @@session.net_write_timeout = 120;",
"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';",
"SET CHARACTER SET DEFAULT",
"SET @a = 1, @b = 'two', global max_heap_table_size = 128000000;"
};
std::vector<std::string> delete_queries = {
// Single-table DELETE statements
"DELETE FROM customers WHERE customer_id = 101;",
"DELETE LOW_PRIORITY FROM orders WHERE order_date < '2023-01-01'",
"DELETE QUICK IGNORE FROM logs WHERE log_level = 'DEBUG' ORDER BY timestamp DESC LIMIT 1000;",
"DELETE FROM events WHERE event_name = `expired-event`",
// Multi-table DELETE statements
"DELETE t1 FROM table1 AS t1, table2 AS t2 WHERE t1.id = t2.ref_id;",
"DELETE FROM t1, t2 USING table1 AS t1 INNER JOIN table2 AS t2 ON t1.key = t2.key WHERE t1.value > 100;",
"DELETE FROM old_records WHERE last_accessed < '2020-01-01'"
};
const int iterations = 10000;
for (int i = 0; i < iterations; i++) {
if (verbose_print == true) std::cout << "\n======= SELECT QUERIES =======\n";
for (const auto& query : select_queries) {
parse_and_print(parser, "SELECT", query);
}
if (verbose_print == true) std::cout << "\n======= INSERT QUERIES =======\n";
for (const auto& query : insert_queries) {
parse_and_print(parser, "INSERT", query);
}
if (verbose_print == true) std::cout << "\n======= SET QUERIES =======\n";
for (const auto& query : set_queries) {
parse_and_print(parser, "SET", query);
}
if (verbose_print == true) std::cout << "\n======= DELETE QUERIES =======\n";
for (const auto& query : delete_queries) {
parse_and_print(parser, "DELETE", query);
}
// Example of a known failing query (due to function call in expression_placeholder)
if (verbose_print == true) std::cout << "\n======= KNOWN FAILING SET QUERY (Function Call) =======\n";
parse_and_print(parser, "SET", "SET @myvar = some_function(1, 'a');");
if (verbose_print == true) std::cout << "\n======= KNOWN FAILING SET QUERY (Invalid Identifier) =======\n";
parse_and_print(parser, "SET", "SET global invalid-variable = 100;");
if (verbose_print == true) { verbose_print=false; }
if ((i+1) % 1000 == 0) {
std::cout << i+1 << std::endl;
}
}
std::cout << "Queries: " << queries << std::endl;
return 0;
}