1. 29 1月, 2015 2 次提交
  2. 28 1月, 2015 6 次提交
    • 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
      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 8 次提交
    • 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_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
      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
      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 2 次提交
    • 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
  6. 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
  7. 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
  8. 06 1月, 2015 1 次提交
  9. 05 1月, 2015 1 次提交
  10. 03 12月, 2014 1 次提交
  11. 02 12月, 2014 1 次提交
  12. 30 11月, 2014 2 次提交
    • S
      Update Arel usage for rails/arel#98fc2599 · a975407a
      Sean Griffin 提交于
      `where_sql` now requires that we pass it an engine. None of the manager
      classes take an engine in their constructor.
      a975407a
    • S
      Stop using `Arel::Table.engine` · de239066
      Sean Griffin 提交于
      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.
      de239066
  13. 21 11月, 2014 1 次提交
  14. 20 11月, 2014 1 次提交
  15. 18 11月, 2014 1 次提交
    • S
      rm `reorder_bind_params` · c01b20b6
      Sean Griffin 提交于
      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
      c01b20b6
  16. 02 11月, 2014 3 次提交
    • S
      Use a bound parameter for the "id = " portion of update statements · 4b534152
      Sean Griffin 提交于
      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.
      4b534152
    • S
      [ci skip] `Relation#bind` is not public API · b380b1d2
      Sean Griffin 提交于
      b380b1d2
    • S
      Use bind values for joined tables in where statements · 10f75af9
      Sean Griffin 提交于
      In practical terms, this allows serialized columns and tz aware columns
      to be used in wheres that go through joins, where they previously would
      not behave correctly. Internally, this removes 1/3 of the cases where we
      rely on Arel to perform type casting for us.
      
      There were two non-obvious changes required for this. `update_all` on
      relation was merging its bind values with arel's in the wrong order.
      Additionally, through associations were assuming there would be no bind
      parameters in the preloader (presumably because the where would always
      be part of a join)
      
      [Melanie Gilman & Sean Griffin]
      10f75af9