DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Testing N1QL (Part 4): Using Query Templates to Create Queries

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.

Prerna Manaktala user avatar by
Prerna Manaktala
·
Mar. 05, 17 · Tutorial
Like (4)
Save
Tweet
Share
4.19K Views

Join the DZone community and get the full member experience.

Join For Free

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

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 )) )



Database Template

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • How Elasticsearch Works
  • Front-End Troubleshooting Using OpenTelemetry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: