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.



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

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#class_nameObject



65
66
67
# File 'lib/dbbrowser/connection.rb', line 65

def class_name
  @connection.class.name
end

#fetch_table_column_defs(name) ⇒ Object



103
104
105
106
107
108
# File 'lib/dbbrowser/connection.rb', line 103

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



97
98
99
100
101
# File 'lib/dbbrowser/connection.rb', line 97

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

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



111
112
113
114
115
116
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
# File 'lib/dbbrowser/connection.rb', line 111

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



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

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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dbbrowser/connection.rb', line 84

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


74
75
76
# File 'lib/dbbrowser/connection.rb', line 74

def tables
  @tables ||= fetch_table_defs
end