-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement database adapters to be able to have diferent ways of imple…
…menting the same feature
- Loading branch information
1 parent
181e959
commit c513381
Showing
15 changed files
with
231 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,7 @@ | |
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
.ruby-version | ||
.byebug_history | ||
/tmp/ | ||
.DS_STORE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveReporting | ||
module DatabaseAdapters | ||
DatabaseNotSupported = Class.new(StandardError) | ||
MethodNotImplemented = Class.new(StandardError) | ||
# Database adapters are here to solve SQL problems in the | ||
# most idiomatic way in each database | ||
class Base | ||
attr_reader :name | ||
|
||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
# @param [Symbol] interval | ||
# @return [Boolean] | ||
def allowed_datetime_hierarchy?(interval) | ||
datetime_hierarchies.include?(interval.try(:to_sym)) | ||
end | ||
|
||
protected | ||
|
||
# Allowed datetime hierchies in each adapter | ||
# By default (:sqlite) there is none | ||
# | ||
# @return [Array<Symbol>] | ||
def datetime_hierarchies | ||
@datetime_hierarchies ||= [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveReporting | ||
module DatabaseAdapters | ||
class Factory | ||
class << self | ||
ADAPTERS = { | ||
sqlite3: SqliteAdapter, | ||
mysql2: MysqlAdapter, | ||
postgresql: PostgresqlAdapter, | ||
postgis: PostgresqlAdapter | ||
}.freeze | ||
# @param [Symbol] | ||
def for_database(adapter_name) | ||
adapter = ADAPTERS[adapter_name] | ||
|
||
return adapter.new(adapter_name) unless adapter.nil? | ||
|
||
raise( | ||
DatabaseNotSupported, | ||
"Database with this #{adapter_name} is not supported by ActiveReporting" | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveReporting | ||
module DatabaseAdapters | ||
# Database adapters are here to solve SQL problems in the | ||
# most idiomatic way in each database | ||
class MysqlAdapter < Base | ||
# Generate SQL snippet with DATE_TRUNC | ||
# @param [String] interval | ||
# @param [String] field | ||
# @return [String] | ||
def date_trunc(interval, field) | ||
clean_sql( | ||
<<-SQL | ||
DATE_ADD( | ||
"#{super_old_base_date}", | ||
INTERVAL TIMESTAMPDIFF( | ||
#{interval.upcase}, | ||
"#{super_old_base_date}", | ||
#{field} | ||
) #{interval.upcase} | ||
) | ||
SQL | ||
) | ||
end | ||
|
||
protected | ||
|
||
# Remove spaces and put all in one line | ||
def clean_sql(sql) | ||
sql | ||
.strip | ||
.gsub(/\n+/, ' ') | ||
.gsub(/\s+/, ' ') | ||
.gsub(/\(\s+\)/, '(') | ||
.gsub(/\)\s+\)/, ')') | ||
end | ||
|
||
def datetime_hierarchies | ||
@datetime_hierarchies ||= %i[ | ||
year | ||
month | ||
week | ||
] | ||
end | ||
|
||
# Used to generate a diff when implementing | ||
# datetime truncation | ||
# | ||
# @return [String] | ||
def super_old_base_date | ||
'1900-01-01' | ||
end | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
lib/active_reporting/database_adapters/postgresql_adapter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveReporting | ||
module DatabaseAdapters | ||
class PostgresqlAdapter < Base | ||
# Values for the Postgres `date_trunc` method. | ||
# See https://www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC | ||
|
||
# Generate SQL snippet with DATE_TRUNC | ||
# @param [String] interval | ||
# @param [String] field | ||
# @return [String] | ||
def date_trunc(interval, field) | ||
"DATE_TRUNC('#{interval}', #{field})" | ||
end | ||
|
||
protected | ||
|
||
def datetime_hierarchies | ||
@datetime_hierarchies ||= %i[ | ||
microseconds | ||
milliseconds | ||
second | ||
minute | ||
hour | ||
day | ||
week | ||
month | ||
quarter | ||
year | ||
decade | ||
century | ||
millennium | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveReporting | ||
module DatabaseAdapters | ||
class SqliteAdapter < Base | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.