diff options
author | Bjorn Munch <bjorn.munch@oracle.com> | 2010-11-10 09:42:14 +0100 |
---|---|---|
committer | Bjorn Munch <bjorn.munch@oracle.com> | 2010-11-10 09:42:14 +0100 |
commit | 3fab29461661f677fd5ca0be75fab8bcf84eadff (patch) | |
tree | 90c43e6b2e951f532568e83a7a3dc72d59137eb9 /client | |
parent | e0678e2ca624395a447dd8898583c13e31ec2f6a (diff) | |
download | mariadb-git-3fab29461661f677fd5ca0be75fab8bcf84eadff.tar.gz |
Bug #57276 mysqltest: add support for simple compares in if/while conditions
Added more parsing in do_block()
Limitation: left operand must be variable
Also changed var_set_int from 57036 to var_check_int
Added tests to mysqltest.test
Some tests can now be simplified but will take this later
Updated after comments, now white space around operator not needed
Diffstat (limited to 'client')
-rw-r--r-- | client/mysqltest.cc | 145 |
1 files changed, 136 insertions, 9 deletions
diff --git a/client/mysqltest.cc b/client/mysqltest.cc index 1afed9fa310..1cc275a37be 100644 --- a/client/mysqltest.cc +++ b/client/mysqltest.cc @@ -482,7 +482,8 @@ VAR* var_init(VAR* v, const char *name, int name_len, const char *val, int val_len); VAR* var_get(const char *var_name, const char** var_name_end, my_bool raw, my_bool ignore_not_existing); -void eval_expr(VAR* v, const char *p, const char** p_end); +void eval_expr(VAR* v, const char *p, const char** p_end, + bool open_end=false); my_bool match_delimiter(int c, const char *delim, uint length); void dump_result_to_reject_file(char *buf, int size); void dump_warning_messages(); @@ -2040,9 +2041,11 @@ static void var_free(void *v) C_MODE_END -void var_set_int(VAR *v, const char *str) +void var_check_int(VAR *v) { char *endptr; + char *str= v->str_val; + /* Initially assume not a number */ v->int_val= 0; v->is_int= false; @@ -2089,7 +2092,7 @@ VAR *var_init(VAR *v, const char *name, int name_len, const char *val, memcpy(tmp_var->str_val, val, val_len); tmp_var->str_val[val_len]= 0; } - var_set_int(tmp_var, val); + var_check_int(tmp_var); tmp_var->name_len = name_len; tmp_var->str_val_len = val_len; tmp_var->alloced_len = val_alloc_len; @@ -2540,7 +2543,7 @@ void var_copy(VAR *dest, VAR *src) } -void eval_expr(VAR *v, const char *p, const char **p_end) +void eval_expr(VAR *v, const char *p, const char **p_end, bool open_end) { DBUG_ENTER("eval_expr"); @@ -2558,7 +2561,7 @@ void eval_expr(VAR *v, const char *p, const char **p_end) /* Make sure there was just a $variable and nothing else */ const char* end= *p_end + 1; - if (end < expected_end) + if (end < expected_end && !open_end) die("Found junk '%.*s' after $variable in expression", (int)(expected_end - end - 1), end); @@ -2605,7 +2608,7 @@ void eval_expr(VAR *v, const char *p, const char **p_end) v->str_val_len = new_val_len; memcpy(v->str_val, p, new_val_len); v->str_val[new_val_len] = 0; - var_set_int(v, p); + var_check_int(v); } DBUG_VOID_RETURN; } @@ -5498,6 +5501,40 @@ int do_done(struct st_command *command) return 0; } +/* Operands available in if or while conditions */ + +enum block_op { + EQ_OP, + NE_OP, + GT_OP, + GE_OP, + LT_OP, + LE_OP, + ILLEG_OP +}; + + +enum block_op find_operand(const char *start) +{ + char first= *start; + char next= *(start+1); + + if (first == '=' && next == '=') + return EQ_OP; + if (first == '!' && next == '=') + return NE_OP; + if (first == '>' && next == '=') + return GE_OP; + if (first == '>') + return GT_OP; + if (first == '<' && next == '=') + return LE_OP; + if (first == '<') + return LT_OP; + + return ILLEG_OP; +} + /* Process start of a "if" or "while" statement @@ -5523,6 +5560,13 @@ int do_done(struct st_command *command) A '!' can be used before the <expr> to indicate it should be executed if it evaluates to zero. + <expr> can also be a simple comparison condition: + + <variable> <op> <expr> + + The left hand side must be a variable, the right hand side can be a + variable, number, string or `query`. Operands are ==, !=, <, <=, >, >=. + == and != can be used for strings, all can be used for numerical values. */ void do_block(enum block_cmd cmd, struct st_command* command) @@ -5558,6 +5602,9 @@ void do_block(enum block_cmd cmd, struct st_command* command) if (!expr_start++) die("missing '(' in %s", cmd_name); + while (my_isspace(charset_info, *expr_start)) + expr_start++; + /* Check for !<expr> */ if (*expr_start == '!') { @@ -5576,14 +5623,94 @@ void do_block(enum block_cmd cmd, struct st_command* command) die("Missing '{' after %s. Found \"%s\"", cmd_name, p); var_init(&v,0,0,0,0); - eval_expr(&v, expr_start, &expr_end); + /* If expression starts with a variable, it may be a compare condition */ + + if (*expr_start == '$') + { + const char *curr_ptr= expr_end; + eval_expr(&v, expr_start, &curr_ptr, true); + while (my_isspace(charset_info, *++curr_ptr)) + {} + /* If there was nothing past the variable, skip condition part */ + if (curr_ptr == expr_end) + goto NO_COMPARE; + + enum block_op operand= find_operand(curr_ptr); + if (operand == ILLEG_OP) + die("Found junk '%.*s' after $variable in condition", + (int)(expr_end - curr_ptr), curr_ptr); + + /* We could silently allow this, but may be confusing */ + if (not_expr) + die("Negation and comparison should not be combined, please rewrite"); + + /* Skip the 1 or 2 chars of the operand, then white space */ + if (operand == LT_OP || operand == GT_OP) + { + curr_ptr++; + } + else + { + curr_ptr+= 2; + } + while (my_isspace(charset_info, *curr_ptr)) + curr_ptr++; + + VAR v2; + var_init(&v2,0,0,0,0); + eval_expr(&v2, curr_ptr, &expr_end); + + if ((operand!=EQ_OP && operand!=NE_OP) && ! (v.is_int && v2.is_int)) + die ("Only == and != are supported for string values"); + + /* Now we overwrite the first variable with 0 or 1 (for false or true) */ + + switch (operand) + { + case EQ_OP: + if (v.is_int) + v.int_val= (v2.is_int && v2.int_val == v.int_val); + else + v.int_val= !strcmp (v.str_val, v2.str_val); + break; + + case NE_OP: + if (v.is_int) + v.int_val= ! (v2.is_int && v2.int_val == v.int_val); + else + v.int_val= (strcmp (v.str_val, v2.str_val) != 0); + break; + + case LT_OP: + v.int_val= (v.int_val < v2.int_val); + break; + case LE_OP: + v.int_val= (v.int_val <= v2.int_val); + break; + case GT_OP: + v.int_val= (v.int_val > v2.int_val); + break; + case GE_OP: + v.int_val= (v.int_val >= v2.int_val); + break; + } + + v.is_int= TRUE; + } else + { + if (*expr_start != '`' && ! my_isdigit(charset_info, *expr_start)) + die("Expression in if/while must beging with $, ` or a number"); + eval_expr(&v, expr_start, &expr_end); + } + + NO_COMPARE: /* Define inner block */ cur_block++; cur_block->cmd= cmd; - if (v.int_val) + if (v.is_int) { - cur_block->ok= TRUE; + cur_block->ok= (v.int_val != 0); } else /* Any non-empty string which does not begin with 0 is also TRUE */ { |