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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Convert MySQL Database to SQL Server
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Running Streaming ETL Pipelines with Apache Flink on Zeppelin Notebooks
  • The Complete Tutorial on the Top 5 Ways to Query Your Relational Database in JavaScript - Part 2

Trending

  • Navigating Change Management: A Guide for Engineers
  • Using Python Libraries in Java
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Data Engineering
  3. Databases
  4. Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures

Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures

This is part 3 of migrating from Sakila to Couchbase series. Today, we will cover stored procedures that we can implement as functions.

By 
Isha Kandaswamy user avatar
Isha Kandaswamy
·
Dec. 08, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

Stored procedures are a set of SQL statements stored together under a given name so as to be reused by multiple queries. Stored procedures by themselves are not supported in N1QL. (PROCEDURAL SQL - proc: BEGIN END LEAVE PROC) but as a workaround, we can implement them as functions. 

One thing to note is that since we don't have temp table support, creating temp tables is hard to map. However, we can create a temporary collection and use that or create a temp bucket that can house multiple temp tables mapped as collections.

Let's quickly look at some examples that convert a SQL stored procedure into N1QL UDFs: 

rewards_report 

The rewards_report stored procedure generates a customizable list of the top customers for the previous month. 

rewards_report(min_monthly_purchases, min_dollar_amt_purchased)  

This returns count_rewardees and needs to be broken down into several steps when translating to N1QL. 

Step 1 - Date Manipulation 

 
last_month_start = DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH) 

last_month_start = STR_TO_DATE(CONCAT(YEAR(last_month_start),
                                      '-',
                                      MONTH(last_month_start),'-01')
                               ,'%Y-%m-%d');


It becomes the following when translated to N1QL:

 
DATE_FORMAT_STR( DATE_TRUNC_STR( DATE_ADD_STR( 
                                              CLOCK_STR(),-1,"month")
                               ,'month'),
                 '1111-11-11');

last_month_end = LAST_DAY(last_month_start);

select DATE_ADD_STR(DATE_ADD_STR('2021-02-01',1,'month'), -1,'day');

 

Step 2 - The next step is to manually create a collection tmpCustomer - customer_id under Sakila bucket, new scope metad, and then do the following:

 
INSERT INTO Sakila.metad.tmpCustomer (KEY p.customer_id, Value p.customer_id)

SELECT p 
FROM Sakila._default.payment as p 
WHERE p.payment_date BETWEEN $last_month_start AND $last_month_end
GROUP BY p.customer_id
HAVING SUM(p.amount) > min_dollar_amount_purchased
AND COUNT(customer_id) > min_monthly_purchases;

SELECT COUNT(*) as count_rewardees FROM Sakila.metad.tmpCustomer;

SELECT c.* 
FROM Sakila.metad.tmpCustomer as t 
INNER JOIN Sakila._default.customer as c 
ON t.customer_id = c.customer_id; 

 

Step 3 - Drop collection TMP customer and then drop scope metad. 

This can be done using the UI itself. 

Film_in_stock Stored Procedure

The film_in_stock stored procedure determines whether any copies of a given film are in stock at a given store.

 
CREATE FUNCTION film_stock(p_film_id,p_store_id) 
{(SELECT RAW inventory_id
     FROM sakila._default.inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND inventory_in_stock(inventory_id))
 };

CREATE FUNCTION film_count(p_film_if,p_store_id) 
{(SELECT RAW COUNT(*)
     FROM sakila._default.inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND inventory_in_stock(inventory_id))[0]
};

CREATE FUNCTION film_in_stock(p_film_id,p_store_id,countval) 
{ (select RAW case when countval 
                   then film_count(p_film_id,p_store_id) 
                   else film_stock(p_film_id,p_store_id) end)
};


Film_not_in_stock Stored Procedure 

The film_not_in_stock stored procedure determines whether there are any copies of a given film not in stock (rented out) at a given store.

 
CREATE FUNCTION film_not_in_stock(p_film_id,p_store_id) 
{(SELECT RAW inventory_id
     FROM sakila._default.inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND NOT inventory_in_stock(inventory_id))
};

CREATE FUNCTION film_count_not_in(p_film_if,p_store_id) 
{(SELECT RAW COUNT(*)
     FROM sakila._default.inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND NOT  inventory_in_stock(inventory_id))[0]
};

CREATE FUNCTION film_not_in_stock(p_film_id,p_store_id,countval) 
{ (select RAW case when countval 
                   then film_count_not_in(p_film_id,p_store_id) 
                   else film_not_in_stock(p_film_id,p_store_id) end)
};
                   


With the addition of Javascript UDFs in the upcoming release, the mapping of SQL functions and procedures should become easier. In Part 4 we will cover mapping triggers to eventing functions. 

Database sql Drops (app) Release (computing) JavaScript Convert (command) Extract, transform, load House (operating system) Workaround

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert MySQL Database to SQL Server
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Running Streaming ETL Pipelines with Apache Flink on Zeppelin Notebooks
  • The Complete Tutorial on the Top 5 Ways to Query Your Relational Database in JavaScript - Part 2

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!