Module: DataMapper::Migrations::DataObjectsAdapter::SQL

Included in:
DataMapper::Migrations::DataObjectsAdapter
Defined in:
lib/dm-migrations/adapters/dm-do-adapter.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#alter_table_add_column_statement(connection, table_name, schema_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 132

def alter_table_add_column_statement(connection, table_name, schema_hash)
  "ALTER TABLE #{quote_name(table_name)} ADD COLUMN #{property_schema_statement(connection, schema_hash)}"
end

#create_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 158

def create_index_statements(model)
  name       = self.name
  table_name = model.storage_name(name)

  indexes(model).map do |index_name, fields|
    <<-SQL.compress_lines
      CREATE INDEX #{quote_name("index_#{table_name}_#{index_name}")} ON
      #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
    SQL
  end
end

#create_table_statement(connection, model, properties) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



137
138
139
140
141
142
143
144
145
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 137

def create_table_statement(connection, model, properties)
  statement = <<-SQL.compress_lines
    CREATE TABLE #{quote_name(model.storage_name(name))}
    (#{properties.map { |property| property_schema_statement(connection, property_schema_hash(property)) }.join(', ')},
    PRIMARY KEY(#{ properties.key.map { |property| quote_name(property.field) }.join(', ')}))
  SQL

  statement
end

#create_unique_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 171

def create_unique_index_statements(model)
  name           = self.name
  table_name     = model.storage_name(name)
  key            = model.key(name).map { |property| property.field }
  unique_indexes = unique_indexes(model).reject { |index_name, fields| fields == key }

  unique_indexes.map do |index_name, fields|
    <<-SQL.compress_lines
      CREATE UNIQUE INDEX #{quote_name("unique_#{table_name}_#{index_name}")} ON
      #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
    SQL
  end
end

#drop_table_statement(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
151
152
153
154
155
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 148

def drop_table_statement(model)
  table_name = quote_name(model.storage_name(name))
  if supports_drop_table_if_exists?
    "DROP TABLE IF EXISTS #{table_name}"
  else
    "DROP TABLE #{table_name}"
  end
end

#indexes(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



242
243
244
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 242

def indexes(model)
  model.properties(name).indexes
end

#property_schema_hash(property) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 186

def property_schema_hash(property)
  primitive = property.primitive
  type      = property.type
  type_map  = self.class.type_map

  schema = (type_map[property.class] || type_map[primitive] || type_map[type]).merge(:name => property.field)

  schema_primitive = schema[:primitive]

  if primitive == String && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR'
    schema[:length] = property.length
  elsif primitive == BigDecimal || primitive == Float
    schema[:precision] = property.precision
    schema[:scale]     = property.scale
  end

  schema[:allow_nil] = property.allow_nil?
  schema[:serial]    = property.serial?

  default = property.default

  if default.nil? || default.respond_to?(:call)
    # remove the default if the property does not allow nil
    schema.delete(:default) unless schema[:allow_nil]
  else
    schema[:default] = if type.respond_to?(:dump)
      type.dump(default, property)
    else
      default
    end
  end

  schema
end

#property_schema_statement(connection, schema) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 222

def property_schema_statement(connection, schema)
  statement = quote_name(schema[:name])
  statement << " #{schema[:primitive]}"

  length = schema[:length]

  if schema[:precision] && schema[:scale]
    statement << "(#{[ :precision, :scale ].map { |key| connection.quote_value(schema[key]) }.join(', ')})"
  elsif length == 'max'
    statement << '(max)'
  elsif length
    statement << "(#{connection.quote_value(length)})"
  end

  statement << " DEFAULT #{connection.quote_value(schema[:default])}" if schema.key?(:default)
  statement << ' NOT NULL' unless schema[:allow_nil]
  statement
end

#schema_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 127

def schema_name
  raise NotImplementedError, "#{self.class}#schema_name not implemented"
end

#supports_drop_table_if_exists?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


122
123
124
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 122

def supports_drop_table_if_exists?
  false
end

#supports_serial?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adapters that support AUTO INCREMENT fields for CREATE TABLE statements should overwrite this to return true

Returns:

  • (Boolean)


117
118
119
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 117

def supports_serial?
  false
end

#unique_indexes(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



247
248
249
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 247

def unique_indexes(model)
  model.properties(name).unique_indexes
end