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.



37
38
39
40
# File 'lib/dbbrowser/connection.rb', line 37

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



42
43
44
# File 'lib/dbbrowser/connection.rb', line 42

def connection
  @connection
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#fetch_table_column_defs(name) ⇒ Object



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

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



76
77
78
79
80
# File 'lib/dbbrowser/connection.rb', line 76

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

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



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
117
118
119
# File 'lib/dbbrowser/connection.rb', line 90

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



57
58
59
# File 'lib/dbbrowser/connection.rb', line 57

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



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dbbrowser/connection.rb', line 63

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


53
54
55
# File 'lib/dbbrowser/connection.rb', line 53

def tables
  @tables ||= fetch_table_defs
end