Class: SmqlToAR::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/smql_to_ar/query_builder.rb

Overview

Baut die Queries zusammen.

Defined Under Namespace

Classes: Dummy, Vid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/smql_to_ar/query_builder.rb', line 34

def initialize model
	@logger = SmqlToAR.logger
	@table_alias = Hash.new do |h, k|
		k = Array.wrap k
		h[k] = "smql,#{k.join(',')}"
	end
	@_vid, @_where, @_wobs, @model, @quoter = 0, [], {}, model, model.connection
	@base_table = [model.table_name.to_sym]
	@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

Instance Attribute Details

#_joinsObject (readonly)

Returns the value of attribute _joins.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def _joins
  @_joins
end

#_selectObject (readonly)

Returns the value of attribute _select.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def _select
  @_select
end

#_whereObject (readonly)

Returns the value of attribute _where.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def _where
  @_where
end

#_wobsObject (readonly)

Returns the value of attribute _wobs.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def _wobs
  @_wobs
end

#base_tableObject (readonly)

Returns the value of attribute base_table.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def base_table
  @base_table
end

#loggerObject

Returns the value of attribute logger.



32
33
34
# File 'lib/smql_to_ar/query_builder.rb', line 32

def logger
  @logger
end

#modelObject (readonly)

Returns the value of attribute model.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def model
  @model
end

#table_aliasObject (readonly)

Returns the value of attribute table_alias.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def table_alias
  @table_alias
end

#table_modelObject (readonly)

Returns the value of attribute table_model.



31
32
33
# File 'lib/smql_to_ar/query_builder.rb', line 31

def table_model
  @table_model
end

Class Method Details

.calculate(operation, column_name, options = nil) ⇒ Object



144
145
146
147
148
149
# File 'lib/smql_to_ar/query_builder.rb', line 144

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

Instance Method Details

#build_arObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/smql_to_ar/query_builder.rb', line 124

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)
end

#build_join(orig, pretable, table, prekey, key) ⇒ Object



76
77
78
# File 'lib/smql_to_ar/query_builder.rb', line 76

def build_join orig, pretable, table, prekey, key
	" JOIN #{quote_table_name orig.to_sym} AS #{quote_table_name table} ON #{column pretable, prekey} = #{column table, key} "
end

#column(table, name) ⇒ Object



72
73
74
# File 'lib/smql_to_ar/query_builder.rb', line 72

def column table, name
	"#{quote_table_name table.kind_of?(String) ? table : @table_alias[table]}.#{quote_column_name name}"
end

#fix_calculateObject



143
144
145
146
147
148
149
150
151
# File 'lib/smql_to_ar/query_builder.rb', line 143

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

#includes(table) ⇒ Object



103
104
105
106
# File 'lib/smql_to_ar/query_builder.rb', line 103

def includes table
	@_includes.push table
	self
end

#join(table, model) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/smql_to_ar/query_builder.rb', line 80

def join table, model
	return self  if @_joined.include? table # Already joined
	pretable = table[0...-1]
	@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.macro
	when :has_many
		@_joins += build_join model.table_name, pretable, t, premodel.primary_key, refl.primary_key_name
	when :belongs_to
		@_joins += build_join model.table_name, 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 model.table_name, jointable, t, refl.association_foreign_key, refl.association_primary_key
	else raise BuilderError, "Unkown reflection macro: #{refl.macro.inspect}"
	end
	@_joined.push table
	self
end

#order(table, col, o) ⇒ Object



113
114
115
# File 'lib/smql_to_ar/query_builder.rb', line 113

def order table, col, o
	@_order.push "#{column table, col} #{:DESC == o ? :DESC : :ASC}"
end

#quote_column_name(name) ⇒ Object



64
65
66
# File 'lib/smql_to_ar/query_builder.rb', line 64

def quote_column_name name
	@quoter.quote_column_name( name).gsub /"\."/, ','
end

#quote_table_name(name) ⇒ Object



68
69
70
# File 'lib/smql_to_ar/query_builder.rb', line 68

def quote_table_name name
	@quoter.quote_table_name( name).gsub /"\."/, ','
end

#select(col) ⇒ Object



108
109
110
111
# File 'lib/smql_to_ar/query_builder.rb', line 108

def select col
	@_select.push quote_column_name( @table_alias[col])
	self
end

#to_arObject



153
154
155
156
157
# File 'lib/smql_to_ar/query_builder.rb', line 153

def to_ar
	build_ar
	fix_calculate
	@model
end

#vidObject



48
# File 'lib/smql_to_ar/query_builder.rb', line 48

def vid()  Vid.new( @_vid+=1)  end

#where(*cond) ⇒ Object

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 )


54
55
56
57
# File 'lib/smql_to_ar/query_builder.rb', line 54

def where *cond
	@_where.push cond
	self
end

#wobs(vals) ⇒ Object



59
60
61
62
# File 'lib/smql_to_ar/query_builder.rb', line 59

def wobs vals
	@_wobs.update vals
	self
end