how to check if all parameters in procedure begins with “p_” using a regex?
Procedure sample:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_in_sys_blablabla`$$
CREATE PROCEDURE `sp_in_sys_blablabla`(
p_cod_componente INT(11),
cod_tipo_ocorrencia SMALLINT(4),
p_dat_inicio CHAR(30),
dat_fim CHAR(30),
p_cod_usu_inc CHAR(10),
p_datInc CHAR(40)
)
BEGIN
DECLARE v_excecao SMALLINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET v_excecao = 1;
START TRANSACTION;
......
END$$
C# code:
string content = "proceddure code";
Regex regex = new Regex(regras[i].Regex, RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (regex.IsMatch(content)) Console.WriteLine(ERROR_MESSAGE);
From my point of view you have two options:
You can use regular expressions. Read your sql statement line by line. Be sure, that you’re inside the declaration of a procedure! I think that is very error-prone due to the fact, that it is not necessary by syntax. The actual regular expression seems very easy
p_[a-z]+.Use a parser (i.e., Gold parser). Possibly you’ll find a grammar for SQL syntax. You can then grab any parameter within a procedure declaration and check whether it
StartsWith("p_").