activerecord 1.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (178) hide show
  1. data/CHANGELOG +5518 -76
  2. data/README.rdoc +222 -0
  3. data/examples/performance.rb +162 -0
  4. data/examples/simple.rb +14 -0
  5. data/lib/active_record/aggregations.rb +192 -80
  6. data/lib/active_record/association_preload.rb +403 -0
  7. data/lib/active_record/associations/association_collection.rb +545 -53
  8. data/lib/active_record/associations/association_proxy.rb +295 -0
  9. data/lib/active_record/associations/belongs_to_association.rb +91 -0
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +78 -0
  11. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +127 -36
  12. data/lib/active_record/associations/has_many_association.rb +108 -84
  13. data/lib/active_record/associations/has_many_through_association.rb +116 -0
  14. data/lib/active_record/associations/has_one_association.rb +143 -0
  15. data/lib/active_record/associations/has_one_through_association.rb +40 -0
  16. data/lib/active_record/associations/through_association_scope.rb +154 -0
  17. data/lib/active_record/associations.rb +2086 -368
  18. data/lib/active_record/attribute_methods/before_type_cast.rb +33 -0
  19. data/lib/active_record/attribute_methods/dirty.rb +95 -0
  20. data/lib/active_record/attribute_methods/primary_key.rb +50 -0
  21. data/lib/active_record/attribute_methods/query.rb +39 -0
  22. data/lib/active_record/attribute_methods/read.rb +116 -0
  23. data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -0
  24. data/lib/active_record/attribute_methods/write.rb +37 -0
  25. data/lib/active_record/attribute_methods.rb +60 -0
  26. data/lib/active_record/autosave_association.rb +369 -0
  27. data/lib/active_record/base.rb +1603 -721
  28. data/lib/active_record/callbacks.rb +176 -225
  29. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +365 -0
  30. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +113 -0
  31. data/lib/active_record/connection_adapters/abstract/database_limits.rb +57 -0
  32. data/lib/active_record/connection_adapters/abstract/database_statements.rb +329 -0
  33. data/lib/active_record/connection_adapters/abstract/query_cache.rb +81 -0
  34. data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -0
  35. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +739 -0
  36. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +543 -0
  37. data/lib/active_record/connection_adapters/abstract_adapter.rb +165 -279
  38. data/lib/active_record/connection_adapters/mysql_adapter.rb +594 -82
  39. data/lib/active_record/connection_adapters/postgresql_adapter.rb +988 -135
  40. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +53 -0
  41. data/lib/active_record/connection_adapters/sqlite_adapter.rb +365 -71
  42. data/lib/active_record/counter_cache.rb +115 -0
  43. data/lib/active_record/dynamic_finder_match.rb +53 -0
  44. data/lib/active_record/dynamic_scope_match.rb +32 -0
  45. data/lib/active_record/errors.rb +172 -0
  46. data/lib/active_record/fixtures.rb +941 -105
  47. data/lib/active_record/locale/en.yml +40 -0
  48. data/lib/active_record/locking/optimistic.rb +172 -0
  49. data/lib/active_record/locking/pessimistic.rb +55 -0
  50. data/lib/active_record/log_subscriber.rb +48 -0
  51. data/lib/active_record/migration.rb +617 -0
  52. data/lib/active_record/named_scope.rb +138 -0
  53. data/lib/active_record/nested_attributes.rb +417 -0
  54. data/lib/active_record/observer.rb +105 -36
  55. data/lib/active_record/persistence.rb +291 -0
  56. data/lib/active_record/query_cache.rb +36 -0
  57. data/lib/active_record/railtie.rb +91 -0
  58. data/lib/active_record/railties/controller_runtime.rb +38 -0
  59. data/lib/active_record/railties/databases.rake +512 -0
  60. data/lib/active_record/reflection.rb +364 -87
  61. data/lib/active_record/relation/batches.rb +89 -0
  62. data/lib/active_record/relation/calculations.rb +286 -0
  63. data/lib/active_record/relation/finder_methods.rb +355 -0
  64. data/lib/active_record/relation/predicate_builder.rb +41 -0
  65. data/lib/active_record/relation/query_methods.rb +261 -0
  66. data/lib/active_record/relation/spawn_methods.rb +112 -0
  67. data/lib/active_record/relation.rb +393 -0
  68. data/lib/active_record/schema.rb +59 -0
  69. data/lib/active_record/schema_dumper.rb +195 -0
  70. data/lib/active_record/serialization.rb +60 -0
  71. data/lib/active_record/serializers/xml_serializer.rb +244 -0
  72. data/lib/active_record/session_store.rb +340 -0
  73. data/lib/active_record/test_case.rb +67 -0
  74. data/lib/active_record/timestamp.rb +88 -0
  75. data/lib/active_record/transactions.rb +329 -75
  76. data/lib/active_record/validations/associated.rb +48 -0
  77. data/lib/active_record/validations/uniqueness.rb +185 -0
  78. data/lib/active_record/validations.rb +58 -179
  79. data/lib/active_record/version.rb +9 -0
  80. data/lib/active_record.rb +100 -24
  81. data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
  82. data/lib/rails/generators/active_record/migration/templates/migration.rb +17 -0
  83. data/lib/rails/generators/active_record/model/model_generator.rb +38 -0
  84. data/lib/rails/generators/active_record/model/templates/migration.rb +16 -0
  85. data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
  86. data/lib/rails/generators/active_record/model/templates/module.rb +5 -0
  87. data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
  88. data/lib/rails/generators/active_record/observer/templates/observer.rb +2 -0
  89. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +24 -0
  90. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +16 -0
  91. data/lib/rails/generators/active_record.rb +27 -0
  92. metadata +216 -158
  93. data/README +0 -361
  94. data/RUNNING_UNIT_TESTS +0 -36
  95. data/dev-utils/eval_debugger.rb +0 -9
  96. data/examples/associations.rb +0 -87
  97. data/examples/shared_setup.rb +0 -15
  98. data/examples/validation.rb +0 -88
  99. data/install.rb +0 -60
  100. data/lib/active_record/deprecated_associations.rb +0 -70
  101. data/lib/active_record/support/class_attribute_accessors.rb +0 -43
  102. data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
  103. data/lib/active_record/support/clean_logger.rb +0 -10
  104. data/lib/active_record/support/inflector.rb +0 -70
  105. data/lib/active_record/vendor/mysql.rb +0 -1117
  106. data/lib/active_record/vendor/simple.rb +0 -702
  107. data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
  108. data/lib/active_record/wrappings.rb +0 -59
  109. data/rakefile +0 -122
  110. data/test/abstract_unit.rb +0 -16
  111. data/test/aggregations_test.rb +0 -34
  112. data/test/all.sh +0 -8
  113. data/test/associations_test.rb +0 -477
  114. data/test/base_test.rb +0 -513
  115. data/test/class_inheritable_attributes_test.rb +0 -33
  116. data/test/connections/native_mysql/connection.rb +0 -24
  117. data/test/connections/native_postgresql/connection.rb +0 -24
  118. data/test/connections/native_sqlite/connection.rb +0 -24
  119. data/test/deprecated_associations_test.rb +0 -336
  120. data/test/finder_test.rb +0 -67
  121. data/test/fixtures/accounts/signals37 +0 -3
  122. data/test/fixtures/accounts/unknown +0 -2
  123. data/test/fixtures/auto_id.rb +0 -4
  124. data/test/fixtures/column_name.rb +0 -3
  125. data/test/fixtures/companies/first_client +0 -6
  126. data/test/fixtures/companies/first_firm +0 -4
  127. data/test/fixtures/companies/second_client +0 -6
  128. data/test/fixtures/company.rb +0 -37
  129. data/test/fixtures/company_in_module.rb +0 -33
  130. data/test/fixtures/course.rb +0 -3
  131. data/test/fixtures/courses/java +0 -2
  132. data/test/fixtures/courses/ruby +0 -2
  133. data/test/fixtures/customer.rb +0 -30
  134. data/test/fixtures/customers/david +0 -6
  135. data/test/fixtures/db_definitions/mysql.sql +0 -96
  136. data/test/fixtures/db_definitions/mysql2.sql +0 -4
  137. data/test/fixtures/db_definitions/postgresql.sql +0 -113
  138. data/test/fixtures/db_definitions/postgresql2.sql +0 -4
  139. data/test/fixtures/db_definitions/sqlite.sql +0 -85
  140. data/test/fixtures/db_definitions/sqlite2.sql +0 -4
  141. data/test/fixtures/default.rb +0 -2
  142. data/test/fixtures/developer.rb +0 -8
  143. data/test/fixtures/developers/david +0 -2
  144. data/test/fixtures/developers/jamis +0 -2
  145. data/test/fixtures/developers_projects/david_action_controller +0 -2
  146. data/test/fixtures/developers_projects/david_active_record +0 -2
  147. data/test/fixtures/developers_projects/jamis_active_record +0 -2
  148. data/test/fixtures/entrant.rb +0 -3
  149. data/test/fixtures/entrants/first +0 -3
  150. data/test/fixtures/entrants/second +0 -3
  151. data/test/fixtures/entrants/third +0 -3
  152. data/test/fixtures/fixture_database.sqlite +0 -0
  153. data/test/fixtures/fixture_database_2.sqlite +0 -0
  154. data/test/fixtures/movie.rb +0 -5
  155. data/test/fixtures/movies/first +0 -2
  156. data/test/fixtures/movies/second +0 -2
  157. data/test/fixtures/project.rb +0 -3
  158. data/test/fixtures/projects/action_controller +0 -2
  159. data/test/fixtures/projects/active_record +0 -2
  160. data/test/fixtures/reply.rb +0 -21
  161. data/test/fixtures/subscriber.rb +0 -5
  162. data/test/fixtures/subscribers/first +0 -2
  163. data/test/fixtures/subscribers/second +0 -2
  164. data/test/fixtures/topic.rb +0 -20
  165. data/test/fixtures/topics/first +0 -9
  166. data/test/fixtures/topics/second +0 -8
  167. data/test/fixtures_test.rb +0 -20
  168. data/test/inflector_test.rb +0 -104
  169. data/test/inheritance_test.rb +0 -125
  170. data/test/lifecycle_test.rb +0 -110
  171. data/test/modules_test.rb +0 -21
  172. data/test/multiple_db_test.rb +0 -46
  173. data/test/pk_test.rb +0 -57
  174. data/test/reflection_test.rb +0 -78
  175. data/test/thread_safety_test.rb +0 -33
  176. data/test/transactions_test.rb +0 -83
  177. data/test/unconnected_test.rb +0 -24
  178. data/test/validations_test.rb +0 -126
