Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
AbstractAdapter
  • Object
show all
Includes:
ActiveRecord::ConnectionAdapters::PostgreSQL::Quoting, ActiveRecord::ConnectionAdapters::PostgreSQL::ReferentialIntegrity, ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements, ArJdbc::Abstract::ConnectionManagement, ArJdbc::Abstract::Core, ArJdbc::Abstract::DatabaseStatements, ArJdbc::Abstract::StatementCache, ArJdbc::Abstract::TransactionSupport, ArJdbc::PostgreSQL, ArJdbc::PostgreSQL::DatabaseStatements, ArJdbc::PostgreSQL::SchemaStatements, ArJdbc::Util::QuotedCache
Defined in:
lib/arjdbc/postgresql/adapter.rb

Constant Summary collapse

OID =

AR expects OID to be available on the adapter

ActiveRecord::ConnectionAdapters::PostgreSQL::OID
ColumnMethods =
ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods
TableDefinition =
ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition
Table =
ActiveRecord::ConnectionAdapters::PostgreSQL::Table

Constants included from ArJdbc::PostgreSQL::SchemaStatements

ArJdbc::PostgreSQL::SchemaStatements::ForeignKeyDefinition, ArJdbc::PostgreSQL::SchemaStatements::Utils

Constants included from ArJdbc::PostgreSQL

ArJdbc::PostgreSQL::ADAPTER_NAME, ArJdbc::PostgreSQL::NATIVE_DATABASE_TYPES

Constants included from ArJdbc::Abstract::DatabaseStatements

ArJdbc::Abstract::DatabaseStatements::NO_BINDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArJdbc::Util::QuotedCache

#quote_column_name, #quote_table_name

Methods included from ArJdbc::PostgreSQL::SchemaStatements

#foreign_keys

Methods included from ArJdbc::PostgreSQL::DatabaseStatements

#build_explain_clause, #explain

Methods included from ArJdbc::PostgreSQL

#adapter_name, #add_enum_value, #add_order_by_for_association_limiting!, #all_schemas, #build_insert_sql, #build_truncate_statements, #check_version, #client_min_messages, #client_min_messages=, #configure_connection, #create_enum, #default_index_type?, #default_sequence_name, #disable_extension, #disconnect!, #drop_enum, #enable_extension, #enum_types, #escape_bytea, #exec_insert, #execute_batch, #extension_available?, #extension_enabled?, #extensions, #get_advisory_lock, #get_database_version, #index_algorithms, #jdbc_column_class, #last_insert_id_result, #max_identifier_length, #quote_table_name, #release_advisory_lock, #remove_column, #rename_enum, #rename_enum_value, #reset!, #session_auth=, #set_client_encoding, #set_standard_conforming_strings, #supports_advisory_locks?, #supports_bulk_alter?, #supports_check_constraints?, #supports_comments?, #supports_common_table_expressions?, #supports_datetime_with_precision?, #supports_ddl_transactions?, #supports_explain?, #supports_expression_index?, #supports_extensions?, #supports_foreign_keys?, #supports_foreign_tables?, #supports_identity_columns?, #supports_index_sort_order?, #supports_insert_on_conflict?, #supports_insert_returning?, #supports_json?, #supports_lazy_transactions?, #supports_materialized_views?, #supports_nulls_not_distinct?, #supports_optimizer_hints?, #supports_partial_index?, #supports_partitioned_indexes?, #supports_pgcrypto_uuid?, #supports_ranges?, #supports_savepoints?, #supports_transaction_isolation?, #supports_validate_constraints?, #supports_views?, #supports_virtual_columns?, #use_insert_returning?, #write_query?

Methods included from ArJdbc::Abstract::TransactionSupport

#begin_db_transaction, #begin_isolated_db_transaction, #commit_db_transaction, #create_savepoint, #exec_rollback_db_transaction, #exec_rollback_to_savepoint, #release_savepoint, #supports_savepoints?, #supports_transaction_isolation?

Methods included from ArJdbc::Abstract::StatementCache

#delete_cached_statement, #fetch_cached_statement

Methods included from ArJdbc::Abstract::DatabaseStatements

#exec_insert, #exec_update, #internal_exec_query, #select_all

Methods included from ArJdbc::Abstract::ConnectionManagement

#active?, #disconnect!, #really_valid?

Methods included from ArJdbc::Abstract::Core

#jdbc_connection

Constructor Details

