Module: Sequel::Postgres::AutoParameterize::DatasetMethods

Defined in:
lib/sequel/extensions/pg_auto_parameterize.rb

Instance Method Summary collapse

Instance Method Details

#cast_sql_append(sql, expr, type) ⇒ Object

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.



258
259
260
261
262
263
264
265
266
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 258

def cast_sql_append(sql, expr, type)
  if auto_param?(sql) && auto_param_type(expr)
    sql << 'CAST('
    sql.add_arg(expr)
    sql << ' AS ' << db.cast_type_literal(type).to_s << ')'
  else
    super
  end
end

#complex_expression_sql_append(sql, op, args) ⇒ Object

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.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 273

def complex_expression_sql_append(sql, op, args)
  case op
  when :IN, :"NOT IN"
    l, r = args
    if auto_param?(sql) && !l.is_a?(Array) && _integer_array?(r) && r.size > 1
      if op == :IN 
        op = :"="
        func = :ANY
      else
        op = :!=
        func = :ALL
      end
      args = [l, Sequel.function(func, Sequel.cast(_integer_array_auto_param(r), 'int8[]'))]
    end
  end

  super
end

#literal_append(sql, v) ⇒ Object

For strings, numeric arguments, and date/time arguments, add them as parameters to the query instead of literalizing them into the SQL.



304
305
306
307
308
309
310
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 304

def literal_append(sql, v)
  if auto_param?(sql) && (type = auto_param_type(v))
    sql.add_arg(v) << type
  else
    super
  end
end

#multi_insert_sql(columns, values) ⇒ Object

Parameterize insertion of multiple values



293
294
295
296
297
298
299
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 293

def multi_insert_sql(columns, values)
  if @opts[:no_auto_parameterize]
    super
  else
    [clone(:multi_insert_values=>values.map{|r| Array(r)}).insert_sql(columns, LiteralString.new('VALUES '))]
  end
end

#no_auto_parameterizeObject

Return a clone of the dataset that will not do automatic parameterization.



249
250
251
252
253
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 249

def no_auto_parameterize
  cached_dataset(:_no_auto_parameterize_ds) do
    @opts[:no_auto_parameterize] ? self : clone(:no_auto_parameterize=>true)
  end
end

#placeholder_literalizer_classObject

The class to use for placeholder literalizers.



313
314
315
316
317
318
319
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 313

def placeholder_literalizer_class
  if @opts[:no_auto_parameterize]
    super
  else
    PlaceholderLiteralizer
  end
end

#use_cursorObject

Disable automatic parameterization when using a cursor.



322
323
324
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 322

def use_cursor(*)
  super.no_auto_parameterize
end

#with_sql(*a) ⇒ Object

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.



328
329
330
331
332
333
334
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 328

def with_sql(*a)
  ds = super 
  if Symbol === a[0]
    ds = ds.clone(:with_sql_dataset=>self, :with_sql_args=>a.freeze)
  end
  ds
end