Class | ActiveRecord::ConnectionAdapters::PostgreSQLAdapter |
In: |
vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
|
Parent: | AbstractAdapter |
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:
Initializes and connects a PostgreSQL adapter.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 233 233: def initialize(connection, logger, connection_parameters, config) 234: super(connection, logger) 235: @connection_parameters, @config = connection_parameters, config 236: 237: connect 238: end
Is this connection alive and ready for queries?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 241 241: def active? 242: if @connection.respond_to?(:status) 243: @connection.status == PGconn::CONNECTION_OK 244: else 245: # We're asking the driver, not ActiveRecord, so use @connection.query instead of #query 246: @connection.query 'SELECT 1' 247: true 248: end 249: # postgres-pr raises a NoMethodError when querying if no connection is available. 250: rescue PGError, NoMethodError 251: false 252: end
Returns ‘PostgreSQL’ as adapter name for identification purposes.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 228 228: def adapter_name 229: 'PostgreSQL' 230: end
Adds a column to a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 594 594: def add_column(table_name, column_name, type, options = {}) 595: default = options[:default] 596: notnull = options[:null] == false 597: 598: # Add the column. 599: execute("ALTER TABLE #{table_name} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit])}") 600: 601: change_column_default(table_name, column_name, default) if options_include_default?(options) 602: change_column_null(table_name, column_name, false, default) if notnull 603: end
Begins a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 422 422: def begin_db_transaction 423: execute "BEGIN" 424: end
Changes the column of a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 606 606: def change_column(table_name, column_name, type, options = {}) 607: begin 608: execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" 609: rescue ActiveRecord::StatementInvalid 610: # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it. 611: begin_db_transaction 612: tmp_column_name = "#{column_name}_ar_tmp" 613: add_column(table_name, tmp_column_name, type, options) 614: execute "UPDATE #{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])})" 615: remove_column(table_name, column_name) 616: rename_column(table_name, tmp_column_name, column_name) 617: commit_db_transaction 618: end 619: 620: change_column_default(table_name, column_name, options[:default]) if options_include_default?(options) 621: change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null) 622: end
Changes the default value of a table column.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 625 625: def change_column_default(table_name, column_name, default) 626: execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}" 627: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 629 629: def change_column_null(table_name, column_name, null, default = nil) 630: unless null || default.nil? 631: execute("UPDATE #{table_name} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") 632: end 633: execute("ALTER TABLE #{table_name} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL") 634: end
Returns the current client message level.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 510 510: def client_min_messages 511: query('SHOW client_min_messages')[0][0] 512: end
Set the client message level.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 515 515: def client_min_messages=(level) 516: execute("SET client_min_messages TO '#{level}'") 517: end
Returns the list of all column definitions for a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 485 485: def columns(table_name, name = nil) 486: # Limit, precision, and scale are all handled by the superclass. 487: column_definitions(table_name).collect do |name, type, default, notnull| 488: PostgreSQLColumn.new(name, default, type, notnull == 'f') 489: end 490: end
Commits a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 427 427: def commit_db_transaction 428: execute "COMMIT" 429: end
Close the connection.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 266 266: def disconnect! 267: @connection.close rescue nil 268: end
Executes an SQL statement, returning a PGresult object on success or raising a PGError exception otherwise.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 406 406: def execute(sql, name = nil) 407: log(sql, name) do 408: if @async 409: @connection.async_exec(sql) 410: else 411: @connection.exec(sql) 412: end 413: end 414: end
Returns the list of all indexes for a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 450 450: def indexes(table_name, name = nil) 451: result = query("SELECT i.relname, d.indisunique, a.attname\nFROM pg_class t, pg_class i, pg_index d, pg_attribute a\nWHERE i.relkind = 'i'\nAND d.indexrelid = i.oid\nAND d.indisprimary = 'f'\nAND t.oid = d.indrelid\nAND t.relname = '\#{table_name}'\nAND a.attrelid = t.oid\nAND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum\nOR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum\nOR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum\nOR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum\nOR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )\nORDER BY i.relname\n", name) 452: 453: current_index = nil 454: indexes = [] 455: 456: result.each do |row| 457: if current_index != row[0] 458: indexes << IndexDefinition.new(table_name, row[0], row[1] == "t", []) 459: current_index = row[0] 460: end 461: 462: indexes.last.columns << row[2] 463: end 464: 465: indexes 466: end
Executes an INSERT query and returns the new record‘s ID
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 388 388: def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) 389: table = sql.split(" ", 4)[2] 390: super || last_insert_id(table, sequence_name || default_sequence_name(table, pk)) 391: end
Close then reopen the connection.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 255 255: def reconnect! 256: if @connection.respond_to?(:reset) 257: @connection.reset 258: configure_connection 259: else 260: disconnect! 261: connect 262: end 263: end
Drops an index from a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 642 642: def remove_index(table_name, options = {}) 643: execute "DROP INDEX #{index_name(table_name, options)}" 644: end
Renames a column in a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 637 637: def rename_column(table_name, column_name, new_column_name) 638: execute "ALTER TABLE #{table_name} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}" 639: end
Renames a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 589 589: def rename_table(name, new_name) 590: execute "ALTER TABLE #{name} RENAME TO #{new_name}" 591: end
Aborts a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 432 432: def rollback_db_transaction 433: execute "ROLLBACK" 434: end
Returns the active schema search path.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 505 505: def schema_search_path 506: @schema_search_path ||= query('SHOW search_path')[0][0] 507: 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.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 497 497: def schema_search_path=(schema_csv) 498: if schema_csv 499: execute "SET search_path TO #{schema_csv}" 500: @schema_search_path = schema_csv 501: end 502: end
Executes a SELECT query and returns an array of rows. Each row is an array of field values.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 383 383: def select_rows(sql, name = nil) 384: select_raw(sql, name).last 385: end
Does PostgreSQL support migrations?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 288 288: def supports_migrations? 289: true 290: end
Does PostgreSQL support standard conforming strings?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 293 293: def supports_standard_conforming_strings? 294: # Temporarily set the client message level above error to prevent unintentional 295: # error messages in the logs when working on a PostgreSQL database server that 296: # does not support standard conforming strings. 297: client_min_messages_old = client_min_messages 298: self.client_min_messages = 'panic' 299: 300: # postgres-pr does not raise an exception when client_min_messages is set higher 301: # than error and "SHOW standard_conforming_strings" fails, but returns an empty 302: # PGresult instead. 303: has_support = execute('SHOW standard_conforming_strings')[0][0] rescue false 304: self.client_min_messages = client_min_messages_old 305: has_support 306: end
Returns the configured supported identifier length supported by PostgreSQL, or report the default of 63 on PostgreSQL 7.x.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 310 310: def table_alias_length 311: @table_alias_length ||= (postgresql_version >= 80000 ? query('SHOW max_identifier_length')[0][0].to_i : 63) 312: end
Returns the list of all tables in the schema search path or a specified schema.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 439 439: def tables(name = nil) 440: schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') 441: query("SELECT tablename\nFROM pg_tables\nWHERE schemaname IN (\#{schemas})\n", name).map { |row| row[0] } 442: end
Maps logical Rails types to PostgreSQL-specific data types.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 647 647: def type_to_sql(type, limit = nil, precision = nil, scale = nil) 648: return super unless type.to_s == 'integer' 649: 650: if limit.nil? || limit == 4 651: 'integer' 652: elsif limit < 4 653: 'smallint' 654: else 655: 'bigint' 656: end 657: end
Executes an UPDATE query and returns the number of affected tuples.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 417 417: def update_sql(sql, name = nil) 418: super.cmdtuples 419: end
Returns the version of the connected PostgreSQL version.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 696 696: def postgresql_version 697: @postgresql_version ||= 698: if @connection.respond_to?(:server_version) 699: @connection.server_version 700: else 701: # Mimic PGconn.server_version behavior 702: begin 703: query('SELECT version()')[0][0] =~ /PostgreSQL (\d+)\.(\d+)\.(\d+)/ 704: ($1.to_i * 10000) + ($2.to_i * 100) + $3.to_i 705: rescue 706: 0 707: end 708: end 709: end