Class: Graffiti::SquishSelect
- Inherits:
-
SquishQuery
- Object
- SquishQuery
- Graffiti::SquishSelect
- Defined in:
- lib/graffiti/squish.rb
Constant Summary
Constants inherited from SquishQuery
Graffiti::SquishQuery::AGGREGATE, Graffiti::SquishQuery::BN, Graffiti::SquishQuery::BN_SCAN, Graffiti::SquishQuery::INTERNAL, Graffiti::SquishQuery::LITERAL, Graffiti::SquishQuery::LITERAL_SCAN, Graffiti::SquishQuery::NUMBER, Graffiti::SquishQuery::OPERATOR, Graffiti::SquishQuery::PARAMETER, Graffiti::SquishQuery::PARAMETER_AND_LITERAL_SCAN, Graffiti::SquishQuery::QUERY
Instance Attribute Summary
Attributes inherited from SquishQuery
#group, #literal, #nodes, #ns, #order, #order_dir, #pattern, #variables
Instance Method Summary collapse
-
#initialize(config, query) ⇒ SquishSelect
constructor
A new instance of SquishSelect.
-
#to_sql ⇒ Object
translate Squish SELECT query to SQL.
Methods inherited from SquishQuery
#ns_shrink, ns_shrink, #substitute_literals, #to_s, uri_shrink!, #validate_expression
Constructor Details
#initialize(config, query) ⇒ SquishSelect
Returns a new instance of SquishSelect.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/graffiti/squish.rb', line 267 def initialize(config, query) super(config, query) if @key # initialized from a String, not a Hash 'SELECT' == @key or raise ProgrammingError, 'Wrong query type: SELECT expected intead of ' + @key @nodes = @nodes.split(/\s*,\s*/).map {|node| validate_expression(node) } end end |
Instance Method Details
#to_sql ⇒ Object
translate Squish SELECT query to SQL
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/graffiti/squish.rb', line 282 def to_sql where = @sql_mapper.where select = @nodes.dup select.push(@order) unless @order.empty? or @nodes.include?(@order) # now put it all together sql = %{\nFROM #{@sql_mapper.from}} sql << %{\nWHERE #{where}} unless where.empty? sql << %{\nGROUP BY #{@group}} unless @group.empty? sql << %{\nORDER BY #{@order} #{@order_dir}} unless @order.empty? select = select.map do |expr| bind_blank_nodes(expr) + (BN.match(expr) ? (' AS ' + $1) : '') end sql = 'SELECT DISTINCT ' << select.join(', ') << bind_blank_nodes(sql) sql =~ /\?/ and raise ProgrammingError, "Unexpected '?' in translated query (probably, caused by unmapped blank node): #{sql.gsub(/\s+/, ' ')};" substitute_literals(sql) end |