Skip to content

Commit 257312e

Browse files
aidanharanAidan Haran
andauthored
Refactored method to get the table name from raw SQL (#951)
Co-authored-by: Aidan Haran <aharan@fusioneer.com>
1 parent 3a62f12 commit 257312e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,22 @@ def remove_indexes(table_name, column_name)
531531

532532
# === SQLServer Specific (Misc Helpers) ========================= #
533533

534+
# Parses just the table name from the SQL. Table name does not include database/schema/etc.
534535
def get_table_name(sql)
535-
tn = if sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)(\s+INTO)?\s+([^\(\s]+)\s*|^\s*update\s+([^\(\s]+)\s*/i
536-
Regexp.last_match[3] || Regexp.last_match[4]
537-
elsif sql =~ /FROM\s+([^\(\s]+)\s*/i
538-
Regexp.last_match[1]
539-
else
540-
nil
541-
end
536+
tn = get_raw_table_name(sql)
542537
SQLServer::Utils.extract_identifiers(tn).object
543538
end
544539

540+
# Parses the raw table name that is used in the SQL. Table name could include database/schema/etc.
541+
def get_raw_table_name(sql)
542+
case sql
543+
when /^\s*(INSERT|EXEC sp_executesql N'INSERT)(\s+INTO)?\s+([^\(\s]+)\s*|^\s*update\s+([^\(\s]+)\s*/i
544+
Regexp.last_match[3] || Regexp.last_match[4]
545+
when /FROM\s+([^\(\s]+)\s*/i
546+
Regexp.last_match[1]
547+
end
548+
end
549+
545550
def default_constraint_name(table_name, column_name)
546551
"DF_#{table_name}_#{column_name}"
547552
end

0 commit comments

Comments
 (0)