Module: ActiveRecord::ConnectionAdapters::Firebird::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::FirebirdAdapter
Defined in:
lib/active_record/connection_adapters/firebird/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#foreign_keys(table_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/firebird/schema_statements.rb', line 52

def foreign_keys(table_name)
  result = query(<<-SQL.strip_heredoc, "SCHEMA")
    WITH FK_FIELDS AS (
      SELECT
        AA2.RDB$RELATION_NAME,
        AA2.RDB$CONSTRAINT_NAME,
        BB2.RDB$CONST_NAME_UQ AS LINK_UK_OR_PK,
        EE2.RDB$RELATION_NAME AS REFERENCE_TABLE,
        CC2.RDB$FIELD_NAME,
        AA2.RDB$CONSTRAINT_TYPE
      FROM
        RDB$RELATION_CONSTRAINTS AA2
        LEFT JOIN RDB$REF_CONSTRAINTS BB2 ON BB2.RDB$CONSTRAINT_NAME = AA2.RDB$CONSTRAINT_NAME
        LEFT JOIN RDB$INDEX_SEGMENTS CC2 ON CC2.RDB$INDEX_NAME = AA2.RDB$INDEX_NAME
        LEFT JOIN RDB$RELATION_FIELDS DD2 ON DD2.RDB$FIELD_NAME = CC2.RDB$FIELD_NAME AND DD2.RDB$RELATION_NAME = AA2.RDB$RELATION_NAME
        LEFT JOIN RDB$RELATION_CONSTRAINTS EE2 ON EE2.RDB$CONSTRAINT_NAME = BB2.RDB$CONST_NAME_UQ
    )
    SELECT
      AA.RDB$CONSTRAINT_NAME,
      BB.REFERENCE_TABLE,
      BB.FIELDS,
      BB.REFERENCE_FIELDS
    FROM
      RDB$RELATION_CONSTRAINTS AA
      LEFT JOIN (
        SELECT
          AA1.RDB$RELATION_NAME,
          AA1.RDB$CONSTRAINT_NAME,
          AA1.LINK_UK_OR_PK,
          AA1.REFERENCE_TABLE,
          (
            SELECT LIST(TRIM(AA3.RDB$FIELD_NAME), ', ') FROM FK_FIELDS AA3 WHERE AA3.RDB$CONSTRAINT_NAME = AA1.RDB$CONSTRAINT_NAME ROWS 1
          ) AS FIELDS,
          (
            SELECT LIST(TRIM(AA4.RDB$FIELD_NAME), ', ') FROM FK_FIELDS AA4 WHERE AA4.RDB$CONSTRAINT_NAME = AA1.LINK_UK_OR_PK ROWS 1
          ) AS REFERENCE_FIELDS
        FROM
          FK_FIELDS AA1
        GROUP BY
          AA1.RDB$RELATION_NAME,
          AA1.RDB$CONSTRAINT_NAME,
          AA1.REFERENCE_TABLE,
          AA1.LINK_UK_OR_PK
      ) BB ON BB.RDB$RELATION_NAME = AA.RDB$RELATION_NAME AND BB.RDB$CONSTRAINT_NAME = AA.RDB$CONSTRAINT_NAME
    WHERE
      AA.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
      AND AA.RDB$RELATION_NAME = '#{table_name.upcase}';
  SQL

  result.map do |row|
    options = {
      column: row[2].downcase,
      name: row[0].strip.downcase,
      primary_key: row[3].downcase
    }

    ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row[1].strip.downcase, options)
  end
end

#indexes(table_name) ⇒ Object



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
# File 'lib/active_record/connection_adapters/firebird/schema_statements.rb', line 13

def indexes(table_name)
  result = query(<<~SQL, "SCHEMA")
    SELECT
      rdb$index_name,
      rdb$unique_flag
    FROM
      rdb$indices
    WHERE
      rdb$relation_name = '#{table_name.upcase}';
  SQL

  result.map do |row|
    index_name = row[0].strip
    unique = row[1] == 1
    columns = query_values(<<~SQL, "SCHEMA")
      SELECT
        rdb$field_name
      FROM
        rdb$index_segments
      WHERE
        rdb$index_name = '#{index_name}'
      ORDER BY
        rdb$field_position
    SQL

    ActiveRecord::ConnectionAdapters::IndexDefinition.new(
      table_name.downcase,
      index_name.downcase,
      unique,
      columns.map(&:strip).map(&:downcase),
    )
  end
end

#remove_index(table_name, column, options = {}) ⇒ Object



47
48
49
50
# File 'lib/active_record/connection_adapters/firebird/schema_statements.rb', line 47

def remove_index(table_name, column, options = {})
  index_name = index_name_for_remove(table_name, column, options)
  execute "DROP INDEX #{quote_column_name(index_name)}"
end

#tables(_name = nil) ⇒ Object



5
6
7
# File 'lib/active_record/connection_adapters/firebird/schema_statements.rb', line 5

def tables(_name = nil)
  @connection.table_names
end

#viewsObject



9
10
11
# File 'lib/active_record/connection_adapters/firebird/schema_statements.rb', line 9

def views
  @connection.view_names
end