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)
- - (Object) add_column_statement private
- - (Object) alter_table_add_column_statement(connection, table_name, schema_hash) private
- - (Object) create_index_statement(model, index_name, fields) private
- - (Object) create_index_statements(model) private
- - (Object) create_table_statement(connection, model, properties) private
- - (Object) create_unique_index_statements(model) private
- - (Object) drop_table_statement(model) private
- - (Object) indexes(model) private
- - (Object) property_schema_hash(property) private
- - (Object) property_schema_statement(connection, schema) private
- - (Object) schema_name private
- - (Boolean) supports_drop_table_if_exists? private
-
- (Boolean) supports_serial?
private
Adapters that support AUTO INCREMENT fields for CREATE TABLE statements should overwrite this to return true.
- - (Object) unique_indexes(model) private
Instance Method Details
- (Object) add_column_statement
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.
263 264 265 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 263 def add_column_statement 'ADD COLUMN' end |
- (Object) alter_table_add_column_statement(connection, table_name, schema_hash)
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 |
- (Object) create_index_statement(model, index_name, fields)
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 |
- (Object) create_index_statements(model)
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 |
- (Object) create_table_statement(connection, model, properties)
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 |
- (Object) create_unique_index_statements(model)
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 |
- (Object) drop_table_statement(model)
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 |
- (Object) indexes(model)
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 indexes(model) model.properties(name).indexes end |
- (Object) property_schema_hash(property)
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) dump_class = property.dump_class type_map = self.class.type_map schema = (type_map[property.class] || type_map[dump_class]).merge(:name => property.field) schema_primitive = schema[:primitive] if dump_class.equal?(String) && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR' && schema_primitive != 'BYTEA' schema[:length] = property.length elsif dump_class.equal?(BigDecimal) || dump_class.equal?(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 |
- (Object) property_schema_statement(connection, schema)
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 246 247 248 249 250 |
# 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 default = schema[:default] if default statement << " DEFAULT #{connection.quote_value(default)}" end statement << ' NOT NULL' unless schema[:allow_nil] statement end |
- (Object) schema_name
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.
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 |
- (Boolean) supports_drop_table_if_exists?
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.
126 127 128 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 126 def supports_drop_table_if_exists? false end |
- (Boolean) supports_serial?
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
121 122 123 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 121 def supports_serial? false end |
- (Object) unique_indexes(model)
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 unique_indexes(model) model.properties(name).unique_indexes end |