Class: DbBrowser::ConnectionMan::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/dbbrowser/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, key) ⇒ Connection

Returns a new instance of Connection.



39
40
41
42
# File 'lib/dbbrowser/connection.rb', line 39

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



44
45
46
# File 'lib/dbbrowser/connection.rb', line 44

def connection
  @connection
end

#keyObject (readonly)

Returns the value of attribute key.



45
46
47
# File 'lib/dbbrowser/connection.rb', line 45

def key
  @key
end

Instance Method Details

#class_nameObject



50
51
52
# File 'lib/dbbrowser/connection.rb', line 50

def class_name
  @connection.class.name
end

#fetch_table_column_defs(name) ⇒ Object



88
89
90
91
92
93
# File 'lib/dbbrowser/connection.rb', line 88

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



82
83
84
85
86
# File 'lib/dbbrowser/connection.rb', line 82

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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dbbrowser/connection.rb', line 96

def fetch_table_select_all( name, opts={} )
  per_page = (opts[:perpage] || 33).to_i   # 33 records per page (for now default)

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

  # 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[ :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



63
64
65
# File 'lib/dbbrowser/connection.rb', line 63

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dbbrowser/connection.rb', line 69

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


59
60
61
# File 'lib/dbbrowser/connection.rb', line 59

def tables
  @tables ||= fetch_table_defs
end