Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Sep 13, 2023
1 parent e6201e2 commit e151456
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,37 @@ def write_query?(sql) # :nodoc:
end

def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)

# binding.pry
result = nil

sql = transform_query(sql)

log(sql, name, async: async) do
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
handle = if id_insert_table_name = query_requires_identity_insert?(sql)
with_identity_insert_enabled(id_insert_table_name) { _execute(sql, conn) }
else
_execute(sql, conn)
end

if id_insert_table_name = query_requires_identity_insert?(sql)
handle = with_identity_insert_enabled(id_insert_table_name) { _execute(sql, conn) }
else
handle = _execute(sql, conn)
end

# begin
result = handle.do
# handle_to_names_and_values(handle)
# ensure
# finish_statement_handle(handle)
#
# end
# handle = _execute(sql, conn)


# # sync_timezone_changes(conn)
# result = conn.query(sql)
# # handle_warnings(sql)
# result
result = handle.do
end
end

result

#
# if preventing_writes? && write_query?(sql)
# raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
# end
#
# materialize_transactions
# mark_transaction_written_if_write(sql)

# 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

def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
result = nil

sql = transform_query(sql)

check_if_write_query(sql)
mark_transaction_written_if_write(sql)

# TODO: Can be removed when using `with_raw_connection`.
# materialize_transactions




# TODO: Should this be renamed?
# sp_executesql(sql, name, binds, prepare: prepare, async: async)




unless without_prepared_statement?(binds)
types, params = sp_executesql_types_and_parameters(binds)
sql = sp_executesql_sql(sql, types, params, name)
end

type_casted_binds = type_casted_binds(binds)

log(sql, name, binds, type_casted_binds, async: async) do
log(sql, name, binds, async: async) do
with_raw_connection do |conn|
begin
options = { ar_result: true }
Expand Down Expand Up @@ -124,7 +79,7 @@ def exec_update(sql, name, binds)
end

def begin_db_transaction
do_execute "BEGIN TRANSACTION", "TRANSACTION"
execute "BEGIN TRANSACTION", "TRANSACTION"
end

def transaction_isolation_levels
Expand All @@ -137,25 +92,25 @@ def begin_isolated_db_transaction(isolation)
end

def set_transaction_isolation_level(isolation_level)
do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}", "TRANSACTION"
execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}", "TRANSACTION"
end

def commit_db_transaction
do_execute "COMMIT TRANSACTION", "TRANSACTION"
execute "COMMIT TRANSACTION", "TRANSACTION"
end

def exec_rollback_db_transaction
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION"
execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION"
end

include Savepoints

def create_savepoint(name = current_savepoint_name)
do_execute "SAVE TRANSACTION #{name}", "TRANSACTION"
execute "SAVE TRANSACTION #{name}", "TRANSACTION"
end

def exec_rollback_to_savepoint(name = current_savepoint_name)
do_execute "ROLLBACK TRANSACTION #{name}", "TRANSACTION"
execute "ROLLBACK TRANSACTION #{name}", "TRANSACTION"
end

def release_savepoint(name = current_savepoint_name)
Expand Down Expand Up @@ -228,8 +183,6 @@ def build_insert_sql(insert) # :nodoc:
# === SQLServer Specific ======================================== #

def execute_procedure(proc_name, *variables)
# materialize_transactions

vars = if variables.any? && variables.first.is_a?(Hash)
variables.first.map { |k, v| "@#{k} = #{quote(v)}" }
else
Expand Down Expand Up @@ -265,7 +218,7 @@ def use_database(database = nil)
return if sqlserver_azure?

name = SQLServer::Utils.extract_identifiers(database || @connection_parameters[:database]).quoted
do_execute "USE #{name}" unless name.blank?
execute "USE #{name}" unless name.blank?
end

def user_options
Expand Down Expand Up @@ -362,25 +315,13 @@ def sql_for_insert(sql, pk, binds, _returning)
# === SQLServer Specific ======================================== #

def set_identity_insert(table_name, enable = true)
do_execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
execute "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}"
rescue Exception
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
end

# === SQLServer Specific (Executing) ============================ #

# TODO: Remove 'do_execute' and just use `execute`.
def do_execute(sql, name = "SQL")
execute(sql, name)

# connect if @raw_connection.nil?
#
# materialize_transactions
# mark_transaction_written_if_write(sql)
#
# log(sql, name) { raw_connection_do(sql) }
end

# TODO: Adapter should be refactored to use `with_raw_connection` to translate exceptions.
def sp_executesql(sql, name, binds, options = {})
options[:ar_result] = true if options[:fetch] != :rows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def create_database(database, options = {})

def drop_database(database)
name = SQLServer::Utils.extract_identifiers(database)
do_execute "ALTER DATABASE #{name} SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
do_execute "DROP DATABASE #{name}"
execute "ALTER DATABASE #{name} SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
execute "DROP DATABASE #{name}"
end

