Class: Sequel::Postgres::AutoParameterize::QueryString

Inherits:
String
  • Object
show all
Defined in:
lib/sequel/extensions/pg_auto_parameterize.rb

Overview

SQL query string that also holds an array of parameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#blank?, #camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, inflections, #lit, #pluralize, #singularize, #tableize, #titleize, #to_date, #to_datetime, #to_sequel_blob, #to_sequel_time, #to_time, #underscore

Methods included from SQL::CastMethods

#cast, #cast_numeric, #cast_string

Methods included from SQL::AliasMethods

#as

Instance Attribute Details

#argsObject (readonly)

The array of parameters used by this query.



101
102
103
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 101

def args
  @args
end

Instance Method Details

#+(other) ⇒ Object

Return a new QueryString with the given string appended to the receiver, and the same arguments.



122
123
124
125
126
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 122

def +(other)
  v = self.class.new(super)
  v.instance_variable_set(:@args, @args) if @args
  v
end

#add_arg(s) ⇒ Object

Add a new parameter to this query, which adds the parameter to the array of parameters, and an SQL placeholder to the query itself.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 106

def add_arg(s)
  unless defined?(@args)
    @args = []
    @arg_map = {}
    @arg_map.compare_by_identity
  end

  unless pos = @arg_map[s]
    @args << s
    pos = @arg_map[s] = @args.length.to_s
  end
  self << '$' << pos
end

#auto_param?Boolean

Whether this query string currently supports automatic parameterization. Automatic parameterization is disabled at certain points during query building where PostgreSQL does not support it.

Returns:

  • (Boolean)


132
133
134
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 132

def auto_param?
  !@skip_auto_param
end

#freezeObject

Freeze the stored arguments when freezing the query string.



150
151
152
153
154
155
156
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 150

def freeze
  if @args
    @args.freeze
    @arg_map.freeze
  end
  super
end

#initialize_copy(other) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 163

def initialize_copy(other)
  super
  if args = other.instance_variable_get(:@args)
    @args = args.dup
    @arg_map = other.instance_variable_get(:@arg_map).dup
  end
end

#inspectObject

Show args when the query string is inspected



159
160
161
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 159

def inspect
  @args ? "#{self}; #{@args.inspect}".inspect : super
end

#skip_auto_paramObject

Skip automatic parameterization inside the passed block. This is used during query generation to disable automatic parameterization for clauses not supporting it.



139
140
141
142
143
144
145
146
147
# File 'lib/sequel/extensions/pg_auto_parameterize.rb', line 139

def skip_auto_param
  skip_auto_param = @skip_auto_param
  begin
    @skip_auto_param = true
    yield
  ensure
    @skip_auto_param = skip_auto_param
  end
end