activerecord_sqlserver_crm 4.2.10 → 4.2.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 852af08ab4e5b1408ea6f7f939429e6a1de0a483
4
- data.tar.gz: ba5b5a0d5065d9d48d7705f9b6c06dbb2bf5fbc7
3
+ metadata.gz: 37cc057428329e3fb1237f1865b2467eb345c1ab
4
+ data.tar.gz: c8543c1bd36bc079c045864c922267225279538f
5
5
  SHA512:
6
- metadata.gz: c3c671afd7d84e06a1d8680ac5ceb0f2cfeb023f3230e71b8a4a725e6872f326be3ce9450e2a9758f55aca5a79ccd86ecbbb6e8ae36b70cf2a601e13a8439f7a
7
- data.tar.gz: 7bf8bd37343cb91a0c248767e52e7c62c633a72b9d380a402f0dcd5a89edd200b2dab41fea688521c44b8b8fd74a8564e8643944284f21db397e8fee38b8cbe8
6
+ metadata.gz: 41768daa59810f26010bc8841078e4b7f6512e560a10f7aab88dd338dcbb2fca3db33a4d3f1193311749d7fa30d33c73a0bf6819d173dab7875b8faaad48015f
7
+ data.tar.gz: 19290167d84ba6f6448826a771f77c0990b1e88ec5fbf6c71e353af223262c718af14a0067cab73b2acd027bf1a233adea2e17a9139c9cdf2722a1a293e1d314
@@ -0,0 +1,156 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module SQLServer
4
+ module SchemaStatements
5
+
6
+ SCHEMA_EXPIRES_IN = 1.day
7
+
8
+ def views_real_column_name(table_name, column_name)
9
+ view_definition = Rails.cache.fetch("sqlserver_table_view_info_#{table_name}", expires_in: SCHEMA_EXPIRES_IN) do
10
+ schema_cache.view_information(table_name)[:VIEW_DEFINITION]
11
+ end
12
+ return column_name unless view_definition
13
+ # match_data = view_definition.match(/([\w-]*)\s+as\s+#{column_name}/im)
14
+ # ret = match_data ? match_data[1] : column_name
15
+ ret = column_name
16
+ ret
17
+ end
18
+
19
+ def column_definitions(table_name)
20
+ col_defn = Rails.cache.fetch("sqlserver_col_defns_#{table_name}", expires_in: SCHEMA_EXPIRES_IN) do
21
+ identifier = if database_prefix_remote_server?
22
+ SQLServer::Utils.extract_identifiers("#{database_prefix}#{table_name}")
23
+ else
24
+ SQLServer::Utils.extract_identifiers(table_name)
25
+ end
26
+ database = identifier.fully_qualified_database_quoted
27
+ view_exists = schema_cache.view_exists?(table_name)
28
+ view_tblnm = table_name_or_views_table_name(table_name) if view_exists
29
+ sql = %{
30
+ SELECT DISTINCT
31
+ #{lowercase_schema_reflection_sql('columns.TABLE_NAME')} AS table_name,
32
+ #{lowercase_schema_reflection_sql('columns.COLUMN_NAME')} AS name,
33
+ columns.DATA_TYPE AS type,
34
+ columns.COLUMN_DEFAULT AS default_value,
35
+ columns.NUMERIC_SCALE AS numeric_scale,
36
+ columns.NUMERIC_PRECISION AS numeric_precision,
37
+ columns.DATETIME_PRECISION AS datetime_precision,
38
+ columns.COLLATION_NAME AS collation,
39
+ columns.ordinal_position,
40
+ CASE
41
+ WHEN columns.DATA_TYPE IN ('nchar','nvarchar','char','varchar') THEN columns.CHARACTER_MAXIMUM_LENGTH
42
+ ELSE COL_LENGTH('#{database}.'+columns.TABLE_SCHEMA+'.'+columns.TABLE_NAME, columns.COLUMN_NAME)
43
+ END AS [length],
44
+ CASE
45
+ WHEN columns.IS_NULLABLE = 'YES' THEN 1
46
+ ELSE NULL
47
+ END AS [is_nullable],
48
+ CASE
49
+ WHEN KCU.COLUMN_NAME IS NOT NULL AND TC.CONSTRAINT_TYPE = N'PRIMARY KEY' THEN 1
50
+ ELSE NULL
51
+ END AS [is_primary],
52
+ c.is_identity AS [is_identity]
53
+ FROM #{database}.INFORMATION_SCHEMA.COLUMNS columns
54
+ LEFT OUTER JOIN #{database}.INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
55
+ ON TC.TABLE_NAME = columns.TABLE_NAME
56
+ AND TC.CONSTRAINT_TYPE = N'PRIMARY KEY'
57
+ LEFT OUTER JOIN #{database}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
58
+ ON KCU.COLUMN_NAME = columns.COLUMN_NAME
59
+ AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
60
+ AND KCU.CONSTRAINT_CATALOG = TC.CONSTRAINT_CATALOG
61
+ AND KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
62
+ INNER JOIN #{database}.sys.schemas AS s
63
+ ON s.name = columns.TABLE_SCHEMA
64
+ AND s.schema_id = s.schema_id
65
+ INNER JOIN #{database}.sys.objects AS o
66
+ ON s.schema_id = o.schema_id
67
+ AND o.is_ms_shipped = 0
68
+ AND o.type IN ('U', 'V')
69
+ AND o.name = columns.TABLE_NAME
70
+ INNER JOIN #{database}.sys.columns AS c
71
+ ON o.object_id = c.object_id
72
+ AND c.name = columns.COLUMN_NAME
73
+ WHERE columns.TABLE_NAME = @0
74
+ AND columns.TABLE_SCHEMA = #{identifier.schema.blank? ? 'schema_name()' : '@1'}
75
+ ORDER BY columns.ordinal_position
76
+ }.gsub(/[ \t\r\n]+/, ' ')
77
+ binds = [[info_schema_table_name_column, identifier.object]]
78
+ binds << [info_schema_table_schema_column, identifier.schema] unless identifier.schema.blank?
79
+ results = sp_executesql(sql, 'SCHEMA', binds)
80
+ ret = results.map do |ci|
81
+ ci = ci.symbolize_keys
82
+ ci[:_type] = ci[:type]
83
+ ci[:table_name] = view_tblnm || table_name
84
+ ci[:type] = case ci[:type]
85
+ when /^bit|image|text|ntext|datetime$/
86
+ ci[:type]
87
+ when /^datetime2|datetimeoffset$/i
88
+ "#{ci[:type]}(#{ci[:datetime_precision]})"
89
+ when /^time$/i
90
+ "#{ci[:type]}(#{ci[:datetime_precision]})"
91
+ when /^numeric|decimal$/i
92
+ "#{ci[:type]}(#{ci[:numeric_precision]},#{ci[:numeric_scale]})"
93
+ when /^float|real$/i
94
+ "#{ci[:type]}"
95
+ when /^char|nchar|varchar|nvarchar|binary|varbinary|bigint|int|smallint$/
96
+ ci[:length].to_i == -1 ? "#{ci[:type]}(max)" : "#{ci[:type]}(#{ci[:length]})"
97
+ else
98
+ ci[:type]
99
+ end
100
+ ci[:default_value],
101
+ ci[:default_function] = begin
102
+ default = ci[:default_value]
103
+ if default.nil? && view_exists
104
+ default = column_default(database, view_tblnm, views_real_column_name(table_name, ci[:name]))
105
+ end
106
+ case default
107
+ when nil
108
+ [nil, nil]
109
+ when /\A\((\w+\(\))\)\Z/
110
+ default_function = Regexp.last_match[1]
111
+ [nil, default_function]
112
+ when /\A\(N'(.*)'\)\Z/m
113
+ string_literal = SQLServer::Utils.unquote_string(Regexp.last_match[1])
114
+ [string_literal, nil]
115
+ when /CREATE DEFAULT/mi
116
+ [nil, nil]
117
+ else
118
+ type = case ci[:type]
119
+ when /smallint|int|bigint/ then ci[:_type]
120
+ else ci[:type]
121
+ end
122
+ value = default.match(/\A\((.*)\)\Z/m)[1]
123
+ value = select_value "SELECT CAST(#{value} AS #{type}) AS value", 'SCHEMA'
124
+ [value, nil]
125
+ end
126
+ end
127
+ ci[:null] = ci[:is_nullable].to_i == 1
128
+ ci.delete(:is_nullable)
129
+ ci[:is_primary] = ci[:is_primary].to_i == 1
130
+ ci[:is_identity] = ci[:is_identity].to_i == 1 unless [TrueClass, FalseClass].include?(ci[:is_identity].class)
131
+ ci
132
+ end
133
+ ret
134
+ end
135
+ col_defn
136
+ end
137
+
138
+ def column_default(database, table_name, col_name)
139
+ columns = Rails.cache.fetch("sqlserver_def_col_#{database}_#{table_name}", expires_in: SCHEMA_EXPIRES_IN) do
140
+ result = select "
141
+ SELECT c.COLUMN_NAME, c.COLUMN_DEFAULT
142
+ FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
143
+ WHERE c.TABLE_NAME = '#{table_name}'".squish, 'SCHEMA'
144
+ default_column_values = {}
145
+ result.each do |row|
146
+ default_column_values[row["COLUMN_NAME"]] = row["COLUMN_DEFAULT"]
147
+ end
148
+ default_column_values
149
+ end
150
+ columns[col_name]
151
+ end
152
+
153
+ end
154
+ end
155
+ end
156
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiverecordSqlserverCrm
2
- VERSION = "4.2.10"
2
+ VERSION = "4.2.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_sqlserver_crm
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.10
4
+ version: 4.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rolf Lawrenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-13 00:00:00.000000000 Z
11
+ date: 2016-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -55,6 +55,9 @@ dependencies:
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
60
+ - - ">="
58
61
  - !ruby/object:Gem::Version
