Module: DataMapper::Reflection::PostgresAdapter

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

Overview

TODO:

The postgres adapter extensions have not been tested yet.

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(<<-SQL.compress_lines, table).to_set
        SELECT "key_column_usage"."column_name"
          FROM "information_schema"."table_constraints"
    INNER JOIN "information_schema"."key_column_usage" USING("constraint_schema", "constraint_name")
         WHERE "table_constraints"."constraint_type" = 'PRIMARY KEY'
           AND "table_constraints"."table_name" = ?
  SQL
end

#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(<<-SQL.compress_lines, schema_name, table)
      SELECT "column_name"
           , "data_type"
           , "column_default"
           , "is_nullable"
           , "character_maximum_length"
           , "numeric_precision"
        FROM "information_schema"."columns"
       WHERE "table_schema" = ?
         AND "table_name" = ?
    ORDER BY "ordinal_position"
  SQL

  primary_keys = get_primary_keys(table)

  columns.map do |column|
    type     = get_type(column.data_type)
    default  = column.column_default
    required = column.is_nullable == 'NO'
    key      = primary_keys.include?(column.column_name)
    length   = column.character_maximum_length

    if type == Integer && default
      if key
        type    = DataMapper::Types::Serial if default[/\Anextval/]
        default = nil
      else
        default = default.to_i
      end
    end

    if length
      length = (required ? 1 : 0)..length
    end
    
    field_name = column.column_name.downcase

    attribute = {
      :name     => field_name,
      :type     => type,
      :required => required,
      :default  => default,
      :key      => key,
      :length   => length,
    }

    if type == Integer && field_name[-3,3] == "_id"
      # This is a foriegn key. So this model belongs_to the other (_id) one.
      # Add a special set of values and flag this as a relationship so the reflection code
      # can rebuild the relationship when it's building the model.
      attribute[:type] = DataMapper::Associations::Relationship
      attribute[:relationship] = { 
        :parent => ActiveSupport::Inflector.classify(field_name[0..-4]), 
        :child => ActiveSupport::Inflector.classify(table), 
        # When we can detect more from the database we can optimize this
        :cardinality => Infinity, 
        :bidirectional => true }
    end

    # TODO: use the naming convention to compare the name vs the column name
    unless attribute[:name] == column.column_name
      attribute[:field] = column.column_name
    end

    attribute
  end
end

#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(<<-SQL.compress_lines)
    SELECT "relname"
      FROM "pg_stat_user_tables"
     WHERE "schemaname" = 'public'
  SQL
end

#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