Skip to content

Commit

Permalink
parse _ in host variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gsauthof committed Jul 28, 2013
1 parent 2be79e9 commit 6941981
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions parsesql.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ struct SQL_Token {
typedef struct SQL_Token SQL_Token;


static is_hostvar_char(char c)
{
return isalnum(c) || c == '_';
}

size_t sql_count_host_vars(const char *s)
{
size_t ret = 0;
Expand All @@ -49,7 +54,7 @@ size_t sql_count_host_vars(const char *s)
if (!a || b<a) {
++ret;
pos = b + 1;
for (; isalnum(*pos); ++pos)
for (; is_hostvar_char(*pos); ++pos)
;
if (*pos == ':')
++pos;
Expand Down Expand Up @@ -97,7 +102,7 @@ int sql_tokenize(const char *s,
}
const char *str_pos = b+1;
for (;;) {
for (; isalnum(*str_pos); ++str_pos)
for (; is_hostvar_char(*str_pos); ++str_pos)
;
if (*str_pos != ':')
break;
Expand Down
34 changes: 34 additions & 0 deletions unittest/test_parsesql.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ START_TEST(token_06)
}
END_TEST

START_TEST(token_07)
{
const char inp[] = "foo :xyz_id bar in :ab_id:ab_id_ind blah);";
SQL_Token *tokens = 0;
size_t size = 0;
int ret = sql_tokenize(inp, &tokens, &size);
ck_assert_int_eq(ret, 0);
ck_assert(size>3);
struct st {
const char *a;
bool b;
};
typedef struct st st;
const st src[] = {
{ "foo ", false },
{ ":xyz_id", true },
{ " bar in ", false },
{ ":ab_id:ab_id_ind", true },
{ " blah);", false },
{ 0, 0}
};
for (size_t i = 0; i<5; ++i) {
ck_assert(tokens[i].host_var == src[i].b);
char b[128] = {0};
strncpy(b, tokens[i].begin, tokens[i].end - tokens[i].begin);
ck_assert_str_eq(b, src[i].a);
}
ck_assert(!tokens[5].begin);
ck_assert(!tokens[5].end);
free(tokens);
}
END_TEST

START_TEST(quote_01)
{
char out[32] = {0};
Expand Down Expand Up @@ -319,6 +352,7 @@ TCase *parsesql_tc_create()
tcase_add_test(tc, token_04);
tcase_add_test(tc, token_05);
tcase_add_test(tc, token_06);
tcase_add_test(tc, token_07);
tcase_add_test(tc, quote_01);
tcase_add_test(tc, quote_02);
tcase_add_test(tc, quote_03);
Expand Down

0 comments on commit 6941981

Please sign in to comment.