SqlParser::SqlParser(GPtrArray *tk, GString *sql) : tokens(tk), inputSql(sql) { needFreeTokens = tokens == NULL; if (tokens == NULL) { tokens = g_ptr_array_new(); sql_tokenizer(tokens, sql->str, sql->len); } }
/** * split the SQL query into a stream of tokens */ int proxy_tokenize(lua_State *L) { size_t str_len; const char *str = luaL_checklstring(L, 1, &str_len); GPtrArray *tokens = sql_tokens_new(); GPtrArray **tokens_p; sql_tokenizer(tokens, str, str_len); tokens_p = lua_newuserdata(L, sizeof(tokens)); /* (sp += 1) */ *tokens_p = tokens; sql_tokenizer_lua_getmetatable(L); lua_setmetatable(L, -2); /* tie the metatable to the udata (sp -= 1) */ return 1; }
int main(void) { const char *sql = test_sql; int tlen; int tok; bool stdstr = false; while (1) { tok = sql_tokenizer(sql, &tlen, stdstr); if (tok == 0) { printf("EOF\n"); break; } else if (tok < 0) { printf("ERR\n"); return 1; } printf("tok=%d len=%d str=<%.*s>\n", tok, tlen, tlen, sql); sql += tlen; } return 0; }