diff --git a/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index d5c6ca6..79aecdf 100644 --- a/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -1,11 +1,25 @@ require 'active_record/connection_adapters/abstract_adapter' +begin + require_library_or_gem 'pg' +rescue LoadError => e + begin + require_library_or_gem 'postgres' + class PGresult + alias_method :nfields, :num_fields unless self.method_defined?(:nfields) + alias_method :ntuples, :num_tuples unless self.method_defined?(:ntuples) + alias_method :ftype, :type unless self.method_defined?(:ftype) + alias_method :cmd_tuples, :cmdtuples unless self.method_defined?(:cmd_tuples) + end + rescue LoadError + raise e + end +end + module ActiveRecord class Base # Establishes a connection to the database that's used by all Active Record objects def self.postgresql_connection(config) # :nodoc: - require_library_or_gem 'postgres' unless self.class.const_defined?(:PGconn) - config = config.symbolize_keys host = config[:host] port = config[:port] || 5432 @@ -300,7 +314,7 @@ module ActiveRecord # postgres-pr does not raise an exception when client_min_messages is set higher # than error and "SHOW standard_conforming_strings" fails, but returns an empty # PGresult instead. - has_support = execute('SHOW standard_conforming_strings')[0][0] rescue false + has_support = query('SHOW standard_conforming_strings')[0][0] rescue false self.client_min_messages = client_min_messages_old has_support end @@ -390,14 +404,28 @@ module ActiveRecord super || last_insert_id(table, sequence_name || default_sequence_name(table, pk)) end - # Queries the database and returns the results in an Array or nil otherwise. + # create a 2D array representing the result set + def result_as_array(res) #:nodoc: + ary = [] + for i in 0...res.ntuples do + ary << [] + for j in 0...res.nfields do + ary[i] << res.getvalue(i,j) + end + end + return ary + end + + + # Queries the database and returns the results in an Array-like object def query(sql, name = nil) #:nodoc: log(sql, name) do if @async - @connection.async_query(sql) + res = @connection.async_exec(sql) else - @connection.query(sql) + res = @connection.exec(sql) end + return result_as_array(res) end end @@ -415,7 +443,7 @@ module ActiveRecord # Executes an UPDATE query and returns the number of affected tuples. def update_sql(sql, name = nil) - super.cmdtuples + super.cmd_tuples end # Begins a transaction. @@ -780,10 +808,10 @@ module ActiveRecord def select_raw(sql, name = nil) res = execute(sql, name) - results = res.result + results = result_as_array(res) fields = [] rows = [] - if results.length > 0 + if res.ntuples > 0 fields = res.fields results.each do |row| hashed_row = {} @@ -792,7 +820,7 @@ module ActiveRecord # then strip them off. Indeed it would be prettier to do this in # PostgreSQLColumn.string_to_decimal but would break form input # fields that call value_before_type_cast. - if res.type(cell_index) == MONEY_COLUMN_TYPE_OID + if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID # Because money output is formatted according to the locale, there are two # cases to consider (note the decimal separators): # (1) $12,345,678.12 -- libgit2 0.21.2