Class: ActiveRecordUtils::Browser::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/utils/browser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, key) ⇒ Connection

Returns a new instance of Connection.



60
61
62
63
# File 'lib/activerecord/utils/browser.rb', line 60

def initialize( connection, key )
  @connection = connection
  @key        = key
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



65
66
67
# File 'lib/activerecord/utils/browser.rb', line 65

def connection
  @connection
end

#keyObject (readonly)

Returns the value of attribute key.



66
67
68
# File 'lib/activerecord/utils/browser.rb', line 66

def key
  @key
end

Instance Method Details

#class_nameObject



71
72
73
# File 'lib/activerecord/utils/browser.rb', line 71

def class_name
  @connection.class.name
end

#fetch_table_column_defs(name) ⇒ Object



109
110
111
112
113
114
# File 'lib/activerecord/utils/browser.rb', line 109

def fetch_table_column_defs( name )
  ### fix/todo: add reference to table_def
  @connection.columns( name ).map do |col|
    Column.new( col.name, col.sql_type, col.default, col.null )
  end
end

#fetch_table_defsObject



103
104
105
106
107
# File 'lib/activerecord/utils/browser.rb', line 103

def fetch_table_defs
  @connection.tables.sort.map do |name|
    Table.new( self, name )
  end
end

#fetch_table_select_all(name, opts = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/activerecord/utils/browser.rb', line 117

def fetch_table_select_all( name, opts={} )
  limit =  (opts[:limit] || 33).to_i # 33 records limit/per page (for now default)
  limit =  33   if limit == 0   # use default page size if limit 0 (from not a number para)
  
  offset = (opts[:offset] || 0).to_i

  sql = "select * from #{name} limit #{limit}"

  sql << " offset #{offset}"   if offset > 0     # add offset if present (e.g greater zero)

  # page = (opts[:page] || 1 ).try(:to_i)
  # fields = opts[:fields] || nil
 
  # 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

  result = {}
  result[ :sql  ] = sql    # note: lets also always add sql query to result too
  result[ :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( result )
rescue StandardError => ex
  Result.new( error: ex )
end

#table(name) ⇒ Object



84
85
86
# File 'lib/activerecord/utils/browser.rb', line 84

def table( name )
  tables.find { |t| t.name.downcase == name.downcase }
end

#table_columns(name) ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/activerecord/utils/browser.rb', line 90

def table_columns( name )
  cols = fetch_table_column_defs( name )
  ### fix/to be done
  # cols.sort_by{|col|
  #    [
  #      fields_to_head.index(col.name) || 1e6,
  #      -(fields_to_tail.index(col.name) || 1e6),
  #      col.name
  #    ]
  #  }
  cols
end

#tablesObject

delegate :quote_table_name, :quote_column_name, :quote,

:update, :insert, :delete,
:add_limit_offset!,
:to => :connection


80
81
82
# File 'lib/activerecord/utils/browser.rb', line 80

def tables
  @tables ||= fetch_table_defs
end