Commit 59eef108516a98f0e3666c1676af65cd0bc54cc2
1 parent
13348b17
ActionItem927: applying patch for PostgreSQL 8.3
applying upstream patch over rails 2.0.x to make it work with postgresql 8.3. See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=476449
Showing
1 changed file
with
38 additions
and
10 deletions
Show diff stats
vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
| 1 | 1 | require 'active_record/connection_adapters/abstract_adapter' |
| 2 | 2 | |
| 3 | +begin | |
| 4 | + require_library_or_gem 'pg' | |
| 5 | +rescue LoadError => e | |
| 6 | + begin | |
| 7 | + require_library_or_gem 'postgres' | |
| 8 | + class PGresult | |
| 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) | |
| 13 | + end | |
| 14 | + rescue LoadError | |
| 15 | + raise e | |
| 16 | + end | |
| 17 | +end | |
| 18 | + | |
| 3 | 19 | module ActiveRecord |
| 4 | 20 | class Base |
| 5 | 21 | # Establishes a connection to the database that's used by all Active Record objects |
| 6 | 22 | def self.postgresql_connection(config) # :nodoc: |
| 7 | - require_library_or_gem 'postgres' unless self.class.const_defined?(:PGconn) | |
| 8 | - | |
| 9 | 23 | config = config.symbolize_keys |
| 10 | 24 | host = config[:host] |
| 11 | 25 | port = config[:port] || 5432 |
| ... | ... | @@ -300,7 +314,7 @@ module ActiveRecord |
| 300 | 314 | # postgres-pr does not raise an exception when client_min_messages is set higher |
| 301 | 315 | # than error and "SHOW standard_conforming_strings" fails, but returns an empty |
| 302 | 316 | # PGresult instead. |
| 303 | - has_support = execute('SHOW standard_conforming_strings')[0][0] rescue false | |
| 317 | + has_support = query('SHOW standard_conforming_strings')[0][0] rescue false | |
| 304 | 318 | self.client_min_messages = client_min_messages_old |
| 305 | 319 | has_support |
| 306 | 320 | end |
| ... | ... | @@ -390,14 +404,28 @@ module ActiveRecord |
| 390 | 404 | super || last_insert_id(table, sequence_name || default_sequence_name(table, pk)) |
| 391 | 405 | end |
| 392 | 406 | |
| 393 | - # Queries the database and returns the results in an Array or nil otherwise. | |
| 407 | + # create a 2D array representing the result set | |
| 408 | + def result_as_array(res) #:nodoc: | |
| 409 | + ary = [] | |
| 410 | + for i in 0...res.ntuples do | |
| 411 | + ary << [] | |
| 412 | + for j in 0...res.nfields do | |
| 413 | + ary[i] << res.getvalue(i,j) | |
| 414 | + end | |
| 415 | + end | |
| 416 | + return ary | |
| 417 | + end | |
| 418 | + | |
| 419 | + | |
| 420 | + # Queries the database and returns the results in an Array-like object | |
| 394 | 421 | def query(sql, name = nil) #:nodoc: |
| 395 | 422 | log(sql, name) do |
| 396 | 423 | if @async |
| 397 | - @connection.async_query(sql) | |
| 424 | + res = @connection.async_exec(sql) | |
| 398 | 425 | else |
| 399 | - @connection.query(sql) | |
| 426 | + res = @connection.exec(sql) | |
| 400 | 427 | end |
| 428 | + return result_as_array(res) | |
| 401 | 429 | end |
| 402 | 430 | end |
| 403 | 431 | |
| ... | ... | @@ -415,7 +443,7 @@ module ActiveRecord |
| 415 | 443 | |
| 416 | 444 | # Executes an UPDATE query and returns the number of affected tuples. |
| 417 | 445 | def update_sql(sql, name = nil) |
| 418 | - super.cmdtuples | |
| 446 | + super.cmd_tuples | |
| 419 | 447 | end |
| 420 | 448 | |
| 421 | 449 | # Begins a transaction. |
| ... | ... | @@ -780,10 +808,10 @@ module ActiveRecord |
| 780 | 808 | |
| 781 | 809 | def select_raw(sql, name = nil) |
| 782 | 810 | res = execute(sql, name) |
| 783 | - results = res.result | |
| 811 | + results = result_as_array(res) | |
| 784 | 812 | fields = [] |
| 785 | 813 | rows = [] |
| 786 | - if results.length > 0 | |
| 814 | + if res.ntuples > 0 | |
| 787 | 815 | fields = res.fields |
| 788 | 816 | results.each do |row| |
| 789 | 817 | hashed_row = {} |
| ... | ... | @@ -792,7 +820,7 @@ module ActiveRecord |
| 792 | 820 | # then strip them off. Indeed it would be prettier to do this in |
| 793 | 821 | # PostgreSQLColumn.string_to_decimal but would break form input |
| 794 | 822 | # fields that call value_before_type_cast. |
| 795 | - if res.type(cell_index) == MONEY_COLUMN_TYPE_OID | |
| 823 | + if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID | |
| 796 | 824 | # Because money output is formatted according to the locale, there are two |
| 797 | 825 | # cases to consider (note the decimal separators): |
| 798 | 826 | # (1) $12,345,678.12 | ... | ... |