Module: ActiveRecord::ConnectionAdapters::SQLServer::DatabaseStatements
- Included in:
- ActiveRecord::ConnectionAdapters::SQLServerAdapter
- Defined in:
- lib/active_record/connection_adapters/sqlserver/database_statements.rb
Instance Method Summary collapse
- #begin_db_transaction ⇒ Object
- #begin_isolated_db_transaction(isolation) ⇒ Object
-
#build_insert_sql(insert) ⇒ Object
:nodoc:.
- #case_sensitive_comparison(attribute, value) ⇒ Object
- #commit_db_transaction ⇒ Object
- #exec_delete(sql, name, binds) ⇒ Object
- #exec_rollback_db_transaction ⇒ Object
- #exec_update(sql, name, binds) ⇒ Object
-
#execute_procedure(proc_name, *variables) ⇒ Object
SQLServer Specific ======================================== #.
-
#insert_fixtures_set(fixture_set, tables_to_delete = []) ⇒ Object
We should propose this change to Rails team.
- #internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false) ⇒ Object
- #internal_exec_sql_query(sql, conn) ⇒ Object
- #newid_function ⇒ Object
- #newsequentialid_function ⇒ Object
- #raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
- #set_transaction_isolation_level(isolation_level) ⇒ Object
- #transaction_isolation_levels ⇒ Object
- #use_database(database = nil) ⇒ Object
- #user_options ⇒ Object
- #user_options_dateformat ⇒ Object
- #user_options_isolation_level ⇒ Object
- #user_options_language ⇒ Object
- #with_identity_insert_enabled(table_name, conn) ⇒ Object
-
#write_query?(sql) ⇒ Boolean
:nodoc:.
Instance Method Details
#begin_db_transaction ⇒ Object
76 77 78 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 76 def begin_db_transaction internal_execute("BEGIN TRANSACTION", "TRANSACTION", allow_retry: true, materialize_transactions: false) end |
#begin_isolated_db_transaction(isolation) ⇒ Object
84 85 86 87 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 84 def begin_isolated_db_transaction(isolation) set_transaction_isolation_level(transaction_isolation_levels.fetch(isolation)) begin_db_transaction end |
#build_insert_sql(insert) ⇒ Object
:nodoc:
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 149 def build_insert_sql(insert) # :nodoc: sql = +"INSERT #{insert.into}" if returning = insert.send(:insert_all).returning returning_sql = if returning.is_a?(String) returning else returning.map { |column| "INSERTED.#{quote_column_name(column)}" }.join(", ") end sql << " OUTPUT #{returning_sql}" end sql << " #{insert.values_list}" sql end |
#case_sensitive_comparison(attribute, value) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 101 def case_sensitive_comparison(attribute, value) column = column_for_attribute(attribute) if column.collation && !column.case_sensitive? attribute.eq(Arel::Nodes::Bin.new(value)) else super end end |
#commit_db_transaction ⇒ Object
93 94 95 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 93 def commit_db_transaction internal_execute("COMMIT TRANSACTION", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#exec_delete(sql, name, binds) ⇒ Object
66 67 68 69 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 66 def exec_delete(sql, name, binds) sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows" super(sql, name, binds).rows.first.first end |
#exec_rollback_db_transaction ⇒ Object
97 98 99 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 97 def exec_rollback_db_transaction internal_execute("IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#exec_update(sql, name, binds) ⇒ Object
71 72 73 74 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 71 def exec_update(sql, name, binds) sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows" super(sql, name, binds).rows.first.first end |
#execute_procedure(proc_name, *variables) ⇒ Object
SQLServer Specific ======================================== #
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 167 def execute_procedure(proc_name, *variables) vars = if variables.any? && variables.first.is_a?(Hash) variables.first.map { |k, v| "@#{k} = #{quote(v)}" } else variables.map { |v| quote(v) } end.join(", ") sql = "EXEC #{proc_name} #{vars}".strip log(sql, "Execute Procedure") do |notification_payload| with_raw_connection do |conn| result = internal_raw_execute(sql, conn) verified! = { as: :hash, cache_rows: true, timezone: ActiveRecord.default_timezone || :utc } result.each() do |row| r = row.with_indifferent_access yield(r) if block_given? end result = result.each.map { |row| row.is_a?(Hash) ? row.with_indifferent_access : row } notification_payload[:row_count] = result.count result end end end |
#insert_fixtures_set(fixture_set, tables_to_delete = []) ⇒ Object
We should propose this change to Rails team
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 112 def insert_fixtures_set(fixture_set, tables_to_delete = []) fixture_inserts = [] fixture_set.each do |table_name, fixtures| fixtures.each_slice(insert_rows_length) do |batch| fixture_inserts << build_fixture_sql(batch, table_name) end end table_deletes = tables_to_delete.map { |table| "DELETE FROM #{quote_table_name table}" } total_sqls = Array.wrap(table_deletes + fixture_inserts) disable_referential_integrity do transaction(requires_new: true) do total_sqls.each do |sql| execute sql, "Fixtures Load" yield if block_given? end end end end |
#internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 31 def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false) sql = transform_query(sql) check_if_write_query(sql) mark_transaction_written_if_write(sql) unless without_prepared_statement?(binds) types, params = sp_executesql_types_and_parameters(binds) sql = sp_executesql_sql(sql, types, params, name) end log(sql, name, binds, async: async) do |notification_payload| with_raw_connection do |conn| result = if id_insert_table_name = query_requires_identity_insert?(sql) with_identity_insert_enabled(id_insert_table_name, conn) do internal_exec_sql_query(sql, conn) end else internal_exec_sql_query(sql, conn) end verified! notification_payload[:row_count] = result.count result end end end |
#internal_exec_sql_query(sql, conn) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 59 def internal_exec_sql_query(sql, conn) handle = internal_raw_execute(sql, conn) handle_to_names_and_values(handle, ar_result: true) ensure finish_statement_handle(handle) end |
#newid_function ⇒ Object
259 260 261 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 259 def newid_function select_value "SELECT NEWID()" end |
#newsequentialid_function ⇒ Object
263 264 265 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 263 def newsequentialid_function select_value "SELECT NEWSEQUENTIALID()" end |
#raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 16 def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) log(sql, name, async: async) do |notification_payload| with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn| result = if id_insert_table_name = query_requires_identity_insert?(sql) with_identity_insert_enabled(id_insert_table_name, conn) { internal_raw_execute(sql, conn, perform_do: true) } else internal_raw_execute(sql, conn, perform_do: true) end verified! notification_payload[:row_count] = result result end end end |
#set_transaction_isolation_level(isolation_level) ⇒ Object
89 90 91 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 89 def set_transaction_isolation_level(isolation_level) internal_execute("SET TRANSACTION ISOLATION LEVEL #{isolation_level}", "TRANSACTION", allow_retry: true, materialize_transactions: false) end |
#transaction_isolation_levels ⇒ Object
80 81 82 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 80 def transaction_isolation_levels super.merge snapshot: "SNAPSHOT" end |
#use_database(database = nil) ⇒ Object
201 202 203 204 205 206 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 201 def use_database(database = nil) return if sqlserver_azure? name = SQLServer::Utils.extract_identifiers(database || @connection_parameters[:database]).quoted execute("USE #{name}", "SCHEMA") unless name.blank? end |
#user_options ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 208 def return {} if sqlserver_azure? rows = select_rows("DBCC USEROPTIONS WITH NO_INFOMSGS", "SCHEMA") rows = rows.first if rows.size == 2 && rows.last.empty? rows.reduce(HashWithIndifferentAccess.new) do |values, row| if row.instance_of? Hash set_option = row.values[0].gsub(/\s+/, "_") user_value = row.values[1] elsif row.instance_of? Array set_option = row[0].gsub(/\s+/, "_") user_value = row[1] end values[set_option] = user_value values end end |
#user_options_dateformat ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 226 def if sqlserver_azure? select_value "SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID", "SCHEMA" else ["dateformat"] end end |
#user_options_isolation_level ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 234 def if sqlserver_azure? sql = %(SELECT CASE [transaction_isolation_level] WHEN 0 THEN NULL WHEN 1 THEN 'READ UNCOMMITTED' WHEN 2 THEN 'READ COMMITTED' WHEN 3 THEN 'REPEATABLE READ' WHEN 4 THEN 'SERIALIZABLE' WHEN 5 THEN 'SNAPSHOT' END AS [isolation_level] FROM [sys].[dm_exec_sessions] WHERE [session_id] = @@SPID).squish select_value sql, "SCHEMA" else ["isolation_level"] end end |
#user_options_language ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 251 def if sqlserver_azure? select_value "SELECT @@LANGUAGE AS [language]", "SCHEMA" else ["language"] end end |
#with_identity_insert_enabled(table_name, conn) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 193 def with_identity_insert_enabled(table_name, conn) table_name = quote_table_name(table_name) set_identity_insert(table_name, conn, true) yield ensure set_identity_insert(table_name, conn, false) end |
#write_query?(sql) ⇒ Boolean
:nodoc:
10 11 12 13 14 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 10 def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) rescue ArgumentError # Invalid encoding !READ_QUERY.match?(sql.b) end |