| 56 | | def initialize(name, default, sql_type = nil, identity = false, null = true) # TODO: check ok to remove scale_value = 0 |
|---|
| 57 | | super(name, default, sql_type, null) |
|---|
| 58 | | @identity = identity |
|---|
| 59 | | @is_special = sql_type =~ /text|ntext|image/i |
|---|
| | 56 | def initialize(info) |
|---|
| | 57 | if info[:type] =~ /numeric|decimal/i |
|---|
| | 58 | type = "#{info[:type]}(#{info[:numeric_precision]},#{info[:numeric_scale]})" |
|---|
| | 59 | else |
|---|
| | 60 | type = "#{info[:type]}(#{info[:length]})" |
|---|
| | 61 | end |
|---|
| | 62 | super(info[:name], info[:default_value], type, info[:is_nullable]) |
|---|
| | 63 | @identity = info[:is_identity] |
|---|
| | 64 | @is_special = ["text", "ntext", "image"].include?(info[:type]) |
|---|
| 266 | | cols.COLUMN_NAME as ColName, |
|---|
| 267 | | cols.COLUMN_DEFAULT as DefaultValue, |
|---|
| 268 | | cols.NUMERIC_SCALE as numeric_scale, |
|---|
| 269 | | cols.NUMERIC_PRECISION as numeric_precision, |
|---|
| 270 | | cols.DATA_TYPE as ColType, |
|---|
| 271 | | cols.IS_NULLABLE As IsNullable, |
|---|
| 272 | | COL_LENGTH(cols.TABLE_NAME, cols.COLUMN_NAME) as Length, |
|---|
| 273 | | COLUMNPROPERTY(OBJECT_ID(cols.TABLE_NAME), cols.COLUMN_NAME, 'IsIdentity') as IsIdentity, |
|---|
| 274 | | cols.NUMERIC_SCALE as Scale |
|---|
| 275 | | FROM INFORMATION_SCHEMA.COLUMNS cols |
|---|
| 276 | | WHERE cols.TABLE_NAME = '#{table_name}' |
|---|
| | 270 | columns.COLUMN_NAME as name, |
|---|
| | 271 | columns.DATA_TYPE as type, |
|---|
| | 272 | CASE |
|---|
| | 273 | WHEN columns.COLUMN_DEFAULT = '(null)' OR columns.COLUMN_DEFAULT = '(NULL)' THEN NULL |
|---|
| | 274 | ELSE columns.COLUMN_DEFAULT |
|---|
| | 275 | END default_value, |
|---|
| | 276 | columns.NUMERIC_SCALE as numeric_scale, |
|---|
| | 277 | columns.NUMERIC_PRECISION as numeric_precision, |
|---|
| | 278 | COL_LENGTH(columns.TABLE_NAME, columns.COLUMN_NAME) as length, |
|---|
| | 279 | CASE |
|---|
| | 280 | WHEN constraint_column_usage.constraint_name IS NULL THEN NULL |
|---|
| | 281 | ELSE 1 |
|---|
| | 282 | END is_primary_key, |
|---|
| | 283 | CASE |
|---|
| | 284 | WHEN columns.IS_NULLABLE = 'YES' THEN 1 |
|---|
| | 285 | ELSE NULL |
|---|
| | 286 | end is_nullable, |
|---|
| | 287 | CASE |
|---|
| | 288 | WHEN COLUMNPROPERTY(OBJECT_ID(columns.TABLE_NAME), columns.COLUMN_NAME, 'IsIdentity') = 0 THEN NULL |
|---|
| | 289 | ELSE 1 |
|---|
| | 290 | END is_identity |
|---|
| | 291 | FROM INFORMATION_SCHEMA.COLUMNS columns |
|---|
| | 292 | LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS primary_key_constraints ON ( |
|---|
| | 293 | primary_key_constraints.table_name = columns.table_name |
|---|
| | 294 | AND primary_key_constraints.constraint_type = 'PRIMARY KEY' |
|---|
| | 295 | ) |
|---|
| | 296 | LEFT OUTER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE constraint_column_usage ON ( |
|---|
| | 297 | constraint_column_usage.table_name = primary_key_constraints.table_name |
|---|
| | 298 | AND constraint_column_usage.column_name = columns.column_name |
|---|
| | 299 | ) |
|---|
| | 300 | WHERE columns.TABLE_NAME = '#{table_name}' |
|---|
| 278 | | # Comment out if you want to have the Columns select statment logged. |
|---|
| 279 | | # Personally, I think it adds unnecessary bloat to the log. |
|---|
| 280 | | # If you do comment it out, make sure to un-comment the "result" line that follows |
|---|
| 281 | | result = log(sql, name) { @connection.select_all(sql) } |
|---|
| 282 | | #result = @connection.select_all(sql) |
|---|
| 283 | | columns = [] |
|---|
| 284 | | result.each do |field| |
|---|
| 285 | | default = field[:DefaultValue].to_s.gsub!(/[()\']/,"") =~ /null/ ? nil : field[:DefaultValue] |
|---|
| 286 | | if field[:ColType] =~ /numeric|decimal/i |
|---|
| 287 | | type = "#{field[:ColType]}(#{field[:numeric_precision]},#{field[:numeric_scale]})" |
|---|
| 288 | | else |
|---|
| 289 | | type = "#{field[:ColType]}(#{field[:Length]})" |
|---|
| 290 | | end |
|---|
| 291 | | is_identity = field[:IsIdentity] == 1 |
|---|
| 292 | | is_nullable = field[:IsNullable] == 'YES' |
|---|
| 293 | | columns << SQLServerColumn.new(field[:ColName], default, type, is_identity, is_nullable) |
|---|
| | 302 | |
|---|
| | 303 | result = select(sql, name, true) |
|---|
| | 304 | result.collect do |column_info| |
|---|
| | 305 | # Remove brackets and outer quotes (if quoted) of default value returned by db, i.e: |
|---|
| | 306 | # "(1)" => "1", "('1')" => "1", "((-1))" => "-1", "('(-1)')" => "(-1)" |
|---|
| | 307 | column_info.symbolize_keys! |
|---|
| | 308 | column_info[:default_value] = column_info[:default_value].match(/\A\(+'?(.*?)'?\)+\Z/)[1] if column_info[:default_value] |
|---|
| | 309 | SQLServerColumn.new(column_info) |
|---|