class SQLManager
@params = nil
@record = nil
@columns = nil
@orders = nil
@groups = nil
@conditions = nil
@joins = nil
@havings = nil
@firsts = nil
@lasts = nil
@limit = nil
@offset = nil
@table_name = nil
def initialize(record)
@record = record
@table_name = record.class.table_name
@params = {}
@params.merge(record.attributes)
@columns = []
@orders = []
@groups = []
@conditions = []
@joins = []
@havings = []
@firsts = []
@lasts = []
end
def self.clone(sm)
c_sm = sm.clone
new_sm = SQLManager.new(c_sm.record)
new_sm.columns + Marshal.load(Marshal.dump(sm.columns))
new_sm.orders + Marshal.load(Marshal.dump(sm.orders))
new_sm.groups + Marshal.load(Marshal.dump(sm.groups))
new_sm.conditions + Marshal.load(Marshal.dump(sm.conditions))
new_sm.joins + Marshal.load(Marshal.dump(sm.joins))
new_sm.havings + Marshal.load(Marshal.dump(sm.havings))
new_sm.firsts + Marshal.load(Marshal.dump(sm.firsts))
new_sm.lasts + Marshal.load(Marshal.dump(sm.lasts))
new_sm.limit = c_sm.limit
new_sm.offset = c_sm.offset
new_sm
end
def self.union(*unions)
u_sql = unions[0].to_query
a = *unions
a.each do |u|
u_sql << " UNION " << u.to_query
end
u_sql
end
def self.union_all(*unions)
u_sql = unions[0].to_query
a = *unions
a.each do |u|
u_sql << " UNION ALL " << u.to_query
end
u_sql
end
def record
@record
end
def params
@params
end
def merge_param(param)
@params = @params.merge(param)
end
def columns
@columns
end
def orders
@orders
end
def groups
@groups
end
def conditions
@conditions
end
def joins
@joins
end
def havings
@havings
end
def firsts
@firsts
end
def lasts
@lasts
end
def limit
@limit
end
def limit=(att)
@limit = att
end
def offset
@offset
end
def offset=(att)
@offset = att
end
def table_name
@table_name
end
def to_columns
str = ""
@columns.each do |item|
str << item + ','
end
str = str.chop
(str << @table_name << ".*") if columns.size == 0
str
end
def to_orders
str = ""
@orders.each do |item|
str << item + ','
end
str.chop
end
def to_groups
str = ""
@groups.each do |item|
str << item + ','
end
str.chop
end
def to_conditions
str = ""
@conditions.each do |item|
str << item + ' '
end
str.chop
end
def to_havings
str = ""
@havings.each do |item|
str << item + ' '
end
str.chop
end
def to_firsts
str = ""
@firsts.each do |item|
str << item + ' '
end
str.chop
end
def to_lasts
str = ""
@lasts.each do |item|
str << item + ' '
end
str.chop
end
def to_joins
str = ""
@joins.each do |item|
str << item + ' '
end
str.chop
end
def to_query
first = to_firsts
condition = to_conditions
join = to_joins
column = to_columns
group = to_groups
having = to_havings
order = to_orders
last = to_lasts
sql = "SELECT "
sql << column if !column.empty?
sql << " FORM "
sql << @table_name
sql << " " << join if !join.empty?
sql << " WHERE " << condition if !condition.empty?
sql << group if !group.empty?
sql << having if !having.empty?
sql << order if !order.empty?
sql << " LIMIT " << @limit.to_s if !@limit.nil?
if !@limit.nil?
sql << "," << @offset.to_s if !@offset.nil?
else
sql << "0," << @offset.to_s if !@offset.nil?
end
(sql << " " << last) if !last.empty?
(sql.insert 0,(first << " ")) if !first.empty?
sql
end
end
ruby sql_manager
最新推荐文章于 2022-02-25 20:00:37 发布