Module: DmRails::Introspection

Defined in:
lib/dm_rails/introspection.rb

Overview

ActiveRecord(mysql2 适配器)自省补丁。

背景:mysql2 适配器的表结构自省依赖 SHOW FULL FIELDS / SHOW KEYS / SHOW CREATE TABLE / information_schema,这些经代理透传到达梦(MySQL 兼容模式) 均不支持。列信息改由达梦侧辅助视图(sql/dm_rails_columns.sql)提供, 表清单用代理可自答的 SHOW TABLES。

Instance Method Summary collapse

Instance Method Details

#column_definitions(table_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dm_rails/introspection.rb', line 11

def column_definitions(table_name)
  tbl = table_name.to_s.downcase
  json_cols = DmRails.json_columns.fetch(tbl, [])
  sql = "SELECT fld, ctype, is_null, key_kind, def_val, extra_str FROM #{DmRails.view_name} " \
        "WHERE tbl = #{quote(tbl)} ORDER BY col_order"
  execute(sql, "SCHEMA").to_a.map do |row|
    vals = row.is_a?(Hash) ? row.values : row
    ctype = json_cols.include?(vals[0]) ? "json" : vals[1]
    { Field: vals[0], Type: ctype, Collation: "", Null: vals[2],
      Key: vals[3].to_s, Default: dm_rails_normalize_default(vals[4]), Extra: vals[5].to_s,
      Privileges: "", Comment: "" }
  end
end

#create_table_info(_table_name) ⇒ Object

达梦链路无法执行 SHOW CREATE TABLE(AR 解析生成列/默认值类型的兜底路径)



69
70
71
# File 'lib/dm_rails/introspection.rb', line 69

def create_table_info(_table_name)
  ""
end

#data_source_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dm_rails/introspection.rb', line 43

def data_source_exists?(name)
  data_sources.include?(name.to_s.downcase)
end

#data_sourcesObject



31
32
33
# File 'lib/dm_rails/introspection.rb', line 31

def data_sources
  execute("SHOW TABLES", "SCHEMA").to_a.map { |row| (row.is_a?(Hash) ? row.values : row)[0] }
end

#default_type(_table_name, _field_name) ⇒ Object



73
74
75
# File 'lib/dm_rails/introspection.rb', line 73

def default_type(_table_name, _field_name)
  nil
end

#indexes(_table_name) ⇒ Object

索引/表选项仅用于 schema dump 与迁移生成,运行时不依赖;达梦链路无对应查询通道



56
57
58
# File 'lib/dm_rails/introspection.rb', line 56

def indexes(_table_name)
  []
end

#primary_keys(table_name) ⇒ Object



25
26
27
28
29
# File 'lib/dm_rails/introspection.rb', line 25

def primary_keys(table_name)
  sql = "SELECT fld FROM #{DmRails.view_name} WHERE tbl = #{quote(table_name.to_s.downcase)} " \
        "AND key_kind = 'PRI' ORDER BY col_order"
  execute(sql, "SCHEMA").to_a.map { |row| (row.is_a?(Hash) ? row.values : row)[0] }
end

#table_comment(_table_name) ⇒ Object



64
65
66
# File 'lib/dm_rails/introspection.rb', line 64

def table_comment(_table_name)
  nil
end

#table_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dm_rails/introspection.rb', line 47

def table_exists?(name)
  data_source_exists?(name)
end

#table_options(_table_name) ⇒ Object



60
61
62
# File 'lib/dm_rails/introspection.rb', line 60

def table_options(_table_name)
  {}
end

#tablesObject



35
36
37
# File 'lib/dm_rails/introspection.rb', line 35

def tables
  data_sources
end

#view_exists?(_name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dm_rails/introspection.rb', line 51

def view_exists?(_name)
  false
end

#viewsObject



39
40
41
# File 'lib/dm_rails/introspection.rb', line 39

def views
  []
end