Class: DataMapper::Query
Overview
This class handles option parsing and SQL generation.
Constant Summary collapse
- OPTIONS =
These are the standard finder options
[ :select, :offset, :limit, :include, :reload, :conditions, :join, :order, :after_row_materialization ]
Instance Method Summary collapse
-
#initialize(adapter, klass, options = {}) ⇒ Query
constructor
A new instance of Query.
-
#parameters ⇒ Object
Parameters for query.
-
#to_sql ⇒ Object
SQL for query.
Constructor Details
#initialize(adapter, klass, options = {}) ⇒ Query
Returns a new instance of Query.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/data_mapper/query.rb', line 11 def initialize(adapter, klass, = {}) # Set some of the standard options @adapter, @klass = adapter, klass @from = @adapter.table(@klass) @parameters = [] @joins = [] # Parse simple options @limit = .fetch(:limit, nil) @offset = .fetch(:offset, nil) @reload = .fetch(:reload, false) @order = .fetch(:order, nil) @after_row_materialization = .fetch(:after_row_materialization, nil) # Parse :include option @includes = case = [:include] when Array then .dup when Symbol then [] when NilClass then [] else raise ArgumentError.new(":include must be an Array, Symbol or nil, but was #{include_options.inspect}") end # Include lazy columns if specified in :include option @columns = @from.columns.select do |column| !column.lazy? || @includes.delete(column.name) end # Qualify columns with their table name if joins are present @qualify = !@includes.empty? # Generate SQL for joins @includes.each do |association_name| association = @from.associations[association_name] @joins << association.to_sql @columns += association.associated_table.columns.select do |column| !column.lazy? end end # Prepare conditions for parsing @conditions = [] # Each non-standard option is assumed to be a column .each_pair do |k,v| unless OPTIONS.include?(k) append_condition(k, v) end end # If a :conditions option is present, parse it if conditions_option = [:conditions] if conditions_option.is_a?(String) @conditions << conditions_option else append_condition(*conditions_option) end end # If the table is paranoid, add a filter to the conditions if @from.paranoid? @conditions << "#{@from.paranoid_column.to_sql(qualify?)} IS NULL OR #{@from.paranoid_column.to_sql(qualify?)} > #{@adapter.class::SYNTAX[:now]}" end end |
Instance Method Details
#parameters ⇒ Object
Parameters for query
86 87 88 |
# File 'lib/data_mapper/query.rb', line 86 def parameters @parameters end |
#to_sql ⇒ Object
SQL for query
77 78 79 80 81 82 83 |
# File 'lib/data_mapper/query.rb', line 77 def to_sql sql = "SELECT #{columns.map { |column| column.to_sql(qualify?) }.join(', ')} FROM #{from.to_sql}" sql << " " << joins.join($/) unless joins.empty? sql << " WHERE (#{conditions.join(") AND (")})" unless conditions.empty? return sql end |