Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing N1QL (Part 4): Using Query Templates to Create Queries

DZone's Guide to

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.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

Welcome back to Testing N1QL! If you're just getting in, check out part onepart 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

SELECT *
FROM   simple_table
WHERE  (( char_field1 IS NOT NULL ))
      AND (( ( decimal_field1 >= ( 5139 - 4 ) )
             AND ( decimal_field1 >= Truncate(1000 * Abs(5139), 0) ) ))


SELECT *
FROM   simple_table_db_9237_simple_table
WHERE  (( char_field1 IS NOT NULL ))
      AND (( ( decimal_field1 >= ( 5139 - 4 ) )
             AND ( decimal_field1 >= Trunc(1000 * Abs(5139), 0) ) ))

   

SELECT DISTINCT( int_field1 )
FROM   simple_table
WHERE  NOT (( datetime_field1 > '2006-05-16 00:00:00' + interval 1 hour ))


SELECT DISTINCT( int_field1 )
FROM   simple_table_db_9237_simple_table
WHERE  NOT (( datetime_field1 > Date_add_str('2006-05-16 00:00:00', 1, 'hour') ))


SELECT int_field1,
      primary_key_id,
      bool_field1
FROM   simple_table
WHERE  ( NOT ( int_field1 = ( 5114 / Round(Pi()) ) ) )


SELECT int_field1 ,
      primary_key_id ,
      bool_field1
FROM   simple_table_db_9237_simple_tableuse index(`#PRIMARY` using gsi)
WHERE  (
             NOT (
                    int_field1 = ( 5114 / round(pi()) )) )


SELECT primary_key_id,
      varchar_field1
FROM   simple_table
WHERE  ( ( NOT ( ( char_field1 NOT LIKE '%N' )
                 OR ( varchar_field1 <> Replace("aaklsiiabz", "c", "d") ) ) )
        AND ( varchar_field1 IS NULL ) )


SELECT primary_key_id,
      varchar_field1
FROM   simple_table_db_9237_simple_table
WHERE  ( ( NOT ( ( char_field1 NOT LIKE '%N' )
                 OR ( varchar_field1 <> Replace("aaklsiiabz", "c", "d") ) ) )
        AND ( varchar_field1 IS NULL ) )


SELECT int_field1
FROM   simple_table
WHERE  (( decimal_field1 = 5139 ))
      AND ( NOT (( bool_field1 )) )


SELECT int_field1
FROM   simple_table_db_9237_simple_table
WHERE  (( decimal_field1 = 5139 ))
      AND ( NOT (( bool_field1 )) )



Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

Topics:
n1ql ,query template ,database ,mysql ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}