drysql 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,26 @@ module ActiveRecord
|
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
5
|
class SQLServerColumn < Column# :nodoc:
|
6
|
+
attr_reader :identity, :is_special
|
6
7
|
alias :generated? :identity
|
7
8
|
|
8
9
|
def default_specified?
|
9
10
|
!default.nil?
|
10
11
|
end
|
12
|
+
|
13
|
+
# Borrowed verbatim from standard Rails sqlserver_adapter.
|
14
|
+
# Had to re-implement a bit of functionality here to avoid
|
15
|
+
# a dependency on SQLServerColumn.identity accessor
|
16
|
+
#
|
17
|
+
# Once the ActiveRecord gem releases the latest version of
|
18
|
+
# sqlserver_adapter, I'll be to remove this code
|
19
|
+
def initialize(name, default, sql_type = nil, identity = false, null = true)
|
20
|
+
super(name, default, sql_type, null)
|
21
|
+
@identity = identity
|
22
|
+
@is_special = sql_type =~ /text|ntext|image/i
|
23
|
+
# SQL Server only supports limits on *char and float types
|
24
|
+
@limit = nil unless @type == :float or @type == :string
|
25
|
+
end
|
11
26
|
|
12
27
|
end
|
13
28
|
|
@@ -33,6 +48,53 @@ module ActiveRecord
|
|
33
48
|
end
|
34
49
|
|
35
50
|
class SQLServerAdapter < AbstractAdapter
|
51
|
+
|
52
|
+
# Borrowed verbatim from Rails sqlserver_adapter.
|
53
|
+
#
|
54
|
+
# Since we can't create a dependency from ActiveRecord to DrySQL,
|
55
|
+
# we need to instantiate SQLServer columns in this class extension.
|
56
|
+
#
|
57
|
+
# I'll be able to remove this code once the ActiveRecord gem is
|
58
|
+
# released with the new version of the sqlserver_adapter that defines
|
59
|
+
# the SQLServerColumn class
|
60
|
+
def columns(table_name, name = nil)
|
61
|
+
return [] if table_name.blank?
|
62
|
+
table_name = table_name.to_s if table_name.is_a?(Symbol)
|
63
|
+
table_name = table_name.split('.')[-1] unless table_name.nil?
|
64
|
+
sql = %Q{
|
65
|
+
SELECT
|
66
|
+
cols.COLUMN_NAME as ColName,
|
67
|
+
cols.COLUMN_DEFAULT as DefaultValue,
|
68
|
+
cols.NUMERIC_SCALE as numeric_scale,
|
69
|
+
cols.NUMERIC_PRECISION as numeric_precision,
|
70
|
+
cols.DATA_TYPE as ColType,
|
71
|
+
cols.IS_NULLABLE As IsNullable,
|
72
|
+
COL_LENGTH(cols.TABLE_NAME, cols.COLUMN_NAME) as Length,
|
73
|
+
COLUMNPROPERTY(OBJECT_ID(cols.TABLE_NAME), cols.COLUMN_NAME, 'IsIdentity') as IsIdentity,
|
74
|
+
cols.NUMERIC_SCALE as Scale
|
75
|
+
FROM INFORMATION_SCHEMA.COLUMNS cols
|
76
|
+
WHERE cols.TABLE_NAME = '#{table_name}'
|
77
|
+
}
|
78
|
+
# Comment out if you want to have the Columns select statment logged.
|
79
|
+
# Personally, I think it adds unnecessary bloat to the log.
|
80
|
+
# If you do comment it out, make sure to un-comment the "result" line that follows
|
81
|
+
result = log(sql, name) { @connection.select_all(sql) }
|
82
|
+
#result = @connection.select_all(sql)
|
83
|
+
columns = []
|
84
|
+
result.each do |field|
|
85
|
+
default = field[:DefaultValue].to_s.gsub!(/[()\']/,"") =~ /null/ ? nil : field[:DefaultValue]
|
86
|
+
if field[:ColType] =~ /numeric|decimal/i
|
87
|
+
type = "#{field[:ColType]}(#{field[:numeric_precision]},#{field[:numeric_scale]})"
|
88
|
+
else
|
89
|
+
type = "#{field[:ColType]}(#{field[:Length]})"
|
90
|
+
end
|
91
|
+
is_identity = field[:IsIdentity] == 1
|
92
|
+
is_nullable = field[:IsNullable] == 'YES'
|
93
|
+
columns << SQLServerColumn.new(field[:ColName], default, type, is_identity, is_nullable)
|
94
|
+
end
|
95
|
+
columns
|
96
|
+
end
|
97
|
+
|
36
98
|
|
37
99
|
def constraints(table_name, name = nil)#:nodoc:
|
38
100
|
constraints = []
|
metadata
CHANGED