Class: RDBI::Driver::PostgreSQL::Database
- Inherits:
-
RDBI::Database
- Object
- RDBI::Database
- RDBI::Driver::PostgreSQL::Database
- 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
- #quote(item) ⇒ 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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rdbi/driver/postgresql.rb', line 17 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| quote(named[x] || indexed[x]) end end |
Instance Attribute Details
#pg_conn ⇒ Object
Returns the value of attribute pg_conn.
15 16 17 |
# File 'lib/rdbi/driver/postgresql.rb', line 15 def pg_conn @pg_conn end |
Instance Method Details
#commit ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/rdbi/driver/postgresql.rb', line 55 def commit if ! in_transaction? raise RDBI::TransactionError.new( "Cannot commit when not in a transaction" ) end execute('COMMIT').finish super end |
#disconnect ⇒ Object
35 36 37 38 |
# File 'lib/rdbi/driver/postgresql.rb', line 35 def disconnect super @pg_conn.close end |
#new_statement(query) ⇒ Object
63 64 65 |
# File 'lib/rdbi/driver/postgresql.rb', line 63 def new_statement( query ) Statement.new( query, self ) end |
#ping ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rdbi/driver/postgresql.rb', line 127 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 |
#quote(item) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rdbi/driver/postgresql.rb', line 146 def quote(item) case item when Numeric item.to_s when TrueClass 'true' when FalseClass 'false' when NilClass 'NULL' else "E'#{@pg_conn.escape_string(item.to_s)}'" end end |
#rollback ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/rdbi/driver/postgresql.rb', line 48 def rollback if ! in_transaction? raise RDBI::TransactionError.new( "Cannot rollback when not in a transaction" ) end execute('ROLLBACK').finish super end |
#schema(pg_schema = 'public') ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rdbi/driver/postgresql.rb', line 115 def schema( pg_schema = 'public' ) schemata = [] execute(%Q[ 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
67 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 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rdbi/driver/postgresql.rb', line 67 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 else sch.type = :table end execute( %q[ SELECT c.column_name, c.data_type, c.is_nullable, tc.constraint_type FROM information_schema.columns c LEFT JOIN information_schema.key_column_usage kcu ON kcu.column_name = c.column_name AND kcu.table_name = c.table_name LEFT JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name WHERE c.table_schema = ? and c.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" col.primary_key = row[3] == "PRIMARY KEY" sch.columns << col end sch end |
#transaction(&block) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rdbi/driver/postgresql.rb', line 40 def transaction( &block ) if in_transaction? raise RDBI::TransactionError.new( "Already in transaction (not supported by PostgreSQL)" ) end execute('BEGIN').finish super(&block) end |