Class: Groupdate::Adapters::SQLiteAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/groupdate/adapters/sqlite_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#column, #day_start, #n_seconds, #period, #week_start

Instance Method Summary collapse

Methods inherited from BaseAdapter

#generate, #initialize

Constructor Details

This class inherits a constructor from Groupdate::Adapters::BaseAdapter

Instance Method Details

#group_clauseObject

Raises:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/groupdate/adapters/sqlite_adapter.rb', line 4

def group_clause
  raise Groupdate::Error, "Time zones not supported for SQLite" unless @time_zone.utc_offset.zero?
  raise Groupdate::Error, "day_start not supported for SQLite" unless day_start.zero?

  query =
    if period == :week
      ["strftime('%Y-%m-%d', #{column}, '-6 days', ?)", "weekday #{(week_start + 1) % 7}"]
    elsif period == :custom
      ["datetime((strftime('%s', #{column}) / ?) * ?, 'unixepoch')", n_seconds, n_seconds]
    else
      format =
        case period
        when :minute_of_hour
          "%M"
        when :hour_of_day
          "%H"
        when :day_of_week
          "%w"
        when :day_of_month
          "%d"
        when :day_of_year
          "%j"
        when :month_of_year
          "%m"
        when :second
          "%Y-%m-%d %H:%M:%S UTC"
        when :minute
          "%Y-%m-%d %H:%M:00 UTC"
        when :hour
          "%Y-%m-%d %H:00:00 UTC"
        when :day
          "%Y-%m-%d"
        when :month
          "%Y-%m-01"
        when :quarter
          raise Groupdate::Error, "Quarter not supported for SQLite"
        else # year
          "%Y-01-01"
        end

      ["strftime(?, #{column})", format]
    end

  @relation.send(:sanitize_sql_array, query)
end