Module: Torque::PostgreSQL::Adapter::DatabaseStatements

Included in:
Torque::PostgreSQL::Adapter
Defined in:
lib/torque/postgresql/adapter/database_statements.rb

Constant Summary collapse

EXTENDED_DATABASE_TYPES =
%i(enum interval)

Instance Method Summary collapse

Instance Method Details

#configure_connectionObject

Configure the interval format



25
26
27
28
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 25

def configure_connection
  super
  execute("SET SESSION IntervalStyle TO 'iso_8601'", 'SCHEMA')
end

#extended_typesObject

Get the list of extended types



14
15
16
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 14

def extended_types
  EXTENDED_DATABASE_TYPES
end

#initialize_type_map(m) ⇒ Object

Change some of the types being mapped



31
32
33
34
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 31

def initialize_type_map(m)
  super
  m.register_type 'interval', OID::Interval.new
end

#load_additional_types(type_map, oids = nil) ⇒ Object

Add the composite types to be loaded too.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 37

def load_additional_types(type_map, oids = nil)
  super

  filter = "AND     a.typelem::integer IN (%s)" % oids.join(", ") if oids

  query = <<-SQL
    SELECT      a.typelem AS oid, t.typname, t.typelem,
                t.typdelim, t.typbasetype, t.typtype
    FROM        pg_type t
    INNER JOIN  pg_type a ON (a.oid = t.typarray)
    LEFT JOIN   pg_catalog.pg_namespace n ON n.oid = t.typnamespace
    WHERE       n.nspname NOT IN ('pg_catalog', 'information_schema')
    AND     t.typtype IN ( 'e' )
    #{filter}
    AND     NOT EXISTS(
              SELECT 1 FROM pg_catalog.pg_type el
                WHERE el.oid = t.typelem AND el.typarray = t.oid
              )
    AND     (t.typrelid = 0 OR (
              SELECT c.relkind = 'c' FROM pg_catalog.pg_class c
                WHERE c.oid = t.typrelid
              ))
  SQL

  execute_and_clear(query, 'SCHEMA', []) do |records|
    records.each do |row|
      typtype = row['typtype']
      type = begin
        case
        when typtype == 'e'.freeze then OID::Enum.create(row)
        end
      end

      type_map.register_type row['oid'].to_i, type
    end
  end
end

#type_exists?(name) ⇒ Boolean Also known as: data_type_exists?

Returns true if type exists.

Returns:

  • (Boolean)


19
20
21
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 19

def type_exists?(name)
  user_defined_types.key? name.to_s
end

#user_defined_types(category = nil) ⇒ Object

Gets a list of user defined types. You can even choose the typcategory filter



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 77

def user_defined_types(category = nil)
  category_condition = "AND     typtype = '#{category}'" unless category.nil?
  select_all(<<-SQL).rows.to_h
    SELECT      t.typname AS name,
                CASE t.typtype
                WHEN 'e' THEN 'enum'
                END AS type
    FROM        pg_type t
    LEFT JOIN   pg_catalog.pg_namespace n ON n.oid = t.typnamespace
    WHERE       n.nspname NOT IN ('pg_catalog', 'information_schema')
    #{category_condition}
    AND     NOT EXISTS(
              SELECT 1 FROM pg_catalog.pg_type el
                WHERE el.oid = t.typelem AND el.typarray = t.oid
              )
    AND     (t.typrelid = 0 OR (
              SELECT c.relkind = 'c' FROM pg_catalog.pg_class c
                WHERE c.oid = t.typrelid
              ))
    ORDER BY    t.typtype DESC
  SQL
end

#valid_type?(type) ⇒ Boolean

Check if a given type is valid.

Returns:

  • (Boolean)


9
10
11
# File 'lib/torque/postgresql/adapter/database_statements.rb', line 9

def valid_type?(type)
  super || extended_types.include?(type)
end