Module: PostgresqlWeb::Connection
Instance Attribute Summary collapse
-
#dbname ⇒ Object
Returns the value of attribute dbname.
-
#pool_size ⇒ Object
Returns the value of attribute pool_size.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #columns(table_name) ⇒ Object
- #connection ⇒ Object
- #data(table_name, per_page, page) ⇒ Object
- #initial_default_params ⇒ Object
- #is_pg_activerecord? ⇒ Boolean
- #tables ⇒ Object
Instance Attribute Details
#dbname ⇒ Object
Returns the value of attribute dbname.
7 8 9 |
# File 'lib/postgresql_web/connection.rb', line 7 def dbname @dbname end |
#pool_size ⇒ Object
Returns the value of attribute pool_size.
7 8 9 |
# File 'lib/postgresql_web/connection.rb', line 7 def pool_size @pool_size end |
#timeout ⇒ Object
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 |
#connection ⇒ Object
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_params ⇒ Object
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
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 |
#tables ⇒ Object
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 |