Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 68 lines (56 sloc) 1.389 kB
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
1 module ActiveRecord
2 ###
4c30304 @tenderlove updating the docco for ActiveRecord::Result
tenderlove authored
3 # This class encapsulates a Result returned from calling +exec_query+ on any
0034b78 @smartinez87 Remove extra white spaces on ActiveRecord docs.
smartinez87 authored
4 # database connection adapter. For example:
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
5 #
4c30304 @tenderlove updating the docco for ActiveRecord::Result
tenderlove authored
6 # x = ActiveRecord::Base.connection.exec_query('SELECT * FROM foo')
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
7 # x # => #<ActiveRecord::Result:0xdeadbeef>
8 class Result
9 include Enumerable
10
00b62ab @tenderlove moving column types to an ivar on the result
tenderlove authored
11 attr_reader :columns, :rows, :column_types
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
12
5e47ba0 @kennyj Refactor AR::Result or inherits. Because we have redundant codes about
kennyj authored
13 def initialize(columns, rows, column_types = {})
2068d30 @spastorino Freeze columns just before using them as hash keys
spastorino authored
14 @columns = columns
00b62ab @tenderlove moving column types to an ivar on the result
tenderlove authored
15 @rows = rows
16 @hash_rows = nil
5e47ba0 @kennyj Refactor AR::Result or inherits. Because we have redundant codes about
kennyj authored
17 @column_types = column_types
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
18 end
19
20 def each
21 hash_rows.each { |row| yield row }
22 end
23
59f7780 @tenderlove adjust query counts to be consistent across databases, make sure data…
tenderlove authored
24 def to_hash
25 hash_rows
26 end
27
40ce682 @tenderlove made the result set object act more like an array
tenderlove authored
28 alias :map! :map
29 alias :collect! :map
30
89fff79 @robzolkos Doc for ActiveRecord::Result empty? method
robzolkos authored
31 # Returns true if there are no records.
40ce682 @tenderlove made the result set object act more like an array
tenderlove authored
32 def empty?
33 rows.empty?
34 end
35
36 def to_ary
37 hash_rows
38 end
39
40 def [](idx)
41 hash_rows[idx]
42 end
43
44 def last
45 hash_rows.last
46 end
47
d6e41f3 @tenderlove QueryCache will just dup an AR::Result, AR::Result can deep copy
tenderlove authored
48 def initialize_copy(other)
49 @columns = columns.dup
50 @rows = rows.dup
51 @hash_rows = nil
52 end
53
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
54 private
55 def hash_rows
da400fb @spastorino Freeze columns only once per Result
spastorino authored
56 @hash_rows ||=
57 begin
58 # We freeze the strings to prevent them getting duped when
9e4c41c @jonleighton Remove ActiveRecord::Model
jonleighton authored
59 # used as keys in ActiveRecord::Base's @attributes hash
da400fb @spastorino Freeze columns only once per Result
spastorino authored
60 columns = @columns.map { |c| c.dup.freeze }
61 @rows.map { |row|
62 Hash[columns.zip(row)]
63 }
64 end
cc468d3 @tenderlove exec returns an AR::Result
tenderlove authored
65 end
66 end
67 end
Something went wrong with that request. Please try again.