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 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:
BYTEA_COLUMN_TYPE_OID | = | 17 |
NUMERIC_COLUMN_TYPE_OID | = | 1700 |
TIMESTAMPOID | = | 1114 |
TIMESTAMPTZOID | = | 1184 |
update | -> | delete |
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 55 55: def initialize(connection, logger, config = {}) 56: super(connection, logger) 57: @config = config 58: @async = config[:allow_concurrency] 59: configure_connection 60: end
Is this connection alive and ready for queries?
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 63 63: def active? 64: if @connection.respond_to?(:status) 65: @connection.status == PGconn::CONNECTION_OK 66: else 67: @connection.query 'SELECT 1' 68: true 69: end 70: # postgres-pr raises a NoMethodError when querying if no conn is available 71: rescue PGError, NoMethodError 72: false 73: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 51 51: def adapter_name 52: 'PostgreSQL' 53: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 334 334: def add_column(table_name, column_name, type, options = {}) 335: default = options[:default] 336: notnull = options[:null] == false 337: 338: # Add the column. 339: execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit])}") 340: 341: # Set optional default. If not null, update nulls to the new default. 342: unless default.nil? 343: change_column_default(table_name, column_name, default) 344: if notnull 345: execute("UPDATE #{table_name} SET #{column_name}='#{default}' WHERE #{column_name} IS NULL") 346: end 347: end 348: 349: if notnull 350: execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL") 351: end 352: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 258 258: def default_sequence_name(table_name, pk = nil) 259: default_pk, default_seq = pk_and_sequence_for(table_name) 260: default_seq || "#{table_name}_#{pk || default_pk || 'id'}_seq" 261: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 84 84: def disconnect! 85: # Both postgres and postgres-pr respond to :close 86: @connection.close rescue nil 87: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 89 89: def native_database_types 90: { 91: :primary_key => "serial primary key", 92: :string => { :name => "character varying", :limit => 255 }, 93: :text => { :name => "text" }, 94: :integer => { :name => "integer" }, 95: :float => { :name => "float" }, 96: :decimal => { :name => "decimal" }, 97: :datetime => { :name => "timestamp" }, 98: :timestamp => { :name => "timestamp" }, 99: :time => { :name => "time" }, 100: :date => { :name => "date" }, 101: :binary => { :name => "bytea" }, 102: :boolean => { :name => "boolean" } 103: } 104: end
Find a table’s primary key and sequence.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 283 283: def pk_and_sequence_for(table) 284: # First try looking for a sequence with a dependency on the 285: # given table's primary key. 286: result = query("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] 287: 288: if result.nil? or result.empty? 289: # If that fails, try parsing the primary key's default value. 290: # Support the 7.x and 8.0 nextval('foo'::text) as well as 291: # the 8.1+ nextval('foo'::regclass). 292: # TODO: assumes sequence is in same schema as table. 293: result = query("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] 294: end 295: # check for existence of . in sequence name as in public.foo_sequence. if it does not exist, return unqualified sequence 296: # We cannot qualify unqualified sequences, as rails doesn't qualify any table access, using the search path 297: [result.first, result.last] 298: rescue 299: nil 300: end
QUOTING ==================================================
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 116 116: def quote(value, column = nil) 117: if value.kind_of?(String) && column && column.type == :binary 118: "'#{escape_bytea(value)}'" 119: else 120: super 121: end 122: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 124 124: def quote_column_name(name) 125: %("#{name}") 126: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 128 128: def quoted_date(value) 129: value.strftime("%Y-%m-%d %H:%M:%S.#{sprintf("%06d", value.usec)}") 130: end
Close then reopen the connection.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 76 76: def reconnect! 77: # TODO: postgres-pr doesn't have PGconn#reset. 78: if @connection.respond_to?(:reset) 79: @connection.reset 80: configure_connection 81: end 82: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 330 330: def rename_table(name, new_name) 331: execute "ALTER TABLE #{name} RENAME TO #{new_name}" 332: end
Resets sequence to the max value of the table’s pk if present.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 264 264: def reset_pk_sequence!(table, pk = nil, sequence = nil) 265: unless pk and sequence 266: default_pk, default_sequence = pk_and_sequence_for(table) 267: pk ||= default_pk 268: sequence ||= default_sequence 269: end 270: if pk 271: if sequence 272: 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' 273: else 274: @logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger 275: end 276: end 277: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 106 106: def supports_migrations? 107: true 108: end