Module: RailsNexus::DatabaseAdapter
- Defined in:
- lib/rails_nexus/database_adapter.rb
Class Method Summary collapse
- .database_version(connection = ActiveRecord::Base.connection) ⇒ Object
- .family(connection = ActiveRecord::Base.connection) ⇒ Object
- .time_bucket_expression(column: "created_at", period: :hour, connection: ActiveRecord::Base.connection) ⇒ Object
- .time_component_expression(column: "created_at", component:, connection: ActiveRecord::Base.connection) ⇒ Object
Class Method Details
.database_version(connection = ActiveRecord::Base.connection) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/rails_nexus/database_adapter.rb', line 43 def database_version(connection = ActiveRecord::Base.connection) case family(connection) when :sqlite then connection.select_value("SELECT sqlite_version()") when :postgresql, :mysql then connection.select_value("SELECT version()") else "N/A" end rescue ActiveRecord::StatementInvalid "N/A" end |
.family(connection = ActiveRecord::Base.connection) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/rails_nexus/database_adapter.rb', line 7 def family(connection = ActiveRecord::Base.connection) name = connection.adapter_name.to_s.downcase return :postgresql if name.include?("postgres") return :mysql if name.include?("mysql") || name.include?("trilogy") return :sqlite if name.include?("sqlite") :unsupported end |
.time_bucket_expression(column: "created_at", period: :hour, connection: ActiveRecord::Base.connection) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rails_nexus/database_adapter.rb', line 16 def time_bucket_expression(column: "created_at", period: :hour, connection: ActiveRecord::Base.connection) column = connection.quote_column_name(column) case [family(connection), period.to_sym] when [:postgresql, :hour] then "TO_CHAR(DATE_TRUNC('hour', #{column}), 'YYYY-MM-DD HH24:00')" when [:mysql, :hour] then "DATE_FORMAT(#{column}, '%Y-%m-%d %H:00')" when [:sqlite, :hour] then "strftime('%Y-%m-%d %H:00', #{column})" else raise NotImplementedError, "Time grouping is not supported by this database adapter" end end |
.time_component_expression(column: "created_at", component:, connection: ActiveRecord::Base.connection) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rails_nexus/database_adapter.rb', line 28 def time_component_expression(column: "created_at", component:, connection: ActiveRecord::Base.connection) column = connection.quote_column_name(column) case [family(connection), component.to_sym] when [:postgresql, :hour] then "CAST(EXTRACT(HOUR FROM #{column}) AS INTEGER)" when [:mysql, :hour] then "HOUR(#{column})" when [:sqlite, :hour] then "CAST(strftime('%H', #{column}) AS INTEGER)" when [:postgresql, :weekday] then "CAST(EXTRACT(DOW FROM #{column}) AS INTEGER)" when [:mysql, :weekday] then "DAYOFWEEK(#{column}) - 1" when [:sqlite, :weekday] then "CAST(strftime('%w', #{column}) AS INTEGER)" else raise NotImplementedError, "Time component grouping is not supported by this database adapter" end end |