def current_database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def drop_table(table_name, **options)
pktable = fkdata["PKTABLE_NAME"]
pkcolmn = fkdata["PKCOLUMN_NAME"]
remove_foreign_key fktable, name: fkdata["FK_NAME"]
do_execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
execute "DELETE FROM #{quote_table_name(fktable)} WHERE #{quote_column_name(fkcolmn)} IN ( SELECT #{quote_column_name(pkcolmn)} FROM #{quote_table_name(pktable)} )"
end
end
if options[:if_exists] && version_year < 2016
Expand Down Expand Up @@ -129,7 +129,7 @@ def rename_table(table_name, new_name, **options)
validate_table_length!(new_name) unless options[:_uses_legacy_table_name]
schema_cache.clear_data_source_cache!(table_name.to_s)
schema_cache.clear_data_source_cache!(new_name.to_s)
do_execute "EXEC sp_rename '#{table_name}', '#{new_name}'"
execute "EXEC sp_rename '#{table_name}', '#{new_name}'"
rename_table_indexes(table_name, new_name)
end

Expand All @@ -140,7 +140,7 @@ def remove_column(table_name, column_name, type = nil, **options)
remove_check_constraints(table_name, column_name)
remove_default_constraint(table_name, column_name)
remove_indexes(table_name, column_name)
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
end

def change_column(table_name, column_name, type, options = {})
Expand Down Expand Up @@ -171,7 +171,7 @@ def change_column(table_name, column_name, type, options = {})
indexes.each do |index|
sql_commands << "CREATE INDEX #{quote_table_name(index.name)} ON #{quote_table_name(table_name)} (#{index.columns.map { |c| quote_column_name(c) }.join(', ')})"
end
sql_commands.each { |c| do_execute(c) }
sql_commands.each { |c| execute(c) }
clear_cache!
end

Expand All @@ -182,7 +182,7 @@ def change_column_default(table_name, column_name, default_or_changes)

remove_default_constraint(table_name, column_name)
default = extract_new_default_value(default_or_changes)
do_execute "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{quote_default_expression(default, column)} FOR #{quote_column_name(column_name)}"
execute "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{quote_default_expression(default, column)} FOR #{quote_column_name(column_name)}"
clear_cache!
end

Expand All @@ -202,7 +202,7 @@ def rename_index(table_name, old_name, new_name)
end

def remove_index!(table_name, index_name)
do_execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end

def foreign_keys(table_name)
Expand Down Expand Up @@ -275,11 +275,11 @@ def change_column_null(table_name, column_name, allow_null, default = nil)
column_id = SQLServer::Utils.extract_identifiers(column_name)
column = column_for(table_name, column_name)
if !allow_null.nil? && allow_null == false && !default.nil?
do_execute("UPDATE #{table_id} SET #{column_id}=#{quote(default)} WHERE #{column_id} IS NULL")
execute("UPDATE #{table_id} SET #{column_id}=#{quote(default)} WHERE #{column_id} IS NULL")
end
sql = "ALTER TABLE #{table_id} ALTER COLUMN #{column_id} #{type_to_sql column.type, limit: column.limit, precision: column.precision, scale: column.scale}"
sql += " NOT NULL" if !allow_null.nil? && allow_null == false
do_execute sql
execute sql
end

def create_schema_dumper(options)
Expand Down Expand Up @@ -531,7 +531,7 @@ def remove_columns_for_alter(table_name, *column_names, **options)
def remove_check_constraints(table_name, column_name)
constraints = select_values "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '#{quote_string(table_name)}' and COLUMN_NAME = '#{quote_string(column_name)}'", "SCHEMA"
constraints.each do |constraint|
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_column_name(constraint)}"
execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{quote_column_name(constraint)}"
end
end

Expand All @@ -540,7 +540,7 @@ def remove_default_constraint(table_name, column_name)
execute_procedure(:sp_helpconstraint, table_name, "nomsg").flatten.select do |row|
row["constraint_type"] == "DEFAULT on column #{column_name}"
end.each do |row|
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{row['constraint_name']}"
execute "ALTER TABLE #{quote_table_name(table_name)} DROP CONSTRAINT #{row['constraint_name']}"
end
end

Expand Down
15 changes: 3 additions & 12 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ def schema_creation
SQLServer::SchemaCreation.new(self)
end

# def self.database_exists?(config)
# !!ActiveRecord::Base.sqlserver_connection(config)
# rescue ActiveRecord::NoDatabaseError
# false
# end

def supports_ddl_transactions?
true
end
Expand Down Expand Up @@ -224,10 +218,10 @@ def return_value_after_insert?(column) # :nodoc:

def disable_referential_integrity
tables = tables_with_referential_integrity
tables.each { |t| do_execute "ALTER TABLE #{quote_table_name(t)} NOCHECK CONSTRAINT ALL" }
tables.each { |t| execute "ALTER TABLE #{quote_table_name(t)} NOCHECK CONSTRAINT ALL" }
yield
ensure
tables.each { |t| do_execute "ALTER TABLE #{quote_table_name(t)} CHECK CONSTRAINT ALL" }
tables.each { |t| execute "ALTER TABLE #{quote_table_name(t)} CHECK CONSTRAINT ALL" }
end

# === Abstract Adapter (Connection Management) ================== #
Expand Down Expand Up @@ -266,7 +260,7 @@ def clear_cache!(...)

def reset!
reset_transaction
do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
end

# === Abstract Adapter (Misc Support) =========================== #
Expand Down Expand Up @@ -504,9 +498,6 @@ def sqlserver_version

def connect
@raw_connection = self.class.new_client(@connection_parameters)

# TODO: Should not need to manually call this once adapter is using `with_raw_connection`.
configure_connection
end

def configure_connection
Expand Down

0 comments on commit e151456

Please sign in to comment.