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 | require 'active_record/connection_adapters/abstract_adapter' | 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 | module ActiveRecord | 19 | module ActiveRecord |
| 4 | class Base | 20 | class Base |
| 5 | # Establishes a connection to the database that's used by all Active Record objects | 21 | # Establishes a connection to the database that's used by all Active Record objects |
| 6 | def self.postgresql_connection(config) # :nodoc: | 22 | def self.postgresql_connection(config) # :nodoc: |
| 7 | - require_library_or_gem 'postgres' unless self.class.const_defined?(:PGconn) | ||
| 8 | - | ||
| 9 | config = config.symbolize_keys | 23 | config = config.symbolize_keys |
| 10 | host = config[:host] | 24 | host = config[:host] |
| 11 | port = config[:port] || 5432 | 25 | port = config[:port] || 5432 |
| @@ -300,7 +314,7 @@ module ActiveRecord | @@ -300,7 +314,7 @@ module ActiveRecord | ||
| 300 | # postgres-pr does not raise an exception when client_min_messages is set higher | 314 | # postgres-pr does not raise an exception when client_min_messages is set higher |
| 301 | # than error and "SHOW standard_conforming_strings" fails, but returns an empty | 315 | # than error and "SHOW standard_conforming_strings" fails, but returns an empty |
| 302 | # PGresult instead. | 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 | self.client_min_messages = client_min_messages_old | 318 | self.client_min_messages = client_min_messages_old |
| 305 | has_support | 319 | has_support |
| 306 | end | 320 | end |
| @@ -390,14 +404,28 @@ module ActiveRecord | @@ -390,14 +404,28 @@ module ActiveRecord | ||
| 390 | super || last_insert_id(table, sequence_name || default_sequence_name(table, pk)) | 404 | super || last_insert_id(table, sequence_name || default_sequence_name(table, pk)) |
| 391 | end | 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 | def query(sql, name = nil) #:nodoc: | 421 | def query(sql, name = nil) #:nodoc: |
| 395 | log(sql, name) do | 422 | log(sql, name) do |
| 396 | if @async | 423 | if @async |
| 397 | - @connection.async_query(sql) | 424 | + res = @connection.async_exec(sql) |
| 398 | else | 425 | else |
| 399 | - @connection.query(sql) | 426 | + res = @connection.exec(sql) |
| 400 | end | 427 | end |
| 428 | + return result_as_array(res) | ||
| 401 | end | 429 | end |
| 402 | end | 430 | end |
| 403 | 431 | ||
| @@ -415,7 +443,7 @@ module ActiveRecord | @@ -415,7 +443,7 @@ module ActiveRecord | ||
| 415 | 443 | ||
| 416 | # Executes an UPDATE query and returns the number of affected tuples. | 444 | # Executes an UPDATE query and returns the number of affected tuples. |
| 417 | def update_sql(sql, name = nil) | 445 | def update_sql(sql, name = nil) |
| 418 | - super.cmdtuples | 446 | + super.cmd_tuples |
| 419 | end | 447 | end |
| 420 | 448 | ||
| 421 | # Begins a transaction. | 449 | # Begins a transaction. |
| @@ -780,10 +808,10 @@ module ActiveRecord | @@ -780,10 +808,10 @@ module ActiveRecord | ||
| 780 | 808 | ||
| 781 | def select_raw(sql, name = nil) | 809 | def select_raw(sql, name = nil) |
| 782 | res = execute(sql, name) | 810 | res = execute(sql, name) |
| 783 | - results = res.result | 811 | + results = result_as_array(res) |
| 784 | fields = [] | 812 | fields = [] |
| 785 | rows = [] | 813 | rows = [] |
| 786 | - if results.length > 0 | 814 | + if res.ntuples > 0 |
| 787 | fields = res.fields | 815 | fields = res.fields |
| 788 | results.each do |row| | 816 | results.each do |row| |
| 789 | hashed_row = {} | 817 | hashed_row = {} |
| @@ -792,7 +820,7 @@ module ActiveRecord | @@ -792,7 +820,7 @@ module ActiveRecord | ||
| 792 | # then strip them off. Indeed it would be prettier to do this in | 820 | # then strip them off. Indeed it would be prettier to do this in |
| 793 | # PostgreSQLColumn.string_to_decimal but would break form input | 821 | # PostgreSQLColumn.string_to_decimal but would break form input |
| 794 | # fields that call value_before_type_cast. | 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 | # Because money output is formatted according to the locale, there are two | 824 | # Because money output is formatted according to the locale, there are two |
| 797 | # cases to consider (note the decimal separators): | 825 | # cases to consider (note the decimal separators): |
| 798 | # (1) $12,345,678.12 | 826 | # (1) $12,345,678.12 |