Class: Zena::Use::QueryNode::Compiler
- Inherits:
-
QueryBuilder::Processor
- Object
- QueryBuilder::Processor
- Zena::Use::QueryNode::Compiler
- Defined in:
- lib/zena/use/query_node.rb
Constant Summary collapse
- CORE_CONTEXTS =
%w{parent project section}
Class Attribute Summary collapse
-
.filter_fields ⇒ Object
Returns the value of attribute filter_fields.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
?.
Class Method Summary collapse
Instance Method Summary collapse
- #get_scope_index_field(field_name) ⇒ Object
-
#need_join_scope?(scope_name) ⇒ Boolean
Special case ‘in site’ that is a noop scope and just avoids the insertion of the default ‘in parent’ scope.
-
#parse_custom_query_argument(key, value) ⇒ Object
******** And maybe overwrite these **********.
- #process_attr(attribute) ⇒ Object
-
#process_equal(left, right) ⇒ Object
Handle special case for ‘class = ’ and ‘role = ’ and ‘foo.date =’.
-
#process_field(field_name) ⇒ Object
Overwrite this and take care to check for valid fields.
- #process_function(arg, method, *args) ⇒ Object
- #process_idx_field(scope_field) ⇒ Object
-
#process_like(left, right) ⇒ Object
Handle special case for ‘class like ’.
-
#process_param(pagination_key) ⇒ Object
Process pagination parameter.
-
#resolve_main_class(class_name) ⇒ Object
Resolve ‘main_class’ from a class name.
-
#resolve_missing_table(query, table_name, table_alias) ⇒ Object
This is used to avoid finding random indexed objects or links in clauses with and without link filters like this: “image or icon” (‘image’ is a filter in ‘parent’ scope, ‘icon’ is a relation found through links).
- #resolve_scope_idx_fields(arg1, arg2) ⇒ Object
-
#scope_fields(scope) ⇒ Object
Scope current context with previous context.
Class Attribute Details
.filter_fields ⇒ Object
Returns the value of attribute filter_fields.
128 129 130 |
# File 'lib/zena/use/query_node.rb', line 128 def filter_fields @filter_fields end |
Instance Attribute Details
#context ⇒ Object (readonly)
?
109 110 111 |
# File 'lib/zena/use/query_node.rb', line 109 def context @context end |
Class Method Details
.add_filter_field(key, fld_def) ⇒ Object
130 131 132 |
# File 'lib/zena/use/query_node.rb', line 130 def add_filter_field(key, fld_def) self.filter_fields[key] = fld_def end |
Instance Method Details
#get_scope_index_field(field_name) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/zena/use/query_node.rb', line 178 def get_scope_index_field(field_name) return nil if @query.main_class.real_class.column_names.include?(field_name) # scope index klass = @query.main_class if index_model = klass.kind_of?(VirtualClass) ? klass.idx_class : nil index_model = Zena.resolve_const(index_model) rescue NilClass if index_model < Zena::Use::ScopeIndex::IndexMethods && index_model.column_names.include?(field_name) table_to_use = add_key_value_table('scope_index', index_model.table_name) do |tbl_name| # This block is only executed once add_filter "#{table('nodes')}.id = #{tbl_name}.node_id" end "#{table_to_use}.#{field_name}" else # invalid field_name: ignore nil end else # no index model: ignore nil end end |
#need_join_scope?(scope_name) ⇒ Boolean
Special case ‘in site’ that is a noop scope and just avoids the insertion of the default ‘in parent’ scope.
415 416 417 |
# File 'lib/zena/use/query_node.rb', line 415 def need_join_scope?(scope_name) scope_name != 'site' end |
#parse_custom_query_argument(key, value) ⇒ Object
******** And maybe overwrite these **********
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/zena/use/query_node.rb', line 379 def parse_custom_query_argument(key, value) return nil unless value super.gsub(/(RELATION_ID|NODE_ATTR|SECURE_TABLE)\(([^)]+)\)|(REF_DATE|NODE_ID|VISITOR_LANG)/) do type, value = $1, $2 type ||= $3 case type when 'RELATION_ID' role = value if rel = RelationProxy.find_by_role(role.singularize) rel[:id] else raise ::QueryBuilder::Error.new("Custom query: could not find Relation '#{role}'") end when 'SECURE_TABLE' table_name = value add_filter "\#{secure_scope('#{table_name}')}" when 'NODE_ATTR' attribute = value if Node.safe_method_type([attribute]) insert_bind("#{node_name}.#{attribute}") else # not found: consider it's a property insert_bind("#{node_name}.prop[#{attribute.inspect}]") end when 'REF_DATE' context[:ref_date] ? insert_bind(context[:ref_date]) : 'now()' when 'NODE_ID' insert_bind("#{node_name}.id") when 'VISITOR_LANG' insert_bind("visitor.lang") end end end |
#process_attr(attribute) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/zena/use/query_node.rb', line 164 def process_attr(attribute) case attribute when 'project_id', 'section_id', 'discussion_id' # Special accessor insert_bind "#{node_name}.get_#{attribute}" when 'id', 'parent_id' # Not RubyLess safe insert_bind "#{node_name}.#{attribute}" else # Use RubyLess super end end |
#process_equal(left, right) ⇒ Object
Handle special case for ‘class = ’ and ‘role = ’ and ‘foo.date =’
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/zena/use/query_node.rb', line 297 def process_equal(left, right) if (left == [:field, 'class'] || left == [:field, 'klass']) && (right[0] == :field || right[0] == :string) if klass = Node.get_class(right.last) "#{field_or_attr('kpath')} = #{quote(klass.kpath)}" else raise ::QueryBuilder::Error.new("Unknown class #{right.last.inspect}.") end elsif left == [:field, 'role'] && (right[0] == :field || right[0] == :string) if role = Node.get_role(right[1]) # FIXME: how to only add table once if the other clause in not an OR ? add_table('nodes_roles') "(#{table('nodes_roles')}.node_id = #{table('nodes')}.id AND #{table('nodes_roles')}.role_id = #{role.id})" end elsif left.first == :function && left.last.last == 'date' # transform "foo.date = baz" # [:function, [:field, "foo"], [:method, "date"]] # [:field, baz] # ==> into # "baz >= foo and foo < baz + 1 day" a = left[1] b = right process([:and, [:<=, b, a], [:<, a, [:+, b, [:interval, [:integer, '1'], 'day']]]]) else super end end |
#process_field(field_name) ⇒ Object
Overwrite this and take care to check for valid fields.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/zena/use/query_node.rb', line 205 def process_field(field_name) if fld = @query.attributes_alias[field_name] # use custom query alias value defined in select clause: 'custom_a AS validation' return processing_filter? ? "(#{fld})" : fld elsif processing_filter? && map_def = self.class.filter_fields[field_name] # Special filter fields such as 'role', 'tag' or 'class' if map_def.kind_of?(String) return map_def elsif table_def = map_def[:table] use_name, source, target, filter = table_def table_to_use = add_key_value_table(use_name, target, map_def[:key]) do |tbl_name| # This block is only executed once add_filter filter.gsub( 'TABLE1', table(source) ).gsub( 'TABLE2', tbl_name ) end else table_to_use = table end "#{table_to_use}.#{map_def[:key]}" elsif %w{id parent_id project_id section_id user_id}.include?(field_name) || (Node.safe_method_type([field_name]) && Node.column_names.include?(field_name)) "#{table}.#{field_name}" elsif @query.tables.include?('links') && (key = field_name[/^l_(.+)$/,1]) && (key == 'id' || Zena::Use::Relations::LINK_ATTRIBUTES.include?(key.to_sym)) "#{table('links')}.#{key}" elsif field_name == 'random' Zena::Db.sql_function(field_name, nil) else if processing_filter? && field_name =~ /^(.*)_ids?$/ # tag_id = 33 ===> join links as lk, nodes as tt ....... rel = $1 # Fake field_or_attr so it does not use 'zip' on nodes context[:processing] = :relation if join_relation($1, 'jnode') res = "#{table('jnode')}.zip" end context[:processing] = :filter return res end # property or real column # FIXME !!!! Why does this happen ? return nil if @query.main_class.columns.kind_of?(Array) column = @query.main_class.columns[field_name] if column && column.indexed? if column.index == true group_name = column.type elsif column.index =~ Property::Index::FIELD_INDEX_REGEXP # field in nodes return "#{table}.#{$1}" else group_name = column.index end index_table = @query.main_class.real_class.index_table_name(group_name) # We use the add_key_value_table rule to avoid inserting the # same index access twice. tbl = add_key_value_table(group_name, index_table, field_name) do |tbl_name| # This block is only executed once add_filter "#{tbl_name}.node_id = #{table}.id" add_filter "#{tbl_name}.key = #{quote(field_name)}" if group_name.to_s =~ /^ml_/ add_filter "#{tbl_name}.lang = #{quote(visitor.lang)}" end # no need for distinct, the new table makes a 1-1 relation end "#{tbl}.value" else super # raises an error end end end |
#process_function(arg, method, *args) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/zena/use/query_node.rb', line 366 def process_function(arg, method, *args) # Resolve scope index fields arg, method = resolve_scope_idx_fields(arg, method) if method arg, method = process(arg), process(method) args = [arg] + args.map{|a| process(a)} Zena::Db.sql_function(method, *args) else process(arg) end end |
#process_idx_field(scope_field) ⇒ Object
200 201 202 |
# File 'lib/zena/use/query_node.rb', line 200 def process_idx_field(scope_field) scope_field end |
#process_like(left, right) ⇒ Object
Handle special case for ‘class like ’
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/zena/use/query_node.rb', line 326 def process_like(left, right) if left == [:field, 'class'] && right[0] == :field if klass = Node.get_class(right[1]) "#{field_or_attr('kpath')} LIKE #{quote(klass.kpath + '%')}" else raise QueryBuilder::QueryException.new("Unknown class #{right.last.inspect}.") end else process_op(:like, left, right) end end |
#process_param(pagination_key) ⇒ Object
Process pagination parameter
292 293 294 |
# File 'lib/zena/use/query_node.rb', line 292 def process_param(pagination_key) "params[#{pagination_key.to_sym.inspect}]" end |
#resolve_main_class(class_name) ⇒ Object
Resolve ‘main_class’ from a class name.
123 124 125 |
# File 'lib/zena/use/query_node.rb', line 123 def resolve_main_class(class_name) VirtualClass[class_name] end |
#resolve_missing_table(query, table_name, table_alias) ⇒ Object
This is used to avoid finding random indexed objects or links in clauses with and without link filters like this: “image or icon” (‘image’ is a filter in ‘parent’ scope, ‘icon’ is a relation found through links).
422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/zena/use/query_node.rb', line 422 def resolve_missing_table(query, table_name, table_alias) if table_name =~ /^idx_nodes/ # index tables query.where.insert 0, "#{table_alias}.node_id = 0" elsif table_name == 'links' || table_name =~ /^idx_/ # index tables query.where.insert 0, "#{table_alias}.id = 0" else # Raise an error super end end |
#resolve_scope_idx_fields(arg1, arg2) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/zena/use/query_node.rb', line 338 def resolve_scope_idx_fields(arg1, arg2) if arg1.first == :function # contact.log_at.year # arg1 = [:function, [:field, "tag"], [:method, "created_at"]] # arg2 = [:method, "year"] class_name = arg1[1][1] field_name = arg1[2][1] function = arg2 elsif arg1[0] == :field && arg2[0] == :method # contact.log_at or log_at.year # arg1 = [:field, "contact"] class_name = arg1[1] # arg2 = [:method, "name"] field_name = arg2[1] function = nil else return [arg1, arg2] end scope_idx_field = "#{class_name}_#{field_name}" if fld = get_scope_index_field(scope_idx_field) return [[:idx_field, fld], function] else # not a scope index field return [arg1, arg2] end end |
#scope_fields(scope) ⇒ Object
Scope current context with previous context. For example:
current previous
['parent_id', 'id'] ==> no1.parent_id = nodes.id
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zena/use/query_node.rb', line 148 def scope_fields(scope) case scope when 'self' ['parent_id', 'id'] when *CORE_CONTEXTS last? ? %W{#{scope}_id #{scope}_id} : %W{#{scope}_id id} when 'site', main_table # not an error, but do not scope [] else #if CORE_CONTEXTS.include?(scope) # error nil end end |