Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex to split an SQL field list

Tags:

regex

php

I'm looking for a regular expression to split a field list from an SQL SELECT statement.

I have already extracted the field list from the rest of the query, so I am left with a string that may look like:

field1,field2,field3,CONCAT(field1,field2) AS concatted,IF(field1 = field2,field3,field4) AS logic

I think the logical approach would be to split the string on a comma, provided the comma doesn't appear within parantheses, so I just need to find the right regular expression to do that...?

The end result of what I'm trying to achieve is to find out which fields appear to be straight selects from the table, and which ones are made from SQL expressions, so that I can decide later whether to filter them using WHERE or HAVING clauses.

Once I have the list of fields, I can check for the presence of the 'AS' keyword which although isn't perfect, should work with the way I write SQL.

(Bonus points for suggesting another way that differenciates between fields that are not expressions but are aliased, and expressions that may not be aliased, or may be but without using 'AS')

like image 799
Codecraft Avatar asked Jun 23 '12 15:06

Codecraft


1 Answers

This regex should work :

/,(?![^()]*+\\))/

An example implementation in PHP, here :

$vv = 'field1,field2,field3,CONCAT(field1,field2) AS concatted,IF(field1 = field2,field3,field4) AS logic';

$arr = preg_split ("/,(?![^()]*+\\))/", $vv);
foreach ($arr as &$value) {
    print($value);
}
like image 94
aleroot Avatar answered Sep 29 '22 21:09

aleroot