Class: RDBI::Driver::PostgreSQL::Database
- Inherits:
-
RDBI::Database
- Object
- RDBI::Database
- RDBI::Driver::PostgreSQL::Database
- Extended by:
- MethLab
- Defined in:
- lib/rdbi/driver/postgresql.rb
Instance Attribute Summary collapse
-
#pg_conn ⇒ Object
Returns the value of attribute pg_conn.
Instance Method Summary collapse
- #commit ⇒ Object
- #disconnect ⇒ Object
-
#initialize(*args) ⇒ Database
constructor
A new instance of Database.
- #new_statement(query) ⇒ Object
- #ping ⇒ Object
- #rollback ⇒ Object
- #schema(pg_schema = 'public') ⇒ Object
- #table_schema(table_name, pg_schema = 'public') ⇒ Object
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Database
Returns a new instance of Database.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rdbi/driver/postgresql.rb', line 18 def initialize( *args ) super( *args ) self.database_name = @connect_args[:dbname] || @connect_args[:database] || @connect_args[:db] @pg_conn = PGconn.new( @connect_args[:host] || @connect_args[:hostname], @connect_args[:port], @connect_args[:options], @connect_args[:tty], self.database_name, @connect_args[:user] || @connect_args[:username], @connect_args[:password] || @connect_args[:auth] ) @preprocess_quoter = proc do |x, named, indexed| @pg_conn.escape_string((named[x] || indexed[x]).to_s) end end |
Instance Attribute Details
#pg_conn ⇒ Object
Returns the value of attribute pg_conn.
16 17 18 |
# File 'lib/rdbi/driver/postgresql.rb', line 16 def pg_conn @pg_conn end |
Instance Method Details
#commit ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/rdbi/driver/postgresql.rb', line 56 def commit if ! in_transaction? raise RDBI::TransactionError.new( "Cannot commit when not in a transaction" ) end execute 'COMMIT' super end |
#disconnect ⇒ Object
36 37 38 39 |
# File 'lib/rdbi/driver/postgresql.rb', line 36 def disconnect @pg_conn.close super end |
#new_statement(query) ⇒ Object
64 65 66 |
# File 'lib/rdbi/driver/postgresql.rb', line 64 def new_statement( query ) Statement.new( query, self ) end |
#ping ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rdbi/driver/postgresql.rb', line 112 def ping start = Time.now rows = begin execute("SELECT 1").result_count rescue PGError => e # XXX Sorry this sucks. PGconn is completely useless without a # connection... like asking it if it's connected. raise RDBI::DisconnectedError.new(e.) end stop = Time.now if rows > 0 stop.to_i - start.to_i else raise RDBI::DisconnectedError, "disconnected during ping" end end |
#rollback ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rdbi/driver/postgresql.rb', line 49 def rollback if ! in_transaction? raise RDBI::TransactionError.new( "Cannot rollback when not in a transaction" ) end execute 'ROLLBACK' super end |
#schema(pg_schema = 'public') ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/rdbi/driver/postgresql.rb', line 104 def schema( pg_schema = 'public' ) schemata = [] execute( "SELECT table_name FROM information_schema.tables WHERE table_schema = '#{pg_schema}';" ).fetch( :all ).each do |row| schemata << table_schema( row[0], pg_schema ) end schemata end |
#table_schema(table_name, pg_schema = 'public') ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rdbi/driver/postgresql.rb', line 68 def table_schema( table_name, pg_schema = 'public' ) info_row = execute( "SELECT table_type FROM information_schema.tables WHERE table_schema = ? AND table_name = ?", pg_schema, table_name ).fetch( :first ) rescue nil if info_row.nil? return nil end sch = RDBI::Schema.new( [], [] ) sch.tables << table_name.to_sym case info_row[ 0 ] when 'BASE TABLE' sch.type = :table when 'VIEW' sch.type = :view end execute( "SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = ? AND table_name = ?", pg_schema, table_name ).fetch( :all ).each do |row| col = RDBI::Column.new col.name = row[0].to_sym col.type = row[1].to_sym # TODO: ensure this ruby_type is solid, especially re: dates and times col.ruby_type = row[1].to_sym col.nullable = row[2] == "YES" sch.columns << col end sch end |
#transaction(&block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/rdbi/driver/postgresql.rb', line 41 def transaction( &block ) if in_transaction? raise RDBI::TransactionError.new( "Already in transaction (not supported by PostgreSQL)" ) end execute 'BEGIN' super &block end |