Module: DataObjects::Quoting
- Defined in:
- lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb
Instance Method Summary collapse
-
#escape_sql(args) ⇒ Object
Escape a string of SQL with a set of arguments.
- #quote_array(value) ⇒ Object
- #quote_boolean(value) ⇒ Object
- #quote_class(value) ⇒ Object
- #quote_date(value) ⇒ Object
- #quote_datetime(value) ⇒ Object
- #quote_numeric(value) ⇒ Object
- #quote_range(value) ⇒ Object
- #quote_regexp(value) ⇒ Object
- #quote_string(value) ⇒ Object
- #quote_symbol(value) ⇒ Object
- #quote_time(value) ⇒ Object
- #quote_value(value) ⇒ Object
Instance Method Details
#escape_sql(args) ⇒ Object
Escape a string of SQL with a set of arguments. The first argument is assumed to be the SQL to escape, the remaining arguments (if any) are assumed to be values to escape and interpolate.
Examples
escape_sql("SELECT * FROM zoos")
# => "SELECT * FROM zoos"
escape_sql("SELECT * FROM zoos WHERE name = ?", "Dallas")
# => "SELECT * FROM zoos WHERE name = `Dallas`"
escape_sql("SELECT * FROM zoos WHERE name = ? AND acreage > ?", "Dallas", 40)
# => "SELECT * FROM zoos WHERE name = `Dallas` AND acreage > 40"
Warning
This method is meant mostly for adapters that don’t support bind-parameters.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 22 def escape_sql(args) sql = @text.dup unless args.empty? sql.gsub!(/\?/) do |x| quote_value(args.shift) end end sql end |
#quote_array(value) ⇒ Object
90 91 92 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 90 def quote_array(value) "(#{value.map { |entry| quote_value(entry) }.join(', ')})" end |
#quote_boolean(value) ⇒ Object
86 87 88 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 86 def quote_boolean(value) value.to_s.upcase end |
#quote_class(value) ⇒ Object
70 71 72 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 70 def quote_class(value) quote_string(value.name) end |
#quote_date(value) ⇒ Object
82 83 84 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 82 def quote_date(value) "'#{value.strftime("%Y-%m-%d")}'" end |
#quote_datetime(value) ⇒ Object
78 79 80 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 78 def quote_datetime(value) "'#{value.dup}'" end |
#quote_numeric(value) ⇒ Object
62 63 64 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 62 def quote_numeric(value) value.to_s end |
#quote_range(value) ⇒ Object
94 95 96 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 94 def quote_range(value) "#{quote_value(value.first)} AND #{quote_value(value.last)}" end |
#quote_regexp(value) ⇒ Object
98 99 100 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 98 def quote_regexp(value) quote_string(value.source) end |
#quote_string(value) ⇒ Object
66 67 68 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 66 def quote_string(value) "'#{value.gsub("'", "''")}'" end |
#quote_symbol(value) ⇒ Object
58 59 60 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 58 def quote_symbol(value) quote_string(value.to_s) end |
#quote_time(value) ⇒ Object
74 75 76 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 74 def quote_time(value) "'#{value.strftime('%Y-%m-%d %H:%M:%S')}" + (value.usec > 0 ? ".#{value.usec.to_s.rjust(6, '0')}'" : "'") end |
#quote_value(value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gems/data_objects-0.9.10.1/lib/data_objects/quoting.rb', line 34 def quote_value(value) return 'NULL' if value.nil? case value when Numeric then quote_numeric(value) when String then quote_string(value) when Class then quote_class(value) when Time then quote_time(value) when DateTime then quote_datetime(value) when Date then quote_date(value) when TrueClass, FalseClass then quote_boolean(value) when Array then quote_array(value) when Range then quote_range(value) when Symbol then quote_symbol(value) when Regexp then quote_regexp(value) else if value.respond_to?(:to_sql) value.to_sql else raise "Don't know how to quote #{value.inspect}" end end end |