$Query366='SELECT FaUserLogin.*, UserMeta.meta_value, datediff(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration FROM ';
$Query415='SELECT id, user_id, item_id, secondary_item_id, component_name, component_action, date_notified, is_new, COUNT(id) as total_count FROM wp_bp_notifications n WHERE user_id IN ';
$Query366_2='SELECT FaUserLogin.*, UserMeta.meta_value, dated if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, table.Oops, function(col1)as"g" FROM ';
$Query366_3='SELECT FaUserLogin.*, UserMeta.meta_value, dated if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4,col3,col4)AS"test2" FROM ';
$QueryOneIF='SELECT col1, if(1==2,"a","bbb")AS test, col3, DATEDIFF(c4, c5, c6)AS"t2" FROM ';
$QueryTwoIFs='SELECT col1, if(1==2,"a","bbb")AS test, col3, IF(c7==2, c8, c9)AS"t3" FROM ';
-----------------------------------------------
Original query from Issue #366:
SELECT FaUserLogin.*, UserMeta.meta_value, datediff(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration FROM
Bug in original pattern results in (array elements 3 - 6 [below] are used):
dated CASE WHEN f(SECOND THEN FaUserLogin.time_login ELSE FaUserLogin.time_last_seen END as duration
Array
(
[0] => iff(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration
[1] => iff(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration
[2] => f(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen
[3] => f(SECOND
[4] => FaUserLogin.time_login
[5] => FaUserLogin.time_last_seen
[6] => as duration
)
-----------------------------------------------
Original query from Issue #415:
SELECT id, user_id, item_id, secondary_item_id, component_name, component_action, date_notified, is_new, COUNT(id) as total_count FROM wp_bp_notifications n WHERE user_id IN
Bug in original pattern results in (array elements 3 - 6 [below] are used):
date_not CASE WHEN ied THEN is_new ELSE COUNT(id END as total_count
Array
(
[0] => ified, is_new, COUNT(id) as total_count
[1] => ified, is_new, COUNT(id) as total_count
[2] => ied, is_new, COUNT(id
[3] => ied
[4] => is_new
[5] => COUNT(id
[6] => as total_count
)
-----------------------------------------------
Updated query from Issue #366 with more SELECT fields including a function:
SELECT FaUserLogin.*, UserMeta.meta_value, dated if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, table.Oops, function(col1)as"g" FROM
Bug in original pattern also captures to last ") AS" found:
Array
(
[0] => if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, table.Oops, function(col1)as
[1] => if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, table.Oops, function(col1)as
[2] => SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, table.Oops, function(col1
[3] => SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration
[4] => table.Oops
[5] => function(col1
[6] => as
)
-----------------------------------------------
Updated query from Issue #366 with an extra IF() function:
SELECT FaUserLogin.*, UserMeta.meta_value, dated if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4,col3,col4)AS"test2" FROM
Bug in original pattern also captures to last ") AS" found:
Array
(
[0] => if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4,col3,col4)AS
[1] => if(SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4,col3,col4)AS
[2] => SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4,col3,col4
[3] => SECOND,FaUserLogin.time_login,FaUserLogin.time_last_seen) as duration, if(3!=4
[4] => col3
[5] => col4
[6] => AS
)
-----------------------------------------------
New pattern:
a) makes the match much more specific
b) removes unused groups
Matches for updated pattern applied to original query for Issue #366 (should be none):
Array
(
)
------
Matches for updated pattern applied to query for Issue #415 (should be none):
Array
(
)
-----------------------------------------------
Test query that should match on 1 IF function:
SELECT col1, if(1==2,"a","bbb")AS test, col3, DATEDIFF(c4, c5, c6)AS"t2" FROM
Updated pattern captures the following:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
[1] => 1==2
[2] => "a"
[3] => "bbb"
[4] => AS test
)
)
-----------------------------------------------
Test query that should match on 2 IF functions:
SELECT col1, if(1==2,"a","bbb")AS test, col3, IF(c7==2, c8, c9)AS"t3" FROM
Updated pattern captures the following:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
[1] => 1==2
[2] => "a"
[3] => "bbb"
[4] => AS test
)
[1] => Array
(
[0] => IF(c7==2, c8, c9)AS
[1] => c7==2
[2] => c8
[3] => c9
[4] => AS
)
)
==========================================================
While the new pattern will work, there is an even better option:
The new IIF() function, introduced in SQL Server 2012, which is the same
as MySQL's IF() function, except it has an extra "I" in the name.
We can simplify the updated pattern by removing the capture groups for
the three items separated by the commas and within the parens since there
is nothing to reconstruct. Now the operation is a simple concatenation:
a RegEx Replace will simply prefix an "I" onto any match:
-----------------------------------------------
Revised updated pattern captures the following for the 1 IF query:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
)
)
---------
And the RegEx Replace returns:
SELECT col1, Iif(1==2,"a","bbb")AS test, col3, DATEDIFF(c4, c5, c6)AS"t2" FROM
-----------------------------------------------
Revised updated pattern captures the following for the 2 IFs query:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
)
[1] => Array
(
[0] => IF(c7==2, c8, c9)AS
)
)
---------
And the RegEx Replace returns:
SELECT col1, Iif(1==2,"a","bbb")AS test, col3, IIF(c7==2, c8, c9)AS"t3" FROM
----------------------------------------------------------
We can even get fancy and capture the "I" in "IF" in
order to match the case of the captured character.
-----------------------------------------------
RE-revised updated pattern captures the following for the 1 IF query:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
[1] => i
)
)
---------
And the RegEx Replace returns:
SELECT col1, iif(1==2,"a","bbb")AS test, col3, DATEDIFF(c4, c5, c6)AS"t2" FROM
-----------------------------------------------
RE-revised updated pattern captures the following for the 2 IFs query:
Array
(
[0] => Array
(
[0] => if(1==2,"a","bbb")AS test
[1] => i
)
[1] => Array
(
[0] => IF(c7==2, c8, c9)AS
[1] => I
)
)
---------
And the RegEx Replace returns:
SELECT col1, iif(1==2,"a","bbb")AS test, col3, IIF(c7==2, c8, c9)AS"t3" FROM
Test for Project Nami RegEx bug for "IF" in SELECT statements.