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

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

December 8, 2021
Third Eye Open mixes creepy tales with paper theatre

Third Eye Open mixes creepy tales with paper theatre

May 23, 2022
Mario Strikers: Battle League Adverts Shoot For Customisation And Chaos

Mario Strikers: Battle League Adverts Shoot For Customisation And Chaos

May 23, 2022
Steelrising steals the show at Bigben Week

Steelrising steals the show at Bigben Week

May 23, 2022
Oppo Pad Air and Enco R buds unveiled

Oppo Pad Air and Enco R buds unveiled

May 23, 2022
Citizen Sleeper Review – A Sleeper Hit

Citizen Sleeper Review – A Sleeper Hit

May 23, 2022
V Rising Dev Reacts to Huge Early Success: “I Don’t Think Anybody Expected it to Be Quite This Big”

V Rising Dev Reacts to Huge Early Success: “I Don’t Think Anybody Expected it to Be Quite This Big”

May 23, 2022
How Many Digimon Are There?

How Many Digimon Are There?

May 23, 2022
Selenium vs. Protractor: What’s the Difference?

Selenium vs. Protractor: What’s the Difference?

May 23, 2022
The perfect iPhone case for photography? A look at the REEFLEX Pro Series Metal

The perfect iPhone case for photography? A look at the REEFLEX Pro Series Metal

May 23, 2022
Apple official MagSafe charger sees first 2022 discount, more- 9to5Mac

Apple official MagSafe charger sees first 2022 discount, more- 9to5Mac

May 23, 2022
Video: The Switch eShop Is Bad, And It’s Making Us Sad

Video: The Switch eShop Is Bad, And It’s Making Us Sad

May 23, 2022
PS1 Games on the New PS Plus Will Seemingly Have a CRT Filter Option and More

PS1 Games on the New PS Plus Will Seemingly Have a CRT Filter Option and More

May 23, 2022
Advertise with us
Monday, May 23, 2022
Bookmarks
  • Login
  • Register
GetUpdated
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

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

December 8, 2021
in Software Development
Reading Time:5 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


Read part 1 Migrating From Sakila-MySQL to Couchbase – ETL

Read Part 2 Migrating from Sakila-MySQL to Couchbase – Part 2: Views and User-Defined Functions

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. 



Source link

ShareSendTweet
Previous Post

Behind the picture: Satellites by Jonas Bendisken

Next Post

Fortnite Is Now Being Developed In Unreal Engine 5

Related Posts

Selenium vs. Protractor: What’s the Difference?

May 23, 2022
0
0
Selenium vs. Protractor: What’s the Difference?
Software Development

What Is Selenium? Selenium is an open-source testing tool used to automate web application testing. Selenium is designed explicitly for...

Read more

A Primer on OpenShift CLI Tools

May 23, 2022
0
0
A Primer on OpenShift CLI Tools
Software Development

The command-line interface (CLI) is an effective text-based user interface (UI). Today, many users rely on graphical user interfaces and...

Read more
Next Post
Fortnite Is Now Being Developed In Unreal Engine 5

Fortnite Is Now Being Developed In Unreal Engine 5

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?