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.



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

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.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 154

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.



133
134
135
136
137
138
139
140
141
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 133

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.



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

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.



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

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.



238
239
240
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 238

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.



182
183
184
185
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
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 182

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.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 218

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)


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

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)


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

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)


113
114
115
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 113

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.



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

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