Class: Lore::Clause

Inherits:
String show all
Defined in:
lib/lore/clause.rb

Overview

class }}}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#lore_escape

Constructor Details

#initialize(field_name = '', left_side = '', value_string = '', plan = {}) ⇒ Clause

Returns a new instance of Clause.



107
108
109
110
111
# File 'lib/lore/clause.rb', line 107

def initialize(field_name='', left_side='', value_string='', plan={})
  @value_string  = value_string
  @left_side     = left_side
  @field_name    = field_name
end

Instance Attribute Details

#field_nameObject (readonly)

{{{



105
106
107
# File 'lib/lore/clause.rb', line 105

def field_name
  @field_name
end

#left_sideObject (readonly)

{{{



105
106
107
# File 'lib/lore/clause.rb', line 105

def left_side
  @left_side
end

#planObject (readonly)

{{{



105
106
107
# File 'lib/lore/clause.rb', line 105

def plan
  @plan
end

#value_stringObject (readonly)

{{{



105
106
107
# File 'lib/lore/clause.rb', line 105

def value_string
  @value_string
end

Class Method Details

.for(accessor) ⇒ Object



113
114
115
# File 'lib/lore/clause.rb', line 113

def self.for(accessor)
  Clause_Parser.new(accessor)
end

Instance Method Details

#&(value) ⇒ Object Also known as: and



243
244
245
246
247
248
249
250
251
# File 'lib/lore/clause.rb', line 243

def &(value) 
  return unless value
  if @left_side.gsub(' ','') != '' then
    @value_string = ' AND ' << value.left_side 
  else 
    @value_string = value.left_side
  end
  Clause.new(@field_name, @left_side+@value_string, '', @plan)
end

#+(value) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/lore/clause.rb', line 155

def +(value)
  if value.instance_of? String then 
    value = '\''+value.lore_escape+'\'::text'
  else 
    value = value.to_s.lore_escape
  end
  @value_string = @field_name.to_s + '+'+value
  return Clause.new(@value_string, @left_side.to_s+@value_string.to_s)
end

#-(value) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/lore/clause.rb', line 164

def -(value)
  if value.instance_of? String then 
    value = '\''+value.lore_escape+'\'::text'
  else 
    value = value.to_s.lore_escape
  end
  @value_string = @field_name + '-'+ value
  return Clause.new(@value_string, @left_side+@value_string)
end

#<(value) ⇒ Object



184
185
186
187
# File 'lib/lore/clause.rb', line 184

def <(value) 
  @value_string = @field_name + ' < ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#<=(value) ⇒ Object



188
189
190
191
# File 'lib/lore/clause.rb', line 188

def <=(value) 
  @value_string = @field_name + ' <= ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#<=>(value) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/lore/clause.rb', line 230

def <=>(value) 
  if(value != :NULL)
    @value_string = @field_name << ' != ' << Lore.parse_field_value(value)
  else 
    @value_string = @field_name << ' NOT NULL '
  end
  Clause.new(@field_name, @left_side+@value_string, '', @plan)
end

#==(value) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/lore/clause.rb', line 215

def ==(value) 
  if value.instance_of? Clause then
    value = value.field_name
  else 
    value = value.to_s.lore_escape
    value = '\'' << value << '\''
  end
  if(value != :NULL)
    @value_string = @field_name << ' = ' << value
  else
    @value_string = @field_name << ' IS NULL' 
  end
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#>(value) ⇒ Object



192
193
194
195
# File 'lib/lore/clause.rb', line 192

def >(value) 
  @value_string = @field_name + ' > ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#>=(value) ⇒ Object



196
197
198
199
# File 'lib/lore/clause.rb', line 196

def >=(value) 
  @value_string = @field_name + ' >= ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#between(range_begin, range_end) ⇒ Object



150
151
152
153
# File 'lib/lore/clause.rb', line 150

def between(range_begin, range_end)
  @value_string = @field_name << " BETWEEN #{range_begin} AND #{range_end} "
  Clause.new(@value_string, @left_side+@value_string)
end

#has_element(element) ⇒ Object



254
255
256
257
258
259
# File 'lib/lore/clause.rb', line 254

def has_element(element)
  element = element.to_s if element.kind_of? Clause
  element = '\''+element.lore_escape+'\'' if element.kind_of? String
  @value_string = element + ' = ANY ('  << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#has_element_ilike(element) ⇒ Object

This query requires a custom operator, defined by:

create function irlike(text,text) returns bool as 'select $2 ilike $1' language sql strict immutable; 
create operator ~~~~ (procedure = irlike, leftarg = text, rightarg = text, commutator = ~~);


278
279
280
281
282
283
# File 'lib/lore/clause.rb', line 278

def has_element_ilike(element)
  element = element.to_s if element.kind_of? Clause
  element = '\'' + element.lore_escape + '\'' if element.kind_of? String
  @value_string = element + ' ~~~~ ANY (' << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#has_element_like(element) ⇒ Object

This query requires a custom operator, defined by:

create function rlike(text,text) returns bool as 'select $2 like $1' language sql strict immutable; 
create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~);


266
267
268
269
270
271
# File 'lib/lore/clause.rb', line 266

def has_element_like(element)
  element = element.to_s if element.kind_of? Clause
  element = '\''+element.lore_escape+'\'' if element.kind_of? String
  @value_string = element + ' ~~~ ANY ('  << @field_name+ ')'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#ilike(value) ⇒ Object



205
206
207
208
209
# File 'lib/lore/clause.rb', line 205

def ilike(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' ILIKE ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#in(nested_query_string) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lore/clause.rb', line 131

def in(nested_query_string)
  # Check for Refined_Select needed for functionality of 
  # Refined_Select. Example: 
  # 
  #   User.all.with(User.user_id.in( Admin.all(:user_id) ))
  # 
  if(nested_query_string.instance_of? Refined_Select) then
    nested_query_string = nested_query_string.to_select
  elsif nested_query_string.instance_of? Array then
    raise ::Exception.new('IN field expects at least one element. ') if nested_query_string.length == 0
    nested_query_string = nested_query_string.join(',')
  elsif nested_query_string.instance_of? Range then
    return between(nested_query_string.first, nested_query_string.last)
  end
  @value_string = @field_name << ' IN (' << "\n" << nested_query_string.to_s << ') '
  Clause.new(@value_string, @left_side+@value_string)

end

#inspectObject



297
298
299
# File 'lib/lore/clause.rb', line 297

def inspect
  return 'Clause('+to_s+')'
end

#is_not_nullObject



179
180
181
182
# File 'lib/lore/clause.rb', line 179

def is_not_null()
  @value_string = @field_name + ' IS NOT NULL'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#is_nullObject



174
175
176
177
# File 'lib/lore/clause.rb', line 174

def is_null()
  @value_string = @field_name + ' IS NULL'
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#like(value) ⇒ Object



200
201
202
203
204
# File 'lib/lore/clause.rb', line 200

def like(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' LIKE ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#not_in(nested_query_string) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lore/clause.rb', line 117

def not_in(nested_query_string)
  # Check for Refined_Select needed for functionality of 
  # Refined_Select. Example: 
  # 
  #   User.all.with(User.user_id.in( Admin.all(:user_id) ))
  # 
  if(nested_query_string.instance_of? Refined_Select) then
    nested_query_string.to_inner_select
  else
    @value_string = @field_name << ' NOT IN (' << "\n" << nested_query_string.to_s << ') '
    Clause.new(@value_string, @left_side+@value_string)
  end
end

#posreg_like(value) ⇒ Object



210
211
212
213
214
# File 'lib/lore/clause.rb', line 210

def posreg_like(value) 
  value = value.to_s.lore_escape
  @value_string = @field_name + ' ~* ' << Lore.parse_field_value(value)
  Clause.new(@value_string, @left_side+@value_string, '', @plan)
end

#tagObject



293
294
295
# File 'lib/lore/clause.rb', line 293

def tag 
  @field_name.gsub('.','--')
end

#to_sObject

Important to return @field_name here, as Table_Accessor.attribute should return schema.table_name.attribute. See Table_Accessor.load_attribute_fields



289
290
291
# File 'lib/lore/clause.rb', line 289

def to_s
  @field_name.to_s
end

#to_sqlObject



301
302
303
# File 'lib/lore/clause.rb', line 301

def to_sql
  @left_side + @value_string
end

#|(value) ⇒ Object Also known as: or



238
239
240
241
# File 'lib/lore/clause.rb', line 238

def |(value) 
  @value_string = ' OR ' << value.left_side 
  Clause.new(@value_string, '(' << @left_side+@value_string + ')', '', @plan)
end