# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 801 def primary_key(table) pk_and_sequence = pk_and_sequence_for(table) pk_and_sequence && pk_and_sequence.first end
The PostgreSQL adapter works both with the native C (ruby.scripting.ca/postgres/) and the pure Ruby (available both as gem and from rubyforge.org/frs/?group_id=234&release_id=1944) 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 used in a SET client_encoding TO <encoding> call on the connection.
-
:min_messages - An optional client min messages that is used in a SET client_min_messages TO <min_messages> call on the connection.
-
:allow_concurrency - If true, use async query methods so Ruby threads don’t deadlock; otherwise, use blocking query methods.
- active?
- adapter_name
- add_column
- begin_db_transaction
- change_column
- change_column_default
- change_column_null
- client_min_messages
- client_min_messages=
- columns
- commit_db_transaction
- create_database
- create_savepoint
- current_database
- disconnect!
- encoding
- escape_bytea
- execute
- index_name_length
- indexes
- insert
- new
- outside_transaction?
- postgresql_version
- primary_key
- quote_table_name
- reconnect!
- release_savepoint
- rename_column
- rename_table
- rollback_db_transaction
- rollback_to_savepoint
- schema_search_path
- schema_search_path=
- select_rows
- set_standard_conforming_strings
- supports_ddl_transactions?
- supports_insert_with_returning?
- supports_migrations?
- supports_savepoints?
- table_alias_length
- table_exists?
- tables
- translate_exception
- type_to_sql
- unescape_bytea
- update_sql
ADAPTER_NAME | = | 'PostgreSQL'.freeze |
NATIVE_DATABASE_TYPES | = | { :primary_key => "serial primary key".freeze, :string => { :name => "character varying", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "integer" }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "timestamp" }, :timestamp => { :name => "timestamp" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "bytea" }, :boolean => { :name => "boolean" }, :xml => { :name => "xml" } } |
Initializes and connects a PostgreSQL adapter.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 237 def initialize(connection, logger, connection_parameters, config) super(connection, logger) @connection_parameters, @config = connection_parameters, config # @local_tz is initialized as nil to avoid warnings when connect tries to use it @local_tz = nil @table_alias_length = nil @postgresql_version = nil connect @local_tz = execute('SHOW TIME ZONE').first["TimeZone"] end
Is this connection alive and ready for queries?
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 251 def active? if @connection.respond_to?(:status) @connection.status == PGconn::CONNECTION_OK else # We're asking the driver, not Active Record, so use @connection.query instead of #query @connection.query 'SELECT 1' true end # postgres-pr raises a NoMethodError when querying if no connection is available. rescue PGError, NoMethodError false end
Returns ‘PostgreSQL’ as adapter name for identification purposes.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 232 def adapter_name ADAPTER_NAME end
Adds a new column to the named table. See TableDefinition#column for details of the options you can use.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 813 def add_column(table_name, column_name, type, options = {}) default = options[:default] notnull = options[:null] == false # Add the column. execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}") change_column_default(table_name, column_name, default) if options_include_default?(options) change_column_null(table_name, column_name, false, default) if notnull end
Begins a transaction.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 529 def begin_db_transaction execute "BEGIN" end
Changes the column of a table.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 825 def change_column(table_name, column_name, type, options = {}) quoted_table_name = quote_table_name(table_name) begin execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" rescue ActiveRecord::StatementInvalid => e raise e if postgresql_version > 80000 # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it. begin begin_db_transaction tmp_column_name = "#{column_name}_ar_tmp" add_column(table_name, tmp_column_name, type, options) execute "UPDATE #{quoted_table_name} SET #{quote_column_name(tmp_column_name)} = CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, options[:limit], options[:precision], options[:scale])})" remove_column(table_name, column_name) rename_column(table_name, tmp_column_name, column_name) commit_db_transaction rescue rollback_db_transaction end end change_column_default(table_name, column_name, options[:default]) if options_include_default?(options) change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null) end
Changes the default value of a table column.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 851 def change_column_default(table_name, column_name, default) execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}" end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 855 def change_column_null(table_name, column_name, null, default = nil) unless null || default.nil? execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") end execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL") end
Returns the current client message level.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 716 def client_min_messages query('SHOW client_min_messages')[0][0] end
Set the client message level.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 721 def client_min_messages=(level) execute("SET client_min_messages TO '#{level}'") end
Returns the list of all column definitions for a table.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 678 def columns(table_name, name = nil) # Limit, precision, and scale are all handled by the superclass. column_definitions(table_name).collect do |name, type, default, notnull| PostgreSQLColumn.new(name, default, type, notnull == 'f') end end
Commits a transaction.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 534 def commit_db_transaction execute "COMMIT" end
Create a new PostgreSQL database. Options include :owner, :template, :encoding, :tablespace, and :connection_limit (note that MySQL uses :charset while PostgreSQL uses :encoding).
Example:
create_database config[:database], config create_database 'foo_development', :encoding => 'unicode'
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 573 def create_database(name, options = {}) options = options.reverse_merge(:encoding => "utf8") option_string = options.symbolize_keys.sum do |key, value| case key when :owner " OWNER = \"#{value}\"" when :template " TEMPLATE = \"#{value}\"" when :encoding " ENCODING = '#{value}'" when :tablespace " TABLESPACE = \"#{value}\"" when :connection_limit " CONNECTION LIMIT = #{value}" else "" end end execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}" end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 547 def create_savepoint execute("SAVEPOINT #{current_savepoint_name}") end
Returns the current database name.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 686 def current_database query('select current_database()')[0][0] end
Close the connection.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 276 def disconnect! @connection.close rescue nil end
Returns the current database encoding format.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 691 def encoding query( SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database WHERE pg_database.datname LIKE '#{current_database}')[0][0] end
Escapes binary strings for bytea input to the database.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 323 def escape_bytea(value) @connection.escape_bytea(value) if value end
Executes an SQL statement, returning a PGresult object on success or raising a PGError exception otherwise.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 513 def execute(sql, name = nil) log(sql, name) do if @async @connection.async_exec(sql) else @connection.exec(sql) end end end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 871 def index_name_length 63 end
Returns the list of all indexes for a table.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 644 def indexes(table_name, name = nil) schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') result = query( SELECT distinct i.relname, d.indisunique, d.indkey, t.oid FROM pg_class t, pg_class i, pg_index d WHERE i.relkind = 'i' AND d.indexrelid = i.oid AND d.indisprimary = 'f' AND t.oid = d.indrelid AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) ) ORDER BY i.relname, name) result.map do |row| index_name = row[0] unique = row[1] == 't' indkey = row[2].split(" ") oid = row[3] columns = Hash[query( SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{indkey.join(",")}), "Columns for index #{row[0]} on #{table_name}")] column_names = columns.values_at(*indkey).compact column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names) end.compact end
Executes an INSERT query and returns the new record’s ID
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 425 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) # Extract the table from the insert sql. Yuck. table = sql.split(" ", 4)[2].gsub('"', '') # Try an insert with 'returning id' if available (PG >= 8.2) if supports_insert_with_returning? pk, sequence_name = *pk_and_sequence_for(table) unless pk if pk id = select_value("#{sql} RETURNING #{quote_column_name(pk)}") clear_query_cache return id end end # Otherwise, insert then grab last_insert_id. if insert_id = super insert_id else # If neither pk nor sequence name is given, look them up. unless pk || sequence_name pk, sequence_name = *pk_and_sequence_for(table) end # If a pk is given, fallback to default sequence name. # Don't fetch last insert id for a table without a pk. if pk && sequence_name ||= default_sequence_name(table, pk) last_insert_id(table, sequence_name) end end end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 543 def outside_transaction? @connection.transaction_status == PGconn::PQTRANS_IDLE end
Returns just a table’s primary key
[ show source ]
Checks the following cases:
-
table_name
-
“table.name”
-
schema_name.table_name
-
schema_name.“table.name”
-
“schema.name”.table_name
-
“schema.name”.“table.name”
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 373 def quote_table_name(name) schema, name_part = extract_pg_identifier_from_name(name.to_s) unless name_part quote_column_name(schema) else table_name, name_part = extract_pg_identifier_from_name(name_part) "#{quote_column_name(schema)}.#{quote_column_name(table_name)}" end end
Close then reopen the connection.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 265 def reconnect! if @connection.respond_to?(:reset) @connection.reset configure_connection else disconnect! connect end end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 555 def release_savepoint execute("RELEASE SAVEPOINT #{current_savepoint_name}") end
Renames a column in a table.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 863 def rename_column(table_name, column_name, new_column_name) execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}" end
Renames a table.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 807 def rename_table(name, new_name) execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}" end
Aborts a transaction.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 539 def rollback_db_transaction execute "ROLLBACK" end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 551 def rollback_to_savepoint execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}") end
Returns the active schema search path.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 711 def schema_search_path @schema_search_path ||= query('SHOW search_path')[0][0] end
Sets the schema search path to a string of comma-separated schema names. Names beginning with $ have to be quoted (e.g. $user => ‘$user’). See: www.postgresql.org/docs/current/static/ddl-schemas.html
This should be not be called manually but set in database.yml.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 703 def schema_search_path=(schema_csv) if schema_csv execute "SET search_path TO #{schema_csv}" @schema_search_path = schema_csv end end
Executes a SELECT query and returns an array of rows. Each row is an array of field values.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 420 def select_rows(sql, name = nil) select_raw(sql, name).last end
Enable standard-conforming strings if available.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 295 def set_standard_conforming_strings old, self.client_min_messages = client_min_messages, 'panic' execute('SET standard_conforming_strings = on') rescue nil ensure self.client_min_messages = old end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 306 def supports_ddl_transactions? true end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 302 def supports_insert_with_returning? postgresql_version >= 80200 end
Does PostgreSQL support migrations?
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 285 def supports_migrations? true end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 310 def supports_savepoints? true end
Returns the configured supported identifier length supported by PostgreSQL, or report the default of 63 on PostgreSQL 7.x.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 316 def table_alias_length @table_alias_length ||= (postgresql_version >= 80000 ? query('SHOW max_identifier_length')[0][0].to_i : 63) end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 621 def table_exists?(name) name = name.to_s schema, table = name.split('.', 2) unless table # A table was provided without a schema table = schema schema = nil end if name =~ /^"/ # Handle quoted table names table = name schema = nil end query( SELECT COUNT(*) FROM pg_tables WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}' #{schema ? "AND schemaname = '#{schema}'" : ''}).first[0].to_i > 0 end
Returns the list of all tables in the schema search path or a specified schema.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 613 def tables(name = nil) query( SELECT tablename FROM pg_tables WHERE schemaname = ANY (current_schemas(false)), name).map { |row| row[0] } end
Maps logical Rails types to PostgreSQL-specific data types.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 876 def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' return 'integer' unless limit case limit when 1, 2; 'smallint' when 3, 4; 'integer' when 5..8; 'bigint' else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.") end end
Unescapes bytea output from a database to the binary string it represents. NOTE: This is NOT an inverse of escape_bytea! This is only to be used
on escaped binary output from database drive.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 330 def unescape_bytea(value) @connection.unescape_bytea(value) if value end
Executes an UPDATE query and returns the number of affected tuples.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 524 def update_sql(sql, name = nil) super.cmd_tuples end
Returns the version of the connected PostgreSQL version.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 911 def postgresql_version @postgresql_version ||= if @connection.respond_to?(:server_version) @connection.server_version else # Mimic PGconn.server_version behavior begin if query('SELECT version()')[0][0] =~ /PostgreSQL ([0-9.]+)/ major, minor, tiny = $1.split(".") (major.to_i * 10000) + (minor.to_i * 100) + tiny.to_i else 0 end rescue 0 end end end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 930 def translate_exception(exception, message) case exception.message when /duplicate key value violates unique constraint/ RecordNotUnique.new(message, exception) when /violates foreign key constraint/ InvalidForeignKey.new(message, exception) else super end end