59
62
  version: 1.0.4
60
63
  type: :runtime
@@ -62,6 +65,9 @@ dependencies:
62
65
  version_requirements: !ruby/object:Gem::Requirement
63
66
  requirements:
64
67
  - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - - ">="
65
71
  - !ruby/object:Gem::Version
66
72
  version: 1.0.4
67
73
  - !ruby/object:Gem::Dependency
@@ -69,6 +75,9 @@ dependencies:
69
75
  requirement: !ruby/object:Gem::Requirement
70
76
  requirements:
71
77
  - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.1'
80
+ - - ">="
72
81
  - !ruby/object:Gem::Version
73
82
  version: 1.1.0
74
83
  type: :runtime
@@ -76,6 +85,9 @@ dependencies:
76
85
  version_requirements: !ruby/object:Gem::Requirement
77
86
  requirements:
78
87
  - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ - - ">="
79
91
  - !ruby/object:Gem::Version
80
92
  version: 1.1.0
81
93
  - !ruby/object:Gem::Dependency
@@ -140,6 +152,7 @@ files:
140
152
  - app/models/crm/user.rb
141
153
  - config/initializers/01_constants.rb
142
154
  - config/initializers/extensions.rb
155
+ - config/initializers/schema_statements.rb
143
156
  - config/routes.rb
144
157
  - lib/active_record_extension.rb
145
158
  - lib/activerecord_sqlserver_crm.rb