Class: RailsDbBrowser::ConnectionKeeper::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_browser/connection_keeper.rb

Overview

performs common operations on connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



38
39
40
# File 'lib/rails_db_browser/connection_keeper.rb', line 38

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



31
32
33
# File 'lib/rails_db_browser/connection_keeper.rb', line 31

def connection
  @connection
end

#fields_to_headObject

fields to see first



64
65
66
# File 'lib/rails_db_browser/connection_keeper.rb', line 64

def fields_to_head
  @fields_to_head ||= %w{id name login value}
end

#fields_to_tailObject

fields to see last



69
70
71
# File 'lib/rails_db_browser/connection_keeper.rb', line 69

def fields_to_tail
  @fields_to_tail ||= %w{created_at created_on updated_at updated_on}
end

Instance Method Details

#column_names(table) ⇒ Object



55
56
57
# File 'lib/rails_db_browser/connection_keeper.rb', line 55

def column_names(table)
  columns(table).map{|c| c.name}
end

#columns(table) ⇒ Object

getting list of column definitions and order them to be more human readable



44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_db_browser/connection_keeper.rb', line 44

def columns(table)
  columns = get_column_definitions(table)
  columns.sort_by{|c| 
    [
      fields_to_head.index(c.name) || 1e6,
      -(fields_to_tail.index(c.name) || 1e6),
      c.name
    ]
  }
end

#query(sql, opts = {}) ⇒ Object

performs query with appropriate method



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_db_browser/connection_keeper.rb', line 83

def query(sql, opts={})
  per_page = (opts[:perpage] || nil).to_i
  page     = (opts[:page]    || 1  ).try(:to_i)
  fields   = opts[:fields]   || nil
  case sql
  when /\s*select/i , /\s*(update|insert|delete).+returning/im
    rez = {:fields => fields}
    if sql =~ /\s*select/i && per_page > 0
      rez[:count] = select_value("select count(*) from (#{sql}) as t").to_i
      rez[:pages] = (rez[:count].to_f / per_page).ceil
      sql = "select * from (#{sql}) as t"
      add_limit_offset!( sql, 
                        :limit => per_page,
                        :offset => per_page * (page - 1))
    end

    rez[:rows] = select_all( sql )

    unless rez[:rows].blank?
      rez[:fields] ||= []
      rez[:fields].concat( self.sort_fields(rez[:rows].first.keys) - rez[:fields] )
    end

    Result.new(rez)
  when /\s*update/i
    Result.new :value => update( sql )
  when /\s*insert/i
    Result.new :value => insert( sql )
  when /\s*delete/i
    Result.new :value => delete( sql )
  end
rescue StandardError => e
  Result.new :error => e
end

#sort_fields(fields) ⇒ Object

sort field names in a rezult



76
77
78
79
80
# File 'lib/rails_db_browser/connection_keeper.rb', line 76

def sort_fields(fields)
  fields = (fields_to_head & fields) | (fields - fields_to_head)
  fields = (fields - fields_to_tail) | (fields_to_tail & fields)
  fields
end

#table_namesObject



59
60
61
# File 'lib/rails_db_browser/connection_keeper.rb', line 59

def table_names
  @connection.tables.sort
end