@@ -84,6 +84,23 @@ class Zend_Db_Select
84
84
const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/ ' ;
85
85
const REGEX_COLUMN_EXPR_ORDER = '/^([\w]+\s*\(([^\(\)]|(?1))*\))$/ ' ;
86
86
const REGEX_COLUMN_EXPR_GROUP = '/^([\w]+\s*\(([^\(\)]|(?1))*\))$/ ' ;
87
+
88
+ // @see http://stackoverflow.com/a/13823184/2028814
89
+ const REGEX_SQL_COMMENTS = '@
90
+ (([ \'"]).*?[^ \\\]\2) # $1 : Skip single & double quoted expressions
91
+ |( # $3 : Match comments
92
+ (?:\#|--).*?$ # - Single line comments
93
+ | # - Multi line (nested) comments
94
+ /\* # . comment open marker
95
+ (?: [^/*] # . non comment-marker characters
96
+ |/(?!\*) # . ! not a comment open
97
+ |\*(?!/) # . ! not a comment close
98
+ |(?R) # . recursive case
99
+ )* # . repeat eventually
100
+ \*\/ # . comment close marker
101
+ )\s* # Trim after comments
102
+ |(?<=;)\s+ # Trim after semi-colon
103
+ @msx ' ;
87
104
88
105
/**
89
106
* Bind variables for query
@@ -513,7 +530,9 @@ public function group($spec)
513
530
}
514
531
515
532
foreach ($ spec as $ val ) {
516
- if (preg_match (self ::REGEX_COLUMN_EXPR_GROUP , (string ) $ val )) {
533
+ // Remove comments from SQL statement
534
+ $ noComments = preg_replace (self ::REGEX_SQL_COMMENTS , '$1 ' , (string ) $ val );
535
+ if (preg_match (self ::REGEX_COLUMN_EXPR_GROUP , $ noComments )) {
517
536
$ val = new Zend_Db_Expr ($ val );
518
537
}
519
538
$ this ->_parts [self ::GROUP ][] = $ val ;
@@ -605,7 +624,9 @@ public function order($spec)
605
624
$ val = trim ($ matches [1 ]);
606
625
$ direction = $ matches [2 ];
607
626
}
608
- if (preg_match (self ::REGEX_COLUMN_EXPR_ORDER , (string ) $ val )) {
627
+ // Remove comments from SQL statement
628
+ $ noComments = preg_replace (self ::REGEX_SQL_COMMENTS , '$1 ' , (string ) $ val );
629
+ if (preg_match (self ::REGEX_COLUMN_EXPR_ORDER , $ noComments )) {
609
630
$ val = new Zend_Db_Expr ($ val );
610
631
}
611
632
$ this ->_parts [self ::ORDER ][] = array ($ val , $ direction );
0 commit comments