incubator-alois-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fla...@apache.org
Subject svn commit: r1088492 - in /incubator/alois/trunk/rails: app/models/view.rb test/unit/view_test.rb
Date Mon, 04 Apr 2011 07:55:18 GMT
Author: flavio
Date: Mon Apr  4 07:55:17 2011
New Revision: 1088492

URL: http://svn.apache.org/viewvc?rev=1088492&view=rev
Log:
Added group by for update query in view.rb

Modified:
    incubator/alois/trunk/rails/app/models/view.rb
    incubator/alois/trunk/rails/test/unit/view_test.rb

Modified: incubator/alois/trunk/rails/app/models/view.rb
URL: http://svn.apache.org/viewvc/incubator/alois/trunk/rails/app/models/view.rb?rev=1088492&r1=1088491&r2=1088492&view=diff
==============================================================================
--- incubator/alois/trunk/rails/app/models/view.rb (original)
+++ incubator/alois/trunk/rails/app/models/view.rb Mon Apr  4 07:55:17 2011
@@ -185,7 +185,7 @@
     #      | INTO DUMPFILE 'file_name'
     #      | INTO var_name [, var_name]]
     #    [FOR UPDATE | LOCK IN SHARE MODE]]
-    
+
     def View.insert_generic(type, query, value, regexps)
       return query if value == "" or value.nil?
 
@@ -224,6 +224,15 @@
 	query + " " + val
     end
 
+    def View.insert_group(query, group_by)
+      # we cannot simply replace a group by because a grouping on a grouping is not the same
+      # as the second grouping alone.
+      raise "There exist already a group by in the query, cannot insert group by: #{query.inspect}"
if query =~ /GROUP\s+BY/
+      val = "GROUP BY #{group_by}"
+      insert_generic(:before, query, val, [/HAVING/i,/(ORDER BY)/i,/(LIMIT)/i,/(PROCEDURE)/i,/(INTO\s)/i,/(FOR
UPDATE)/i, /(LOCK IN)/i]) or
+	query + " " + val      
+    end
+
     def View.insert_order(query, order)
       val = "ORDER BY #{order}"
       insert_generic(:replace, query, "#{val} LIMIT", [/ORDER\s*BY\s.*\s*LIMIT/i]) or
@@ -263,6 +272,10 @@
 
 	part = View.insert_condition(part,options[:conditions]) if options[:conditions]	
 	part = View.insert_limit_offset(part,options[:limit],options[:offset]) if options[:limit]
+        if options[:group]
+          raise "Cannot insert group by for union query: #{query.inspect}" if parts.length
> 1
+          part = View.insert_group(part,options[:group])
+        end
 	part = View.insert_order(part,options[:order]) if options[:order]
 	part += " " unless part.ends_with?(" ")
 	start + part

Modified: incubator/alois/trunk/rails/test/unit/view_test.rb
URL: http://svn.apache.org/viewvc/incubator/alois/trunk/rails/test/unit/view_test.rb?rev=1088492&r1=1088491&r2=1088492&view=diff
==============================================================================
--- incubator/alois/trunk/rails/test/unit/view_test.rb (original)
+++ incubator/alois/trunk/rails/test/unit/view_test.rb Mon Apr  4 07:55:17 2011
@@ -61,6 +61,22 @@ class ViewTest < ActiveSupport::TestCase
     assert_equal "SELECT * FROM x ORDER BY #{order} LIMIT 324, 234 ", View.insert_order("SELECT
* FROM x ORDER BY sum(xxx) , ss ASC LIMIT 324, 234 ", order)
     assert_equal "SELECT * FROM x ORDER BY #{order}", View.insert_order("SELECT * FROM x
ORDER BY sum(xxx) , ss ASC", order)
   end
+
+  def test_insert_group
+    group = "field1, field2"
+    assert_equal "SELECT * FROM x GROUP BY #{group}", View.insert_group("SELECT * FROM x",group)
+    assert_equal "SELECT * FROM x WHERE a = b GROUP BY #{group}", View.insert_group("SELECT
* FROM x WHERE a = b",group)
+    assert_equal "SELECT * FROM x GROUP BY #{group} LIMIT 324,234", View.insert_group("SELECT
* FROM x LIMIT 324,234",group)
+    assert_raise(RuntimeError) {
+      View.insert_group("SELECT * FROM x GROUP BY sum(xxx) , ss ASC LIMIT 324, 234 ", group)
+    }
+    assert_raise(RuntimeError) {
+      View.insert_group("SELECT * FROM x GROUP BY sum(xxx) , ss ASC", group)
+    }
+    assert_raise(RuntimeError) {
+      View.update_query("SELECT * FROM x UNION SELECT * FROM y", :group => "f1,f2")
+    }
+  end
   
   def test_update_query
     condition = "abc = 'xyz'"



Mime
View raw message