File: sql_parser_test.cpp

package info (click to toggle)
mysql-workbench 5.2.40%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 53,880 kB
  • sloc: cpp: 419,850; yacc: 74,784; xml: 54,510; python: 31,455; sh: 9,423; ansic: 4,736; makefile: 2,442; php: 529; java: 237
file content (216 lines) | stat: -rw-r--r-- 6,559 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* 
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "tut_stdafx.h"

#ifndef _WIN32
#include <sys/types.h>
#include <sys/stat.h>
#endif

// This file contains common TUT test cases for SQL parser, lexer and splitter.

#define VERBOSE_OUTPUT 0

//----------------------------------------------------------------------------------------------------------------------

#include "test.h"
#include "db.mysql.sqlparser/src/mysql_sql_parser_fe.h"
#include "wb_helpers.h"
// only module_sql_parser_test


BEGIN_TEST_DATA_CLASS(module_sql_parser_test)
protected:
  GRTManagerTest grtm;
END_TEST_DATA_CLASS

TEST_MODULE(module_sql_parser_test, "SQL parser test suite");

//----------------------------------------------------------------------------------------------------------------------

static const char *test_function_1_input[] =
{
  "CREATE DATABASE IF NOT EXISTS `i-flow_dev` CHARACTER SET latin1 COLLATE latin1_swedish_ci;",
  "select * from a; select * from b; insert into c values (1,2,3); /* comment ; */; ;",
  "select * from a1; delimiter %%%\n delimiter $$$%%%\n select * from a2 $$$%%% select ",
  "select * from x; # comment ; \n select * from b",
  "/* comment ; ",
  "\" string ",
  "\' string ",
  "-- comment ; something ",
  "# comment ; something ",
  "select * from z; // blablabla ",
  "DELIMITER |\n CREATE1 |CREATE2|",
  "SELECT `тест`",
  "DELIMI",
  "DELIMITER",
  "DELIMITER ",
  "DELIMITER |\nx|",
  "DELIMITER //\nSELECTz1//SELECTz2",
  NULL
};

static const char *test_function_1_output[] =
{
  "CREATE DATABASE IF NOT EXISTS `i-flow_dev` CHARACTER SET latin1 COLLATE latin1_swedish_ci",

  "select * from a",
  " select * from b",
  " insert into c values (1,2,3)",
  //" /* comment ; */", // comments are never returned
  //" ",  // empty statments are never returned

  "select * from a1",
  " select * from a2 ",
  " select ",

  "select * from x",
  " # comment ; \n select * from b",

  //"/* comment ; ",

  "\" string ",

  "\' string ",

  //"-- comment ; something ",

  //"# comment ; something ",

  "select * from z",
  " // blablabla ",

  " CREATE1 ",
  "CREATE2",

  "SELECT `тест`",

  "DELIMI",

  "DELIMITER",

  "DELIMITER ",

  "x",

  "SELECTz1",
  "SELECTz2"
};

static int test_function_1_output_index;
static bool test_function_1_success_flag;

int test_function_1_cb(void* user_data, const MyxStatementParser *splitter, const char *sql, const SqlAstNode *tree,
    int stmt_begin_lineno, int stmt_begin_line_pos, int stmt_end_lineno, int stmt_end_line_pos,
    int err_tok_lineno, int err_tok_line_pos, int err_tok_len, const std::string &err_msg)
{
  test_function_1_success_flag &= (strcmp(sql, test_function_1_output[test_function_1_output_index++]) == 0);
  return 0;
}

TEST_FUNCTION(1)
{
  test_function_1_output_index= 0;
  test_function_1_success_flag= true;

  Mysql_sql_parser_fe sql_parser_fe(grtm.get_grt());
  sql_parser_fe.ignore_dml = false;

  for(int i= 0; test_function_1_input[i] != NULL; i++)
  {
    sql_parser_fe.parse_sql_script(test_function_1_input[i], test_function_1_cb, reinterpret_cast<void *>(i));
    ensure("test 20", test_function_1_success_flag);
  }
}

static int test_function_2_counter;

int test_function_2_cb(void* user_data, const MyxStatementParser *splitter, const char *sql, const SqlAstNode *tree,
    int stmt_begin_lineno, int stmt_begin_line_pos, int stmt_end_lineno, int stmt_end_line_pos,
    int err_tok_lineno, int err_tok_line_pos, int err_tok_len, const std::string &err_msg)
{
  test_function_2_counter++;
  return 0;
}

TEST_FUNCTION(2)
{
  Mysql_sql_parser_fe sql_parser_fe(grtm.get_grt());
  sql_parser_fe.ignore_dml = false;

  test_function_2_counter= 0;
  sql_parser_fe.parse_sql_script_file("data/db/menagerie-db/cr_event_tbl.sql", test_function_2_cb, NULL);
  ensure("test 25 (1)", test_function_2_counter == 2);

  test_function_2_counter= 0;
  sql_parser_fe.parse_sql_script_file("data/db/menagerie-db/cr_pet_tbl.sql", test_function_2_cb, NULL);
  ensure("test 25 (2)", test_function_2_counter == 2);

  test_function_2_counter= 0;
  sql_parser_fe.parse_sql_script_file("data/db/menagerie-db/ins_puff_rec.sql", test_function_2_cb, NULL);
  ensure("test 25 (3)", test_function_2_counter == 1);

  test_function_2_counter= 0;
  sql_parser_fe.parse_sql_script_file("data/db/menagerie-db/load_pet_tbl.sql", test_function_2_cb, NULL);
  ensure("test 25 (4)", test_function_2_counter == 2);

  test_function_2_counter= 0;
  sql_parser_fe.parse_sql_script_file("data/db/sakila-db/sakila-schema.sql", test_function_2_cb, NULL);
  ensure("test 25 (5)", test_function_2_counter == 41);
}

int test_function_30_cb(void* user_data, const MyxStatementParser *splitter, const char *sql, const SqlAstNode *tree,
  int stmt_begin_lineno, int stmt_begin_line_pos, int stmt_end_lineno, int stmt_end_line_pos,
  int err_tok_lineno, int err_tok_line_pos, int err_tok_len, const std::string &err_msg)
{
  return 0;
}

#ifdef __WIN__

// performance test
TEST_FUNCTION(3)
{
  ensure("This hangs tests compleately", false);
  const char *filename= "data/db/sakila-db/sakila-data.sql";

  ensure("File does not exist.", g_file_test(filename, G_FILE_TEST_EXISTS) == TRUE);

  test_time_point t1;
  Mysql_sql_parser_fe sql_parser_fe(grtm.get_grt());
  sql_parser_fe.ignore_dml = false;
  sql_parser_fe.parse_sql_script_file(filename, test_function_30_cb, NULL);

#if VERBOSE_OUTPUT
  test_time_point t2;

  float time_rate= ((float)1000.)/(t2 - t1).get_ticks();
  float size_per_sec= ((float)statbuf.st_size)*time_rate/1024/1024;
  std::cout << "Splitter performance test: " << std::endl 
    << "sakila-data.sql was processed in " << (t2 - t1) << " [" << size_per_sec << " MB/sec]" << std::endl;
#endif
}

#endif

END_TESTS;

//----------------------------------------------------------------------------------------------------------------------