lib/saulabs/reportable/grouping.rb in reportable-1.2.0 vs lib/saulabs/reportable/grouping.rb in reportable-1.3.0
- old
+ new
@@ -39,10 +39,12 @@
from_mysql_db_string(db_string)
when /sqlite/i
from_sqlite_db_string(db_string)
when /postgres/i
from_postgresql_db_string(db_string)
+ when /mssql/i, /sqlserver/i
+ from_sqlserver_db_string(db_string)
end
end
# Converts the grouping into a DB specific string that can be used to group records.
#
@@ -55,10 +57,12 @@
mysql_format(date_column)
when /sqlite/i
sqlite_format(date_column)
when /postgres/i
postgresql_format(date_column)
+ when /mssql/i, /sqlserver/i
+ sqlserver_format(date_column)
end
end
private
@@ -92,10 +96,18 @@
when :month
return db_string[0..6].split('-')[0..1].map(&:to_i)
end
end
+ def from_sqlserver_db_string(db_string)
+ if @identifier == :week
+ parts = [db_string[0..3], db_string[5..6]].map(&:to_i)
+ else
+ db_string.split(/[- ]/).map(&:to_i)
+ end
+ end
+
def mysql_format(date_column)
case @identifier
when :hour
"DATE_FORMAT(#{date_column}, '%Y/%m/%d/%H')"
when :day
@@ -128,9 +140,22 @@
"date_trunc('day', #{date_column})"
when :week
"date_trunc('week', #{date_column})"
when :month
"date_trunc('month', #{date_column})"
+ end
+ end
+
+ def sqlserver_format(date_column)
+ case @identifier
+ when :hour
+ "DATEADD(hh,DATEDIFF(hh,DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',#{date_column}), '1 Jan 1900'),#{date_column}), DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',#{date_column}), '1 Jan 1900'))"
+ when :day
+ "DATEADD(dd,DATEDIFF(dd,'1 Jan 1900',#{date_column}), '1 Jan 1900')"
+ when :week
+ "LEFT(CONVERT(varchar,#{date_column},120), 4) + '-' + CAST(DATEPART(isowk,#{date_column}) AS VARCHAR)"
+ when :month
+ "DATEADD(mm,DATEDIFF(mm,'1 Jan 1900',#{date_column}), '1 Jan 1900')"
end
end
end