Module: PostgresqlWeb::Connection

Extended by:
Connection
Included in:
Connection
Defined in:
lib/postgresql_web/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbnameObject

Returns the value of attribute dbname.



7
8
9
# File 'lib/postgresql_web/connection.rb', line 7

def dbname
  @dbname
end

#pool_sizeObject

Returns the value of attribute pool_size.



7
8
9
# File 'lib/postgresql_web/connection.rb', line 7

def pool_size
  @pool_size
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/postgresql_web/connection.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#columns(table_name) ⇒ Object



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

def columns(table_name)
  connection.exec("select * from INFORMATION_SCHEMA.COLUMNS where table_name = $1", [table_name]).map do |col|
    Column.new(col)
  end
end

#connectionObject



9
10
11
12
13
14
15
16
17
# File 'lib/postgresql_web/connection.rb', line 9

def connection
  if @connection.nil?
    initial_default_params
    @connection = ConnectionPool::Wrapper.new(size: pool_size, timeout: pool_size) do 
      PG::Connection.new(dbname: dbname)
    end
  end
  @connection
end

#data(table_name, per_page, page) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/postgresql_web/connection.rb', line 43

def data(table_name, per_page, page)
  if page <= 1
    page = 1
  end
  
  total_records = connection.exec("select count(1) from #{table_name}").column_values(0).first
  sql = "select * from #{table_name}"
  if page > 1
   sql += " offset #{per_page * (page - 1)}"
  end
  sql += " limit #{per_page}"
  puts sql
  data = connection.exec(sql).to_a
  puts 
  [data, total_records]
end

#initial_default_paramsObject



19
20
21
22
23
24
25
# File 'lib/postgresql_web/connection.rb', line 19

def initial_default_params
  self.pool_size = 5;
  self.timeout = 5
  if is_pg_activerecord?
    self.dbname = ActiveRecord::Base.connection.current_database
  end
end

#is_pg_activerecord?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/postgresql_web/connection.rb', line 27

def is_pg_activerecord?
  defined?(Rails) and defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
end

#tablesObject



32
33
34
35
# File 'lib/postgresql_web/connection.rb', line 32

def tables 
  result = connection.exec("select tablename from pg_tables where schemaname = 'public'")
  result.column_values(0).sort
end