is anyone familiar with mysql's rendition of stored procs? I think they claim to comply with some standard called SQL:2003, whatever that means.
What I would like to do is use some logic in the stored proc to build the WHERE clause for a select statement. right now I'm doing simple if variable = 0 then SELECT blah WHERE blah=blah else SELECT... so I end up getting one statement the way I want it, and then copy and paste it into the rest of the conditions. Problem being, if I change something in one statement, and forget to copy and paste it into the rest of the conditions, I get weird results.
I've tried putting the IF THEN ELSE block inside the select statement, but all I get are errors.
I must not be searching with the right keywords, as google is not much help.
Here is one of my SP's - the only thing that changes in each statement is the WHERE clause and the GROUP BY clause. Just for the curious, in case it's not obvious, this code is for analyzing firewall logs.
if goodbad = 0 then
SELECT
MAX(ulog.`timestamp`) AS 'Time',
inet_ntoa(ip_saddr) AS 'Source_IP',
proto.protocol AS 'Protocol',
inet_ntoa(ip_daddr) AS 'Dest_IP',
ulogdb.ulog.tcp_dport AS 'tcp',
ulogdb.ulog.udp_dport AS 'udp',
ulogdb.ulog.oob_prefix AS 'Rule',
count(*) AS 'Con_Count'
FROM ulogdb.ulog
Left Join webfwlog.hostnames AS s_hostnames ON s_hostnames.ip_addr = ulogdb.ulog.ip_saddr
Left Join webfwlog.hostnames AS d_hostnames ON d_hostnames.ip_addr = ulogdb.ulog.ip_daddr
Left Join ulogdb.ip_protocols AS proto ON proto.ip_protocol = ulog.ip_protocol
WHERE
ulogdb.ulog.oob_in LIKE 'eth0' AND
ulog.oob_prefix LIKE '%ACCEPT%' AND /* accepted packets only */
ulogdb.ulog.`timestamp` >= SUBDATE(Now(), dayscount)
GROUP BY ip_saddr, tcp_dport, udp_dport, ip_daddr
ORDER BY
`Time` DESC;
elseif goodbad = 1 then
SELECT
ulog.`timestamp` AS 'Time',
inet_ntoa(ip_saddr) AS 'Source_IP',
proto.protocol AS 'Protocol',
inet_ntoa(ip_daddr) AS 'Dest_IP',
ulogdb.ulog.tcp_dport AS 'tcp',
ulogdb.ulog.udp_dport AS 'udp',
ulogdb.ulog.oob_prefix AS 'Rule',
count(*) AS 'Con_Count'
FROM ulogdb.ulog
Left Join webfwlog.hostnames AS s_hostnames ON s_hostnames.ip_addr = ulogdb.ulog.ip_saddr
Left Join webfwlog.hostnames AS d_hostnames ON d_hostnames.ip_addr = ulogdb.ulog.ip_daddr
Left Join ulogdb.ip_protocols AS proto ON proto.ip_protocol = ulog.ip_protocol
WHERE
ulogdb.ulog.oob_in LIKE 'eth0' AND
(ulog.oob_prefix LIKE '%DENY%' OR /* accepted packets only */
ulog.oob_prefix LIKE '%REJECT%') AND /* accepted packets only */
ulogdb.ulog.`timestamp` >= SUBDATE(Now(), dayscount)
GROUP BY ip_saddr, tcp_dport, udp_dport, ip_daddr
ORDER BY `Time` DESC;
else
SELECT
max(ulog.`timestamp`) AS 'Time',
inet_ntoa(ip_saddr) AS 'Source_IP',
proto.protocol AS 'Protocol',
inet_ntoa(ip_daddr) AS 'Dest_IP',
ulogdb.ulog.tcp_dport AS 'tcp',
ulogdb.ulog.udp_dport AS 'udp',
ulogdb.ulog.oob_prefix AS 'Rule',
count(*) AS 'Con_Count'
FROM ulogdb.ulog
Left Join webfwlog.hostnames AS s_hostnames ON s_hostnames.ip_addr = ulogdb.ulog.ip_saddr
Left Join webfwlog.hostnames AS d_hostnames ON d_hostnames.ip_addr = ulogdb.ulog.ip_daddr
Left Join ulogdb.ip_protocols AS proto ON proto.ip_protocol = ulog.ip_protocol
WHERE
ulogdb.ulog.oob_in LIKE 'eth0' AND
ulogdb.ulog.`timestamp` >= SUBTIME(Now(),' 12:00:00.00')
GROUP BY ip_saddr, tcp_dport, udp_dport, ip_daddr
ORDER BY `Time` DESC;
end if