提交 7d539937 编写于 作者: R Ryuta Kamizono

Make `in_batches` queries to preparable

Before:

```sql
SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > 2 ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > 4 ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > 6 ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > 8 ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > 10 ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
```

After:

```sql
SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ?  [["id", 4], ["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ?  [["id", 6], ["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ?  [["id", 8], ["LIMIT", 2]]
SELECT  "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ?  [["id", 10], ["LIMIT", 2]]
```
上级 ca18922a
......@@ -251,15 +251,22 @@ def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore:
end
end
batch_relation = relation.where(arel_attribute(primary_key).gt(primary_key_offset))
attr = Relation::QueryAttribute.new(primary_key, primary_key_offset, klass.type_for_attribute(primary_key))
batch_relation = relation.where(arel_attribute(primary_key).gt(Arel::Nodes::BindParam.new(attr)))
end
end
private
def apply_limits(relation, start, finish)
relation = relation.where(arel_attribute(primary_key).gteq(start)) if start
relation = relation.where(arel_attribute(primary_key).lteq(finish)) if finish
if start
attr = Relation::QueryAttribute.new(primary_key, start, klass.type_for_attribute(primary_key))
relation = relation.where(arel_attribute(primary_key).gteq(Arel::Nodes::BindParam.new(attr)))
end
if finish
attr = Relation::QueryAttribute.new(primary_key, finish, klass.type_for_attribute(primary_key))
relation = relation.where(arel_attribute(primary_key).lteq(Arel::Nodes::BindParam.new(attr)))
end
relation
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册