Sha256: f5630292fbfeeeaeb21c9b099c40919ca61650e09736a3f103332022e4a3a2b3

Contents?: true

Size: 848 Bytes

Versions: 3

Compression:

Stored size: 848 Bytes

Contents

module RailsDb
  class TableData
    include Connection
    include TablePagination

    attr_reader :table, :time
    attr_accessor :current_page, :offset, :per_page, :sort_column, :sort_order

    delegate :each, :to => :data
    delegate :count, :to => :table

    def initialize(table)
      @table = table
    end

    def data
      @data ||= begin
        commands = ["SELECT * FROM #{table.name}"]
        if sort_column
          commands.push("ORDER BY #{sort_column} #{sort_order}")
        end
        if per_page && offset
          commands.push("LIMIT #{per_page} OFFSET #{offset}")
        end
        results, @time = Database.adapter.exec_query(commands.join(' '))
        results
      end
    end

    def count
      Database.adapter.exec_query("SELECT COUNT(*) FROM #{table.name}")[0].rows.flatten.last.to_i
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_db-0.3 lib/rails_db/table_data.rb
rails_db-0.2.1 lib/rails_db/table_data.rb
rails_db-0.2 lib/rails_db/table_data.rb