Last active
August 29, 2015 14:07
-
-
Save damirm/03055fa0acb551523336 to your computer and use it in GitHub Desktop.
Convert currencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION `convert_currency`(cur_from INT, cur_to INT, amount DOUBLE, rate_rub DOUBLE, rate_dollar DOUBLE, rate_euro DOUBLE) | |
RETURNS double DETERMINISTIC | |
BEGIN | |
DECLARE result DOUBLE DEFAULT amount; | |
DECLARE delta DOUBLE; | |
DECLARE TYPE_DOLLAR INT; | |
DECLARE TYPE_EURO INT; | |
DECLARE TYPE_RUBLES INT; | |
SET TYPE_DOLLAR = 0; | |
SET TYPE_EURO = 1; | |
SET TYPE_RUBLES = 2; | |
IF cur_from = TYPE_DOLLAR THEN -- FROM DOLLAR | |
IF cur_to = TYPE_EURO THEN -- TO EURO | |
SET delta = rate_dollar / rate_euro; | |
SET result = delta * amount; | |
ELSEIF cur_to = TYPE_RUBLES THEN -- TO RUBLES | |
SET result = amount * rate_dollar; | |
END IF; | |
ELSEIF cur_from = TYPE_EURO THEN -- FROM EURO | |
IF cur_to = TYPE_DOLLAR THEN -- TO DOLLAR | |
SET delta = rate_euro / rate_dollar; | |
SET result = delta * amount; | |
ELSEIF cur_to = TYPE_RUBLES THEN -- TO RUBLES | |
SET result = amount * rate_euro; | |
END IF; | |
ELSE -- FROM RUBLES | |
IF cur_to = TYPE_DOLLAR THEN -- TO DOLLAR | |
SET result = amount / rate_dollar; | |
ELSEIF cur_to = TYPE_EURO THEN -- TO EURO | |
SET result = amount / rate_euro; | |
END IF; | |
END IF; | |
RETURN result; | |
END; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment