1. 29 1月, 2015 3 次提交
  2. 28 1月, 2015 7 次提交
    • S
      Remove Relation#bind_params · b06f64c3
      Sean Griffin 提交于
      `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.
      b06f64c3
    • S
      Use an `Attribute` object to represent a bind value · 6c235dd9
      Sean Griffin 提交于
      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.
      6c235dd9
    • S
      Minor refactorings on `Relation#build_joins` · ae299dd4
      Sean Griffin 提交于
      Attempting to grok this code by refactoring it as I go through it.
      ae299dd4
    • S
      `WhereClause#predicates` does not need to be public · d26dd008
      Sean Griffin 提交于
      The only place it was accessed was in tests. Many of them have another
      way that they can test their behavior, that doesn't involve reaching
      into internals as far as they did. `AssociationScopeTest` is testing a
      situation where the where clause would have one bind param per
      predicate, so it can just ignore the predicates entirely. The where
      chain test was primarly duplicating the logic tested on `WhereClause`
      directly, so I instead just make sure it calls the appropriate method
      which is fully tested in isolation.
      d26dd008
    • S
      Use the `WhereClause` ast building logic for having · c2c95cd2
      Sean Griffin 提交于
      c2c95cd2
    • S
      Move where grouping into `WhereClause` · a5fcdae0
      Sean Griffin 提交于
      a5fcdae0
    • S
      Unify access to bind values on Relation · 16ce2eec
      Sean Griffin 提交于
      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.
      16ce2eec
  3. 27 1月, 2015 5 次提交
    • S
      Move the `from` bind logic to a `FromClause` class · bdc51416
      Sean Griffin 提交于
      Contrary to my previous commit message, it wasn't overkill, and led to
      much cleaner code.
      
      [Sean Griffin & anthonynavarre]
      bdc51416
    • S
      Remove `Relation#bind_values=` · 8436e2c2
      Sean Griffin 提交于
      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.
      8436e2c2
    • S
      765a3123
    • S
      Remove `Relation#build_where` · 76661dc6
      Sean Griffin 提交于
      All of its uses have been moved to better places
      76661dc6
    • S
      Change `having_values` to use the `WhereClause` class · 39f2c3b3
      Sean Griffin 提交于
      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]
      39f2c3b3
  4. 26 1月, 2015 13 次提交
    • S
      Remove `where_values` and `where_values=` · c414fc60
      Sean Griffin 提交于
      We've now removed all uses of them across the board. All logic lives on
      `WhereClause`.
      c414fc60
    • S
      Correct the implementation for `unscope(:where)` · fcb95d67
      Sean Griffin 提交于
      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
      fcb95d67
    • S
      Move `where_values_hash` over to `WhereClause` · 4b71ab08
      Sean Griffin 提交于
      4b71ab08
    • S
      Move `where_unscoping` logic over to `WhereClause` · d6110799
      Sean Griffin 提交于
      d6110799
    • S
      Remove most references to `where_values` in `QueryMethods` · 7227e4fb
      Sean Griffin 提交于
      We're still using it in `where_unscoping`, which will require moving
      additional logic.
      7227e4fb
    • S
      `Relation#Merger` can merge all clause methods · b6a9c620
      Sean Griffin 提交于
      This will make it easy to add `having_clause` and `join_clause` later.
      b6a9c620
    • S
      924127e2
    • S
      Move `where.not` logic into `WhereClause` · 2ae49dd0
      Sean Griffin 提交于
      2ae49dd0
    • S
      Move the construction of `WhereClause` objects out of `Relation` · 2da8f215
      Sean Griffin 提交于
      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.
      2da8f215
    • S
      320600c7
    • S
      Remove references to `:bind` in `except` · 87726b93
      Sean Griffin 提交于
      Bind values are no longer a thing, so this is unnecessary.
      87726b93
    • S
      Move where merging logic over to `WhereClause` · def2879d
      Sean Griffin 提交于
      This object being a black box, it knows the details of how to merge
      itself with another where clause. This removes all references to where
      values or bind values in `Relation::Merger`
      def2879d
    • S
      Introduce `Relation::WhereClause` · 2c46d6db
      Sean Griffin 提交于
      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.
      2c46d6db
  5. 25 1月, 2015 4 次提交
    • S
      Expand the number of types which can use prepared statements · 3327cd3f
      Sean Griffin 提交于
      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.
      3327cd3f
    • S
      Don't mutate `where_values` · ae8cd56c
      Sean Griffin 提交于
      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`)
      ae8cd56c
    • S
      Don't rely on relation mutability when building through associations · c80487eb
      Sean Griffin 提交于
      Specifically, the issue is relying on `where_unscoping` mutating the
      where values. It does not, however, mutate the bind values, which could
      cause an error under certain circumstances. This was not exposed by the
      tests, since the only place which would have been affected is unscoping
      a boolean, which doesn't go through prepared statements. I had a hard
      time getting better test coverage to demonstrate the issue.
      
      This in turn, caused `merge` to go through proper logic, and try to
      clear out the binds associated with the unscoped relation, which then
      exposed a source of `nil` for the columns, as binds weren't expanding
      `{ "posts.id" => 1 }` to `{ "posts" => { "id" => 1 } }`. This has been
      fixed.
      
      The bulk of `create_binds` needed to be moved to a separate method,
      since the dot notation should not be expanded recursively.
      
      I'm pretty sure this removes a subtle quirk that a ton of code in
      `Relation::Merger` is working around, and I suspect that code can be
      greatly simplified. However, unraveling that rats nest is no small task.
      c80487eb
    • S
      4c0a9922
  6. 24 1月, 2015 1 次提交
  7. 20 1月, 2015 4 次提交
    • S
      Fix bind value copying from subqueried relations · 04d1c371
      Sean Griffin 提交于
      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)
      04d1c371
    • S
      Move `create_binds` over to the `PredicateBuilder` · 50a8cdf0
      Sean Griffin 提交于
      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.
      50a8cdf0
    • S
      Whether a column exists or not doesn't affect whether we can use binds · 40887135
      Sean Griffin 提交于
      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.
      40887135
    • S
      Don't mutate bind values in `Relation` · 76d7d957
      Sean Griffin 提交于
      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`.
      76d7d957
  8. 10 1月, 2015 1 次提交
    • S
      Properly copy nested bind values from subqueried relations · ec475547
      Sean Griffin 提交于
      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
      ec475547
  9. 06 1月, 2015 1 次提交
  10. 05 1月, 2015 1 次提交