The PostgreSQL adapter works both with the C-based (www.postgresql.jp/interfaces/ruby/) and the Ruby-base (available both as gem and from rubyforge.org/frs/?group_id=234&release_id=1145) drivers.
Options:
- :host — Defaults to localhost
- :port — Defaults to 5432
- :username — Defaults to nothing
- :password — Defaults to nothing
- :database — The name of the database. No default, must be provided.
- :schema_search_path — An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the :schema_order option.
- :encoding — An optional client encoding that is using in a SET client_encoding TO <encoding> call on connection.
- :min_messages — An optional client min messages that is using in a SET client_min_messages TO <min_messages> call on connection.
Methods
- active?
- adapter_name
- add_column
- default_sequence_name
- disconnect!
- native_database_types
- new
- pk_and_sequence_for
- quote
- quote_column_name
- reconnect!
- rename_table
- reset_pk_sequence!
- supports_migrations?
- table_alias_length
Constants
BYTEA_COLUMN_TYPE_OID | = | 17 |
TIMESTAMPOID | = | 1114 |
TIMESTAMPTZOID | = | 1184 |
Public Class methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 54 54: def initialize(connection, logger, config = {}) 55: super(connection, logger) 56: @config = config 57: configure_connection 58: end
Public Instance methods
Is this connection alive and ready for queries?
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 61 61: def active? 62: if @connection.respond_to?(:status) 63: @connection.status == PGconn::CONNECTION_OK 64: else 65: @connection.query 'SELECT 1' 66: true 67: end 68: # postgres-pr raises a NoMethodError when querying if no conn is available 69: rescue PGError, NoMethodError 70: false 71: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 50 50: def adapter_name 51: 'PostgreSQL' 52: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 314 314: def add_column(table_name, column_name, type, options = {}) 315: execute("ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}") 316: execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL") if options[:null] == false 317: change_column_default(table_name, column_name, options[:default]) unless options[:default].nil? 318: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 239 239: def default_sequence_name(table_name, pk = nil) 240: default_pk, default_seq = pk_and_sequence_for(table_name) 241: default_seq || "#{table_name}_#{pk || default_pk || 'id'}_seq" 242: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 82 82: def disconnect! 83: # Both postgres and postgres-pr respond to :close 84: @connection.close rescue nil 85: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 87 87: def native_database_types 88: { 89: :primary_key => "serial primary key", 90: :string => { :name => "character varying", :limit => 255 }, 91: :text => { :name => "text" }, 92: :integer => { :name => "integer" }, 93: :float => { :name => "float" }, 94: :datetime => { :name => "timestamp" }, 95: :timestamp => { :name => "timestamp" }, 96: :time => { :name => "time" }, 97: :date => { :name => "date" }, 98: :binary => { :name => "bytea" }, 99: :boolean => { :name => "boolean" } 100: } 101: end
Find a table’s primary key and sequence.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 264 264: def pk_and_sequence_for(table) 265: # First try looking for a sequence with a dependency on the 266: # given table's primary key. 267: result = execute("SELECT attr.attname, name.nspname, seq.relname\nFROM pg_class seq,\npg_attribute attr,\npg_depend dep,\npg_namespace name,\npg_constraint cons\nWHERE seq.oid = dep.objid\nAND seq.relnamespace = name.oid\nAND seq.relkind = 'S'\nAND attr.attrelid = dep.refobjid\nAND attr.attnum = dep.refobjsubid\nAND attr.attrelid = cons.conrelid\nAND attr.attnum = cons.conkey[1]\nAND cons.contype = 'p'\nAND dep.refobjid = '\#{table}'::regclass\n", 'PK and serial sequence')[0] 268: 269: if result.nil? or result.empty? 270: # If that fails, try parsing the primary key's default value. 271: # Support the 7.x and 8.0 nextval('foo'::text) as well as 272: # the 8.1+ nextval('foo'::regclass). 273: # TODO: assumes sequence is in same schema as table. 274: result = execute("SELECT attr.attname, name.nspname, split_part(def.adsrc, '\\\\\\'', 2)\nFROM pg_class t\nJOIN pg_namespace name ON (t.relnamespace = name.oid)\nJOIN pg_attribute attr ON (t.oid = attrelid)\nJOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)\nJOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])\nWHERE t.oid = '\#{table}'::regclass\nAND cons.contype = 'p'\nAND def.adsrc ~* 'nextval'\n", 'PK and custom sequence')[0] 275: end 276: # check for existence of . in sequence name as in public.foo_sequence. if it does not exist, join the current namespace 277: result.last['.'] ? [result.first, result.last] : [result.first, "#{result[1]}.#{result[2]}"] 278: rescue 279: nil 280: end
QUOTING ==================================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 113 113: def quote(value, column = nil) 114: if value.kind_of?(String) && column && column.type == :binary 115: "'#{escape_bytea(value)}'" 116: else 117: super 118: end 119: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 121 121: def quote_column_name(name) 122: %("#{name}") 123: end
Close then reopen the connection.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 74 74: def reconnect! 75: # TODO: postgres-pr doesn't have PGconn#reset. 76: if @connection.respond_to?(:reset) 77: @connection.reset 78: configure_connection 79: end 80: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 310 310: def rename_table(name, new_name) 311: execute "ALTER TABLE #{name} RENAME TO #{new_name}" 312: end
Resets sequence to the max value of the table’s pk if present.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 245 245: def reset_pk_sequence!(table, pk = nil, sequence = nil) 246: unless pk and sequence 247: default_pk, default_sequence = pk_and_sequence_for(table) 248: pk ||= default_pk 249: sequence ||= default_sequence 250: end 251: if pk 252: if sequence 253: select_value "SELECT setval('\#{sequence}', (SELECT COALESCE(MAX(\#{pk})+(SELECT increment_by FROM \#{sequence}), (SELECT min_value FROM \#{sequence})) FROM \#{table}), false)\n", 'Reset sequence' 254: else 255: @logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger 256: end 257: end 258: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 103 103: def supports_migrations? 104: true 105: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 107 107: def table_alias_length 108: 63 109: end