| 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 247
247: def initialize(connection, logger, connection_parameters, config)
248: super(connection, logger)
249: @connection_parameters, @config = connection_parameters, config
250:
251: connect
252: end
Is this connection alive and ready for queries?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 255
255: def active?
256: if @connection.respond_to?(:status)
257: @connection.status == PGconn::CONNECTION_OK
258: else
259: # We're asking the driver, not ActiveRecord, so use @connection.query instead of #query
260: @connection.query 'SELECT 1'
261: true
262: end
263: # postgres-pr raises a NoMethodError when querying if no connection is available.
264: rescue PGError, NoMethodError
265: false
266: end
Returns ‘PostgreSQL’ as adapter name for identification purposes.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 242
242: def adapter_name
243: 'PostgreSQL'
244: end
Adds a new column to the named table. See TableDefinition#column for details of the options you can use.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 689
689: def add_column(table_name, column_name, type, options = {})
690: default = options[:default]
691: notnull = options[:null] == false
692:
693: # Add the column.
694: 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])}")
695:
696: change_column_default(table_name, column_name, default) if options_include_default?(options)
697: change_column_null(table_name, column_name, false, default) if notnull
698: end
Begins a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 461
461: def begin_db_transaction
462: execute "BEGIN"
463: end
Changes the column of a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 701
701: def change_column(table_name, column_name, type, options = {})
702: quoted_table_name = quote_table_name(table_name)
703:
704: begin
705: execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
706: rescue ActiveRecord::StatementInvalid
707: # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it.
708: begin
709: begin_db_transaction
710: tmp_column_name = "#{column_name}_ar_tmp"
711: add_column(table_name, tmp_column_name, type, options)
712: 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])})"
713: remove_column(table_name, column_name)
714: rename_column(table_name, tmp_column_name, column_name)
715: commit_db_transaction
716: rescue
717: rollback_db_transaction
718: end
719: end
720:
721: change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
722: change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
723: end
Changes the default value of a table column.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 726
726: def change_column_default(table_name, column_name, default)
727: execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
728: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 730
730: def change_column_null(table_name, column_name, null, default = nil)
731: unless null || default.nil?
732: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
733: end
734: execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
735: end
Returns the current client message level.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 595
595: def client_min_messages
596: query('SHOW client_min_messages')[0][0]
597: end
Set the client message level.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 600
600: def client_min_messages=(level)
601: execute("SET client_min_messages TO '#{level}'")
602: end
Returns the list of all column definitions for a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 570
570: def columns(table_name, name = nil)
571: # Limit, precision, and scale are all handled by the superclass.
572: column_definitions(table_name).collect do |name, type, default, notnull|
573: PostgreSQLColumn.new(name, default, type, notnull == 'f')
574: end
575: end
Commits a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 466
466: def commit_db_transaction
467: execute "COMMIT"
468: 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'
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 489
489: def create_database(name, options = {})
490: options = options.reverse_merge(:encoding => "utf8")
491:
492: option_string = options.symbolize_keys.sum do |key, value|
493: case key
494: when :owner
495: " OWNER = '#{value}'"
496: when :template
497: " TEMPLATE = #{value}"
498: when :encoding
499: " ENCODING = '#{value}'"
500: when :tablespace
501: " TABLESPACE = #{value}"
502: when :connection_limit
503: " CONNECTION LIMIT = #{value}"
504: else
505: ""
506: end
507: end
508:
509: execute "CREATE DATABASE #{name}#{option_string}"
510: end
Close the connection.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 280
280: def disconnect!
281: @connection.close rescue nil
282: 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 445
445: def execute(sql, name = nil)
446: log(sql, name) do
447: if @async
448: @connection.async_exec(sql)
449: else
450: @connection.exec(sql)
451: end
452: end
453: end
Returns the list of all indexes for a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 533
533: def indexes(table_name, name = nil)
534: schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
535: result = query("SELECT distinct 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 i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (\#{schemas}) )\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)
536:
537: current_index = nil
538: indexes = []
539:
540: result.each do |row|
541: if current_index != row[0]
542: indexes << IndexDefinition.new(table_name, row[0], row[1] == "t", [])
543: current_index = row[0]
544: end
545:
546: indexes.last.columns << row[2]
547: end
548:
549: indexes
550: 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 413
413: def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
414: table = sql.split(" ", 4)[2].gsub('"', '')
415: super || pk && last_insert_id(table, sequence_name || default_sequence_name(table, pk))
416: end
Close then reopen the connection.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 269
269: def reconnect!
270: if @connection.respond_to?(:reset)
271: @connection.reset
272: configure_connection
273: else
274: disconnect!
275: connect
276: end
277: end
Drops an index from a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 743
743: def remove_index(table_name, options = {})
744: execute "DROP INDEX #{index_name(table_name, options)}"
745: end
Renames a column in a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 738
738: def rename_column(table_name, column_name, new_column_name)
739: execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
740: end
Renames a table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 683
683: def rename_table(name, new_name)
684: execute "ALTER TABLE #{name} RENAME TO #{new_name}"
685: end
Aborts a transaction.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 471
471: def rollback_db_transaction
472: execute "ROLLBACK"
473: end
Returns the active schema search path.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 590
590: def schema_search_path
591: @schema_search_path ||= query('SHOW search_path')[0][0]
592: 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 582
582: def schema_search_path=(schema_csv)
583: if schema_csv
584: execute "SET search_path TO #{schema_csv}"
585: @schema_search_path = schema_csv
586: end
587: 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 408
408: def select_rows(sql, name = nil)
409: select_raw(sql, name).last
410: end
Does PostgreSQL support migrations?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 302
302: def supports_migrations?
303: true
304: end
Does PostgreSQL support standard conforming strings?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 307
307: def supports_standard_conforming_strings?
308: # Temporarily set the client message level above error to prevent unintentional
309: # error messages in the logs when working on a PostgreSQL database server that
310: # does not support standard conforming strings.
311: client_min_messages_old = client_min_messages
312: self.client_min_messages = 'panic'
313:
314: # postgres-pr does not raise an exception when client_min_messages is set higher
315: # than error and "SHOW standard_conforming_strings" fails, but returns an empty
316: # PGresult instead.
317: has_support = query('SHOW standard_conforming_strings')[0][0] rescue false
318: self.client_min_messages = client_min_messages_old
319: has_support
320: 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 324
324: def table_alias_length
325: @table_alias_length ||= (postgresql_version >= 80000 ? query('SHOW max_identifier_length')[0][0].to_i : 63)
326: 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 522
522: def tables(name = nil)
523: schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
524: query("SELECT tablename\nFROM pg_tables\nWHERE schemaname IN (\#{schemas})\n", name).map { |row| row[0] }
525: end
Maps logical Rails types to PostgreSQL-specific data types.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 748
748: def type_to_sql(type, limit = nil, precision = nil, scale = nil)
749: return super unless type.to_s == 'integer'
750:
751: if limit.nil? || limit == 4
752: 'integer'
753: elsif limit < 4
754: 'smallint'
755: else
756: 'bigint'
757: end
758: 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 456
456: def update_sql(sql, name = nil)
457: super.cmd_tuples
458: end
Returns the version of the connected PostgreSQL version.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 797
797: def postgresql_version
798: @postgresql_version ||=
799: if @connection.respond_to?(:server_version)
800: @connection.server_version
801: else
802: # Mimic PGconn.server_version behavior
803: begin
804: query('SELECT version()')[0][0] =~ /PostgreSQL (\d+)\.(\d+)\.(\d+)/
805: ($1.to_i * 10000) + ($2.to_i * 100) + $3.to_i
806: rescue
807: 0
808: end
809: end
810: end