FEATURE: add query_array and group_by to builder (#31)

FEATURE: add query_array and group_by to builder (#31)

diff --git a/lib/mini_sql/builder.rb b/lib/mini_sql/builder.rb
index 353749d..785234b 100644
--- a/lib/mini_sql/builder.rb
+++ b/lib/mini_sql/builder.rb
@@ -9,7 +9,7 @@ class MiniSql::Builder
     @connection = connection
   end
 
-  [:set, :where2, :where, :order_by, :limit, :left_join, :join, :offset, :select].each do |k|
+  [:set, :where2, :where, :order_by, :limit, :left_join, :join, :offset, :select, :group_by].each do |k|
     define_method k do |data, *args|
       if args && (args.length == 1) && (Hash === args[0])
         @args ||= {}
@@ -43,6 +43,8 @@ class MiniSql::Builder
         joined = (+"OFFSET ") << v.last.to_i.to_s
       when :order_by
         joined = (+"ORDER BY ") << v.join(" , ")
+      when :group_by
+        joined = (+"GROUP BY ") << v.join(" , ")
       when :set
         joined = (+"SET ") << v.join(" , ")
       end
@@ -59,7 +61,7 @@ class MiniSql::Builder
     end
   end
 
-  [:query, :query_single, :query_hash, :exec].each do |m|
+  [:query, :query_single, :query_hash, :query_array, :exec].each do |m|
     class_eval <<~RUBY
       def #{m}(hash_args = nil)
         @connection.#{m}(to_sql(hash_args))
diff --git a/test/mini_sql/builder_tests.rb b/test/mini_sql/builder_tests.rb
index acefffb..486fe91 100644
--- a/test/mini_sql/builder_tests.rb
+++ b/test/mini_sql/builder_tests.rb
@@ -54,6 +54,19 @@ module MiniSql::BuilderTests
     assert_equal(3, r.length)
   end
 
+  def test_group_by
+    @connection.exec("create temporary table ta(x int, t text)")
+    @connection.exec("insert into ta(x,t) values(1 ,'a'),(2,'a'),(3,  'b')")
+
+    builder = @connection.build("/*select*/ from ta /*group_by*/")
+    builder.select('t, SUM(x)')
+    builder.group_by("t")
+
+    r = builder.query_array.to_h
+
+    assert_equal({ 'a' => 3, 'b' => 3 }, r)
+  end
+
   def test_set
     @connection.exec("create temp table ta(x int, y int)")
 

GitHub sha: 8987a815

This commit appears in #31 which was merged by SamSaffron.