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

#add_column_statementObject

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.



258
259
260
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 258

def add_column_statement
  'ADD COLUMN'
end

#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.



136
137
138
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 136

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

#create_index_statement(model, index_name, fields) ⇒ 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.



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

def create_index_statement(model, index_name, fields)
  table_name = model.storage_name(name)

  DataMapper::Ext::String.compress_lines(<<-SQL)
    CREATE INDEX #{quote_name("index_#{table_name}_#{index_name}")} ON
    #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
  SQL
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.



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

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

  indexes(model).map do |index_name, fields|
    create_index_statement(model, index_name, fields)
  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.



141
142
143
144
145
146
147
148
149
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 141

def create_table_statement(connection, model, properties)
  statement = DataMapper::Ext::String.compress_lines(<<-SQL)
    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.



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 182

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|
    DataMapper::Ext::String.compress_lines(<<-SQL)
      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.



152
153
154
155
156
157
158
159
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 152

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.



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

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 197

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

  schema = (type_map[property.class] || type_map[property.class.superclass] || type_map[primitive]).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] = property.dump(default)
  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.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 228

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)


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

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)


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

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)


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

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.



253
254
255
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 253

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