#initializePostgreSQLAdapter

Returns a new instance of PostgreSQLAdapter.



900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/arjdbc/postgresql/adapter.rb', line 900

def initialize(...)
  super

  conn_params = @config.compact

  @connection_parameters = conn_params

  # @local_tz is initialized as nil to avoid warnings when connect tries to use it
  @local_tz = nil
  @max_identifier_length = nil

  @use_insert_returning = @config.key?(:insert_returning) ?
    self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


915
916
917
918
919
920
921
922
# File 'lib/arjdbc/postgresql/adapter.rb', line 915

def self.database_exists?(config)
  conn = ActiveRecord::Base.postgresql_connection(config)
  conn && conn.really_valid?
rescue ActiveRecord::NoDatabaseError
  false
ensure
  conn.disconnect! if conn
end

.dbconsole(config, options = {}) ⇒ Object



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/arjdbc/postgresql/adapter.rb', line 880

def dbconsole(config, options = {})
  pg_config = config.configuration_hash

  ENV["PGUSER"]         = pg_config[:username] if pg_config[:username]
  ENV["PGHOST"]         = pg_config[:host] if pg_config[:host]
  ENV["PGPORT"]         = pg_config[:port].to_s if pg_config[:port]
  ENV["PGPASSWORD"]     = pg_config[:password].to_s if pg_config[:password] && options[:include_password]
  ENV["PGSSLMODE"]      = pg_config[:sslmode].to_s if pg_config[:sslmode]
  ENV["PGSSLCERT"]      = pg_config[:sslcert].to_s if pg_config[:sslcert]
  ENV["PGSSLKEY"]       = pg_config[:sslkey].to_s if pg_config[:sslkey]
  ENV["PGSSLROOTCERT"]  = pg_config[:sslrootcert].to_s if pg_config[:sslrootcert]
  if pg_config[:variables]
    ENV["PGOPTIONS"] = pg_config[:variables].filter_map do |name, value|
      "-c #{name}=#{value.to_s.gsub(/[ \\]/, '\\\\\0')}" unless value == ":default" || value == :default
    end.join(" ")
  end
  find_cmd_and_exec("psql", config.database)
end

.jdbc_connection_classObject



872
873
874
# File 'lib/arjdbc/postgresql/adapter.rb', line 872

def jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::PostgreSQLJdbcConnection
end

.native_database_typesObject

:nodoc:



937
938
939
940
941
942
943
# File 'lib/arjdbc/postgresql/adapter.rb', line 937

def self.native_database_types # :nodoc:
  @native_database_types ||= begin
    types = NATIVE_DATABASE_TYPES.dup
    types[:datetime] = types[datetime_type]
    types
  end
end

.new_client(conn_params, adapter_instance) ⇒ Object



876
877
878
# File 'lib/arjdbc/postgresql/adapter.rb', line 876

def new_client(conn_params, adapter_instance)
  jdbc_connection_class.new(conn_params, adapter_instance)
end

Instance Method Details

#create_unlogged_tablesObject

:singleton-method: PostgreSQL allows the creation of "unlogged" tables, which do not record data in the PostgreSQL Write-Ahead Log. This can make the tables faster, but significantly increases the risk of data loss if the database crashes. As a result, this should not be used in production environments. If you would like all created tables to be unlogged in the test environment you can add the following line to your test.rb file:

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true



824
# File 'lib/arjdbc/postgresql/adapter.rb', line 824

class_attribute :create_unlogged_tables, default: false

#datetime_typeObject

:singleton-method: PostgreSQL supports multiple types for DateTimes. By default, if you use +datetime+ in migrations, Rails will translate this to a PostgreSQL "timestamp without time zone". Change this in an initializer to use another NATIVE_DATABASE_TYPES. For example, to store DateTimes as "timestamp with time zone":

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :timestamptz

Or if you are adding a custom type:

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:my_custom_type] = { name: "my_custom_type_name" } ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :my_custom_type

If you're using +:ruby+ as your +config.active_record.schema_format+ and you change this setting, you should immediately run bin/rails db:migrate to update the types in your schema.rb.



842
# File 'lib/arjdbc/postgresql/adapter.rb', line 842

class_attribute :datetime_type, default: :timestamp

#native_database_typesObject

:nodoc:



933
934
935
# File 'lib/arjdbc/postgresql/adapter.rb', line 933

def native_database_types # :nodoc:
  self.class.native_database_types
end