Format query classifier modules

Formatted query classifier modules with Astyle.
This commit is contained in:
Markus Mäkelä
2017-01-17 13:51:57 +02:00
parent a48be9badf
commit 3d92263cb3
4 changed files with 519 additions and 493 deletions

View File

@ -638,25 +638,25 @@ static uint32_t resolve_query_type(parsing_info_t *pi, THD* thd)
{ {
type |= QUERY_TYPE_GSYSVAR_READ; type |= QUERY_TYPE_GSYSVAR_READ;
} }
/** /**
* SET syntax http://dev.mysql.com/doc/refman/5.6/en/set-statement.html * SET syntax http://dev.mysql.com/doc/refman/5.6/en/set-statement.html
*/ */
else if (lex->sql_command == SQLCOM_SET_OPTION) else if (lex->sql_command == SQLCOM_SET_OPTION)
{ {
type |= QUERY_TYPE_GSYSVAR_WRITE; type |= QUERY_TYPE_GSYSVAR_WRITE;
} }
/* /*
* SHOW GLOBAL STATUS - Route to master * SHOW GLOBAL STATUS - Route to master
*/ */
else if (lex->sql_command == SQLCOM_SHOW_STATUS) else if (lex->sql_command == SQLCOM_SHOW_STATUS)
{ {
type = QUERY_TYPE_WRITE; type = QUERY_TYPE_WRITE;
} }
/** /**
* REVOKE ALL, ASSIGN_TO_KEYCACHE, * REVOKE ALL, ASSIGN_TO_KEYCACHE,
* PRELOAD_KEYS, FLUSH, RESET, CREATE|ALTER|DROP SERVER * PRELOAD_KEYS, FLUSH, RESET, CREATE|ALTER|DROP SERVER
*/ */
else else
{ {
@ -674,9 +674,9 @@ static uint32_t resolve_query_type(parsing_info_t *pi, THD* thd)
{ {
type |= QUERY_TYPE_SYSVAR_READ; type |= QUERY_TYPE_SYSVAR_READ;
} }
/** /**
* SET syntax http://dev.mysql.com/doc/refman/5.6/en/set-statement.html * SET syntax http://dev.mysql.com/doc/refman/5.6/en/set-statement.html
*/ */
else if (lex->sql_command == SQLCOM_SET_OPTION) else if (lex->sql_command == SQLCOM_SET_OPTION)
{ {
/** Either user- or system variable write */ /** Either user- or system variable write */
@ -941,7 +941,7 @@ static uint32_t resolve_query_type(parsing_info_t *pi, THD* thd)
} }
break; break;
/** System session variable */ /** System session variable */
case Item_func::GSYSVAR_FUNC: case Item_func::GSYSVAR_FUNC:
{ {
const char* name = item->name; const char* name = item->name;
@ -962,7 +962,7 @@ static uint32_t resolve_query_type(parsing_info_t *pi, THD* thd)
} }
break; break;
/** User-defined variable read */ /** User-defined variable read */
case Item_func::GUSERVAR_FUNC: case Item_func::GUSERVAR_FUNC:
func_qtype |= QUERY_TYPE_USERVAR_READ; func_qtype |= QUERY_TYPE_USERVAR_READ;
MXS_DEBUG("%lu [resolve_query_type] " MXS_DEBUG("%lu [resolve_query_type] "
@ -971,7 +971,7 @@ static uint32_t resolve_query_type(parsing_info_t *pi, THD* thd)
pthread_self()); pthread_self());
break; break;
/** User-defined variable modification */ /** User-defined variable modification */
case Item_func::SUSERVAR_FUNC: case Item_func::SUSERVAR_FUNC:
func_qtype |= QUERY_TYPE_USERVAR_WRITE; func_qtype |= QUERY_TYPE_USERVAR_WRITE;
MXS_DEBUG("%lu [resolve_query_type] " MXS_DEBUG("%lu [resolve_query_type] "
@ -1170,9 +1170,9 @@ char* qc_get_stmtname(GWBUF* buf)
mysql->thd == NULL || mysql->thd == NULL ||
(THD *) (mysql->thd))->lex == NULL || (THD *) (mysql->thd))->lex == NULL ||
(THD *) (mysql->thd))->lex->prepared_stmt_name == NULL) (THD *) (mysql->thd))->lex->prepared_stmt_name == NULL)
{ {
return NULL; return NULL;
} }
return ((THD *) (mysql->thd))->lex->prepared_stmt_name.str; return ((THD *) (mysql->thd))->lex->prepared_stmt_name.str;
} }
@ -1297,7 +1297,7 @@ int32_t qc_mysql_get_table_names(GWBUF* querybuf, int32_t fullnames, char*** tab
{ {
if (i >= currtblsz) if (i >= currtblsz)
{ {
tmp = (char**) malloc(sizeof (char*)*(currtblsz * 2 + 1)); tmp = (char**) malloc(sizeof (char*) * (currtblsz * 2 + 1));
if (tmp) if (tmp)
{ {
@ -1616,7 +1616,7 @@ int32_t qc_mysql_get_database_names(GWBUF* querybuf, char*** databasesp, int* si
if (i >= currsz) if (i >= currsz)
{ {
tmp = (char**) realloc(databases, tmp = (char**) realloc(databases,
sizeof (char*)*(currsz * 2 + 1)); sizeof (char*) * (currsz * 2 + 1));
if (tmp == NULL) if (tmp == NULL)
{ {
@ -2609,7 +2609,8 @@ const int IDX_DATADIR = 2;
const int IDX_LANGUAGE = 3; const int IDX_LANGUAGE = 3;
const int N_OPTIONS = (sizeof(server_options) / sizeof(server_options[0])) - 1; const int N_OPTIONS = (sizeof(server_options) / sizeof(server_options[0])) - 1;
const char* server_groups[] = { const char* server_groups[] =
{
"embedded", "embedded",
"server", "server",
"server", "server",
@ -2720,48 +2721,48 @@ void qc_mysql_thread_end(void)
extern "C" extern "C"
{ {
MXS_MODULE* MXS_CREATE_MODULE() MXS_MODULE* MXS_CREATE_MODULE()
{
static QUERY_CLASSIFIER qc =
{ {
qc_mysql_setup, static QUERY_CLASSIFIER qc =
qc_mysql_process_init,
qc_mysql_process_end,
qc_mysql_thread_init,
qc_mysql_thread_end,
qc_mysql_parse,
qc_mysql_get_type,
qc_mysql_get_operation,
qc_mysql_get_created_table_name,
qc_mysql_is_drop_table_query,
qc_mysql_get_table_names,
NULL,
qc_mysql_query_has_clause,
qc_mysql_get_database_names,
qc_mysql_get_prepare_name,
qc_mysql_get_field_info,
qc_mysql_get_function_info,
qc_mysql_get_preparable_stmt,
};
static MXS_MODULE info =
{
MXS_MODULE_API_QUERY_CLASSIFIER,
MXS_MODULE_IN_DEVELOPMENT,
QUERY_CLASSIFIER_VERSION,
"Query classifier based upon MySQL Embedded",
"V1.0.0",
&qc,
qc_mysql_process_init,
qc_mysql_process_end,
qc_mysql_thread_init,
qc_mysql_thread_end,
{ {
{MXS_END_MODULE_PARAMS} qc_mysql_setup,
} qc_mysql_process_init,
}; qc_mysql_process_end,
qc_mysql_thread_init,
qc_mysql_thread_end,
qc_mysql_parse,
qc_mysql_get_type,
qc_mysql_get_operation,
qc_mysql_get_created_table_name,
qc_mysql_is_drop_table_query,
qc_mysql_get_table_names,
NULL,
qc_mysql_query_has_clause,
qc_mysql_get_database_names,
qc_mysql_get_prepare_name,
qc_mysql_get_field_info,
qc_mysql_get_function_info,
qc_mysql_get_preparable_stmt,
};
return &info; static MXS_MODULE info =
} {
MXS_MODULE_API_QUERY_CLASSIFIER,
MXS_MODULE_IN_DEVELOPMENT,
QUERY_CLASSIFIER_VERSION,
"Query classifier based upon MySQL Embedded",
"V1.0.0",
&qc,
qc_mysql_process_init,
qc_mysql_process_end,
qc_mysql_thread_init,
qc_mysql_thread_end,
{
{MXS_END_MODULE_PARAMS}
}
};
return &info;
}
} }

View File

@ -993,7 +993,7 @@ static void update_field_infos(QC_SQLITE_INFO* info,
default: default:
MXS_DEBUG("Token %d not handled explicitly.", pExpr->op); MXS_DEBUG("Token %d not handled explicitly.", pExpr->op);
// Fallthrough intended. // Fallthrough intended.
case TK_BETWEEN: case TK_BETWEEN:
case TK_CASE: case TK_CASE:
case TK_EXISTS: case TK_EXISTS:

View File

@ -49,8 +49,8 @@ int main(int argc, char** argv)
qc_setup("qc_sqlite", NULL); qc_setup("qc_sqlite", NULL);
qc_process_init(); qc_process_init();
infile = fopen(argv[1],"rb"); infile = fopen(argv[1], "rb");
outfile = fopen(argv[2],"wb"); outfile = fopen(argv[2], "wb");
if (infile == NULL || outfile == NULL) if (infile == NULL || outfile == NULL)
{ {

View File

@ -12,520 +12,545 @@
static char datadir[1024] = ""; static char datadir[1024] = "";
static char mysqldir[1024] = ""; static char mysqldir[1024] = "";
static char* server_options[] = { static char* server_options[] =
"MariaDB Corporation MaxScale", {
"--datadir=", "MariaDB Corporation MaxScale",
"--default-storage-engine=myisam", "--datadir=",
NULL "--default-storage-engine=myisam",
NULL
}; };
const int num_elements = (sizeof(server_options) / sizeof(char *)) - 1; const int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char* server_groups[] = { static char* server_groups[] =
"embedded", {
"server", "embedded",
"server", "server",
NULL "server",
NULL
}; };
static void slcursor_add_case( static void slcursor_add_case(
slist_cursor_t* c, slist_cursor_t* c,
void* data) void* data)
{ {
slcursor_add_data(c, data); slcursor_add_data(c, data);
} }
typedef struct query_test_st query_test_t; typedef struct query_test_st query_test_t;
struct query_test_st { struct query_test_st
skygw_chk_t qt_chk_top; {
const char* qt_query_str; skygw_chk_t qt_chk_top;
skygw_query_type_t qt_query_type; const char* qt_query_str;
skygw_query_type_t qt_result_type; skygw_query_type_t qt_query_type;
bool qt_should_fail; skygw_query_type_t qt_result_type;
bool qt_exec_also_in_server; bool qt_should_fail;
skygw_chk_t qt_chk_tail; bool qt_exec_also_in_server;
skygw_chk_t qt_chk_tail;
}; };
static query_test_t* query_test_init( static query_test_t* query_test_init(
const char* query_str, const char* query_str,
skygw_query_type_t query_type, skygw_query_type_t query_type,
bool case_should_fail, bool case_should_fail,
bool exec_also_in_server) bool exec_also_in_server)
{ {
query_test_t* qtest; query_test_t* qtest;
qtest = (query_test_t *)calloc(1, sizeof(query_test_t)); qtest = (query_test_t *)calloc(1, sizeof(query_test_t));
ss_dassert(qtest != NULL); ss_dassert(qtest != NULL);
qtest->qt_chk_top = CHK_NUM_QUERY_TEST; qtest->qt_chk_top = CHK_NUM_QUERY_TEST;
qtest->qt_chk_tail = CHK_NUM_QUERY_TEST; qtest->qt_chk_tail = CHK_NUM_QUERY_TEST;
qtest->qt_query_str = query_str; qtest->qt_query_str = query_str;
qtest->qt_query_type = query_type; qtest->qt_query_type = query_type;
qtest->qt_should_fail = case_should_fail; qtest->qt_should_fail = case_should_fail;
qtest->qt_exec_also_in_server = exec_also_in_server; qtest->qt_exec_also_in_server = exec_also_in_server;
return qtest; return qtest;
} }
const char* query_test_get_querystr( const char* query_test_get_querystr(
query_test_t* qt) query_test_t* qt)
{ {
CHK_QUERY_TEST(qt); CHK_QUERY_TEST(qt);
return qt->qt_query_str; return qt->qt_query_str;
} }
static skygw_query_type_t query_test_get_query_type( static skygw_query_type_t query_test_get_query_type(
query_test_t* qt) query_test_t* qt)
{ {
CHK_QUERY_TEST(qt); CHK_QUERY_TEST(qt);
return qt->qt_query_type; return qt->qt_query_type;
} }
static skygw_query_type_t query_test_get_result_type( static skygw_query_type_t query_test_get_result_type(
query_test_t* qt) query_test_t* qt)
{ {
CHK_QUERY_TEST(qt); CHK_QUERY_TEST(qt);
return qt->qt_result_type; return qt->qt_result_type;
} }
static bool query_test_types_match( static bool query_test_types_match(
query_test_t* qt) query_test_t* qt)
{ {
CHK_QUERY_TEST(qt); CHK_QUERY_TEST(qt);
return (qt->qt_query_type == qt->qt_result_type); return (qt->qt_query_type == qt->qt_result_type);
} }
static bool query_test_exec_also_in_server( static bool query_test_exec_also_in_server(
query_test_t* qt) query_test_t* qt)
{ {
CHK_QUERY_TEST(qt); CHK_QUERY_TEST(qt);
return (qt->qt_exec_also_in_server); return (qt->qt_exec_also_in_server);
} }
static query_test_t* slcursor_get_case( static query_test_t* slcursor_get_case(
slist_cursor_t* c) slist_cursor_t* c)
{ {
return (query_test_t*)slcursor_get_data(c); return (query_test_t*)slcursor_get_data(c);
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
slist_cursor_t* c; slist_cursor_t* c;
const char* q; const char* q;
query_test_t* qtest; query_test_t* qtest;
bool succp; bool succp;
bool failp = true; bool failp = true;
unsigned int f = 0; unsigned int f = 0;
int nsucc = 0; int nsucc = 0;
int nfail = 0; int nfail = 0;
int rc = 0; int rc = 0;
MYSQL* mysql; MYSQL* mysql;
char* workingdir; char* workingdir;
char ddoption[1024]; char ddoption[1024];
ss_dfprintf(stderr, ">> testmain\n"); ss_dfprintf(stderr, ">> testmain\n");
c = slist_init(); c = slist_init();
/** Test some functions */ /** Test some functions */
q = "SELECT MY_UDF('Hello')"; q = "SELECT MY_UDF('Hello')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
/** This could be QUERY_TYPE_LOCAL_READ */ /** This could be QUERY_TYPE_LOCAL_READ */
q = "SELECT repeat('a', 1024)"; q = "SELECT repeat('a', 1024)";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
/** This could be QUERY_TYPE_LOCAL_READ */ /** This could be QUERY_TYPE_LOCAL_READ */
q = "SELECT soundex('Hello')"; q = "SELECT soundex('Hello')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
q = "SELECT ssoundexx('Hello')"; q = "SELECT ssoundexx('Hello')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
/** This could be QUERY_TYPE_LOCAL_READ */ /** This could be QUERY_TYPE_LOCAL_READ */
q = "SELECT now()"; q = "SELECT now()";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
/** This could be QUERY_TYPE_LOCAL_READ */ /** This could be QUERY_TYPE_LOCAL_READ */
q = "SELECT rand()"; q = "SELECT rand()";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
q = "SELECT rand(234), MY_UDF('Hello'), soundex('Hello')"; q = "SELECT rand(234), MY_UDF('Hello'), soundex('Hello')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
/** Read-only SELECTs */ /** Read-only SELECTs */
q = "SELECT user from mysql.user"; q = "SELECT user from mysql.user";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
q = "select tt1.id, tt2.id from t1 tt1, t2 tt2 where tt1.name is " q = "select tt1.id, tt2.id from t1 tt1, t2 tt2 where tt1.name is "
"not null and tt2.name is not null"; "not null and tt2.name is not null";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, false)); query_test_init(q, QUERY_TYPE_READ, false, false));
/** SELECT ..INTO clauses > session updates */ /** SELECT ..INTO clauses > session updates */
q = "SELECT user from mysql.user INTO DUMPFILE '/tmp/dump1'"; q = "SELECT user from mysql.user INTO DUMPFILE '/tmp/dump1'";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "SELECT user INTO DUMPFILE '/tmp/dump2 ' from mysql.user"; q = "SELECT user INTO DUMPFILE '/tmp/dump2 ' from mysql.user";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "SELECT user from mysql.user INTO OUTFILE '/tmp/out1'"; q = "SELECT user from mysql.user INTO OUTFILE '/tmp/out1'";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
/** Database and table name must be separated by a dot */ /** Database and table name must be separated by a dot */
q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql-user"; q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql-user";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, false));
/** Database and table name must be separated by a dot */ /** Database and table name must be separated by a dot */
q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql_foo_user"; q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql_foo_user";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "SELECT user FROM mysql.user limit 1 INTO @local_variable"; q = "SELECT user FROM mysql.user limit 1 INTO @local_variable";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "SELECT user INTO @local_variable FROM mysql.user limit 1"; q = "SELECT user INTO @local_variable FROM mysql.user limit 1";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "SELECT non_existent_attr INTO @d FROM non_existent_table"; q = "SELECT non_existent_attr INTO @d FROM non_existent_table";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
q = "select * from table1 " q = "select * from table1 "
"where table1.field IN " "where table1.field IN "
"(select * from table1a union select * from table1b) union " "(select * from table1a union select * from table1b) union "
"select * from table2 where table2.field = " "select * from table2 where table2.field = "
"(select (select f1 from table2a where table2a.f2 = table2b.f3) " "(select (select f1 from table2a where table2a.f2 = table2b.f3) "
"from table2b where table2b.f1 = table2.f2) union " "from table2b where table2b.f1 = table2.f2) union "
"select * from table3"; "select * from table3";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_READ, false, true)); query_test_init(q, QUERY_TYPE_READ, false, true));
/** RENAME TABLEs */ /** RENAME TABLEs */
q = "RENAME TABLE T1 to T2"; q = "RENAME TABLE T1 to T2";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
/** INSERTs */ /** INSERTs */
q = "INSERT INTO T1 (SELECT * FROM T2)"; q = "INSERT INTO T1 (SELECT * FROM T2)";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "INSERT INTO T1 VALUES(2, 'foo', 'toomanyattributes')"; q = "INSERT INTO T1 VALUES(2, 'foo', 'toomanyattributes')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "INSERT INTO T2 VALUES(1, 'sthrgey')"; q = "INSERT INTO T2 VALUES(1, 'sthrgey')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
q = "INSERT INTO T2 VALUES(8, 'ergstrhe')"; q = "INSERT INTO T2 VALUES(8, 'ergstrhe')";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
q = "INSERT INTO T2 VALUES(9, NULL)"; q = "INSERT INTO T2 VALUES(9, NULL)";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
/** Ok, delimeter is client-side parameter which shouldn't be handled /** Ok, delimeter is client-side parameter which shouldn't be handled
* on server side. * on server side.
*/ */
q = "delimiter //"; q = "delimiter //";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, true)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, true));
/** SETs, USEs > Session updates */ /** SETs, USEs > Session updates */
q = "SET @a=1"; q = "SET @a=1";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
q = "USE TEST"; q = "USE TEST";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
/** Object creation statements */ /** Object creation statements */
q = "create procedure si (out param1 int) \nbegin select count(*) " q = "create procedure si (out param1 int) \nbegin select count(*) "
"into param1 from t1; \nend"; "into param1 from t1; \nend";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "CREATE TABLE T1 (id integer primary key, name varchar(10))"; q = "CREATE TABLE T1 (id integer primary key, name varchar(10))";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "DROP TABLE T1"; q = "DROP TABLE T1";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
q = "ALTER TABLE T1 ADD COLUMN WHYME INTEGER NOT NULL"; q = "ALTER TABLE T1 ADD COLUMN WHYME INTEGER NOT NULL";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
q = "TRUNCATE TABLE T1"; q = "TRUNCATE TABLE T1";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, false)); query_test_init(q, QUERY_TYPE_WRITE, false, false));
q = "DROP SERVER IF EXISTS VICTIMSRV"; q = "DROP SERVER IF EXISTS VICTIMSRV";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
q = "CREATE USER FOO IDENTIFIED BY 'BAR'"; q = "CREATE USER FOO IDENTIFIED BY 'BAR'";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "OPTIMIZE NO_WRITE_TO_BINLOG TABLE T1"; q = "OPTIMIZE NO_WRITE_TO_BINLOG TABLE T1";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
q = "SELECT NOW();CREATE TABLE T1 (ID INTEGER);" q = "SELECT NOW();CREATE TABLE T1 (ID INTEGER);"
"SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)"; "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_WRITE, false, true)); query_test_init(q, QUERY_TYPE_WRITE, false, true));
/** Setting database makes this SESSION_WRITE */ /** Setting database makes this SESSION_WRITE */
q = "USE TEST;CREATE TABLE T1 (ID INTEGER);" q = "USE TEST;CREATE TABLE T1 (ID INTEGER);"
"SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)"; "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
slcursor_add_case( slcursor_add_case(
c, c,
query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true)); query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
/** /**
* Init libmysqld. * Init libmysqld.
*/ */
workingdir = getenv("PWD"); workingdir = getenv("PWD");
if (workingdir == NULL) {
fprintf(stderr,
"Failed to resolve the working directory, $PWD is not "
"set.\n");
ss_dassert(workingdir != NULL);
} else if (access(workingdir, R_OK) != 0) {
char errbuf[STRERROR_BUFLEN];
fprintf(stderr,
"Failed to access the working directory due %d, %s\n",
errno,
strerror_r(errno, errbuf, sizeof(errbuf)));
ss_dassert(false);
} else {
char** so = server_options;
snprintf(datadir, 1023, "%s/data", workingdir);
mkdir(datadir, 0777);
snprintf(ddoption, 1023, "--datadir=%s", datadir);
while (strncmp(*so++, "--datadir=", 10) != 0) ;
if (*so == NULL) {
fprintf(stderr, "Failed to find datadir option.\n");
ss_dassert(*so != NULL);
}
*so = ddoption;
snprintf(mysqldir, 1023, "%s/mysql", workingdir);
setenv("MYSQL_HOME", mysqldir, 1);
}
failp = mysql_library_init(num_elements, server_options, server_groups);
if (failp) {
MYSQL* mysql = mysql_init(NULL);
ss_dassert(mysql != NULL);
fprintf(stderr,
"mysql_init failed, %d : %s\n",
mysql_errno(mysql),
mysql_error(mysql));
ss_dassert(!failp);
}
if (workingdir == NULL)
{
fprintf(stderr, fprintf(stderr,
"\nExecuting selected cases in " "Failed to resolve the working directory, $PWD is not "
"skygw_query_classifier_get_type :\n\n"); "set.\n");
/** ss_dassert(workingdir != NULL);
* Set cursor to the beginning, scan through the list and execute }
* test cases. else if (access(workingdir, R_OK) != 0)
*/ {
succp = slcursor_move_to_begin(c); char errbuf[STRERROR_BUFLEN];
while(succp) {
qtest = slcursor_get_case(c);
qtest->qt_result_type =
skygw_query_classifier_get_type(qtest->qt_query_str, f,
&mysql);
succp = slcursor_step_ahead(c);
}
/**
* Scan through test results and compare them against expected
* results.
*/
succp = slcursor_move_to_begin(c);
fprintf(stderr, "\nScanning through the results :\n\n");
while(succp) {
qtest = slcursor_get_case(c);
if (!query_test_types_match(qtest)) {
nfail += 1;
ss_dfprintf(stderr,
"* Failed: \"%s\" -> %s (Expected %s)\n",
query_test_get_querystr(qtest),
STRQTYPE(query_test_get_result_type(qtest)),
STRQTYPE(query_test_get_query_type(qtest)));
} else {
nsucc += 1;
ss_dfprintf(stderr,
"Succeed\t: \"%s\" -> %s\n",
query_test_get_querystr(qtest),
STRQTYPE(query_test_get_query_type(qtest)));
}
succp = slcursor_step_ahead(c);
}
fprintf(stderr, fprintf(stderr,
"------------------------------------------\n" "Failed to access the working directory due %d, %s\n",
"Tests in total %d, SUCCEED %d, FAILED %d\n", errno,
nsucc+nfail, strerror_r(errno, errbuf, sizeof(errbuf)));
nsucc, ss_dassert(false);
nfail); }
else
{
char** so = server_options;
snprintf(datadir, 1023, "%s/data", workingdir);
mkdir(datadir, 0777);
snprintf(ddoption, 1023, "--datadir=%s", datadir);
/** while (strncmp(*so++, "--datadir=", 10) != 0) ;
* Scan test results and re-execute those which are marked to be
* executed also in the server. This serves mostly debugging purposes.
*/
succp = slcursor_move_to_begin(c);
mysql = mysql_init(NULL);
if (mysql == NULL) { if (*so == NULL)
fprintf(stderr, "mysql_init failed\n"); {
ss_dassert(mysql != NULL); fprintf(stderr, "Failed to find datadir option.\n");
ss_dassert(*so != NULL);
} }
*so = ddoption;
mysql_options(mysql, snprintf(mysqldir, 1023, "%s/mysql", workingdir);
MYSQL_READ_DEFAULT_GROUP, setenv("MYSQL_HOME", mysqldir, 1);
"libmysqld_client"); }
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL); failp = mysql_library_init(num_elements, server_options, server_groups);
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql = mysql_real_connect(mysql,
NULL,
"skygw",
"skygw",
NULL,
0,
NULL,
CLIENT_MULTI_STATEMENTS);
if (mysql == NULL) {
fprintf(stderr, "mysql_real_connect failed\n");
ss_dassert(mysql != NULL);
}
if (failp)
{
MYSQL* mysql = mysql_init(NULL);
ss_dassert(mysql != NULL);
fprintf(stderr, fprintf(stderr,
"\nRe-execution of selected cases in Embedded server :\n\n"); "mysql_init failed, %d : %s\n",
mysql_errno(mysql),
mysql_error(mysql));
ss_dassert(!failp);
}
while(succp) { fprintf(stderr,
qtest = slcursor_get_case(c); "\nExecuting selected cases in "
"skygw_query_classifier_get_type :\n\n");
/**
* Set cursor to the beginning, scan through the list and execute
* test cases.
*/
succp = slcursor_move_to_begin(c);
if (query_test_exec_also_in_server(qtest)) { while (succp)
MYSQL_RES* results; {
MYSQL_ROW record; qtest = slcursor_get_case(c);
const char* query_str; qtest->qt_result_type =
skygw_query_classifier_get_type(qtest->qt_query_str, f,
&mysql);
succp = slcursor_step_ahead(c);
}
/**
* Scan through test results and compare them against expected
* results.
*/
succp = slcursor_move_to_begin(c);
fprintf(stderr, "\nScanning through the results :\n\n");
query_str = query_test_get_querystr(qtest); while (succp)
failp = mysql_query(mysql, query_str); {
qtest = slcursor_get_case(c);
if (failp) { if (!query_test_types_match(qtest))
ss_dfprintf(stderr, {
"* Failed: \"%s\" -> %d : %s\n", nfail += 1;
query_str, ss_dfprintf(stderr,
mysql_errno(mysql), "* Failed: \"%s\" -> %s (Expected %s)\n",
mysql_error(mysql)); query_test_get_querystr(qtest),
} else { STRQTYPE(query_test_get_result_type(qtest)),
ss_dfprintf(stderr, STRQTYPE(query_test_get_query_type(qtest)));
"Succeed\t: \"%s\"\n", }
query_str); else
results = mysql_store_result(mysql); {
nsucc += 1;
ss_dfprintf(stderr,
"Succeed\t: \"%s\" -> %s\n",
query_test_get_querystr(qtest),
STRQTYPE(query_test_get_query_type(qtest)));
}
succp = slcursor_step_ahead(c);
}
fprintf(stderr,
"------------------------------------------\n"
"Tests in total %d, SUCCEED %d, FAILED %d\n",
nsucc + nfail,
nsucc,
nfail);
if (results != NULL) { /**
* Scan test results and re-execute those which are marked to be
* executed also in the server. This serves mostly debugging purposes.
*/
succp = slcursor_move_to_begin(c);
mysql = mysql_init(NULL);
while((record = mysql_fetch_row(results))) { if (mysql == NULL)
while(record != NULL && *record != NULL) { {
ss_dfprintf(stderr, "%s ", *record); fprintf(stderr, "mysql_init failed\n");
record++; ss_dassert(mysql != NULL);
} }
ss_dfprintf(stderr, "\n");
} mysql_options(mysql,
mysql_free_result(results); MYSQL_READ_DEFAULT_GROUP,
} "libmysqld_client");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql = mysql_real_connect(mysql,
NULL,
"skygw",
"skygw",
NULL,
0,
NULL,
CLIENT_MULTI_STATEMENTS);
if (mysql == NULL)
{
fprintf(stderr, "mysql_real_connect failed\n");
ss_dassert(mysql != NULL);
}
fprintf(stderr,
"\nRe-execution of selected cases in Embedded server :\n\n");
while (succp)
{
qtest = slcursor_get_case(c);
if (query_test_exec_also_in_server(qtest))
{
MYSQL_RES* results;
MYSQL_ROW record;
const char* query_str;
query_str = query_test_get_querystr(qtest);
failp = mysql_query(mysql, query_str);
if (failp)
{
ss_dfprintf(stderr,
"* Failed: \"%s\" -> %d : %s\n",
query_str,
mysql_errno(mysql),
mysql_error(mysql));
}
else
{
ss_dfprintf(stderr,
"Succeed\t: \"%s\"\n",
query_str);
results = mysql_store_result(mysql);
if (results != NULL)
{
while ((record = mysql_fetch_row(results)))
{
while (record != NULL && *record != NULL)
{
ss_dfprintf(stderr, "%s ", *record);
record++;
} }
ss_dfprintf(stderr, "\n");
}
mysql_free_result(results);
} }
succp = slcursor_step_ahead(c); }
} }
slist_done(c); succp = slcursor_step_ahead(c);
fprintf(stderr, "------------------------------------------\n");
}
slist_done(c);
fprintf(stderr, "------------------------------------------\n");
return_with_handle: return_with_handle:
mysql_close(mysql); mysql_close(mysql);
mysql_thread_end(); mysql_thread_end();
mysql_library_end(); mysql_library_end();
return_without_server: return_without_server:
ss_dfprintf(stderr, "\n<< testmain\n"); ss_dfprintf(stderr, "\n<< testmain\n");
fflush(stderr); fflush(stderr);
return rc; return rc;
} }