Skip to content

Commit 3fb340c

Browse files
committed
Stronger better default values. Views working too!
1 parent 981087b commit 3fb340c

File tree

7 files changed

+137
-73
lines changed

7 files changed

+137
-73
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* product_level
3333
* product_version
3434
* edition
35+
* Removed tests for old issue #164. Handled by core types now.
3536

3637
#### Fixed
3738

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def quote_default_value(value, column)
1919
if column.type == :uuid && value =~ /\(\)/
2020
value
2121
else
22-
quote(value)
22+
quote(value, column)
2323
end
2424
end
2525

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def column_definitions(table_name)
241241
results = do_exec_query(sql, 'SCHEMA', binds)
242242
results.map do |ci|
243243
ci = ci.symbolize_keys
244+
ci[:_type] = ci[:type]
244245
ci[:type] = case ci[:type]
245246
when /^bit|image|text|ntext|datetime$/
246247
ci[:type]
@@ -255,22 +256,35 @@ def column_definitions(table_name)
255256
else
256257
ci[:type]
257258
end
258-
if ci[:default_value].nil? && schema_cache.view_exists?(table_name)
259-
real_table_name = table_name_or_views_table_name(table_name)
260-
real_column_name = views_real_column_name(table_name, ci[:name])
261-
col_default_sql = "SELECT c.COLUMN_DEFAULT FROM #{database}INFORMATION_SCHEMA.COLUMNS c WHERE c.TABLE_NAME = '#{real_table_name}' AND c.COLUMN_NAME = '#{real_column_name}'"
262-
ci[:default_value] = select_value col_default_sql, 'SCHEMA'
259+
ci[:default_value],
260+
ci[:default_function] = begin
261+
default = ci[:default_value]
262+
if default.nil? && schema_cache.view_exists?(table_name)
263+
default = select_value "
264+
SELECT c.COLUMN_DEFAULT
265+
FROM #{database}INFORMATION_SCHEMA.COLUMNS c
266+
WHERE c.TABLE_NAME = '#{table_name_or_views_table_name(table_name)}'
267+
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'".squish, 'SCHEMA'
268+
end
269+
case default
270+
when nil
271+
[nil, nil]
272+
when /\A\((\w+\(\))\)\Z/
273+
default_function = Regexp.last_match[1]
274+
[nil, default_function]
275+
when /\A\(N'(.*)'\)\Z/m
276+
string_literal = SQLServer::Utils.unquote_string(Regexp.last_match[1])
277+
[string_literal, nil]
278+
else
279+
type = case ci[:type]
280+
when /smallint|int|bigint/ then ci[:_type]
281+
else ci[:type]
282+
end
283+
value = default.match(/\A\((.*)\)\Z/m)[1]
284+
value = select_value "SELECT CAST(#{value} AS #{type}) AS value", 'SCHEMA'
285+
[value, nil]
286+
end
263287
end
264-
ci[:default_value] = case ci[:default_value]
265-
when nil, '(null)', '(NULL)'
266-
nil
267-
when /\A\((\w+\(\))\)\Z/
268-
ci[:default_function] = Regexp.last_match[1]
269-
nil
270-
else
271-
match_data = ci[:default_value].match(/\A\(+N?'?(.*?)'?\)+\Z/m)
272-
match_data ? match_data[1].gsub("''", "'") : nil
273-
end
274288
ci[:null] = ci[:is_nullable].to_i == 1
275289
ci.delete(:is_nullable)
276290
ci[:is_primary] = ci[:is_primary].to_i == 1

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,14 @@ def initialize_type_map(m)
192192
m.register_type %r{.*}, SQLServer::Type::UnicodeString.new
193193
# Exact Numerics
194194
register_class_with_limit m, 'bigint(8)', SQLServer::Type::BigInteger
195+
m.alias_type 'bigint', 'bigint(8)'
195196
register_class_with_limit m, 'int(4)', SQLServer::Type::Integer
197+
m.alias_type 'integer', 'int(4)'
198+
m.alias_type 'int', 'int(4)'
196199
register_class_with_limit m, 'smallint(2)', SQLServer::Type::SmallInteger
200+
m.alias_type 'smallint', 'smallint(2)'
197201
register_class_with_limit m, 'tinyint(1)', SQLServer::Type::TinyInteger
202+
m.alias_type 'tinyint', 'tinyint(1)'
198203
m.register_type 'bit', SQLServer::Type::Boolean.new
199204
m.register_type %r{\Adecimal}i do |sql_type|
200205
scale = extract_scale(sql_type)

test/cases/adapter_test_sqlserver.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,6 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
204204
assert_equal '[foo].[bar].[baz]', connection.quote_column_name('foo.bar.baz')
205205
end
206206

207-
it "return 0 for empty string" do
208-
assert_equal '0', connection.quote('', Post.columns_hash['id'])
209-
end
210-
211207
it "surround string with national prefix" do
212208
assert_equal "N'foo'", connection.quote("foo")
213209
end

0 commit comments

Comments
 (0)