aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
Commit message (Collapse)AuthorAgeFilesLines
...
* | | Fix AC::Parameters not being sanitized for query methods.Guo Xiang Tan2015-10-021-5/+3
| | |
* | | Correcting `ActiveRecord::ReadOnlyRecord` Error Message [ci skip]amitkumarsuroliya2015-09-261-1/+1
| | |
* | | docs, add missing closing bracket. [ci skip]amitkumarsuroliya2015-09-251-1/+1
| | |
* | | Ensure aliased attributes passed to `select` are quoted if using `from`Sean Griffin2015-09-211-1/+1
| | | | | | | | | | | | | | | | | | Fixes #21488 [Sean Griffin & johanlunds]
* | | #where fails if opts.responds_to?(:==) unexpectedlySamuel Williams2015-09-051-1/+1
| | | | | | | | | Sometimes opts passed in might respond to ==, e.g. `Arel::Nodes::Grouping`. In this case, `opts == :chain` returns `Arel::Nodes::Equality` which causes odd behaviour. Prefer `if :chain == opts` which guarantees that `Symbol#==` would be invoked. Alternatively consider `eql?`.
* | | Include `Enumerable` in `ActiveRecord::Relation`Sean Griffin2015-06-191-6/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | After discussing, we've decided it makes more sense to include it. We're already forwarding every conflicting method to `to_a`, and there's no conflation of concerns. `Enumerable` has no mutating methods, and it just allows us to simplify the code. No existing methods will have a change in behavior. Un-overridden Enumerable methods will simply delegate to `each`. [Sean Griffin & bogdan]
* | | Ensure symbols passed to `select` are always quotedSean Griffin2015-05-301-9/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Our general contract in Active Record is that strings are assumed to be SQL literals, and symbols are assumed to reference a column. If a from clause is given, we shouldn't include the table name, but we should still quote the value as if it were a column. Upon fixing this, the tests were still failing on SQLite. This was because the column name being returned by the query was `"\"join\""` instead of `"join"`. This is actually a bug in SQLite that was fixed a long time ago, but I was using the version of SQLite included by OS X which has this bug. Since I'm guessing this will be a common case for contributors, I also added an explicit check with a more helpful error message. Fixes #20360
* | | deprecate `Relation#uniq` use `Relation#distinct` instead.Yves Senn2015-05-261-1/+3
| |/ |/| | | | | | | | | | | | | | | See #9683 for the reasons we switched to `distinct`. Here is the discussion that triggered the actual deprecation #20198. `uniq`, `uniq!` and `uniq_value` are still around. They will be removed in the next minor release after Rails 5.
* | Fix c479480638508c20601af69ca46b5b606c2d5b4d to account for from_value -> ↵Jeremy Kemper2015-02-241-1/+1
| | | | | | | | from_clause in bdc5141652770fd227455681cde1f9899f55b0b9
* | Merge pull request #18744 from mfazekas/no-table-name-with-fromRafael Mendonça França2015-02-241-5/+9
| | | | | | | | Fix appending table_name to select and group when used with subquery (fr...
* | Fixed AR::Relation#group method when argument is a SQL reserved keywordBogdan Gusiev2015-01-291-13/+15
| |
* | Bring the implementation of Relation#or up to speedSean Griffin2015-01-281-36/+8
| |
* | Added #or to ActiveRecord::RelationMatthew Draper2015-01-281-0/+59
| | | | | | | | | | | | | | Post.where('id = 1').or(Post.where('id = 2')) # => SELECT * FROM posts WHERE (id = 1) OR (id = 2) [Matthew Draper & Gael Muller]
* | Remove Relation#bind_paramsSean Griffin2015-01-271-10/+1
| | | | | | | | | | | | | | | | `bound_attributes` is now used universally across the board, removing the need for the conversion layer. These changes are mostly mechanical, with the exception of the log subscriber. Additional, we had to implement `hash` on the attribute objects, so they could be used as a key for query caching.
* | Use an `Attribute` object to represent a bind valueSean Griffin2015-01-271-1/+11
| | | | | | | | | | | | | | | | | | | | | | The column is primarily used for type casting, which we're trying to separate from the idea of a column. Since what we really need is the combination of a name, type, and value, let's use the object that we already have to represent that concept, rather than this tuple. No consumers of the bind values have been changed, only the producers (outside of tests which care too much about internals). This is *finally* possible since the bind values are now produced from a reasonable number of lcoations.
* | Minor refactorings on `Relation#build_joins`Sean Griffin2015-01-271-26/+13
| | | | | | | | Attempting to grok this code by refactoring it as I go through it.
* | Use the `WhereClause` ast building logic for havingSean Griffin2015-01-271-4/+1
| |
* | Move where grouping into `WhereClause`Sean Griffin2015-01-271-11/+1
| |
* | Unify access to bind values on RelationSean Griffin2015-01-271-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The bind values can come from four places. `having`, `where`, `joins`, and `from` when selecting from a subquery that contains binds. These need to be kept in a specific order, since the clauses will always appear in that order. Up until recently, they were not. Additionally, `joins` actually did keep its bind values in a separate location (presumably because it's the only case that people noticed was broken). However, this meant that anything accessing just `bind_values` was broken (which most places were). This is no longer possible, there is only a single way to access the bind values, and it includes joins in the proper location. The setter was removed yesterday, so breaking `+=` cases is not possible. I'm still not happy that `joins` is putting it's bind values on the Arel AST, and I'm planning on refactoring it further, but this removes a ton of bug cases.
* | Move the `from` bind logic to a `FromClause` classSean Griffin2015-01-261-12/+13
| | | | | | | | | | | | | | Contrary to my previous commit message, it wasn't overkill, and led to much cleaner code. [Sean Griffin & anthonynavarre]
* | Remove `Relation#bind_values=`Sean Griffin2015-01-261-7/+4
| | | | | | | | | | | | | | | | | | | | The last place that was assigning it was when `from` is called with a relation to use as a subquery. The implementation was actually completely broken, and would break if you called `from` more than once, or if you called it on a relation, which also had its own join clause, as the bind values would get completely scrambled. The simplest solution was to just move it into its own array, since creating a `FromClause` class for this would be overkill.
* | Remove unused `bind` and `bind!` methods from `Relation`Sean Griffin2015-01-261-9/+0
| |
* | Remove `Relation#build_where`Sean Griffin2015-01-261-6/+0
| | | | | | | | All of its uses have been moved to better places
* | Change `having_values` to use the `WhereClause` classSean Griffin2015-01-261-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixed an issue where `having` can only be called after the last call to `where`, because it messes with the same `bind_values` array. With this change, the two can be called as many times as needed, in any order, and the final query will be correct. However, once something assigns `bind_values`, that stops. This is because we have to move all of the bind values from the having clause over to the where clause since we can't differentiate the two, and assignment was likely in the form of: `relation.bind_values += other.bind_values` This will go away once we remove all places that are assigning `bind_values`, which is next on the list. While this fixes a bug that was present in at least 4.2 (more likely present going back as far as 3.0, becoming more likely in 4.1 and later as we switched to prepared statements in more cases), I don't think this can be easily backported. The internal changes to `Relation` are non-trivial, anything that involves modifying the `bind_values` array would need to change, and I'm not confident that we have sufficient test coverage of all of those locations (when `having` was called with a hash that could generate bind values). [Sean Griffin & anthonynavarre]
* | Remove `where_values` and `where_values=`Sean Griffin2015-01-251-8/+0
| | | | | | | | | | We've now removed all uses of them across the board. All logic lives on `WhereClause`.
* | Correct the implementation for `unscope(:where)`Sean Griffin2015-01-251-5/+8
| | | | | | | | | | | | The code assumes that non-single-value methods mean multi value methods. That is not the case. We need to change the accessor name, and only assign an array for multi value methods
* | Move `where_unscoping` logic over to `WhereClause`Sean Griffin2015-01-251-17/+2
| |
* | Remove most references to `where_values` in `QueryMethods`Sean Griffin2015-01-251-2/+2
| | | | | | | | | | We're still using it in `where_unscoping`, which will require moving additional logic.
* | Rename `WhereClause#parts` to `WhereClause#predicates`Sean Griffin2015-01-251-3/+3
| |
* | Move `where.not` logic into `WhereClause`Sean Griffin2015-01-251-15/+2
| |
* | Move the construction of `WhereClause` objects out of `Relation`Sean Griffin2015-01-251-14/+8
| | | | | | | | | | Yes, I know, I called it a factory so I'm basically the worst person ever who loves Java and worships the Gang of Four.
* | Introduce `Relation::WhereClause`Sean Griffin2015-01-251-0/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The way that bind values are currently stored on Relation is a mess. They can come from `having`, `where`, or `join`. I'm almost certain that `having` is actually broken, and calling `where` followed by `having` followed by `where` will completely scramble the binds. Joins don't actually add the bind parameters to the relation itself, but instead add it onto an accessor on the arel AST which is undocumented, and unused in Arel itself. This means that the bind values must always be accessed as `relation.arel.bind_values + relation.bind_values`. Anything that doesn't is likely broken (and tons of bugs have come up for exactly that reason) The result is that everything dealing with `Relation` instances has to know far too much about the internals. The binds are split, combined, and re-stored in non-obvious ways that makes it difficult to change anything about the internal representation of `bind_values`, and is extremely prone to bugs. So the goal is to move a lot of logic off of `Relation`, and into separate objects. This is not the same as what is currently done with `JoinDependency`, as `Relation` knows far too much about its internals, and vice versa. Instead these objects need to be black boxes that can have their implementations swapped easily. The end result will be two classes, `WhereClause` and `JoinClause` (`having` will just re-use `WhereClause`), and there will be a single method to access the bind values of a `Relation` which will be implemented as ``` join_clause.binds + where_clause.binds + having_clause.binds ``` This is the first step towards that refactoring, with the internal representation of where changed, and an intermediate representation of `where_values` and `bind_values` to let the refactoring take small steps. These will be removed shortly.
* | Expand the number of types which can use prepared statementsSean Griffin2015-01-241-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This will allow all types which require no additional handling to use prepared statements. Specifically, this will allow for `true`, `false`, `Date`, `Time`, and any custom PG type to use prepared statements. This also revealed another source of nil columns in bind params, and an inconsistency in their use. The specific inconsistency comes from a nested query coming from a through association, where one of the inversed associations is not bi-directional. The stop-gap is to simply construct the column at the site it is being used. This should simply go away on its own once we use `Attribute` to represent them instead, since we already have all of the information we need.
* | Don't mutate `where_values`Sean Griffin2015-01-241-1/+1
| | | | | | | | | | | | | | | | This is to help facilitate future refactorings, as the internal representation is changed. I'm planning on having `where_values` return an array that's computed on call, which means that mutation will have no affect. This is the only remaining place that was mutating (tested by replacing the method with calling `dup`)
* | Fix bind value copying from subqueried relationsSean Griffin2015-01-191-13/+0
| | | | | | | | | | | | | | | | | | | | | | With the old implementation, the bind values were created, and then we search the attributes for `Relation` objects, and merge them. This completely ignores the order that the actual `where` clause will use. If all non-relation where parameters are before the relations, it will work. However, if we query on both a relation and a value, with the value coming second, it breaks. The order of the hash should not affect the final query (especially since hashes being ordered is an implementation detail)
* | Move `create_binds` over to the `PredicateBuilder`Sean Griffin2015-01-191-32/+1
| | | | | | | | | | | | I'm looking to introduce a `WhereClause` class to handle most of this logic, and this method will eventually move over to there. However, this intermediate refactoring should make that easier to do.
* | Whether a column exists or not doesn't affect whether we can use bindsSean Griffin2015-01-191-6/+3
| | | | | | | | | | | | Looking through the blame, this logic used to be when we actually created the bind tuple. My guess is that `nil` couldn't be handled there at that time. It can, now.
* | Don't mutate bind values in `Relation`Sean Griffin2015-01-191-1/+1
| | | | | | | | | | | | | | In order to better facilitate refactoring, most places that mutated `bind_values` have already been removed. One last spot snuck through. Since we're no longer mutating the array, it also does not need to be duped in `initialize_copy`.
* | Properly copy nested bind values from subqueried relationsSean Griffin2015-01-091-3/+1
| | | | | | | | | | | | | | | | | | | | | | This is cropping up all over the place. After a brief dive, I'm really not sure why we have `arel.bind_values` at all. A cursory grep didn't reveal where they're actually being assigned (it's definitely in AR, not in Arel). I'd like to dig further into it, as I'm fairly certain we don't actually need it, we just need a way for the predicate builder to communicate merged binds upstream. Fixes #18414
* | Correctly fetch bind_values from join in subquerybrainopia2015-01-061-1/+3
| |
* | remove deprecation warning when modifying a Relation with cached arel.Yves Senn2015-01-051-21/+14
| | | | | | | | This adresses https://github.com/rails/rails/commit/1b7aa62b184c4410c99208f71b59bbac5c5f03be#commitcomment-9147803
* | Merge pull request #17886 from mrgilman/refactor-predicate-builderSean Griffin2014-12-021-2/+2
|\ \ | | | | | | Refactor `PredicateBuilder` from singleton to instance
| * | Refactor `PredicateBuilder` from singleton to instanceMelanie Gilman2014-12-021-2/+2
| | |
* | | Allow to unscope where conditions using `arel_table` with Symboldeeeki2014-12-031-1/+1
|/ / | | | | | | | | | | This commit fixes the following case. User.where(User.arel_table[:created_at].lteq(1.year.ago)).unscope(where :created_at)
* | Update Arel usage for rails/arel#98fc259Sean Griffin2014-11-291-1/+1
| | | | | | | | | | `where_sql` now requires that we pass it an engine. None of the manager classes take an engine in their constructor.
* | Stop using `Arel::Table.engine`Sean Griffin2014-11-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | We never actually make use of it on the table, since we're constructing the select manager manually. It looks like if we ever actually were grabbing it from the table, we're grossly misusing it since it's meant to vary by AR class. Its existence on `Arel::Table` appears to be purely for convenience methods that are never used outside of tests. However, in production code it just complicates construction of the tables on the rails side, and the plan is to remove it from `Arel::Table` entirely. I'm not convinced it needs to live on `SelectManager`, etc either.
* | Fix includes on association with a scope containing joins along with conditionssiddharth@vinsol.com2014-11-211-4/+2
| | | | | | | | on the joined assoiciation
* | PERF: avoid string allocationsSam2014-11-201-1/+6
| |
* | rm `reorder_bind_params`Sean Griffin2014-11-171-10/+0
| | | | | | | | | | | | Arel handles this for us automatically. Updated tests, as BindParam is no longer a subclass of SqlLiteral. We should remove the second argument to substitute_at entirely, as it's no longer used
* | Use a bound parameter for the "id = " portion of update statementsSean Griffin2014-11-011-3/+6
| | | | | | | | | | | | We need to re-order the bind parameters since the AST returned by the relation will have the where statement as the first bp, which breaks on PG.