Methods
Public Instance
- cast_sql_append
- complex_expression_sql_append
- literal_append
- multi_insert_sql
- no_auto_parameterize
- placeholder_literalizer_class
- use_cursor
- with_sql
Protected Instance
Public Instance methods
Do not add implicit typecasts for directly typecasted values, since the user is presumably doing so to set the type, not convert from the implicitly typecasted type.
# File lib/sequel/extensions/pg_auto_parameterize.rb 263 def cast_sql_append(sql, expr, type) 264 if auto_param?(sql) && auto_param_type(expr) 265 sql << 'CAST(' 266 sql.add_arg(expr) 267 sql << ' AS ' << db.cast_type_literal(type).to_s << ')' 268 else 269 super 270 end 271 end
Transform column IN (int, …) expressions into column = ANY($) and column NOT IN (int, …) expressions into column != ALL($) using an integer array bound variable for the ANY/ALL argument. This is the same optimization PostgreSQL performs internally, but this reduces the number of bound variables.
# File lib/sequel/extensions/pg_auto_parameterize.rb 278 def complex_expression_sql_append(sql, op, args) 279 case op 280 when :IN, :"NOT IN" 281 l, r = args 282 if auto_param?(sql) && !l.is_a?(Array) && _integer_array?(r) && r.size > 1 283 if op == :IN 284 op = :"=" 285 func = :ANY 286 else 287 op = :!= 288 func = :ALL 289 end 290 args = [l, Sequel.function(func, Sequel.cast(_integer_array_auto_param(r), 'int8[]'))] 291 end 292 end 293 294 super 295 end
For strings, numeric arguments, and date/time arguments, add them as parameters to the query instead of literalizing them into the SQL.
# File lib/sequel/extensions/pg_auto_parameterize.rb 309 def literal_append(sql, v) 310 if auto_param?(sql) && (type = auto_param_type(v)) 311 sql.add_arg(v) << type 312 else 313 super 314 end 315 end
Parameterize insertion of multiple values
# File lib/sequel/extensions/pg_auto_parameterize.rb 298 def multi_insert_sql(columns, values) 299 if @opts[:no_auto_parameterize] 300 super 301 else 302 [clone(:multi_insert_values=>values.map{|r| Array(r)}).insert_sql(columns, LiteralString.new('VALUES '))] 303 end 304 end
Return a clone of the dataset that will not do automatic parameterization.
# File lib/sequel/extensions/pg_auto_parameterize.rb 254 def no_auto_parameterize 255 cached_dataset(:_no_auto_parameterize_ds) do 256 @opts[:no_auto_parameterize] ? self : clone(:no_auto_parameterize=>true) 257 end 258 end
The class to use for placeholder literalizers.
# File lib/sequel/extensions/pg_auto_parameterize.rb 318 def placeholder_literalizer_class 319 if @opts[:no_auto_parameterize] 320 super 321 else 322 PlaceholderLiteralizer 323 end 324 end
Disable automatic parameterization when using a cursor.
# File lib/sequel/extensions/pg_auto_parameterize.rb 327 def use_cursor(*) 328 super.no_auto_parameterize 329 end
Store receiving dataset and args when with_sql is used with a method name symbol, so sql can be parameterized correctly if used as a subselect.
# File lib/sequel/extensions/pg_auto_parameterize.rb 333 def with_sql(*a) 334 ds = super 335 if Symbol === a[0] 336 ds = ds.clone(:with_sql_dataset=>self, :with_sql_args=>a.freeze) 337 end 338 ds 339 end
Protected Instance methods
Disable automatic parameterization for prepared statements, since they will use manual parameterization.
# File lib/sequel/extensions/pg_auto_parameterize.rb 345 def to_prepared_statement(*a) 346 @opts[:no_auto_parameterize] ? super : no_auto_parameterize.to_prepared_statement(*a) 347 end