Module: ActiveRecord::ConnectionAdapters::ArFirebird::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::ArFirebirdAdapter
Defined in:
lib/active_record/connection_adapters/ar_firebird/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_column(table_name, column_name, type, **options) ⇒ Object



130
131
132
133
# File 'lib/active_record/connection_adapters/ar_firebird/schema_statements.rb', line 130

def add_column(table_name, column_name, type, **options)
  type = (options[:limit] == 8 && type == :integer) ? :bigint : type
  super(table_name, column_name, type, **options)
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

:nodoc:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_record/connection_adapters/ar_firebird/schema_statements.rb', line 113

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  clear_cache!
  type = (options[:limit] == 8 && type == :integer) ? "bigint" : type_to_sql(type, **options)

  column = column_definitions(table_name.to_sym).find {|column| column["name"] == column_name.to_s }
  if options[:limit] &&  type != "bigint"
    type = "#{type.gsub(/\(.*\)/,'')}(#{options[:limit]})"
  end

  sql = []
  if type != column["sql_type"]
    sql << "ALTER COLUMN #{column["name"]} TYPE #{type}"
  end
  execute "ALTER TABLE #{table_name} #{sql.join(', ')}" if sql.any?
end

#foreign_keys(table_name) ⇒ Object



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

def foreign_keys(table_name)
  result = query(<<~SQL, "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



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

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, options = {}) ⇒ Object



48
49
50
51
# File 'lib/active_record/connection_adapters/ar_firebird/schema_statements.rb', line 48

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

#tables(_name = nil) ⇒ Object



6
7
8
# File 'lib/active_record/connection_adapters/ar_firebird/schema_statements.rb', line 6

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

#viewsObject



10
11
12
# File 'lib/active_record/connection_adapters/ar_firebird/schema_statements.rb', line 10

def views
  @connection.view_names
end