Posts

Showing posts with the label Stored Functions

[Level 3] rw_ether_atob, rw_ether_btoa stored function in MySQL.

delimiter // drop function if exists rw_ether_atob// create function rw_ether_atob(sAscii char(17)) returns bit(48) deterministic begin declare bReturn bit(48); ##set bReturn=conv(replace(sAscii,':',''),16,2); ## string output not bit ##set bReturn=conv(replace(sAscii,':',''),16,10); ## not work ##set bReturn=cast(replace(sAscii,':','') as bit(48)); ## syntax error ##set bReturn=bin(replace(sAscii,':','')); ## syntax error set bReturn=unhex(replace(sAscii,':','')); return bReturn; end// drop function if exists rw_ether_btoa// create function rw_ether_btoa(sBit bit(48)) returns char(17) deterministic begin declare sReturn char(17); set sReturn=lpad(hex(sBit),12,'0'); set sReturn=concat_ws(':', substr(sReturn,1,2), substr(sReturn,3,2), substr(sReturn,5,2), substr(sReturn,7,2), substr(sReturn,9,2), substr(sReturn,11,2)); return sReturn; end// delimiter ; /* mysql> c...

[Level 2] The Samples of MySQL Stored Function and Stored Procedure.

FYI. [SQL] delimiter // drop function if exists stanley.getSQL// create function stanley.getSQL(name varchar(32)) returns text begin   return concat('Hello World, ',name,'!'); end// delimiter ; [mysql client] mysql> select getSQL('stanley'); +-----------------------+ | getSQL('stanley')     | +-----------------------+ | Hello World, stanley! | +-----------------------+ 1 row in set (0.00 sec)   mysql> select length(getSQL('stanley')); +---------------------------+ | length(getSQL('stanley')) | +---------------------------+ |                        21 | +---------------------------+ 1 row in set (0.00 sec) [SQL] delimiter // drop procedure if exists stanley.setParam// create procedure stanley.setParam(in id int, inout word text) begin   if id > 0 then     set word=concat(word, '> 0'); ...

[Level 3] Enhance MySQL Performance to Store MAC Data with Stored Functions.

We know MySQL support inet_aton()/inet_ntoa() to convert ip address data to unsigned integer. These will reduce the data capacity and enhance performance. But MySQL seems not support MAC address. So I think maybe I can create stored functions just like the purpose of inet_aton()/inet_ntoa() to also enhance performance. By the way, stored function has a declaration option call "DETERMINISTIC". Default is "NOT DETERMINISTIC", it implies that the procedure may produce different results, given the same inputs. The alternative is "DETERMINISTIC", if the procedure always gives the same results given the same input. In this case, to let SQL parser can use the right index, we should declare the stored function with "DETERMINISTIC". My  sample code as following: delimiter // ## create stored function ether_atob(), from ascii to bit ## must define "deterministic", or explain will not use index drop function if exists ether_atob// create...