Permalink
Browse files

サブクエリの中でHAVINGを利用していた場合に、外側のGROUP BYを正しく判定できていなかったのを修正

  • Loading branch information...
ken39arg committed Nov 27, 2012
1 parent 517c09a commit 056a0b0a102e58044be797d8cf0d8d023b617ae2
Showing with 31 additions and 3 deletions.
  1. +4 −3 lib/MySQL/Sharding/Client.pm
  2. +27 −0 t/001_mysql_sharding_client.t
@@ -221,9 +221,10 @@ sub _parse_columns {
sub _parse_group {
my ($self, $sql) = @_;
- unless ($sql =~ m/GROUP +BY +([^\)]+?) *(HAVING|UNION|ORDER|LIMIT|$)[\)]*/i) {
- return undef;
- }
+ my $f = 0;
+ $f = 1 while ($sql =~ m/GROUP +BY +([^\)]+?) *(HAVING|UNION|ORDER|LIMIT|$)[\)]*/gi);
+
+ return undef unless $f;
my @group;
foreach my $column (split / *, */, $1) {
@@ -234,6 +234,33 @@ SQL
offset => 0,
}, "parse sub query with another func.";
+ $parsed = MySQL::Sharding::Client->parse_sql("select
+ mbc,
+ count(ma) as mac
+ from (
+ select
+ ma,
+ count(mb) as mbc
+ from tablem
+ where mc = 1
+ group by ma having mbc >= 10
+ ) m
+ where mbc >= 15
+ group by mbc
+ order by mbc desc
+ ");
+ is_deeply $parsed, {
+ command => "SELECT",
+ columns => [
+ {column => 'mbc', name => 'mbc', command => 'NONE'},
+ {column => 'count(ma)', name => 'mac', command => 'COUNT'},
+ ],
+ group => ['mbc'],
+ order => [{column => 'mbc', order => 'DESC'}],
+ limit => 0,
+ offset => 0,
+ }, "parse sub query which use GROUP BY and HAVING.";
+
$parsed = MySQL::Sharding::Client->parse_sql(<<"SQL");
DESC user
SQL

0 comments on commit 056a0b0

Please sign in to comment.