Class: SmqlToAR

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Benchmarkable
Defined in:
lib/smql_to_ar.rb,
lib/smql_to_ar/query_builder.rb,
lib/smql_to_ar/condition_types.rb

Overview

SmqlToAR - Parser: Converts SMQL to ActiveRecord Copyright © 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 <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: ConditionTypes Classes: BuilderError, Column, NonExistingColumnError, NonExistingRelationError, NonExistingSelectableError, OnlyOrderOnBaseError, ParseError, ProtectedColumnError, QueryBuilder, Railtie, SMQLError, SubSMQLError, UnexpectedColOpError, UnexpectedError

Constant Summary collapse

@@logger =
Logger.new $stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, query, order = nil) ⇒ SmqlToAR

Returns a new instance of SmqlToAR.



178
179
180
181
182
# File 'lib/smql_to_ar.rb', line 178

def initialize model, query, order = nil
	query = JSON.parse query  if query.kind_of? String
	@model, @query, @logger, @order = model, query, @@logger, order
	#p model: @model, query: @query
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



158
159
160
# File 'lib/smql_to_ar.rb', line 158

def builder
  @builder
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



158
159
160
# File 'lib/smql_to_ar.rb', line 158

def conditions
  @conditions
end

#loggerObject

Returns the value of attribute logger.



159
160
161
# File 'lib/smql_to_ar.rb', line 159

def logger
  @logger
end

#modelObject (readonly)

Returns the value of attribute model.



158
159
160
# File 'lib/smql_to_ar.rb', line 158

def model
  @model
end

#orderObject (readonly)

Returns the value of attribute order.



158
159
160
# File 'lib/smql_to_ar.rb', line 158

def order
  @order
end

#queryObject (readonly)

Returns the value of attribute query.



158
159
160
# File 'lib/smql_to_ar.rb', line 158

def query
  @query
end

Class Method Details

.loggerObject



175
# File 'lib/smql_to_ar.rb', line 175

def logger()  @@logger  end

.logger=(logger) ⇒ Object



174
# File 'lib/smql_to_ar.rb', line 174

def logger=(logger)  @@logger = logger  end

.model_of(model, rel) ⇒ Object

Model der Relation ‘rel` von `model`



91
92
93
# File 'lib/smql_to_ar.rb', line 91

def self.model_of model, rel
	model.reflections[ rel.to_sym].andand.klass
end

.to_ar(*params) ⇒ Object



216
217
218
# File 'lib/smql_to_ar.rb', line 216

def self.to_ar *params
	new( *params).to_ar
end

Instance Method Details

#arObject



202
203
204
205
206
# File 'lib/smql_to_ar.rb', line 202

def ar
	@ar ||= benchmark 'SMQL ar' do
		@builder.to_ar
	end
end

#buildObject



192
193
194
195
196
197
198
199
200
# File 'lib/smql_to_ar.rb', line 192

def build
	benchmark 'SMQL build query' do
		@builder = QueryBuilder.new @model
		table = @builder.base_table
		@conditions.each {|condition| condition.build builder, table }
	end
	#p builder: @builder
	self
end

#parseObject



184
185
186
187
188
189
190
# File 'lib/smql_to_ar.rb', line 184

def parse
	benchmark 'SMQL parse' do
		@conditions = ConditionTypes.try_parse @model, @query
	end
	#p conditions: @conditions
	self
end

#to_arObject



208
209
210
211
212
213
214
# File 'lib/smql_to_ar.rb', line 208

def to_ar
	benchmark 'SMQL' do
		parse
		build
		ar.tap {|ar| logger.info ar.to_sql }
	end
end