Module: ActiveRecordADBCAdapter::SchemaStatements

Included in:
Adapter
Defined in:
lib/activerecord_adbc_adapter/schema_statements.rb

Constant Summary collapse

NATIVE_DATABASE_TYPES =
{
  "duckdb" => {
    primary_key: "bigint PRIMARY KEY",
  },
  "postgresql" => {
    primary_key: "bigserial PRIMARY KEY",
    string: {name: "character varying"},
    binary: {name: "bytea"},
    datetime: {name: "timestamp without time zone"},
  },
  "sqlite" => {
    primary_key: "integer PRIMARY KEY AUTOINCREMENT NOT NULL",
    # INTEGER storage class can store 8 bytes value:
    # https://www.sqlite.org/datatype3.html#storage_classes_and_datatypes
    integer: {name: "integer", limit: 8},
    bigint: {name: "bigint", limit: 8},
  },
}

Instance Method Summary collapse

Instance Method Details

#adbc_catalogObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 26

def adbc_catalog
  case backend
  when "duckdb"
    path = @connection_parameters[:path]
    if path
      File.basename(path, ".*")
    else
      "memory"
    end
  else
    nil
  end
end

#adbc_db_schemaObject



40
41
42
43
44
45
46
47
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 40

def adbc_db_schema
  case backend
  when "duckdb"
    "main"
  else
    nil
  end
end

#adbc_table_typeObject



49
50
51
52
53
54
55
56
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 49

def adbc_table_type
  case backend
  when "duckdb"
    "BASE TABLE"
  else
    "table"
  end
end

#adbc_view_typeObject



58
59
60
61
62
63
64
65
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 58

def adbc_view_type
  case backend
  when "duckdb"
    "VIEW"
  else
    "view"
  end
end

#column_definitions(table_name) ⇒ Object



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

def column_definitions(table_name)
  with_raw_connection do |conn|
    objects = conn.get_objects(catalog: adbc_catalog,
                               db_schema: adbc_db_schema,
                               table_name: table_name)
    objects.raw_records.each do |_catalog_name, db_schemas|
      db_schemas.each do |db_schema|
        db_schema_tables = db_schema["db_schema_tables"]
        next if db_schema_tables.nil?
        db_schema_tables.each do |table|
          return table["table_columns"]
        end
      end
    end
    [] # raise?
  end
end

#native_database_typesObject



22
23
24
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 22

def native_database_types
  NATIVE_DATABASE_TYPES[backend] || super
end

#primary_keys(table_name) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 131

def primary_keys(table_name)
  with_raw_connection do |conn|
    objects = conn.get_objects(catalog: adbc_catalog,
                               db_schema: adbc_db_schema,
                               table_name: table_name)
    objects.raw_records.each do |_catalog_name, db_schemas|
      db_schemas.each do |db_schema|
        db_schema_tables = db_schema["db_schema_tables"]
        next if db_schema_tables.nil?
        db_schema_tables.each do |table|
          constraint = table["table_constraints"].find do |constraint|
            constraint["constraint_type"] == "PRIMARY KEY"
          end
          return [] if constraint.nil?
          return constraint["constraint_column_names"] || []
        end
      end
    end
    []
  end
end

#tablesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 67

def tables
  type = adbc_table_type
  with_raw_connection do |conn|
    objects = conn.get_objects(depth: :tables,
                               catalog: adbc_catalog,
                               db_schema: adbc_db_schema,
                               table_types: [type])
    tables = []
    objects.raw_records.each do |_catalog_name, db_schemas|
      db_schemas.each do |db_schema|
        db_schema_tables = db_schema["db_schema_tables"]
        next if db_schema_tables.nil?
        db_schema_tables.each do |t|
          # Some drivers may ignore table_types
          next unless t["table_type"] == type
          tables << t["table_name"]
        end
      end
    end
    tables
  end
end

#viewsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/activerecord_adbc_adapter/schema_statements.rb', line 90

def views
  type = adbc_view_type
  with_raw_connection do |conn|
    objects = conn.get_objects(depth: :tables,
                               catalog: adbc_catalog,
                               db_schema: adbc_db_schema,
                               table_types: [type])
    views = []
    objects.raw_records.each do |_catalog_name, db_schemas|
      db_schemas.each do |db_schema|
        db_schema_tables = db_schema["db_schema_tables"]
        next if db_schema_tables.nil?
        db_schema_tables.each do |t|
          # Some drivers may ignore table_types
          next unless t["table_type"] == type
          views << t["table_name"]
        end
      end
    end
    views
  end
end