Class: Rotulus::DB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rotulus/db/database.rb

Direct Known Subclasses

MySQL, PostgreSQL, SQLite

Instance Method Summary collapse

Instance Method Details

#default_nulls_order(sort_direction) ⇒ Object

SQLite and MySQL considers NULL values to be smaller than any other values. www.sqlite.org/lang_select.html#orderby dev.mysql.com/doc/refman/8.0/en/working-with-null.html



49
50
51
52
53
# File 'lib/rotulus/db/database.rb', line 49

def default_nulls_order(sort_direction)
  return :first if sort_direction == :asc

  :last
end

#nameObject



4
5
6
# File 'lib/rotulus/db/database.rb', line 4

def name
  @name ||= self.class.name.split('::').last.downcase
end

#nullable_order_sql(column_name, sort_direction, nulls) ⇒ Object



26
27
28
29
30
31
# File 'lib/rotulus/db/database.rb', line 26

def nullable_order_sql(column_name, sort_direction, nulls)
  sql = order_sql(column_name, sort_direction)
  return sql if nulls_in_default_order?(sort_direction, nulls)

  "#{sql} #{nulls_order_sql(nulls)}"
end

#nulls_in_default_order?(sort_direction, nulls) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rotulus/db/database.rb', line 42

def nulls_in_default_order?(sort_direction, nulls)
  nulls == default_nulls_order(sort_direction)
end

#nulls_order_sql(nulls) ⇒ Object



12
13
14
15
16
# File 'lib/rotulus/db/database.rb', line 12

def nulls_order_sql(nulls)
  return 'nulls first' if nulls == :first

  'nulls last'
end

#order_sql(column_name, sort_direction) ⇒ Object



18
19
20
# File 'lib/rotulus/db/database.rb', line 18

def order_sql(column_name, sort_direction)
  "#{column_name} #{sort_direction}"
end

#reversed_nullable_order_sql(column_name, sort_direction, nulls) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rotulus/db/database.rb', line 33

def reversed_nullable_order_sql(column_name, sort_direction, nulls)
  sql = reversed_order_sql(column_name, sort_direction)

  nulls = reverse_nulls(nulls)
  return sql if nulls_in_default_order?(reverse_sort_direction(sort_direction), nulls)

  "#{sql} #{nulls_order_sql(nulls)}"
end

#reversed_order_sql(column_name, sort_direction) ⇒ Object



22
23
24
# File 'lib/rotulus/db/database.rb', line 22

def reversed_order_sql(column_name, sort_direction)
  "#{column_name} #{reverse_sort_direction(sort_direction)}"
end

#select_all_sql(table_name) ⇒ Object



8
9
10
# File 'lib/rotulus/db/database.rb', line 8

def select_all_sql(table_name)
  "\"#{table_name}\".*"
end