1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical
21 
22 import org.make.core.Order
23 import scalikejdbc.PagingSQLBuilder
24 import org.make.core.technical.Pagination
25 
26 object PersistentServiceUtils {
27 
28   def sortOrderQuery[Persistent, Model](
29     offset: Pagination.Offset,
30     end: Option[Pagination.End],
31     sort: Option[String],
32     order: Option[Order],
33     query: PagingSQLBuilder[Persistent]
34   )(implicit companion: PersistentCompanion[Persistent, Model]): PagingSQLBuilder[Persistent] = {
35     val queryOrdered = (sort, order) match {
36       case (Some(field), Some(Order.desc)) if companion.columnNames.contains(field) =>
37         query
38           .orderBy(companion.alias.field(field))
39           .desc
40           .offset(offset.extractInt)
41       case (Some(field), _) if companion.columnNames.contains(field) =>
42         query
43           .orderBy(companion.alias.field(field))
44           .asc
45           .offset(offset.extractInt)
46       case (_, _) =>
47         query
48           .orderBy(companion.defaultSortColumns.toList: _*)
49           .asc
50           .offset(offset.extractInt)
51     }
52 
53     end.fold(queryOrdered)(end => queryOrdered.limit(end.extractInt - offset.extractInt))
54   }
55 }
Line Stmt Id Pos Tree Symbol Tests Code
36 20376 1302 - 1339 Apply scala.collection.SeqOps.contains companion.columnNames.contains[String](field)
38 21960 1376 - 1404 Apply scalikejdbc.SQLSyntaxSupportFeature.SQLSyntaxProvider.field companion.alias.field(field)
40 21010 1440 - 1457 Select org.make.core.technical.Pagination.extractInt offset.extractInt
40 20098 1351 - 1458 Apply scalikejdbc.QueryDSLFeature.PagingSQLBuilder.offset query.orderBy(companion.alias.field(field)).desc.offset(offset.extractInt)
41 19509 1490 - 1527 Apply scala.collection.SeqOps.contains companion.columnNames.contains[String](field)
43 21187 1564 - 1592 Apply scalikejdbc.SQLSyntaxSupportFeature.SQLSyntaxProvider.field companion.alias.field(field)
45 21803 1539 - 1645 Apply scalikejdbc.QueryDSLFeature.PagingSQLBuilder.offset query.orderBy(companion.alias.field(field)).asc.offset(offset.extractInt)
45 20137 1627 - 1644 Select org.make.core.technical.Pagination.extractInt offset.extractInt
48 21259 1700 - 1735 Select cats.data.NonEmptyList.toList companion.defaultSortColumns.toList
50 21962 1675 - 1792 Apply scalikejdbc.QueryDSLFeature.PagingSQLBuilder.offset query.orderBy((companion.defaultSortColumns.toList: _*)).asc.offset(offset.extractInt)
50 20327 1774 - 1791 Select org.make.core.technical.Pagination.extractInt offset.extractInt
53 21149 1804 - 1889 Apply scala.Option.fold end.fold[scalikejdbc.PagingSQLBuilder[Persistent]](queryOrdered)(((end: org.make.core.technical.Pagination.End) => queryOrdered.limit(end.extractInt.-(offset.extractInt))))
53 20954 1870 - 1887 Select org.make.core.technical.Pagination.extractInt offset.extractInt
53 19515 1834 - 1888 Apply scalikejdbc.QueryDSLFeature.PagingSQLBuilder.limit queryOrdered.limit(end.extractInt.-(offset.extractInt))
53 20065 1853 - 1887 Apply scala.Int.- end.extractInt.-(offset.extractInt)