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
- #add_limit_offset!(sql, options) ⇒ Object
- #begin_db_transaction ⇒ Object
- #case_sensitive_equality_operator ⇒ Object
- #charset ⇒ Object
- #commit_db_transaction ⇒ Object
- #create_database(database) ⇒ Object
- #create_savepoint ⇒ Object
- #current_database ⇒ Object
- #drop_database(database) ⇒ Object
- #empty_insert_statement_value ⇒ Object
- #execute(sql, name = nil, skip_logging = false) ⇒ Object
-
#execute_procedure(proc_name, *variables) ⇒ Object
SQLServer Specific ======================================== #.
- #limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
- #newid_function ⇒ Object
- #newsequentialid_function ⇒ Object
- #outside_transaction? ⇒ Boolean
-
#recreate_database ⇒ Object
SQLServer Specific (Rake/Test Helpers) ==================== #.
- #recreate_database!(database = nil) ⇒ Object
- #release_savepoint ⇒ Object
- #rollback_db_transaction ⇒ Object
- #rollback_to_savepoint ⇒ Object
- #run_with_isolation_level(isolation_level) ⇒ Object
- #select_one(sql, name = nil) ⇒ Object
- #select_rows(sql, name = nil) ⇒ Object
- #use_database(database = nil) ⇒ Object
- #user_options ⇒ Object
Instance Method Details
#add_limit_offset!(sql, options) ⇒ Object
50 51 52 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 50 def add_limit_offset!(sql, ) raise NotImplementedError, 'This has been moved to the SQLServerCompiler in Arel.' end |
#begin_db_transaction ⇒ Object
27 28 29 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 27 def begin_db_transaction do_execute "BEGIN TRANSACTION" end |
#case_sensitive_equality_operator ⇒ Object
58 59 60 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 58 def case_sensitive_equality_operator cs_equality_operator end |
#charset ⇒ Object
186 187 188 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 186 def charset select_value "SELECT SERVERPROPERTY('SqlCharSetName')" end |
#commit_db_transaction ⇒ Object
31 32 33 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 31 def commit_db_transaction do_execute "COMMIT TRANSACTION" end |
#create_database(database) ⇒ Object
178 179 180 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 178 def create_database(database) do_execute "CREATE DATABASE #{quote_table_name(database)}" end |
#create_savepoint ⇒ Object
39 40 41 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 39 def create_savepoint do_execute "SAVE TRANSACTION #{current_savepoint_name}" end |
#current_database ⇒ Object
182 183 184 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 182 def current_database select_value 'SELECT DB_NAME()' end |
#drop_database(database) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 161 def drop_database(database) retry_count = 0 max_retries = 1 begin do_execute "DROP DATABASE #{quote_table_name(database)}" rescue ActiveRecord::StatementInvalid => err if err. =~ /because it is currently in use/i raise if retry_count >= max_retries retry_count += 1 remove_database_connections_and_rollback(database) retry else raise end end end |
#empty_insert_statement_value ⇒ Object
54 55 56 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 54 def empty_insert_statement_value "DEFAULT VALUES" end |
#execute(sql, name = nil, skip_logging = false) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 15 def execute(sql, name = nil, skip_logging = false) if id_insert_table_name = query_requires_identity_insert?(sql) with_identity_insert_enabled(id_insert_table_name) { do_execute(sql,name) } else do_execute(sql,name) end end |
#execute_procedure(proc_name, *variables) ⇒ Object
SQLServer Specific ======================================== #
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 71 def execute_procedure(proc_name, *variables) vars = variables.map{ |v| quote(v) }.join(', ') sql = "EXEC #{proc_name} #{vars}".strip name = 'Execute Procedure' log(sql, name) do case @connection_options[:mode] when :dblib result = @connection.execute(sql) result.each(:as => :hash, :cache_rows => true) do |row| r = row.with_indifferent_access yield(r) if block_given? end result.each.map{ |row| row.is_a?(Hash) ? row.with_indifferent_access : row } when :odbc results = [] raw_connection_run(sql) do |handle| get_rows = lambda { rows = handle_to_names_and_values handle, :fetch => :all rows.each_with_index { |r,i| rows[i] = r.with_indifferent_access } results << rows } get_rows.call while handle_more_results?(handle) get_rows.call end end results.many? ? results : results.first when :adonet results = [] results << select(sql, name).map { |r| r.with_indifferent_access } results.many? ? results : results.first end end end |
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 62 def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) match_data = where_sql.match(/^(.*?[\]\) ])WHERE[\[\( ]/) limit = match_data[1] where_sql.sub!(limit,'') "WHERE #{quoted_primary_key} IN (SELECT #{limit} #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})" end |
#newid_function ⇒ Object
134 135 136 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 134 def newid_function select_value "SELECT NEWID()" end |
#newsequentialid_function ⇒ Object
138 139 140 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 138 def newsequentialid_function select_value "SELECT NEWSEQUENTIALID()" end |
#outside_transaction? ⇒ Boolean
23 24 25 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 23 def outside_transaction? info_schema_query { select_value("SELECT @@TRANCOUNT") == 0 } end |
#recreate_database ⇒ Object
SQLServer Specific (Rake/Test Helpers) ==================== #
144 145 146 147 148 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 144 def recreate_database remove_database_connections_and_rollback do do_execute "EXEC sp_MSforeachtable 'DROP TABLE ?'" end end |
#recreate_database!(database = nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 150 def recreate_database!(database=nil) current_db = current_database database ||= current_db this_db = database.to_s == current_db do_execute 'USE master' if this_db drop_database(database) create_database(database) ensure use_database(current_db) if this_db end |
#release_savepoint ⇒ Object
43 44 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 43 def release_savepoint end |
#rollback_db_transaction ⇒ Object
35 36 37 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 35 def rollback_db_transaction do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION" end |
#rollback_to_savepoint ⇒ Object
46 47 48 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 46 def rollback_to_savepoint do_execute "ROLLBACK TRANSACTION #{current_savepoint_name}" end |
#run_with_isolation_level(isolation_level) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 123 def run_with_isolation_level(isolation_level) raise ArgumentError, "Invalid isolation level, #{isolation_level}. Supported levels include #{valid_isolation_levels.to_sentence}." if !valid_isolation_levels.include?(isolation_level.upcase) initial_isolation_level = [:isolation_level] || "READ COMMITTED" do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}" begin yield ensure do_execute "SET TRANSACTION ISOLATION LEVEL #{initial_isolation_level}" end if block_given? end |
#select_one(sql, name = nil) ⇒ Object
6 7 8 9 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 6 def select_one(sql, name = nil) result = raw_select sql, name, :fetch => :one (result && result.first.present?) ? result.first : nil end |
#select_rows(sql, name = nil) ⇒ Object
11 12 13 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 11 def select_rows(sql, name = nil) raw_select sql, name, :fetch => :rows end |
#use_database(database = nil) ⇒ Object
106 107 108 109 110 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 106 def use_database(database=nil) return if sqlserver_azure? database ||= @connection_options[:database] do_execute "USE #{quote_table_name(database)}" unless database.blank? end |
#user_options ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/active_record/connection_adapters/sqlserver/database_statements.rb', line 112 def info_schema_query do select_rows("dbcc useroptions").inject(HashWithIndifferentAccess.new) do |values,row| set_option = row[0].gsub(/\s+/,'_') user_value = row[1] values[set_option] = user_value values end end end |