group and having clause init
This commit is contained in:
parent
cc49fdefe9
commit
11466211ac
|
@ -101,6 +101,12 @@ class SmqlToAR
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class UnknownHavingMethod < SMQLError
|
||||||
|
def initialize expected, got
|
||||||
|
super :expected => expected, :got => got
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class BuilderError < Exception; end
|
class BuilderError < Exception; end
|
||||||
|
|
||||||
#############################################################################
|
#############################################################################
|
||||||
|
|
|
@ -382,6 +382,63 @@ class SmqlToAR
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class GroupBy < Function
|
||||||
|
Name = :group
|
||||||
|
Excepted = [String, Array]
|
||||||
|
|
||||||
|
def initialize model, func, args
|
||||||
|
super model, func, args.collect( &Columnt.method( :new))
|
||||||
|
end
|
||||||
|
|
||||||
|
def build builder, table
|
||||||
|
return if @args.blank?
|
||||||
|
@args.each do |col|
|
||||||
|
t = Column.new *(table + col.to_a)
|
||||||
|
raise_unless 1 == t.length, RootOnlyFunctionError.new( t)
|
||||||
|
builder.group t
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Having < Function
|
||||||
|
Name = :having
|
||||||
|
Excepted = [Array]
|
||||||
|
|
||||||
|
def initialize model, func, args
|
||||||
|
args = Hash[ *args.collect do |col, meth|
|
||||||
|
col = Column.new col
|
||||||
|
case meth.to_sym
|
||||||
|
when :max, :min
|
||||||
|
else raise UnknownHavingMethod.new( [:max, :min], meth)
|
||||||
|
end
|
||||||
|
[col, meth]
|
||||||
|
end]
|
||||||
|
super model, func, args
|
||||||
|
end
|
||||||
|
|
||||||
|
def build builder, table
|
||||||
|
return if @args.blank?
|
||||||
|
@args.each do |col, meth|
|
||||||
|
t = Column.new *(table + col.to_a)
|
||||||
|
raise_unless 1 == t.length, RootOnlyFunctionError.new( t)
|
||||||
|
builder.having meth => t
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Max < Having
|
||||||
|
Name = :max
|
||||||
|
Expected = [String]
|
||||||
|
|
||||||
|
def initialize model, func, args
|
||||||
|
super model, :having, Hash[ *args.collect {|col| [col, func] } ]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Min < Max
|
||||||
|
Name = :min
|
||||||
|
end
|
||||||
|
|
||||||
class Limit < Function
|
class Limit < Function
|
||||||
Name = :limit
|
Name = :limit
|
||||||
Expected = [Fixnum]
|
Expected = [Fixnum]
|
||||||
|
|
|
@ -43,6 +43,7 @@ class SmqlToAR
|
||||||
@table_alias[ @base_table] = @base_table.first
|
@table_alias[ @base_table] = @base_table.first
|
||||||
t = quote_table_name @table_alias[ @base_table]
|
t = quote_table_name @table_alias[ @base_table]
|
||||||
@_select, @_joins, @_joined, @_includes, @_order = ["DISTINCT #{t}.*"], "", [@base_table], [], []
|
@_select, @_joins, @_joined, @_includes, @_order = ["DISTINCT #{t}.*"], "", [@base_table], [], []
|
||||||
|
@_group, @_having = {}, {}
|
||||||
@table_model = {@base_table => @model}
|
@table_model = {@base_table => @model}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -66,6 +67,14 @@ class SmqlToAR
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def group col
|
||||||
|
@_group.push col
|
||||||
|
end
|
||||||
|
|
||||||
|
def having h
|
||||||
|
@_having.update h
|
||||||
|
end
|
||||||
|
|
||||||
def quote_column_name name
|
def quote_column_name name
|
||||||
@quoter.quote_column_name( name).gsub /"\."/, ','
|
@quoter.quote_column_name( name).gsub /"\."/, ','
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue