| 1330 | } |
| 1331 | |
| 1332 | int main(int argc, char **argv) { |
| 1333 | |
| 1334 | LEX *sql_lex; |
| 1335 | String *lex_string = NULL; |
| 1336 | GKeyFile *keyfile = NULL; |
| 1337 | GError *error = NULL; |
| 1338 | GOptionContext *context = NULL; |
| 1339 | char *query = NULL; |
| 1340 | char *sqlparse_path = strdup("/usr/local/sqlparser"); |
| 1341 | int i = 0; |
| 1342 | ConnectionOptionsInit(&options); |
| 1343 | context = g_option_context_new("sqladvisor"); |
| 1344 | g_option_context_add_main_entries(context, entries, NULL); |
| 1345 | g_option_context_set_summary(context, "SQL Advisor Summary"); |
| 1346 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 1347 | sql_print_error("option parsing failed:%s\n", error->message); |
| 1348 | exit(1); |
| 1349 | } |
| 1350 | g_option_context_free(context); |
| 1351 | if (options.configfile != NULL) { |
| 1352 | //load config file |
| 1353 | keyfile = g_key_file_new(); |
| 1354 | g_key_file_set_list_separator(keyfile, SEP); |
| 1355 | if (g_key_file_load_from_file(keyfile, options.configfile, |
| 1356 | G_KEY_FILE_NONE, &error) == FALSE) { |
| 1357 | g_key_file_free(keyfile); |
| 1358 | sql_print_error("load config file failed:%s\n", error->message); |
| 1359 | } |
| 1360 | if (g_option_keyfile_parse(keyfile, GROUT_NAME, entries) < 0) { |
| 1361 | g_key_file_free(keyfile); |
| 1362 | sql_print_error("read config file failed:%s\n", error->message); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (mysqld_init(sqlparse_path)) { |
| 1367 | sql_print_error("加载sqlparser模块有错 \n"); |
| 1368 | if(sqlparse_path) free(sqlparse_path); |
| 1369 | return -1; |
| 1370 | } |
| 1371 | if(sqlparse_path) free(sqlparse_path); |
| 1372 | |
| 1373 | if (options.username == NULL || options.password == NULL |
| 1374 | || options.host == NULL || options.dbname == NULL |
| 1375 | || options.query[0] == NULL) { |
| 1376 | mysqld_cleanup(); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | while ((query = options.query[i]) != NULL) { |
| 1380 | sql_lex = sql_parser(query, options.dbname); |
| 1381 | |
| 1382 | if (sql_lex == NULL) { |
| 1383 | sql_print_error("sqlparser 解析出错,返回值lex为空 \n"); |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | current_lex = &sql_lex->select_lex; |
| 1388 | while (current_lex != NULL) { |
| 1389 | lex_string = new (thd->mem_root) String(); |
nothing calls this directly
no test coverage detected