Class: FrontbaseAdapter::SQLQuery
- Inherits:
-
Object
- Object
- FrontbaseAdapter::SQLQuery
- Includes:
- DataMapper::Query::Conditions
- Defined in:
- lib/dm-frontbase-adapter/sql_query.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #columns_statement(properties) ⇒ Object
- #comparison_statement(comparison) ⇒ Object
- #conditions_statement(conditions) ⇒ Object
- #include_operator(value) ⇒ Object
-
#initialize(query, type) ⇒ SQLQuery
constructor
A new instance of SQLQuery.
- #operation_statement(operation) ⇒ Object
- #order_statement(orders) ⇒ Object
- #property_to_column_name(prop) ⇒ Object
- #quote_name(name) ⇒ Object
- #quote_value(value, property) ⇒ Object
- #setup_statement ⇒ Object
- #storage_name(rel, repository) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(query, type) ⇒ SQLQuery
Returns a new instance of SQLQuery.
8 9 10 11 12 13 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 8 def initialize(query, type) @type = type @query = query setup_statement end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 6 def columns @columns end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
6 7 8 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 6 def conditions @conditions end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
6 7 8 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 6 def from @from end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
6 7 8 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 6 def order @order end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 6 def type @type end |
Instance Method Details
#columns_statement(properties) ⇒ Object
32 33 34 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 32 def columns_statement properties properties.map {|property| property_to_column_name(property) }.join(', ') end |
#comparison_statement(comparison) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 76 def comparison_statement(comparison) return conditions_statement(comparison.foreign_key_mapping) if comparison.relationship? value = comparison.value subject = property_to_column_name comparison.subject operator = case comparison when EqualToComparison then '=' when GreaterThanComparison then '>' when LessThanComparison then '<' when GreaterThanOrEqualToComparison then '>=' when LessThanOrEqualToComparison then '<=' when LikeComparison then 'LIKE' when InclusionComparison then include_operator(value) end "#{subject} #{operator} #{quote_value(value, comparison.subject)}" end |
#conditions_statement(conditions) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 61 def conditions_statement(conditions) case conditions when AbstractOperation then operation_statement(conditions) when AbstractComparison then comparison_statement(conditions) end end |
#include_operator(value) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 96 def include_operator(value) case value when Array then 'IN' when Range then 'BETWEEN' end end |
#operation_statement(operation) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 68 def operation_statement(operation) case operation when NotOperation then "NOT(#{conditions_statement(operation.first)})" when AndOperation then "(#{operation.map {|op| conditions_statement(op) }.join(' AND ')})" when OrOperation then "(#{operation.map {|op| conditions_statement(op) }.join(' OR ')})" end end |
#order_statement(orders) ⇒ Object
28 29 30 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 28 def order_statement orders orders.map {|o| "#{o.target.field} #{o.operator.to_s.upcase}" }.join(', ') end |
#property_to_column_name(prop) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 103 def property_to_column_name(prop) res = case prop when DataMapper::Property quote_name(prop.field) when DataMapper::Query::Path rels = prop.relationships names = rels.map {|r| storage_name(r, @query.repository) }.join(".") "#{names}.#{quote_name(prop.field)}" end res end |
#quote_name(name) ⇒ Object
116 117 118 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 116 def quote_name(name) "\"#{name.gsub('"', '""')}\"" end |
#quote_value(value, property) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 125 def quote_value(value, property) if property.kind_of? DataMapper::Property::Boolean return value == DataMapper::Property::Boolean::TRUE ? 'TRUE' : 'FALSE' end case when value.kind_of?(Array) "(#{value.map {|v| quote_value(v, property)}.join(", ")})" when value.kind_of?(NilClass) "NULL" when value.kind_of?(String) "'#{value.to_s.gsub(/'/, "\\'").gsub(/\\/, %{\\\\})}'" else value.to_s end end |
#setup_statement ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 15 def setup_statement @conditions = (@query.conditions) ? conditions_statement(@query.conditions) : '' @order = (@query.order && !@query.order.empty?) ? order_statement(@query.order) : '' @columns = columns_statement @query.fields @from = quote_name(@query.model.storage_name(@query.repository.name)) end |
#storage_name(rel, repository) ⇒ Object
120 121 122 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 120 def storage_name(rel, repository) rel.parent_model.storage_name(repository.name) end |
#to_s ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dm-frontbase-adapter/sql_query.rb', line 36 def to_s statement = '' if @type == :select statement << "SELECT #{columns}" statement << " FROM #{from}" statement << " WHERE #{conditions}" unless conditions.to_s.empty? statement << " ORDER BY #{order}" unless order.empty? statement << ";" if @query.limit || (@query.limit && @query.offset > 0) replacement = "SELECT TOP(" replacement << "#{@query.offset.to_i}," if @query.limit && @query.offset > 0 replacement << "#{@query.limit.to_i}" if @query.limit replacement << ")" statement.gsub!('SELECT', replacement) end end statement end |