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
- native_database_types
- new
- pk_and_sequence_for
- quote
- quote_column_name
- reconnect!
- rename_table
- reset_pk_sequence!
- supports_migrations?
Constants
| BYTEA_COLUMN_TYPE_OID | = | 17 |
Public Class methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 52
52: def initialize(connection, logger, config = {})
53: super(connection, logger)
54: @config = config
55: configure_connection
56: 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 59
59: def active?
60: if @connection.respond_to?(:status)
61: @connection.status == PGconn::CONNECTION_OK
62: else
63: @connection.query 'SELECT 1'
64: true
65: end
66: rescue PGError
67: false
68: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 48
48: def adapter_name
49: 'PostgreSQL'
50: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 303
303: def add_column(table_name, column_name, type, options = {})
304: native_type = native_database_types[type]
305: sql_commands = ["ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}"]
306: if options[:default]
307: sql_commands << "ALTER TABLE #{table_name} ALTER #{column_name} SET DEFAULT '#{options[:default]}'"
308: end
309: if options[:null] == false
310: sql_commands << "ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL"
311: end
312: sql_commands.each { |cmd| execute(cmd) }
313: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 228
228: def default_sequence_name(table_name, pk = nil)
229: default_pk, default_seq = pk_and_sequence_for(table_name)
230: default_seq || "#{table_name}_#{pk || default_pk || 'id'}_seq"
231: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 79
79: def native_database_types
80: {
81: :primary_key => "serial primary key",
82: :string => { :name => "character varying", :limit => 255 },
83: :text => { :name => "text" },
84: :integer => { :name => "integer" },
85: :float => { :name => "float" },
86: :datetime => { :name => "timestamp" },
87: :timestamp => { :name => "timestamp" },
88: :time => { :name => "time" },
89: :date => { :name => "date" },
90: :binary => { :name => "bytea" },
91: :boolean => { :name => "boolean" }
92: }
93: end
Find a table’s primary key and sequence.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 253
253: def pk_and_sequence_for(table)
254: # First try looking for a sequence with a dependency on the
255: # given table's primary key.
256: 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]
257:
258: if result.nil? or result.empty?
259: # If that fails, try parsing the primary key's default value.
260: # Support the 7.x and 8.0 nextval('foo'::text) as well as
261: # the 8.1+ nextval('foo'::regclass).
262: # TODO: assumes sequence is in same schema as table.
263: 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]
264: end
265: # check for existence of . in sequence name as in public.foo_sequence. if it does not exist, join the current namespace
266: result.last['.'] ? [result.first, result.last] : [result.first, "#{result[1]}.#{result[2]}"]
267: rescue
268: nil
269: end
QUOTING ==================================================
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 102
102: def quote(value, column = nil)
103: if value.kind_of?(String) && column && column.type == :binary
104: "'#{escape_bytea(value)}'"
105: else
106: super
107: end
108: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 110
110: def quote_column_name(name)
111: %("#{name}")
112: end
Close then reopen the connection.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 71
71: def reconnect!
72: # TODO: postgres-pr doesn't have PGconn#reset.
73: if @connection.respond_to?(:reset)
74: @connection.reset
75: configure_connection
76: end
77: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 299
299: def rename_table(name, new_name)
300: execute "ALTER TABLE #{name} RENAME TO #{new_name}"
301: 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 234
234: def reset_pk_sequence!(table, pk = nil, sequence = nil)
235: unless pk and sequence
236: default_pk, default_sequence = pk_and_sequence_for(table)
237: pk ||= default_pk
238: sequence ||= default_sequence
239: end
240: if pk
241: if sequence
242: 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'
243: else
244: @logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger
245: end
246: end
247: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 95
95: def supports_migrations?
96: true
97: end