ruby SQLManager


 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,init_params={})
                if(record.class == String)
                   @record = eval(record.classify + ".new")
                else
		   @record = record
                end
		@table_name = @record.class.table_name
                self.init_params init_params.class
		self.merge_param(init_params)
		@columns = []
		@orders = []
		@groups = []
		@conditions = []
		@joins = []
		@havings = []
		@firsts = []
		@lasts = []
	end

        def init_params c
            @params = c.new
        end
	
	def self.clone(sm)
		c_sm = sm.clone
		new_sm = SQLManager.new(c_sm.record,c_sm.params)
		new_sm.columns.concat Marshal.load(Marshal.dump(sm.columns))
		new_sm.orders.concat Marshal.load(Marshal.dump(sm.orders))
		new_sm.groups.concat Marshal.load(Marshal.dump(sm.groups))
		new_sm.conditions.concat Marshal.load(Marshal.dump(sm.conditions))
		new_sm.joins.concat Marshal.load(Marshal.dump(sm.joins))
		new_sm.havings.concat Marshal.load(Marshal.dump(sm.havings))
		new_sm.firsts.concat Marshal.load(Marshal.dump(sm.firsts))
		new_sm.lasts.concat 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)
		a = *unions
		u_sql = a[0]
		p u_sql.to_query
		a.delete_at 0
		a.each do |u|
			 u_sql.lasts << " UNION " << u.to_query
			 u_sql.merge_param(u.params)
		end
		u_sql
	end
	
	def self.union_all(*unions)
		a = *unions
		u_sql = a[0]
		p u_sql.to_query
		a.delete_at 0
		a.each do |u|
			 u_sql.lasts << " UNION ALL " << u.to_query
			 u_sql.merge_param(u.params)
		end
		u_sql
	end
	
	def record
	    @record
	end
	
	def params
	    @params
	end
	
	def merge_param(param)
            c = param.class
            if c == Hash
	        @params = @params.merge(param)	
            elsif c == Array
                @params.concat param
            elsif c == String || c == Fixnum || c == TrueClass || c == Float || c == Time
                @params <<  param
            else
                param.attributes.collect{ |att| 
                    @params =  @params.merge({att[0].to_sym,att[1]})
                }
            end
	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 << " FROM "
		sql << @table_name 	
		sql << " " << join if !join.empty?
		sql << " WHERE " << condition if !condition.empty?
		sql << " GROUP BY " << group if !group.empty?
		sql << " HAVING " << having if !having.empty?
		sql << " ORDER BY " << order if !order.empty?
		
		if @offset.nil?
			sql << " LIMIT " << @limit.to_s if [email protected]?
		else
			sql << " LIMIT #{@offset.to_s},#{@limit.to_s}" if [email protected]?
		end
		
		(sql << " " << last) if !last.empty?
		
		(sql.insert 0,(first << " ")) if !first.empty?
		 sql
	end

        def to_total
               total_sql = SQLManager.clone(self)
               total_sql.limit = nil
               total_sql.offset = nil 
               if [email protected]?
                  total_sql.firsts << "SELECT COUNT(*) AS total FROM ("
                  total_sql.lasts << ") temp_table"
               else
                  total_sql.columns.clear
                  total_sql.columns << "COUNT(#{total_sql.table_name}.id) AS total"
               end
               total_sql
        end
	
	def query
            s = [to_query]
            if(@params.class == Hash)
               s << @params
            else
               s.concat @params
            end
	    @record.class.find_by_sql s
	end
  
        def total
            self.to_total.query.first['total']
        end

        def paging(page=0,limit=20)
            self.limit = limit
            self.offset = limit * page 
            data = self.query
            total = self.total
            page_total = (total.to_f/limit).ceil
            params = self.params
              
            {:data=>data,:total=>total,:page=>page,:page_total=>page_total,:limit=>self.limit,:offset=>self.offset}
        end
	
  end

你可能感兴趣的:(sql,C++,c,C#,Ruby)