Skip to content

Commit 479a2ba

Browse files
committed
Move quoting methods into adapter section for it.
1 parent 37e34a3 commit 479a2ba

File tree

1 file changed

+70
-72
lines changed

1 file changed

+70
-72
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ def supports_ddl_transactions?
294294
true
295295
end
296296

297+
def native_database_types
298+
txt = sqlserver_2005? ? "varchar(max)" : "text"
299+
bin = sqlserver_2005? ? "varbinary(max)" : "image"
300+
{
301+
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
302+
:string => { :name => "varchar", :limit => 255 },
303+
:text => { :name => txt },
304+
:integer => { :name => "int" },
305+
:float => { :name => "float", :limit => 8 },
306+
:decimal => { :name => "decimal" },
307+
:datetime => { :name => "datetime" },
308+
:timestamp => { :name => "datetime" },
309+
:time => { :name => "datetime" },
310+
:date => { :name => "datetime" },
311+
:binary => { :name => bin },
312+
:boolean => { :name => "bit"}
313+
}
314+
end
315+
297316
def database_version
298317
select_value "SELECT @@version"
299318
end
@@ -312,34 +331,63 @@ def sqlserver_2005?
312331

313332
# QUOTING ==================================================#
314333

334+
def quote(value, column = nil)
335+
return value.quoted_id if value.respond_to?(:quoted_id)
336+
case value
337+
when TrueClass then '1'
338+
when FalseClass then '0'
339+
when String, ActiveSupport::Multibyte::Chars
340+
value = value.to_s
341+
# for binary columns, don't quote the result of the string to binary
342+
return column.class.string_to_binary(value) if column && column.type == :binary && column.class.respond_to?(:string_to_binary)
343+
super
344+
else
345+
if value.acts_like?(:time)
346+
"'#{value.strftime("%Y%m%d %H:%M:%S")}'"
347+
elsif value.acts_like?(:date)
348+
"'#{value.strftime("%Y%m%d")}'"
349+
else
350+
super
351+
end
352+
end
353+
end
354+
355+
def quote_string(string)
356+
string.gsub(/\'/, "''")
357+
end
358+
359+
# Quotes the given column identifier.
360+
#
361+
# quote_column_name('foo') # => '[foo]'
362+
# quote_column_name(:foo) # => '[foo]'
363+
# quote_column_name('foo.bar') # => '[foo].[bar]'
364+
def quote_column_name(identifier)
365+
identifier.to_s.split('.').collect do |name|
366+
"[#{name}]"
367+
end.join(".")
368+
end
369+
370+
def quote_table_name(name)
371+
name_split_on_dots = name.to_s.split('.')
372+
if name_split_on_dots.length == 3
373+
# name is on the form "foo.bar.baz"
374+
"[#{name_split_on_dots[0]}].[#{name_split_on_dots[1]}].[#{name_split_on_dots[2]}]"
375+
else
376+
super(name)
377+
end
378+
end
379+
380+
# REFERENTIAL INTEGRITY ====================================#
381+
# TODO: Add #disable_referential_integrity if we can use it
315382

316383

384+
# DATABASE STATEMENTS ======================================
385+
317386

318387
# SCHEMA STATEMENTS ========================================#
319388

320-
def native_database_types
321-
txt = sqlserver_2005? ? "varchar(max)" : "text"
322-
bin = sqlserver_2005? ? "varbinary(max)" : "image"
323-
{
324-
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
325-
:string => { :name => "varchar", :limit => 255 },
326-
:text => { :name => txt },
327-
:integer => { :name => "int" },
328-
:float => { :name => "float", :limit => 8 },
329-
:decimal => { :name => "decimal" },
330-
:datetime => { :name => "datetime" },
331-
:timestamp => { :name => "datetime" },
332-
:time => { :name => "datetime" },
333-
:date => { :name => "datetime" },
334-
:binary => { :name => bin },
335-
:boolean => { :name => "bit"}
336-
}
337-
end
338389

339-
340390

341-
342-
343391
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
344392
# Remove limit for data types which do not require it
345393
# Valid: ALTER TABLE sessions ALTER COLUMN [data] varchar(max)
@@ -514,59 +562,9 @@ def rollback_db_transaction
514562
ensure
515563
@connection["AutoCommit"] = true
516564
end
565+
517566

518-
def quote(value, column = nil)
519-
return value.quoted_id if value.respond_to?(:quoted_id)
520-
521-
case value
522-
when TrueClass then '1'
523-
when FalseClass then '0'
524-
525-
when String, ActiveSupport::Multibyte::Chars
526-
value = value.to_s
527-
528-
# for binary columns, don't quote the result of the string to binary
529-
return column.class.string_to_binary(value) if column && column.type == :binary && column.class.respond_to?(:string_to_binary)
530-
super
531-
else
532-
if value.acts_like?(:time)
533-
"'#{value.strftime("%Y%m%d %H:%M:%S")}'"
534-
elsif value.acts_like?(:date)
535-
"'#{value.strftime("%Y%m%d")}'"
536-
else
537-
super
538-
end
539-
end
540-
end
541-
542-
def quote_string(string)
543-
string.gsub(/\'/, "''")
544-
end
545-
546-
def quote_table_name(name)
547-
name_split_on_dots = name.to_s.split('.')
548-
549-
if name_split_on_dots.length == 3
550-
# name is on the form "foo.bar.baz"
551-
"[#{name_split_on_dots[0]}].[#{name_split_on_dots[1]}].[#{name_split_on_dots[2]}]"
552-
else
553-
super(name)
554-
end
555-
556-
end
557567

558-
# Quotes the given column identifier.
559-
#
560-
# Examples
561-
#
562-
# quote_column_name('foo') # => '[foo]'
563-
# quote_column_name(:foo) # => '[foo]'
564-
# quote_column_name('foo.bar') # => '[foo].[bar]'
565-
def quote_column_name(identifier)
566-
identifier.to_s.split('.').collect do |name|
567-
"[#{name}]"
568-
end.join(".")
569-
end
570568

571569
def add_limit_offset!(sql, options)
572570
if options[:offset]

0 commit comments

Comments
 (0)