Module: DataMapper::Reflection::PostgresAdapter

Defined in:
lib/dm-reflection/adapters/postgres.rb

Instance Method Summary collapse

Instance Method Details

#get_primary_keys(table) ⇒ Array<String>

Returna list of columns in the primary key

Parameters:

  • the (String)

    table name

Returns:

  • (Array<String>)

    the names of the columns in the primary key



55
56
57
58
59
60
61
62
63
# File 'lib/dm-reflection/adapters/postgres.rb', line 55

def get_primary_keys(table)
  select("        SELECT \"key_column_usage\".\"column_name\"\n          FROM \"information_schema\".\"table_constraints\"\n    INNER JOIN \"information_schema\".\"key_column_usage\" USING(\"constraint_schema\", \"constraint_name\")\n         WHERE \"table_constraints\".\"constraint_type\" = 'PRIMARY KEY'\n           AND \"table_constraints\".\"table_name\" = ?\n  SQL\nend\n".compress_lines, table).to_set

#get_properties(table) ⇒ Hash

TODO:

Consider returning actual DataMapper::Properties from this. It would probably require passing in a Model Object.

Get the column specifications for a specific table

Parameters:

  • table (String)

    the name of the table to get column specifications for

Returns:

  • (Hash)

    the column specs are returned in a hash keyed by :name, :field, :type, :required, :default, :key



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dm-reflection/adapters/postgres.rb', line 74

def get_properties(table)
  columns = select("      SELECT \"column_name\"\n           , \"data_type\"\n           , \"column_default\"\n           , \"is_nullable\"\n           , \"character_maximum_length\"\n           , \"numeric_precision\"\n        FROM \"information_schema\".\"columns\"\n       WHERE \"table_schema\" = ?\n         AND \"table_name\" = ?\n    ORDER BY \"ordinal_position\"\n  SQL\n\n  primary_keys = get_primary_keys(table)\n\n  columns.map do |column|\n    type     = get_type(column.data_type)\n    default  = column.column_default\n    required = column.is_nullable == 'NO'\n    key      = primary_keys.include?(column.column_name)\n    length   = column.character_maximum_length\n\n    if type == Integer && default\n      if key\n        type    = DataMapper::Types::Serial if default[/\\Anextval/]\n        default = nil\n      else\n        default = default.to_i\n      end\n    end\n\n    if length\n      length = (required ? 1 : 0)..length\n    end\n    \n    field_name = column.column_name.downcase\n\n    attribute = {\n      :name     => field_name,\n      :type     => type,\n      :required => required,\n      :default  => default,\n      :key      => key,\n      :length   => length,\n    }\n\n    if type == Integer && field_name[-3,3] == \"_id\"\n      # This is a foriegn key. So this model belongs_to the other (_id) one.\n      # Add a special set of values and flag this as a relationship so the reflection code\n      # can rebuild the relationship when it's building the model.\n      attribute[:type] = DataMapper::Associations::Relationship\n      attribute[:relationship] = { \n        :parent => ActiveSupport::Inflector.classify(field_name[0..-4]), \n        :child => ActiveSupport::Inflector.classify(table), \n        # When we can detect more from the database we can optimize this\n        :cardinality => Infinity, \n        :bidirectional => true }\n    end\n\n    # TODO: use the naming convention to compare the name vs the column name\n    unless attribute[:name] == column.column_name\n      attribute[:field] = column.column_name\n    end\n\n    attribute\n  end\nend\n".compress_lines, schema_name, table)

#get_storage_namesString Array

Get the list of table names

Returns:

  • (String Array)

    the names of the tables in the database.



41
42
43
44
45
46
47
# File 'lib/dm-reflection/adapters/postgres.rb', line 41

def get_storage_names
  select("    SELECT \"relname\"\n      FROM \"pg_stat_user_tables\"\n     WHERE \"schemaname\" = 'public'\n  SQL\nend\n".compress_lines)

#get_type(db_type) ⇒ Type

TODO:

This should be verified to identify all mysql primitive types and that they map to the correct DataMapper/Ruby types.

Convert the database type into a DataMapper type

Parameters:

  • db_type (String)

    type specified by the database

Returns:

  • (Type)

    a DataMapper or Ruby type object.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dm-reflection/adapters/postgres.rb', line 17

def get_type(db_type)
  {
    'integer'                     =>  Integer        ,
    'varchar'                     =>  String         ,
    'character varying'           =>  String         ,
    'numeric'                     =>  BigDecimal     ,
    'double precision'            =>  Float          ,
    'datetime'                    =>  DateTime       ,
    'date'                        =>  Date           ,
    'boolean'                     =>  Types::Boolean ,
    'text'                        =>  Types::Text    ,
    'timestamp without time zone' =>  DateTime       ,
  }[db_type] || raise("unknown db type: #{db_type}")
end

#separatorObject



32
33
34
# File 'lib/dm-reflection/adapters/postgres.rb', line 32

def separator
  '--'
end