smql/lib/smql_to_ar/query_builder.rb

193 lines
6 KiB
Ruby
Raw Normal View History

2011-09-08 17:03:52 +02:00
# SmqlToAR - Builds AR-querys: Converts SMQL to ActiveRecord
# Copyright (C) 2011 Denis Knauf
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class SmqlToAR
#######################################################################################
# Baut die Queries zusammen.
class QueryBuilder
# Erzeugt einen eindeutigen Identikator "cX", wobei X iteriert wird.
class Vid
attr_reader :vid
def initialize( vid) @vid = vid end
def to_s() ":smql_c#{@vid}" end
def to_sym() "smql_c#{@vid}".to_sym end
2011-09-08 17:03:52 +02:00
alias sym to_sym
def to_i() @vid end
end
attr_reader :table_alias, :model, :table_model, :base_table, :_where, :_select, :_wobs, :_joins, :prefix, :_vid
2011-09-08 17:03:52 +02:00
attr_accessor :logger
def initialize model, prefix = nil, base_table
@prefix = "smql"
2011-09-11 12:39:13 +02:00
@logger = SmqlToAR.logger
2011-09-08 17:03:52 +02:00
@table_alias = Hash.new do |h, k|
k = Array.wrap k
h[k] = "#{@prefix},#{k.join(',')}"
2011-09-08 17:03:52 +02:00
end
@_vid, @_where, @_wobs, @model, @quoter = 0, [], {}, model, model.connection
@base_table = base_table.blank? ? [model.table_name.to_sym] : Array.wrap( base_table)
2011-09-08 17:03:52 +02:00
@table_alias[ @base_table] = @base_table.first
t = quote_table_name @table_alias[ @base_table]
@_select, @_joins, @_joined, @_includes, @_order = ["DISTINCT #{t}.*"], "", [], [], []
@table_model = {@base_table => @model}
end
def vid() Vid.new( @_vid+=1) end
# Jede via where uebergebene Condition wird geodert und alle zusammen werden geundet.
# "Konjunktive Normalform". Allerdings duerfen Conditions auch Komplexe Abfragen enthalten.
# Ex: builder.where( 'a = a', 'b = c').where( 'c = d', 'e = e').where( 'x = y').where( '( m = n AND o = p )', 'f = g')
# #=> WHERE ( a = a OR b = c ) AND ( c = d OR e = e ) AND x = y ( ( m = n AND o = p ) OR f = g )
def where *cond
@_where.push cond
self
end
def wobs vals
@_wobs.update vals
self
end
def quote_column_name name
@quoter.quote_column_name( name).gsub /"\."/, ','
end
def quote_table_name name
@quoter.quote_table_name( name).gsub /"\."/, ','
end
def column table, name
"#{quote_table_name table.kind_of?(String) ? table : @table_alias[table]}.#{quote_column_name name}"
end
def build_join orig, pretable, table, prekey, key
" JOIN #{orig} AS #{quote_table_name table} ON #{column pretable, prekey} = #{column table, key} "
2011-09-08 17:03:52 +02:00
end
def sub_join table, col, model, query
prefix, base_table = "#{@prefix}_sub", col.col
2011-09-27 16:51:18 +02:00
join_ table, model, "(#{query.build( prefix, base_table).ar.to_sql})"
end
def join_ table, model, query, pretable = nil
pretable ||= table[0...-1]
2011-09-08 17:03:52 +02:00
@table_model[ table] = model
premodel = @table_model[ pretable]
t = @table_alias[ table]
pt = quote_table_name @table_alias[ table[ 0...-1]]
refl = premodel.reflections[table.last]
case refl
when ActiveRecord::Reflection::ThroughReflection
through = refl.through_reflection
throughtable = table[0...-1]+[through.name.to_sym]
srctable = throughtable+[refl.source_reflection.name]
@table_model[ srctable] = model
@table_alias[ table] = @table_alias[ srctable]
join_ throughtable, through.klass, quote_table_name( through.table_name)
join_ srctable, refl.klass, query, throughtable
when ActiveRecord::Reflection::AssociationReflection
case refl.macro
when :has_many
@_joins += build_join query, pretable, t, premodel.primary_key, refl.primary_key_name
when :belongs_to
@_joins += build_join query, pretable, t, refl.primary_key_name, premodel.primary_key
when :has_and_belongs_to_many
jointable = [','] + table
@_joins += build_join refl.options[:join_table], pretable, @table_alias[jointable], premodel.primary_key, refl.primary_key_name
@_joins += build_join query, jointable, t, refl.association_foreign_key, refl.association_primary_key
else raise BuilderError, "Unkown reflection macro: #{refl.macro.inspect}"
end
else raise BuilderError, "Unkown reflection type: #{refl.class.name}"
2011-09-08 17:03:52 +02:00
end
self
end
def join table, model
return self if @_joined.include? table # Already joined
join_ table, model, quote_table_name( model.table_name)
@_joined.push table
end
2011-09-08 17:03:52 +02:00
def includes table
@_includes.push table
self
end
def select col
@_select.push quote_column_name( @table_alias[col])
self
end
def order table, col, o
@_order.push "#{column table, col} #{:DESC == o ? :DESC : :ASC}"
end
def limit count
@_limit = count
end
def offset count
@_offset = count
end
2011-09-08 17:03:52 +02:00
class Dummy
def method_missing m, *a, &e
#p :dummy => m, :pars => a, :block => e
self
end
end
def build_ar
where_str = @_where.collect do |w|
w = Array.wrap w
1 == w.length ? w.first : "( #{w.join( ' OR ')} )"
end.join ' AND '
incls = {}
@_includes.each do |inc|
b = incls
inc[1..-1].collect {|rel| b = b[rel] ||= {} }
end
@logger.debug incls: incls, joins: @_joins
@model = @model.
select( @_select.join( ', ')).
joins( @_joins).
where( where_str, @_wobs).
order( @_order.join( ', ')).
includes( incls)
@model = @model.limit @_limit if @_limit
@model = @model.offset @_offset if @_offset
@model
2011-09-08 17:03:52 +02:00
end
def fix_calculate
def @model.calculate operation, column_name, options = nil
options = options.try(:dup) || {}
options[:distinct] = true unless options.except(:distinct).present?
column_name = klass.primary_key unless column_name.present?
super operation, column_name, options
end
self
end
def to_ar
build_ar
fix_calculate
@model
end
end
end