Testing N1QL (Part 4): Using Query Templates to Create Queries
Using the MySQL Random Query Generator, you can make your own query templates. Let's use those to take SQL queries and turn them into N1QL queries.
Welcome back to Testing N1QL! If you're just getting in, check out part one, part two, and part three. If you've been following along, you've learned how to use the MySQL Random Query Generator to create query templates. Now let's put those templates to good use. First, let's start with a question: How do we generate two equivalent queries from a single template?
The first step is to map this template to a MySQL table. This table has certain fields and values.
Example of a row in the table:
Field Names |
Field Values |
primary_key_id |
“357” |
int_field1 |
393 |
char_field1 |
"H" |
bool_field1 |
true |
decimal_field1 |
5201 |
datetime_field1 |
"2006-04-05 00:00:00" |
We take the signature and map the fields to MySQL table fields, which then converts them into simple queries like:
select * from simple_table where numeric_field =sample_value
Of course in this example, simple_table is a static table definition in our MySQL database schema.
Advantages of Template vs. Actual Queries
By generating these signatures, which are independent of data sets, we can have any data set mapped against the signature. This way, we are generalizing the grammar. When we change the dataset, the queries will change.
SQL queries to be run against MySQL are generated by replacing various field values in the query template with randomized data.
In our code, N1QL queries are generated by tokenizing each string in the query template and replacing keywords wherever necessary. Also, we take care of various bracket changes while parsing.
Here is an Example of five matching SQL/N1QL queries, generated as explained above:
SQL Query |
N1QL Query |
|
|
|
|
|
|
|
|
|
|
Comments