Permalink
Browse files

new paginate method for query builder

  • Loading branch information...
1 parent d9d7997 commit 13e73c9816a62860d8dd52953b406fb665907d71 @musketyr musketyr committed Sep 17, 2013
View
38 core/src/main/groovyx/gaelyk/query/QueryBuilder.groovy
@@ -535,4 +535,42 @@ class QueryBuilder {
return this
}
+ /**
+ * Uses usually user supplied parameter map.
+ *
+ * <pre><code>
+ * if (params.limit) {
+ * limit (params.limit as int)
+ * chunkSize (params.limit as int)
+ * }
+ *
+ * if (params.cursor) {
+ * startAt (params.cursor as String)
+ * } else if (params.offset) {
+ * offset (params.offset as int)
+ * }
+ * </code></pre>
+ * It also sets the max page to 100 to prevent DDoS
+ * or any other user supplied value.
+ *
+ * @param params map containing one of more of following keys: limit, offset, cursor
+ * @param maxPage maximum (or default) limit
+ */
+ QueryBuilder paginate(Map params, int maxPage = 100) {
+ if (params.limit) {
+ int theLimit = Math.min(params.limit as int, maxPage)
+ limit theLimit
+ chunkSize theLimit
+ } else {
+ limit maxPage
+ }
+
+ if (params.cursor) {
+ startAt (params.cursor as String)
+ } else if (params.offset) {
+ offset (params.offset as int)
+ }
+ this
+ }
+
}
View
48 core/src/test/groovyx/gaelyk/querydsl/QueryDslTest.groovy
@@ -7,6 +7,8 @@ import org.codehaus.groovy.tools.ast.TransformTestHelper
import groovyx.gaelyk.query.QueryBuilder;
import groovyx.gaelyk.query.QueryDslTransformation
import org.codehaus.groovy.control.CompilePhase
+
+import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.Key
import com.google.appengine.api.datastore.KeyFactory
@@ -113,6 +115,7 @@ class QueryDslTest extends GroovyTestCase {
select all from persons where alive
''') ==
'SELECT * FROM persons WHERE alive = true'
+
}
@@ -535,4 +538,49 @@ class QueryDslTest extends GroovyTestCase {
QueryBuilder builder = clazz.newInstance().run()
assert builder.createQuery().kind == 'Kind'
}
+
+ void testPaginate() {
+ QueryBuilder query = new QueryBuilder()
+
+ assert !query.@options.limit
+ assert !query.@options.offset
+ assert !query.@options.chunkSize
+ assert !query.@options.startCursor
+
+ query = new QueryBuilder().paginate([:])
+
+ assert query.@options.limit == 100
+ assert !query.@options.offset
+ assert !query.@options.chunkSize
+ assert !query.@options.startCursor
+
+ query = new QueryBuilder().paginate(limit: "10")
+
+ assert query.@options.limit == 10
+ assert !query.@options.offset
+ assert query.@options.chunkSize == 10
+ assert !query.@options.startCursor
+
+ query = new QueryBuilder().paginate(limit: "10", offset: "20")
+
+ assert query.@options.limit == 10
+ assert query.@options.offset == 20
+ assert query.@options.chunkSize == 10
+ assert !query.@options.startCursor
+
+ query = new QueryBuilder().paginate(limit: "10", cursor: "ExQ")
+
+ assert query.@options.limit == 10
+ assert !query.@options.offset
+ assert query.@options.chunkSize == 10
+ assert query.@options.startCursor.toWebSafeString() == "ExQ"
+
+ query = new QueryBuilder().paginate(limit: "10", cursor: "ExQ", offset: 20)
+
+ assert query.@options.limit == 10
+ assert !query.@options.offset
+ assert query.@options.chunkSize == 10
+ assert query.@options.startCursor.toWebSafeString() == "ExQ"
+
+ }
}

0 comments on commit 13e73c9

Please sign in to comment.