@@ -0,0 +1,329 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters # :nodoc:
3
+ module DatabaseStatements
4
+ # Returns an array of record hashes with the column names as keys and
5
+ # column values as values.
6
+ def select_all(sql, name = nil)
7
+ select(sql, name)
8
+ end
9
+
10
+ # Returns a record hash with the column names as keys and column values
11
+ # as values.
12
+ def select_one(sql, name = nil)
13
+ result = select_all(sql, name)
14
+ result.first if result
15
+ end
16
+
17
+ # Returns a single value from a record
18
+ def select_value(sql, name = nil)
19
+ if result = select_one(sql, name)
20
+ result.values.first
21
+ end
22
+ end
23
+
24
+ # Returns an array of the values of the first column in a select:
25
+ # select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
26
+ def select_values(sql, name = nil)
27
+ result = select_rows(sql, name)
28
+ result.map { |v| v[0] }
29
+ end
30
+
31
+ # Returns an array of arrays containing the field values.
32
+ # Order is the same as that returned by +columns+.
33
+ def select_rows(sql, name = nil)
34
+ end
35
+ undef_method :select_rows
36
+
37
+ # Executes the SQL statement in the context of this connection.
38
+ def execute(sql, name = nil, skip_logging = false)
39
+ end
40
+ undef_method :execute
41
+
42
+ # Returns the last auto-generated ID from the affected table.
43
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
44
+ insert_sql(sql, name, pk, id_value, sequence_name)
45
+ end
46
+
47
+ # Executes the update statement and returns the number of rows affected.
48
+ def update(sql, name = nil)
49
+ update_sql(sql, name)
50
+ end
51
+
52
+ # Executes the delete statement and returns the number of rows affected.
53
+ def delete(sql, name = nil)
54
+ delete_sql(sql, name)
55
+ end
56
+
57
+ # Checks whether there is currently no transaction active. This is done
58
+ # by querying the database driver, and does not use the transaction
59
+ # house-keeping information recorded by #increment_open_transactions and
60
+ # friends.
61
+ #
62
+ # Returns true if there is no transaction active, false if there is a
63
+ # transaction active, and nil if this information is unknown.
64
+ #
65
+ # Not all adapters supports transaction state introspection. Currently,
66
+ # only the PostgreSQL adapter supports this.
67
+ def outside_transaction?
68
+ nil
69
+ end
70
+
71
+ # Runs the given block in a database transaction, and returns the result
72
+ # of the block.
73
+ #
74
+ # == Nested transactions support
75
+ #
76
+ # Most databases don't support true nested transactions. At the time of
77
+ # writing, the only database that supports true nested transactions that
78
+ # we're aware of, is MS-SQL.
79
+ #
80
+ # In order to get around this problem, #transaction will emulate the effect
81
+ # of nested transactions, by using savepoints:
82
+ # http://dev.mysql.com/doc/refman/5.0/en/savepoints.html
83
+ # Savepoints are supported by MySQL and PostgreSQL, but not SQLite3.
84
+ #
85
+ # It is safe to call this method if a database transaction is already open,
86
+ # i.e. if #transaction is called within another #transaction block. In case
87
+ # of a nested call, #transaction will behave as follows:
88
+ #
89
+ # - The block will be run without doing anything. All database statements
90
+ # that happen within the block are effectively appended to the already
91
+ # open database transaction.
92
+ # - However, if +:requires_new+ is set, the block will be wrapped in a
93
+ # database savepoint acting as a sub-transaction.
94
+ #
95
+ # === Caveats
96
+ #
97
+ # MySQL doesn't support DDL transactions. If you perform a DDL operation,
98
+ # then any created savepoints will be automatically released. For example,
99
+ # if you've created a savepoint, then you execute a CREATE TABLE statement,
100
+ # then the savepoint that was created will be automatically released.
101
+ #
102
+ # This means that, on MySQL, you shouldn't execute DDL operations inside
103
+ # a #transaction call that you know might create a savepoint. Otherwise,
104
+ # #transaction will raise exceptions when it tries to release the
105
+ # already-automatically-released savepoints:
106
+ #
107
+ # Model.connection.transaction do # BEGIN
108
+ # Model.connection.transaction(:requires_new => true) do # CREATE SAVEPOINT active_record_1
109
+ # Model.connection.create_table(...)
110
+ # # active_record_1 now automatically released
111
+ # end # RELEASE SAVEPOINT active_record_1 <--- BOOM! database error!
112
+ # end
113
+ def transaction(options = {})
114
+ options.assert_valid_keys :requires_new, :joinable
115
+
116
+ last_transaction_joinable = defined?(@transaction_joinable) ? @transaction_joinable : nil
117
+ if options.has_key?(:joinable)
118
+ @transaction_joinable = options[:joinable]
119
+ else
120
+ @transaction_joinable = true
121
+ end
122
+ requires_new = options[:requires_new] || !last_transaction_joinable
123
+
124
+ transaction_open = false
125
+ @_current_transaction_records ||= []
126
+
127
+ begin
128
+ if block_given?
129
+ if requires_new || open_transactions == 0
130
+ if open_transactions == 0
131
+ begin_db_transaction
132
+ elsif requires_new
133
+ create_savepoint
134
+ end
135
+ increment_open_transactions
136
+ transaction_open = true
137
+ @_current_transaction_records.push([])
138
+ end
139
+ yield
140
+ end
141
+ rescue Exception => database_transaction_rollback
142
+ if transaction_open && !outside_transaction?
143
+ transaction_open = false
144
+ decrement_open_transactions
145
+ if open_transactions == 0
146
+ rollback_db_transaction
147
+ rollback_transaction_records(true)
148
+ else
149
+ rollback_to_savepoint
150
+ rollback_transaction_records(false)
151
+ end
152
+ end
153
+ raise unless database_transaction_rollback.is_a?(ActiveRecord::Rollback)
154
+ end
155
+ ensure
156
+ @transaction_joinable = last_transaction_joinable
157
+
158
+ if outside_transaction?
159
+ @open_transactions = 0
160
+ elsif transaction_open
161
+ decrement_open_transactions
162
+ begin
163
+ if open_transactions == 0
164
+ commit_db_transaction
165
+ commit_transaction_records
166
+ else
167
+ release_savepoint
168
+ save_point_records = @_current_transaction_records.pop
169
+ unless save_point_records.blank?
170
+ @_current_transaction_records.push([]) if @_current_transaction_records.empty?
171
+ @_current_transaction_records.last.concat(save_point_records)
172
+ end
173
+ end
174
+ rescue Exception => database_transaction_rollback
175
+ if open_transactions == 0
176
+ rollback_db_transaction
177
+ rollback_transaction_records(true)
178
+ else
179
+ rollback_to_savepoint
180
+ rollback_transaction_records(false)
181
+ end
182
+ raise
183
+ end
184
+ end
185
+ end
186
+
187
+ # Register a record with the current transaction so that its after_commit and after_rollback callbacks
188
+ # can be called.
189
+ def add_transaction_record(record)
190
+ last_batch = @_current_transaction_records.last
191
+ last_batch << record if last_batch
192
+ end
193
+
194
+ # Begins the transaction (and turns off auto-committing).
195
+ def begin_db_transaction() end
196
+
197
+ # Commits the transaction (and turns on auto-committing).
198
+ def commit_db_transaction() end
199
+
200
+ # Rolls back the transaction (and turns on auto-committing). Must be
201
+ # done if the transaction block raises an exception or returns false.
202
+ def rollback_db_transaction() end
203
+
204
+ # Appends +LIMIT+ and +OFFSET+ options to an SQL statement, or some SQL
205
+ # fragment that has the same semantics as LIMIT and OFFSET.
206
+ #
207
+ # +options+ must be a Hash which contains a +:limit+ option
208
+ # and an +:offset+ option.
209
+ #
210
+ # This method *modifies* the +sql+ parameter.
211
+ #
212
+ # ===== Examples
213
+ # add_limit_offset!('SELECT * FROM suppliers', {:limit => 10, :offset => 50})
214
+ # generates
215
+ # SELECT * FROM suppliers LIMIT 10 OFFSET 50
216
+
217
+ def add_limit_offset!(sql, options)
218
+ if limit = options[:limit]
219
+ sql << " LIMIT #{sanitize_limit(limit)}"
220
+ end
221
+ if offset = options[:offset]
222
+ sql << " OFFSET #{offset.to_i}"
223
+ end
224
+ sql
225
+ end
226
+
227
+ def default_sequence_name(table, column)
228
+ nil
229
+ end
230
+
231
+ # Set the sequence to the max value of the table's column.
232
+ def reset_sequence!(table, column, sequence = nil)
233
+ # Do nothing by default. Implement for PostgreSQL, Oracle, ...
234
+ end
235
+
236
+ # Inserts the given fixture into the table. Overridden in adapters that require
237
+ # something beyond a simple insert (eg. Oracle).
238
+ def insert_fixture(fixture, table_name)
239
+ execute "INSERT INTO #{quote_table_name(table_name)} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
240
+ end
241
+
242
+ def empty_insert_statement_value
243
+ "VALUES(DEFAULT)"
244
+ end
245
+
246
+ def case_sensitive_equality_operator
247
+ "="
248
+ end
249
+
250
+ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
251
+ "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
252
+ end
253
+
254
+ protected
255
+ # Returns an array of record hashes with the column names as keys and
256
+ # column values as values.
257
+ def select(sql, name = nil)
258
+ end
259
+ undef_method :select
260
+
261
+ # Returns the last auto-generated ID from the affected table.
262
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
263
+ execute(sql, name)
264
+ id_value
265
+ end
266
+
267
+ # Executes the update statement and returns the number of rows affected.
268
+ def update_sql(sql, name = nil)
269
+ execute(sql, name)
270
+ end
271
+
272
+ # Executes the delete statement and returns the number of rows affected.
273
+ def delete_sql(sql, name = nil)
274
+ update_sql(sql, name)
275
+ end
276
+
277
+ # Sanitizes the given LIMIT parameter in order to prevent SQL injection.
278
+ #
279
+ # +limit+ may be anything that can evaluate to a string via #to_s. It
280
+ # should look like an integer, or a comma-delimited list of integers.
281
+ #
282
+ # Returns the sanitized limit parameter, either as an integer, or as a
283
+ # string which contains a comma-delimited list of integers.
284
+ def sanitize_limit(limit)
285
+ if limit.to_s =~ /,/
286
+ limit.to_s.split(',').map{ |i| i.to_i }.join(',')
287
+ else
288
+ limit.to_i
289
+ end
290
+ end
291
+
292
+ # Send a rollback message to all records after they have been rolled back. If rollback
293
+ # is false, only rollback records since the last save point.
294
+ def rollback_transaction_records(rollback) #:nodoc
295
+ if rollback
296
+ records = @_current_transaction_records.flatten
297
+ @_current_transaction_records.clear
298
+ else
299
+ records = @_current_transaction_records.pop
300
+ end
301
+
302
+ unless records.blank?
303
+ records.uniq.each do |record|
304
+ begin
305
+ record.rolledback!(rollback)
306
+ rescue Exception => e
307
+ record.logger.error(e) if record.respond_to?(:logger) && record.logger
308
+ end
309
+ end
310
+ end
311
+ end
312
+
313
+ # Send a commit message to all records after they have been committed.
314
+ def commit_transaction_records #:nodoc
315
+ records = @_current_transaction_records.flatten
316
+ @_current_transaction_records.clear
317
+ unless records.blank?
318
+ records.uniq.each do |record|
319
+ begin
320
+ record.committed!
321
+ rescue Exception => e
322
+ record.logger.error(e) if record.respond_to?(:logger) && record.logger
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,81 @@
1
+ require 'active_support/core_ext/object/duplicable'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module QueryCache
6
+ class << self
7
+ def included(base)
8
+ dirties_query_cache base, :insert, :update, :delete
9
+ end
10
+
11
+ def dirties_query_cache(base, *method_names)
12
+ method_names.each do |method_name|
13
+ base.class_eval <<-end_code, __FILE__, __LINE__ + 1
14
+ def #{method_name}(*) # def update_with_query_dirty(*args)
15
+ clear_query_cache if @query_cache_enabled # clear_query_cache if @query_cache_enabled
16
+ super # update_without_query_dirty(*args)
17
+ end # end
18
+ end_code
19
+ end
20
+ end
21
+ end
22
+
23
+ attr_reader :query_cache, :query_cache_enabled
24
+
25
+ # Enable the query cache within the block.
26
+ def cache
27
+ old, @query_cache_enabled = @query_cache_enabled, true
28
+ yield
29
+ ensure
30
+ clear_query_cache
31
+ @query_cache_enabled = old
32
+ end
33
+
34
+ # Disable the query cache within the block.
35
+ def uncached
36
+ old, @query_cache_enabled = @query_cache_enabled, false
37
+ yield
38
+ ensure
39
+ @query_cache_enabled = old
40
+ end
41
+
42
+ # Clears the query cache.
43
+ #
44
+ # One reason you may wish to call this method explicitly is between queries
45
+ # that ask the database to randomize results. Otherwise the cache would see
46
+ # the same SQL query and repeatedly return the same result each time, silently
47
+ # undermining the randomness you were expecting.
48
+ def clear_query_cache
49
+ @query_cache.clear
50
+ end
51
+
52
+ def select_all(*args)
53
+ if @query_cache_enabled
54
+ cache_sql(args.first) { super }
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ private
61
+ def cache_sql(sql)
62
+ result =
63
+ if @query_cache.has_key?(sql)
64
+ ActiveSupport::Notifications.instrument("sql.active_record",
65
+ :sql => sql, :name => "CACHE", :connection_id => self.object_id)
66
+ @query_cache[sql]
67
+ else
68
+ @query_cache[sql] = yield
69
+ end
70
+
71
+ if Array === result
72
+ result.collect { |row| row.dup }
73
+ else
74
+ result.duplicable? ? result.dup : result
75
+ end
76
+ rescue TypeError
77
+ result
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,72 @@
1
+ require 'active_support/core_ext/big_decimal/conversions'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module Quoting
6
+ # Quotes the column value to help prevent
7
+ # {SQL injection attacks}[http://en.wikipedia.org/wiki/SQL_injection].
8
+ def quote(value, column = nil)
9
+ # records are quoted as their primary key
10
+ return value.quoted_id if value.respond_to?(:quoted_id)
11
+
12
+ case value
13
+ when String, ActiveSupport::Multibyte::Chars
14
+ value = value.to_s
15
+ if column && column.type == :binary && column.class.respond_to?(:string_to_binary)
16
+ "'#{quote_string(column.class.string_to_binary(value))}'" # ' (for ruby-mode)
17
+ elsif column && [:integer, :float].include?(column.type)
18
+ value = column.type == :integer ? value.to_i : value.to_f
19
+ value.to_s
20
+ else
21
+ "'#{quote_string(value)}'" # ' (for ruby-mode)
22
+ end
23
+ when NilClass then "NULL"
24
+ when TrueClass then (column && column.type == :integer ? '1' : quoted_true)
25
+ when FalseClass then (column && column.type == :integer ? '0' : quoted_false)
26
+ when Float, Fixnum, Bignum then value.to_s
27
+ # BigDecimals need to be output in a non-normalized form and quoted.
28
+ when BigDecimal then value.to_s('F')
29
+ else
30
+ if value.acts_like?(:date) || value.acts_like?(:time)
31
+ "'#{quoted_date(value)}'"
32
+ else
33
+ "'#{quote_string(value.to_s)}'"
34
+ end
35
+ end
36
+ end
37
+
38
+ # Quotes a string, escaping any ' (single quote) and \ (backslash)
39
+ # characters.
40
+ def quote_string(s)
41
+ s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode)
42
+ end
43
+
44
+ # Quotes the column name. Defaults to no quoting.
45
+ def quote_column_name(column_name)
46
+ column_name
47
+ end
48
+
49
+ # Quotes the table name. Defaults to column name quoting.
50
+ def quote_table_name(table_name)
51
+ quote_column_name(table_name)
52
+ end
53
+
54
+ def quoted_true
55
+ "'t'"
56
+ end
57
+
58
+ def quoted_false
59
+ "'f'"
60
+ end
61
+
62
+ def quoted_date(value)
63
+ if value.acts_like?(:time)
64
+ zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
65
+ value.respond_to?(zone_conversion_method) ? value.send(zone_conversion_method) : value
66
+ else
67
+ value
68
+ end.to_s(:db)
69
+ end
70
+ end
71
+ end
72
+ end