1 require 'active_record/connection_adapters/abstract_adapter'
4 require_library_or_gem
'pg'
7 require_library_or_gem
'postgres'
9 alias_method
:nfields, :num_fields unless self.method_defined
?(:nfields)
10 alias_method
:ntuples, :num_tuples unless self.method_defined
?(:ntuples)
11 alias_method
:ftype, :type unless self.method_defined
?(:ftype)
12 alias_method
:cmd_tuples, :cmdtuples unless self.method_defined
?(:cmd_tuples)
21 # Establishes a connection to the database that's used by all Active Record objects
22 def self.postgresql_connection(config
) # :nodoc:
23 config
= config
.symbolize_keys
25 port
= config
[:port] || 5432
26 username
= config
[:username].to_s
if config
[:username]
27 password
= config
[:password].to_s
if config
[:password]
29 if config
.has_key
?(:database)
30 database
= config
[:database]
32 raise ArgumentError
, "No database specified. Missing argument: database."
35 # The postgres drivers don't allow the creation of an unconnected PGconn object,
36 # so just pass a nil connection object for the time being.
37 ConnectionAdapters
::PostgreSQLAdapter.new(nil, logger
, [host
, port
, nil, nil, database
, username
, password
], config
)
41 module ConnectionAdapters
42 # PostgreSQL-specific extensions to column definitions in a table.
43 class PostgreSQLColumn
< Column
#:nodoc:
44 # Instantiates a new PostgreSQL column definition in a table.
45 def initialize(name
, default
, sql_type
= nil, null
= true)
46 super(name
, self.class.extract_value_from_default(default
), sql_type
, null
)
50 def extract_limit(sql_type
)
58 # Extracts the scale from PostgreSQL-specific data types.
59 def extract_scale(sql_type
)
60 # Money type has a fixed scale of 2.
61 sql_type
=~
/^money/ ? 2 : super
64 # Extracts the precision from PostgreSQL-specific data types.
65 def extract_precision(sql_type
)
66 # Actual code is defined dynamically in PostgreSQLAdapter.connect
67 # depending on the server specifics
71 # Maps PostgreSQL-specific data types to logical Rails types.
72 def simplified_type(field_type
)
74 # Numeric and monetary types
75 when /^(?:real|double precision)$/
81 when /^(?:character varying|bpchar)(?:\(\d+\))?$/
87 when /^timestamp with(?:out)? time zone$/
92 when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
94 # Network address types
95 when /^(?:cidr|inet|macaddr)$/
98 when /^bit(?: varying)?(?:\(\d+\))?$/
106 # Object identifier types
109 # Pass through all types that are not specific to PostgreSQL.
115 # Extracts the value from a PostgreSQL column default definition.
116 def self.extract_value_from_default(default
)
119 when /\A\(?(-?\d+(\.\d*)?\)?)\z/
122 when /\A'(.*)'::(?:character varying|bpchar|text)\z/m
124 # Character types (8.1 formatting)
125 when /\AE'(.*)'::(?:character varying|bpchar|text)\z/m
126 $1.gsub(/\\(\d\d\d)/) { $1.oct
.chr
}
128 when /\A'(.*)'::bytea\z/m
131 when /\A'(.+)'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/
133 when /\A'(.*)'::interval\z/
141 when /\A'(.*)'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/
143 # Network address types
144 when /\A'(.*)'::(?:cidr|inet|macaddr)\z/
147 when /\AB'(.*)'::"?bit(?: varying)?"?\z/
150 when /\A'(.*)'::xml\z/m
153 when /\A'(.*)'::"?\D+"?\[\]\z/
155 # Object identifier types
159 # Anything else is blank, some user type, or some function
160 # and we can't know the value of that, so return nil.
167 module ConnectionAdapters
168 # The PostgreSQL adapter works both with the native C (http://ruby.scripting.ca/postgres/) and the pure
169 # Ruby (available both as gem and from http://rubyforge.org/frs/?group_id=234&release_id=1944) drivers.
173 # * <tt>:host</tt> - Defaults to "localhost".
174 # * <tt>:port</tt> - Defaults to 5432.
175 # * <tt>:username</tt> - Defaults to nothing.
176 # * <tt>:password</tt> - Defaults to nothing.
177 # * <tt>:database</tt> - The name of the database. No default, must be provided.
178 # * <tt>:schema_search_path</tt> - An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the <tt>:schema_order</tt> option.
179 # * <tt>:encoding</tt> - An optional client encoding that is used in a <tt>SET client_encoding TO <encoding></tt> call on the connection.
180 # * <tt>:min_messages</tt> - An optional client min messages that is used in a <tt>SET client_min_messages TO <min_messages></tt> call on the connection.
181 # * <tt>:allow_concurrency</tt> - If true, use async query methods so Ruby threads don't deadlock; otherwise, use blocking query methods.
182 class PostgreSQLAdapter
< AbstractAdapter
183 ADAPTER_NAME
= 'PostgreSQL'.freeze
185 NATIVE_DATABASE_TYPES
= {
186 :primary_key => "serial primary key".freeze
,
187 :string => { :name => "character varying", :limit => 255 },
188 :text => { :name => "text" },
189 :integer => { :name => "integer" },
190 :float => { :name => "float" },
191 :decimal => { :name => "decimal" },
192 :datetime => { :name => "timestamp" },
193 :timestamp => { :name => "timestamp" },
194 :time => { :name => "time" },
195 :date => { :name => "date" },
196 :binary => { :name => "bytea" },
197 :boolean => { :name => "boolean" }
200 # Returns 'PostgreSQL' as adapter name for identification purposes.
205 # Initializes and connects a PostgreSQL adapter.
206 def initialize(connection
, logger
, connection_parameters
, config
)
207 super(connection
, logger
)
208 @connection_parameters, @config = connection_parameters
, config
213 # Is this connection alive and ready for queries?
215 if @connection.respond_to
?(:status)
216 @connection.status
== PGconn
::CONNECTION_OK
218 # We're asking the driver, not ActiveRecord, so use @connection.query instead of #query
219 @connection.query
'SELECT 1'
222 # postgres-pr raises a NoMethodError when querying if no connection is available.
223 rescue PGError
, NoMethodError
227 # Close then reopen the connection.
229 if @connection.respond_to
?(:reset)
238 # Close the connection.
240 @connection.close
rescue nil
243 def native_database_types
#:nodoc:
244 NATIVE_DATABASE_TYPES
247 # Does PostgreSQL support migrations?
248 def supports_migrations
?
252 # Does PostgreSQL support standard conforming strings?
253 def supports_standard_conforming_strings
?
254 # Temporarily set the client message level above error to prevent unintentional
255 # error messages in the logs when working on a PostgreSQL database server that
256 # does not support standard conforming strings.
257 client_min_messages_old
= client_min_messages
258 self.client_min_messages
= 'panic'
260 # postgres-pr does not raise an exception when client_min_messages is set higher
261 # than error and "SHOW standard_conforming_strings" fails, but returns an empty
263 has_support
= query('SHOW standard_conforming_strings')[0][0] rescue false
264 self.client_min_messages
= client_min_messages_old
268 def supports_insert_with_returning
?
269 postgresql_version
>= 80200
272 def supports_ddl_transactions
?
276 # Returns the configured supported identifier length supported by PostgreSQL,
277 # or report the default of 63 on PostgreSQL 7.x.
278 def table_alias_length
279 @table_alias_length ||= (postgresql_version
>= 80000 ? query('SHOW max_identifier_length')[0][0].to_i
: 63)
282 # QUOTING ==================================================
284 # Escapes binary strings for bytea input to the database.
285 def escape_bytea(value
)
286 if PGconn
.respond_to
?(:escape_bytea)
287 self.class.instance_eval
do
288 define_method(:escape_bytea) do |value
|
289 PGconn
.escape_bytea(value
) if value
293 self.class.instance_eval
do
294 define_method(:escape_bytea) do |value
|
297 value
.each_byte
{ |c
| result
<< sprintf('\\\\%03o', c
) }
306 # Unescapes bytea output from a database to the binary string it represents.
307 # NOTE: This is NOT an inverse of escape_bytea! This is only to be used
308 # on escaped binary output from database drive.
309 def unescape_bytea(value
)
310 # In each case, check if the value actually is escaped PostgreSQL bytea output
311 # or an unescaped Active Record attribute that was just written.
312 if PGconn
.respond_to
?(:unescape_bytea)
313 self.class.instance_eval
do
314 define_method(:unescape_bytea) do |value
|
315 if value
=~
/\\\d{3}/
316 PGconn
.unescape_bytea(value
)
323 self.class.instance_eval
do
324 define_method(:unescape_bytea) do |value
|
325 if value
=~
/\\\d{3}/
327 i
, max
= 0, value
.size
335 char
= value
[i
+1..i
+3].oct
349 unescape_bytea(value
)
352 # Quotes PostgreSQL-specific data types for SQL input.
353 def quote(value
, column
= nil) #:nodoc:
354 if value
.kind_of
?(String
) && column
&& column
.type
== :binary
355 "#{quoted_string_prefix}'#{escape_bytea(value)}'"
356 elsif value
.kind_of
?(String
) && column
&& column
.sql_type
=~
/^xml$/
357 "xml '#{quote_string(value)}'"
358 elsif value
.kind_of
?(Numeric
) && column
&& column
.sql_type
=~
/^money$/
359 # Not truly string input, so doesn't require (or allow) escape string syntax.
361 elsif value
.kind_of
?(String
) && column
&& column
.sql_type
=~
/^bit/
364 "B'#{value}'" # Bit-string notation
366 "X'#{value}'" # Hexadecimal notation
373 # Quotes strings for use in SQL input in the postgres driver for better performance.
374 def quote_string(s
) #:nodoc:
375 if PGconn
.respond_to
?(:escape)
376 self.class.instance_eval
do
377 define_method(:quote_string) do |s
|
382 # There are some incorrectly compiled postgres drivers out there
383 # that don't define PGconn.escape.
384 self.class.instance_eval
do
385 remove_method(:quote_string)
391 # Quotes column names for use in SQL queries.
392 def quote_column_name(name
) #:nodoc:
396 # Quote date/time values for use in SQL input. Includes microseconds
397 # if the value is a Time responding to usec.
398 def quoted_date(value) #:nodoc:
399 if value.acts_like?(:time) && value.respond_to?(:usec)
400 "#{super}.#{sprintf("%06d", value.usec)}"
406 # REFERENTIAL INTEGRITY ====================================
408 def supports_disable_referential_integrity?() #:nodoc:
409 version = query("SHOW server_version")[0][0].split('.')
410 (version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
415 def disable_referential_integrity(&block) #:nodoc:
416 if supports_disable_referential_integrity?() then
417 execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
421 if supports_disable_referential_integrity?() then
422 execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
426 # DATABASE STATEMENTS ======================================
428 # Executes a SELECT query and returns an array of rows. Each row is an
429 # array of field values.
430 def select_rows(sql, name = nil)
431 select_raw(sql, name).last
434 # Executes an INSERT query and returns the new record's ID
435 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
436 # Extract the table from the insert sql. Yuck.
437 table = sql.split(" ", 4)[2].gsub('"', '')
439 # Try an insert with 'returning id' if available (PG >= 8.2)
440 if supports_insert_with_returning?
441 pk, sequence_name = *pk_and_sequence_for(table) unless pk
443 id = select_value("#{sql} RETURNING #{quote_column_name(pk)}")
449 # Otherwise, insert then grab last_insert_id.
453 # If neither pk nor sequence name is given, look them up.
454 unless pk || sequence_name
455 pk, sequence_name = *pk_and_sequence_for(table)
458 # If a pk is given, fallback to default sequence name.
459 # Don't fetch last insert id for a table without a pk.
460 if pk && sequence_name ||= default_sequence_name(table, pk)
461 last_insert_id(table, sequence_name)
466 # create a 2D array representing the result set
467 def result_as_array(res) #:nodoc:
468 # check if we have any binary column and if they need escaping
470 for j in 0...res.nfields do
471 # unescape string passed BYTEA field (OID == 17)
472 unescape_col << ( res.ftype(j)==17 )
476 for i in 0...res.ntuples do
478 for j in 0...res.nfields do
479 data = res.getvalue(i,j)
480 data = unescape_bytea(data) if unescape_col[j] and data.is_a?(String)
488 # Queries the database and returns the results in an Array-like object
489 def query(sql, name = nil) #:nodoc:
492 res = @connection.async_exec(sql)
494 res = @connection.exec(sql)
496 return result_as_array(res)
500 # Executes an SQL statement, returning a PGresult object on success
501 # or raising a PGError exception otherwise.
502 def execute(sql, name = nil)
505 @connection.async_exec(sql)
507 @connection.exec(sql)
512 # Executes an UPDATE query and returns the number of affected tuples.
513 def update_sql(sql, name = nil)
517 # Begins a transaction.
518 def begin_db_transaction
522 # Commits a transaction.
523 def commit_db_transaction
527 # Aborts a transaction.
528 def rollback_db_transaction
532 # ruby-pg defines Ruby constants for transaction status,
533 # ruby-postgres does not.
534 PQTRANS_IDLE = defined?(PGconn::PQTRANS_IDLE) ? PGconn::PQTRANS_IDLE : 0
536 # Check whether a transaction is active.
537 def transaction_active?
538 @connection.transaction_status != PQTRANS_IDLE
541 # Wrap a block in a transaction. Returns result of block.
542 def transaction(start_db_transaction = true)
543 transaction_open = false
546 if start_db_transaction
548 transaction_open = true
552 rescue Exception => database_transaction_rollback
553 if transaction_open && transaction_active?
554 transaction_open = false
555 rollback_db_transaction
557 raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
560 if transaction_open && transaction_active?
562 commit_db_transaction
563 rescue Exception => database_transaction_rollback
564 rollback_db_transaction
571 # SCHEMA STATEMENTS ========================================
573 def recreate_database(name) #:nodoc:
575 create_database(name)
578 # Create a new PostgreSQL database. Options include <tt>:owner</tt>, <tt>:template</tt>,
579 # <tt>:encoding</tt>, <tt>:tablespace</tt>, and <tt>:connection_limit</tt> (note that MySQL uses
580 # <tt>:charset</tt> while PostgreSQL uses <tt>:encoding</tt>).
583 # create_database config[:database], config
584 # create_database 'foo_development', :encoding => 'unicode'
585 def create_database(name, options = {})
586 options = options.reverse_merge(:encoding => "utf8")
588 option_string = options.symbolize_keys.sum do |key, value|
591 " OWNER = \"#{value}\""
593 " TEMPLATE = \"#{value}\""
595 " ENCODING = '#{value}'"
597 " TABLESPACE = \"#{value}\""
598 when :connection_limit
599 " CONNECTION LIMIT = #{value}"
605 execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
608 # Drops a PostgreSQL database
611 # drop_database 'matt_development'
612 def drop_database(name) #:nodoc:
613 if postgresql_version >= 80200
614 execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}"
617 execute "DROP DATABASE #{quote_table_name(name)}"
618 rescue ActiveRecord::StatementInvalid
619 @logger.warn "#{name} database doesn't exist." if @logger
625 # Returns the list of all tables in the schema search path or a specified schema.
626 def tables(name = nil)
627 schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
628 query(<<-SQL, name).map { |row| row[0] }
631 WHERE schemaname IN (#{schemas})
635 # Returns the list of all indexes for a table.
636 def indexes(table_name
, name
= nil)
637 schemas
= schema_search_path
.split(/,/).map
{ |p
| quote(p
) }.join(',')
638 result
= query(<<-SQL, name)
639 SELECT distinct i.relname, d.indisunique, a.attname
640 FROM pg_class t, pg_class i, pg_index d, pg_attribute a
641 WHERE i.relkind = 'i'
642 AND d.indexrelid = i.oid
643 AND d.indisprimary = 'f'
644 AND t.oid = d.indrelid
645 AND t.relname = '#{table_name}'
646 AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )
647 AND a.attrelid = t.oid
648 AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
649 OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
650 OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
651 OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
652 OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
660 if current_index
!= row
[0]
661 indexes
<< IndexDefinition
.new(table_name
, row
[0], row
[1] == "t", [])
662 current_index
= row
[0]
665 indexes
.last
.columns
<< row
[2]
671 # Returns the list of all column definitions for a table.
672 def columns(table_name
, name
= nil)
673 # Limit, precision, and scale are all handled by the superclass.
674 column_definitions(table_name
).collect
do |name
, type
, default
, notnull
|
675 PostgreSQLColumn
.new(name
, default
, type
, notnull
== 'f')
679 # Returns the current database name.
681 query('select current_database()')[0][0]
684 # Returns the current database encoding format.
686 query(<<-end_sql)[0][0]
687 SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database
688 WHERE pg_database.datname LIKE '#{current_database}'
692 # Sets the schema search path to a string of comma-separated schema names.
693 # Names beginning with $ have to be quoted (e.g. $user => '$user').
694 # See: http://www.postgresql.org/docs/current/static/ddl-schemas.html
696 # This should be not be called manually but set in database.yml.
697 def schema_search_path
=(schema_csv
)
699 execute
"SET search_path TO #{schema_csv}"
700 @schema_search_path = schema_csv
704 # Returns the active schema search path.
705 def schema_search_path
706 @schema_search_path ||= query('SHOW search_path')[0][0]
709 # Returns the current client message level.
710 def client_min_messages
711 query('SHOW client_min_messages')[0][0]
714 # Set the client message level.
715 def client_min_messages
=(level
)
716 execute("SET client_min_messages TO '#{level}'")
719 # Returns the sequence name for a table's primary key or some other specified key.
720 def default_sequence_name(table_name
, pk
= nil) #:nodoc:
721 default_pk
, default_seq
= pk_and_sequence_for(table_name
)
722 default_seq
|| "#{table_name}_#{pk || default_pk || 'id'}_seq"
725 # Resets the sequence of a table's primary key to the maximum value.
726 def reset_pk_sequence
!(table
, pk
= nil, sequence
= nil) #:nodoc:
727 unless pk
and sequence
728 default_pk
, default_sequence
= pk_and_sequence_for(table
)
730 sequence
||= default_sequence
734 quoted_sequence
= quote_column_name(sequence
)
736 select_value
<<-end_sql, 'Reset sequence'
737 SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
740 @logger.warn
"#{table} has primary key #{pk} with no default sequence" if @logger
745 # Returns a table's primary key and belonging sequence.
746 def pk_and_sequence_for(table
) #:nodoc:
747 # First try looking for a sequence with a dependency on the
748 # given table's primary key.
749 result
= query(<<-end_sql, 'PK and serial sequence')[0]
750 SELECT attr.attname, seq.relname
756 WHERE seq.oid = dep.objid
757 AND seq.relkind = 'S'
758 AND attr.attrelid = dep.refobjid
759 AND attr.attnum = dep.refobjsubid
760 AND attr.attrelid = cons.conrelid
761 AND attr.attnum = cons.conkey[1]
762 AND cons.contype = 'p'
763 AND dep.refobjid = '#{table}'::regclass
766 if result
.nil? or result
.empty
?
767 # If that fails, try parsing the primary key's default value.
768 # Support the 7.x and 8.0 nextval('foo'::text) as well as
769 # the 8.1+ nextval('foo'::regclass).
770 result
= query(<<-end_sql, 'PK and custom sequence')[0]
773 WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
774 substr(split_part(def.adsrc, '''', 2),
775 strpos(split_part(def.adsrc, '''', 2), '.')+1)
776 ELSE split_part(def.adsrc, '''', 2)
779 JOIN pg_attribute attr ON (t.oid = attrelid)
780 JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
781 JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
782 WHERE t.oid = '#{table}'::regclass
783 AND cons.contype = 'p'
784 AND def.adsrc ~* 'nextval'
788 # [primary_key, sequence]
789 [result
.first
, result
.last
]
795 def rename_table(name
, new_name
)
796 execute
"ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
799 # Adds a new column to the named table.
800 # See TableDefinition#column for details of the options you can use.
801 def add_column(table_name
, column_name
, type
, options
= {})
802 default
= options
[:default]
803 notnull
= options
[:null] == false
806 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])}")
808 change_column_default(table_name
, column_name
, default
) if options_include_default
?(options
)
809 change_column_null(table_name
, column_name
, false, default
) if notnull
812 # Changes the column of a table.
813 def change_column(table_name
, column_name
, type
, options
= {})
814 quoted_table_name
= quote_table_name(table_name
)
817 execute
"ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
818 rescue ActiveRecord
::StatementInvalid => e
819 raise e
if postgresql_version
> 80000
820 # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it.
823 tmp_column_name
= "#{column_name}_ar_tmp"
824 add_column(table_name
, tmp_column_name
, type
, options
)
825 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])})"
826 remove_column(table_name
, column_name
)
827 rename_column(table_name
, tmp_column_name
, column_name
)
828 commit_db_transaction
830 rollback_db_transaction
834 change_column_default(table_name
, column_name
, options
[:default]) if options_include_default
?(options
)
835 change_column_null(table_name
, column_name
, options
[:null], options
[:default]) if options
.key
?(:null)
838 # Changes the default value of a table column.
839 def change_column_default(table_name
, column_name
, default
)
840 execute
"ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}"
843 def change_column_null(table_name
, column_name
, null
, default
= nil)
844 unless null
|| default
.nil?
845 execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
847 execute("ALTER TABLE #{quote_table_name(table_name)} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL")
850 # Renames a column in a table.
851 def rename_column(table_name
, column_name
, new_column_name
)
852 execute
"ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}"
855 # Drops an index from a table.
856 def remove_index(table_name
, options
= {})
857 execute
"DROP INDEX #{index_name(table_name, options)}"
860 # Maps logical Rails types to PostgreSQL-specific data types.
861 def type_to_sql(type
, limit
= nil, precision
= nil, scale
= nil)
862 return super unless type
.to_s
== 'integer'
865 when 1..2; 'smallint'
866 when 3..4, nil; 'integer'
868 else raise(ActiveRecordError
, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
872 # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
874 # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
875 # requires that the ORDER BY include the distinct column.
877 # distinct("posts.id", "posts.created_at desc")
878 def distinct(columns
, order_by
) #:nodoc:
879 return "DISTINCT #{columns}" if order_by
.blank
?
881 # Construct a clean list of column names from the ORDER BY clause, removing
882 # any ASC/DESC modifiers
883 order_columns
= order_by
.split(',').collect
{ |s
| s
.split
.first
}
884 order_columns
.delete_if
&:blank?
885 order_columns
= order_columns
.zip((0...order_columns
.size
).to_a
).map
{ |s
,i
| "#{s} AS alias_#{i}" }
887 # Return a DISTINCT ON() clause that's distinct on the columns we want but includes
888 # all the required columns for the ORDER BY to work properly.
889 sql
= "DISTINCT ON (#{columns}) #{columns}, "
890 sql
<< order_columns
* ', '
893 # Returns an ORDER BY clause for the passed order option.
895 # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this
896 # by wrapping the +sql+ string as a sub-select and ordering in that query.
897 def add_order_by_for_association_limiting
!(sql
, options
) #:nodoc:
898 return sql
if options
[:order].blank
?
900 order
= options
[:order].split(',').collect
{ |s
| s
.strip
}.reject(&:blank?)
901 order
.map
! { |s
| 'DESC' if s
=~
/\bdesc$/i
}
902 order
= order
.zip((0...order
.size
).to_a
).map
{ |s
,i
| "id_list.alias_#{i} #{s}" }.join(', ')
904 sql
.replace
"SELECT * FROM (#{sql}) AS id_list ORDER BY #{order}"
908 # Returns the version of the connected PostgreSQL version.
909 def postgresql_version
910 @postgresql_version ||=
911 if @connection.respond_to
?(:server_version)
912 @connection.server_version
914 # Mimic PGconn.server_version behavior
916 query('SELECT version()')[0][0] =~
/PostgreSQL (\d+)\.(\d+)\.(\d+)/
917 ($1.to_i
* 10000) + ($2.to_i
* 100) + $3.to_i
925 # The internal PostgreSQL identifier of the money data type.
926 MONEY_COLUMN_TYPE_OID
= 790 #:nodoc:
928 # Connects to a PostgreSQL server and sets up the adapter depending on the
929 # connected server's characteristics.
931 @connection = PGconn
.connect(*@connection_parameters)
932 PGconn
.translate_results
= false if PGconn
.respond_to
?(:translate_results=)
934 # Ignore async_exec and async_query when using postgres-pr.
935 @async = @config[:allow_concurrency] && @connection.respond_to
?(:async_exec)
937 # Use escape string syntax if available. We cannot do this lazily when encountering
938 # the first string, because that could then break any transactions in progress.
939 # See: http://www.postgresql.org/docs/current/static/runtime-config-compatible.html
940 # If PostgreSQL doesn't know the standard_conforming_strings parameter then it doesn't
941 # support escape string syntax. Don't override the inherited quoted_string_prefix.
942 if supports_standard_conforming_strings
?
943 self.class.instance_eval
do
944 define_method(:quoted_string_prefix) { 'E' }
948 # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
949 # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
950 # should know about this but can't detect it there, so deal with it here.
951 money_precision
= (postgresql_version
>= 80300) ? 19 : 10
952 PostgreSQLColumn
.module_eval(<<-end_eval)
953 def extract_precision(sql_type)
954 if sql_type =~ /^money$/
965 # Configures the encoding, verbosity, and schema search path of the connection.
966 # This is called by #connect and should not be called manually.
967 def configure_connection
968 if @config[:encoding]
969 if @connection.respond_to
?(:set_client_encoding)
970 @connection.set_client_encoding(@config[:encoding])
972 execute("SET client_encoding TO '#{@config[:encoding]}'")
975 self.client_min_messages
= @config[:min_messages] if @config[:min_messages]
976 self.schema_search_path
= @config[:schema_search_path] || @config[:schema_order]
979 # Returns the current ID of a table's sequence.
980 def last_insert_id(table
, sequence_name
) #:nodoc:
981 Integer(select_value("SELECT currval('#{sequence_name}')"))
984 # Executes a SELECT query and returns the results, performing any data type
985 # conversions that are required to be performed here instead of in PostgreSQLColumn.
986 def select(sql
, name
= nil)
987 fields
, rows
= select_raw(sql
, name
)
991 fields
.each_with_index
do |f
, i
|
999 def select_raw(sql
, name
= nil)
1000 res
= execute(sql
, name
)
1001 results
= result_as_array(res
)
1006 results
.each
do |row
|
1008 row
.each_index
do |cell_index
|
1009 # If this is a money type column and there are any currency symbols,
1010 # then strip them off. Indeed it would be prettier to do this in
1011 # PostgreSQLColumn.string_to_decimal but would break form input
1012 # fields that call value_before_type_cast.
1013 if res
.ftype(cell_index
) == MONEY_COLUMN_TYPE_OID
1014 # Because money output is formatted according to the locale, there are two
1015 # cases to consider (note the decimal separators):
1016 # (1) $12,345,678.12
1017 # (2) $12.345.678,12
1018 case column
= row
[cell_index
]
1019 when /^-?\D+[\d,]+\.\d{2}$/ # (1)
1020 row
[cell_index
] = column
.gsub(/[^-\d\.]/, '')
1021 when /^-?\D+[\d\.]+,\d{2}$/ # (2)
1022 row
[cell_index
] = column
.gsub(/[^-\d,]/, '').sub(/,/, '.')
1026 hashed_row
[fields
[cell_index
]] = column
1035 # Returns the list of a table's column names, data types, and default values.
1037 # The underlying query is roughly:
1038 # SELECT column.name, column.type, default.value
1039 # FROM column LEFT JOIN default
1040 # ON column.table_id = default.table_id
1041 # AND column.num = default.column_num
1042 # WHERE column.table_id = get_table_id('table_name')
1043 # AND column.num > 0
1044 # AND NOT column.is_dropped
1045 # ORDER BY column.num
1047 # If the table name is not prefixed with a schema, the database will
1048 # take the first match from the schema search path.
1050 # Query implementation notes:
1051 # - format_type includes the column size constraint, e.g. varchar(50)
1052 # - ::regclass is a function that gives the id for a table name
1053 def column_definitions(table_name
) #:nodoc:
1055 SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
1056 FROM pg_attribute a LEFT JOIN pg_attrdef d
1057 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
1058 WHERE a.attrelid = '#{table_name}'::regclass
1059 AND a.attnum > 0 AND NOT a.attisdropped