module Sequel::Postgres::AutoParameterize::DatasetMethods
Public Instance Methods
Source
# 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
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.
Source
# 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
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.
Source
# 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
For strings, numeric arguments, and date/time arguments, add them as parameters to the query instead of literalizing them into the SQL.
Source
# 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
Parameterize insertion of multiple values
Source
# 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
Return a clone of the dataset that will not do automatic parameterization.
Source
# 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
The class to use for placeholder literalizers.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 327 def use_cursor(*) 328 super.no_auto_parameterize 329 end
Disable automatic parameterization when using a cursor.
Source
# 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
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.
Protected Instance Methods
Source
# 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
Disable automatic parameterization for prepared statements, since they will use manual parameterization.
Private Instance Methods
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 408 def _insert_values_sql(sql, values) 409 super 410 411 if values = @opts[:multi_insert_values] 412 expression_list_append(sql, values.map{|r| Array(r)}) 413 end 414 end
Handle parameterization of multi_insert_sql
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 417 def _integer_array?(v) 418 (Array === v || Set === v) && v.all?{|x| nil == x || Integer === x} 419 end
Whether the given argument is an array or set of integers or NULL values, recursively.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 423 def _integer_array_auto_param(v) 424 buf = String.new 425 buf << '{' 426 comma = false 427 v.each do |x| 428 if comma 429 buf << "," 430 else 431 comma = true 432 end 433 434 buf << (x ? x.to_s : 'NULL') 435 end 436 buf << '}' 437 end
Create the bound variable string that will be used for the IN (int, β¦) to = ANY($) optimization for integer arrays.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 395 def auto_param?(sql) 396 sql.is_a?(QueryString) && sql.auto_param? 397 end
Whether the given query string currently supports automatic parameterization.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 354 def auto_param_type(v) 355 case v 356 when Symbol 357 false 358 when String 359 case v 360 when LiteralString 361 false 362 when Sequel::SQL::Blob 363 "::bytea" 364 else 365 "" 366 end 367 when Integer 368 ((v > 2147483647 || v < -2147483648) ? "::int8" : "::int4") 369 when Float 370 # PostgreSQL treats literal floats as numeric, not double precision 371 # But older versions of PostgreSQL don't handle Infinity/NaN in numeric 372 v.finite? ? "::numeric" : "::double precision" 373 when BigDecimal 374 "::numeric" 375 when Sequel::SQLTime 376 "::time" 377 when Time 378 "::#{@db.cast_type_literal(Time)}" 379 when DateTime 380 "::#{@db.cast_type_literal(DateTime)}" 381 when Date 382 "::date" 383 else 384 v.respond_to?(:sequel_auto_param_type) ? v.sequel_auto_param_type(self) : auto_param_type_fallback(v) 385 end 386 end
If auto parameterization is supported for the value, return a string for the implicit typecast to use. Return false/nil if the value should not be automatically parameterized.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 390 def auto_param_type_fallback(v) 391 super if defined?(super) 392 end
Allow other extensions to support auto parameterization in ways that do not require adding the sequel_auto_param_type method.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 403 def default_import_slice 404 @opts[:no_auto_parameterize] ? super : 40 405 end
Default the import slice to 40, since PostgreSQL supports a maximum of 1600 columns per table, and it supports a maximum of 65k parameters. Technically, there can be more than one parameter per column, so this doesnβt prevent going over the limit, though it does make it less likely.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 474 def explain_sql_string_origin(opts) 475 @opts[:no_auto_parameterize] ? super : (QueryString.new << super) 476 end
A mutable string used as the prefix when explaining a query.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 440 def select_limit_sql(sql) 441 if auto_param?(sql) && (@opts[:limit] || @opts[:offset]) 442 sql.skip_auto_param{super} 443 else 444 super 445 end 446 end
Skip auto parameterization in LIMIT and OFFSET clauses
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 450 def select_order_sql(sql) 451 if auto_param?(sql) && (order = @opts[:order]) && order.any?{|o| Integer === o || (SQL::OrderedExpression === o && Integer === o.expression)} 452 sql.skip_auto_param{super} 453 else 454 super 455 end 456 end
Skip auto parameterization in ORDER clause if used with integer values indicating ordering by the nth column.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 459 def select_with_sql_cte_search_cycle(sql,cte) 460 if auto_param?(sql) && cte[:cycle] 461 sql.skip_auto_param{super} 462 else 463 super 464 end 465 end
Skip auto parameterization in CTE CYCLE clause
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 469 def sql_string_origin 470 @opts[:no_auto_parameterize] ? super : QueryString.new 471 end
Unless auto parameterization is disabled, use a string that can store the parameterized arguments.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 500 def static_sql(sql) 501 if @opts[:append_sql] || @opts[:no_auto_parameterize] || String === sql 502 super 503 else 504 query_string = QueryString.new 505 literal_append(query_string, sql) 506 query_string 507 end 508 end
Use auto parameterization for datasets with static SQL using placeholders.
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 491 def subselect_sql_append_sql(sql, ds) 492 if args = ds.opts[:subselect_sql_args] 493 ds.send(*args) 494 else 495 super 496 end 497 end
Source
# File lib/sequel/extensions/pg_auto_parameterize.rb 481 def subselect_sql_dataset(sql, ds) 482 if ws_ds = ds.opts[:with_sql_dataset] 483 super(sql, ws_ds).clone(:subselect_sql_args=>ds.opts[:with_sql_args]) 484 else 485 super 486 end 487 end