/
SparqlQuery.scala
659 lines (563 loc) · 23 KB
/
SparqlQuery.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
* Copyright © 2015-2018 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.messages.util.search
import akka.http.scaladsl.model.{HttpCharsets, MediaType}
import org.knora.webapi._
import org.knora.webapi.exceptions.{AssertionException, GravsearchException}
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.util.ApacheLuceneSupport.LuceneQueryString
/**
* Constants used in processing SPARQL queries.
*/
object SparqlQueryConstants {
/**
* The media type of SPARQL queries.
*/
val `application/sparql-query`: MediaType.WithFixedCharset = MediaType.customWithFixedCharset(
mainType = "application",
subType = "sparql-query",
charset = HttpCharsets.`UTF-8`,
fileExtensions = List("rq")
)
}
/**
* Represents something that can generate SPARQL source code.
*/
sealed trait SparqlGenerator {
def toSparql: String
}
/**
* Represents something that can be the subject, predicate, or object of a triple pattern in a query.
*/
sealed trait Entity extends Expression
/**
* Represents something that can be returned by a SELECT query.
*/
trait SelectQueryColumn extends Entity
/**
* Represents a variable in a query.
*
* @param variableName the name of the variable.
*/
case class QueryVariable(variableName: String) extends SelectQueryColumn {
override def toSparql: String = s"?$variableName"
}
/**
* Represents a GROUP_CONCAT statement that combines several values into one, separated by a character.
*
* @param inputVariable the variable to be concatenated.
* @param separator the separator to be used when concatenating the single results.
* @param outputVariableName the name of the variable representing the concatenated result.
*/
case class GroupConcat(inputVariable: QueryVariable, separator: Char, outputVariableName: String) extends SelectQueryColumn {
val outputVariable: QueryVariable = QueryVariable(outputVariableName)
override def toSparql: String = {
s"""(GROUP_CONCAT(DISTINCT(IF(BOUND(${inputVariable.toSparql}), STR(${inputVariable.toSparql}), "")); SEPARATOR='$separator') AS ${outputVariable.toSparql})"""
}
}
/**
* Represents a COUNT statement that counts how many instances/rows are returned for [[inputVariable]].
*
* @param inputVariable the variable to count.
* @param distinct indicates whether DISTINCT has to be used inside COUNT.
* @param outputVariableName the name of the variable representing the result.
*/
case class Count(inputVariable: QueryVariable, distinct: Boolean, outputVariableName: String) extends SelectQueryColumn {
val outputVariable: QueryVariable = QueryVariable(outputVariableName)
val distinctAsStr: String = if (distinct) {
"DISTINCT"
} else {
""
}
override def toSparql: String = {
s"(COUNT($distinctAsStr ${inputVariable.toSparql}) AS ${outputVariable.toSparql})"
}
}
/**
* Represents an IRI in a query.
*
* @param iri the IRI.
*/
case class IriRef(iri: SmartIri, propertyPathOperator: Option[Char] = None) extends Entity {
/**
* If this is a knora-api entity IRI, converts it to an internal entity IRI.
*
* @return the equivalent internal entity IRI.
*/
def toInternalEntityIri: IriRef = IriRef(iri.toOntologySchema(InternalSchema))
override def toSparql: String = {
if (propertyPathOperator.nonEmpty) {
s"${iri.toSparql}${propertyPathOperator.get}"
} else {
iri.toSparql
}
}
def toOntologySchema(targetSchema: OntologySchema): IriRef = {
copy(iri = iri.toOntologySchema(targetSchema))
}
}
/**
* Represents a literal value with an XSD type.
*
* @param value the literal value.
* @param datatype the value's XSD type IRI.
*/
case class XsdLiteral(value: String, datatype: SmartIri) extends Entity {
implicit private val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
override def toSparql: String = {
// We use xsd:dateTimeStamp in Gravsearch, but xsd:dateTime in the triplestore.
val transformedDatatype = if (datatype.toString == OntologyConstants.Xsd.DateTimeStamp) {
OntologyConstants.Xsd.DateTime.toSmartIri
} else {
datatype
}
"\"" + value + "\"^^" + transformedDatatype.toSparql
}
}
/**
* Represents a statement pattern or block pattern in a query.
*/
sealed trait QueryPattern extends SparqlGenerator
/**
* Represents a statement pattern in a query.
*
* @param subj the subject of the statement.
* @param pred the predicate of the statement.
* @param obj the object of the statement.
* @param namedGraph the named graph this statement should be searched in. Defaults to [[None]].
*/
case class StatementPattern(subj: Entity, pred: Entity, obj: Entity, namedGraph: Option[IriRef] = None) extends QueryPattern {
override def toSparql: String = {
val triple = s"${subj.toSparql} ${pred.toSparql} ${obj.toSparql} ."
namedGraph match {
case Some(graph) =>
s"""GRAPH ${graph.toSparql} {
| $triple
|}
|""".stripMargin
case None =>
triple + "\n"
}
}
def toOntologySchema(targetSchema: OntologySchema): StatementPattern = {
copy(
subj = entityToOntologySchema(subj, targetSchema),
pred = entityToOntologySchema(pred, targetSchema),
obj = entityToOntologySchema(obj, targetSchema)
)
}
private def entityToOntologySchema(entity: Entity, targetSchema: OntologySchema): Entity = {
entity match {
case iriRef: IriRef => iriRef.toOntologySchema(InternalSchema)
case other => other
}
}
}
/**
* A virtual query pattern representing a Lucene full-text index search. Will be replaced by triplestore-specific
* statements during Gravsearch processing.
*
* @param subj a variable representing the subject to be found.
* @param obj a variable representing the literal that is indexed.
* @param queryString the Lucene query string to be matched.
* @param literalStatement a statement that connects `subj` to `obj`. Needed with some triplestores but not others.
* Will be defined only if it has not already been added to the generated SPARQL.
*/
case class LuceneQueryPattern(subj: QueryVariable, obj: QueryVariable, queryString: LuceneQueryString, literalStatement: Option[StatementPattern]) extends QueryPattern {
override def toSparql: String = throw AssertionException("LuceneQueryPattern should have been transformed into statements")
}
/**
* Represents a BIND command in a query.
*
* @param variable the variable in the BIND.
* @param expression the expression to be bound to the variable.
*/
case class BindPattern(variable: QueryVariable, expression: Expression) extends QueryPattern {
override def toSparql: String = {
s"BIND(${expression.toSparql} AS ${variable.toSparql})\n"
}
}
/**
* Provides convenience methods for making statement patterns that are marked as needing inference or not.
*/
object StatementPattern {
/**
* Makes a [[StatementPattern]] whose named graph is [[OntologyConstants.NamedGraphs.KnoraExplicitNamedGraph]].
*
* @param subj the subject of the statement.
* @param pred the predicate of the statement.
* @param obj the object of the statement.
* @return the statement pattern.
*/
def makeExplicit(subj: Entity, pred: Entity, obj: Entity): StatementPattern = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
StatementPattern(
subj = subj,
pred = pred,
obj = obj,
namedGraph = Some(IriRef(OntologyConstants.NamedGraphs.KnoraExplicitNamedGraph.toSmartIri))
)
}
/**
* Makes a [[StatementPattern]] that doesn't specify a named graph.
*
* @param subj the subject of the statement.
* @param pred the predicate of the statement.
* @param obj the object of the statement.
* @return the statement pattern.
*/
def makeInferred(subj: Entity, pred: Entity, obj: Entity): StatementPattern = {
StatementPattern(
subj = subj,
pred = pred,
obj = obj,
namedGraph = None
)
}
}
/**
* Represents the supported logical operators in a [[CompareExpression]].
*/
object CompareExpressionOperator extends Enumeration {
val EQUALS: CompareExpressionOperator.Value = Value("=")
val GREATER_THAN: CompareExpressionOperator.Value = Value(">")
val GREATER_THAN_OR_EQUAL_TO: CompareExpressionOperator.Value = Value(">=")
val LESS_THAN: CompareExpressionOperator.Value = Value("<")
val LESS_THAN_OR_EQUAL_TO: CompareExpressionOperator.Value = Value("<=")
val NOT_EQUALS: CompareExpressionOperator.Value = Value("!=")
val valueMap: Map[String, Value] = values.map(v => (v.toString, v)).toMap
/**
* Given the name of a value in this enumeration, returns the value. If the value is not found, the provided error function is called.
*
* @param name the name of the value.
* @param errorFun the function to be called in case of an error.
* @return the requested value.
*/
def lookup(name: String, errorFun: => Nothing): Value = {
valueMap.get(name) match {
case Some(value) => value
case None => errorFun
}
}
}
/**
* Represents an expression that can be used in a FILTER.
*/
sealed trait Expression extends SparqlGenerator
/**
* Represents a comparison expression in a FILTER.
*
* @param leftArg the left argument.
* @param operator the operator.
* @param rightArg the right argument.
*/
case class CompareExpression(leftArg: Expression, operator: CompareExpressionOperator.Value, rightArg: Expression) extends Expression {
override def toSparql: String = s"(${leftArg.toSparql} $operator ${rightArg.toSparql})"
}
/**
* Represents an AND expression in a filter.
*
* @param leftArg the left argument.
* @param rightArg the right argument.
*/
case class AndExpression(leftArg: Expression, rightArg: Expression) extends Expression {
override def toSparql: String = s"(${leftArg.toSparql} && ${rightArg.toSparql})"
}
/**
* Represents an OR expression in a filter.
*
* @param leftArg the left argument.
* @param rightArg the right argument.
*/
case class OrExpression(leftArg: Expression, rightArg: Expression) extends Expression {
override def toSparql: String = s"(${leftArg.toSparql} || ${rightArg.toSparql})"
}
/**
* A trait representing arithmetic operators.
*/
sealed trait ArithmeticOperator
/**
* Represents the plus operator.
*/
case object PlusOperator extends ArithmeticOperator {
override def toString = "+"
}
/**
* Represents the minus operator.
*/
case object MinusOperator extends ArithmeticOperator {
override def toString = "-"
}
/**
* Represents an integer literal.
*/
case class IntegerLiteral(value: Int) extends Expression {
override def toSparql: String = value.toString
}
/**
* Represents an arithmetic expression.
*/
case class ArithmeticExpression(leftArg: Expression, operator: ArithmeticOperator, rightArg: Expression) extends Expression {
override def toSparql: String = s"""${leftArg.toSparql} $operator ${rightArg.toSparql}"""
}
/**
* Represents a regex function in a query (in a FILTER).
*
* @param textExpr the expression representing the text value or string literal to be checked against the provided pattern.
* @param pattern the REGEX pattern to be used.
* @param modifier the modifier to be used.
*/
case class RegexFunction(textExpr: Expression, pattern: String, modifier: Option[String]) extends Expression {
override def toSparql: String = modifier match {
case Some(modifierStr) =>
s"""regex(${textExpr.toSparql}, "$pattern", "$modifierStr")"""
case None =>
s"""regex(${textExpr.toSparql}, "$pattern")"""
}
}
/**
* Represents a lang function in a query (in a FILTER).
*
* @param textValueVar the variable representing the text value to be restricted to the specified language.
*/
case class LangFunction(textValueVar: QueryVariable) extends Expression {
override def toSparql: String = s"""lang(${textValueVar.toSparql})"""
}
/**
* Represents the SPARQL `substr` function.
*
* @param textLiteralVar the variable containing the string literal from which a substring is to be taken.
* @param startExpression an expression representing the 1-based index of the first character of the substring.
* @param lengthExpression the length of the substring.
*/
case class SubStrFunction(textLiteralVar: QueryVariable, startExpression: Expression, lengthExpression: Expression) extends Expression {
override def toSparql: String = s"""substr(${textLiteralVar.toSparql}, ${startExpression.toSparql}, ${lengthExpression.toSparql})"""
}
/**
* Represents the SPARQL `str` function.
*
* @param textLiteralVar the variable containing the string literal possibly with a language tag from which the string is to be taken.
*/
case class StrFunction(textLiteralVar: QueryVariable) extends Expression {
override def toSparql: String = s"""str(${textLiteralVar.toSparql})"""
}
/**
* Represents a function call in a filter.
*
* @param functionIri the IRI of the function.
* @param args the arguments passed to the function.
*/
case class FunctionCallExpression(functionIri: IriRef, args: Seq[Entity]) extends Expression {
override def toSparql: String = s"${functionIri.iri.toSparql}(${args.map(_.toSparql).mkString(", ")})"
/**
* Gets the argument at the given position as a [[QueryVariable]].
* Throws a [[GravsearchException]] no argument exists at the given position or if it is not a [[QueryVariable]].
*
* @param pos the argument to be returned from [[args]].
* @return a [[QueryVariable]].
*/
def getArgAsQueryVar(pos: Int): QueryVariable = {
if (args.size <= pos) throw GravsearchException(s"Not enough arguments given for call of $functionIri. ${args.size} are given, argument at position $pos is requested (0-based index)")
args(pos) match {
case queryVar: QueryVariable => queryVar
case other => throw GravsearchException(s"Variable required as function argument: $other")
}
}
/**
* Gets the argument at the given position as a [[XsdLiteral]] of the given datatype.
* Throws a [[GravsearchException]] no argument exists at the given position or if it is not a [[XsdLiteral]] of the requested datatype.
*
* @param pos the argument to be returned from [[args]].
* @param xsdDatatype the argeument's datatype.
* @return an [[XsdLiteral]].
*/
def getArgAsLiteral(pos: Int, xsdDatatype: SmartIri): XsdLiteral = {
if (args.size <= pos) throw GravsearchException(s"Not enough arguments given for call of $functionIri. ${args.size} are given, argument at position $pos is requested (0-based index)")
args(pos) match {
case literal: XsdLiteral if literal.datatype == xsdDatatype => literal
case other => throw GravsearchException(s"Literal of type $xsdDatatype required as function argument: $other")
}
}
}
/**
* Represents a FILTER pattern in a query.
*
* @param expression the expression in the FILTER.
*/
case class FilterPattern(expression: Expression) extends QueryPattern {
override def toSparql: String = s"FILTER(${expression.toSparql})\n"
}
/**
* Represents VALUES in a query.
*
* @param variable the variable that the values will be assigned to.
* @param values the IRIs that will be assigned to the variable.
*/
case class ValuesPattern(variable: QueryVariable, values: Set[IriRef]) extends QueryPattern {
override def toSparql: String = s"VALUES ${variable.toSparql} { ${values.map(_.toSparql).mkString(" ")} }\n"
}
/**
* Represents a UNION in the WHERE clause of a query.
*
* @param blocks the blocks of patterns contained in the UNION.
*/
case class UnionPattern(blocks: Seq[Seq[QueryPattern]]) extends QueryPattern {
override def toSparql: String = {
val blocksAsStrings = blocks.map {
block: Seq[QueryPattern] =>
val queryPatternStrings: Seq[String] = block.map {
queryPattern: QueryPattern => queryPattern.toSparql
}
queryPatternStrings.mkString
}
"{\n" + blocksAsStrings.mkString("} UNION {\n") + "}\n"
}
}
/**
* Represents an OPTIONAL in the WHERE clause of a query.
*
* @param patterns the patterns in the OPTIONAL block.
*/
case class OptionalPattern(patterns: Seq[QueryPattern]) extends QueryPattern {
override def toSparql: String = {
val queryPatternStrings: Seq[String] = patterns.map {
queryPattern: QueryPattern => queryPattern.toSparql
}
"OPTIONAL {\n" + queryPatternStrings.mkString + "}\n"
}
}
/**
* Represents a FILTER NOT EXISTS in a query.
*
* @param patterns the patterns contained in the FILTER NOT EXISTS.
*/
case class FilterNotExistsPattern(patterns: Seq[QueryPattern]) extends QueryPattern {
override def toSparql: String = s"FILTER NOT EXISTS {\n ${patterns.map(_.toSparql).mkString}\n}\n"
}
/**
* Represents a MINUS in a query.
*
* @param patterns the patterns contained in the MINUS.
*/
case class MinusPattern(patterns: Seq[QueryPattern]) extends QueryPattern {
override def toSparql: String = s"MINUS {\n ${patterns.map(_.toSparql).mkString}\n}\n"
}
/**
* Represents a CONSTRUCT clause in a query.
*
* @param statements the statements in the CONSTRUCT clause.
* @param querySchema if this is a Gravsearch query, represents the Knora API v2 ontology schema used in the query.
*/
case class ConstructClause(statements: Seq[StatementPattern], querySchema: Option[ApiV2Schema] = None) extends SparqlGenerator {
override def toSparql: String = "CONSTRUCT {\n" + statements.map(_.toSparql).mkString + "} "
}
/**
* Represents a WHERE clause in a query.
*
* @param patterns the patterns in the WHERE clause.
* @param positiveEntities if this is a Gravsearch query, contains the entities that are used in positive contexts
* in the WHERE clause, i.e. not in MINUS or FILTER NOT EXISTS.
* @param querySchema if this is a Gravsearch query, represents the Knora API v2 ontology schema used in the query.
*/
case class WhereClause(patterns: Seq[QueryPattern], positiveEntities: Set[Entity] = Set.empty[Entity], querySchema: Option[ApiV2Schema] = None) extends SparqlGenerator {
override def toSparql: String = "WHERE {\n" + patterns.map(_.toSparql).mkString + "}\n"
}
/**
* Represents a criterion to order by.
*
* @param queryVariable the variable used for ordering.
* @param isAscending indicates if the order is ascending or descending.
*/
case class OrderCriterion(queryVariable: QueryVariable, isAscending: Boolean) extends SparqlGenerator {
override def toSparql: String = if (isAscending) {
s"ASC(${queryVariable.toSparql})"
} else {
s"DESC(${queryVariable.toSparql})"
}
}
/**
* Represents a FROM clause.
*
* @param defaultGraph the graph to be used as the default graph in the query.
*/
case class FromClause(defaultGraph: IriRef) extends SparqlGenerator {
override def toSparql: String = s"FROM ${defaultGraph.toSparql}\n"
}
/**
* Represents a SPARQL CONSTRUCT query.
*
* @param constructClause the CONSTRUCT clause.
* @param fromClause the FROM clause, if any.
* @param whereClause the WHERE clause.
* @param orderBy the variables that the results should be ordered by.
* @param offset if this is a Gravsearch query, represents the OFFSET specified in the query.
* @param querySchema if this is a Gravsearch query, represents the Knora API v2 ontology schema used in the query.
*/
case class ConstructQuery(constructClause: ConstructClause, fromClause: Option[FromClause] = None, whereClause: WhereClause, orderBy: Seq[OrderCriterion] = Seq.empty[OrderCriterion], offset: Long = 0, querySchema: Option[ApiV2Schema] = None) extends SparqlGenerator {
override def toSparql: String = {
val stringBuilder = new StringBuilder
stringBuilder.append(constructClause.toSparql)
.append(fromClause.map(_.toSparql).getOrElse(""))
.append(whereClause.toSparql)
if (orderBy.nonEmpty) {
stringBuilder.append("ORDER BY ").append(orderBy.map(_.toSparql).mkString(" ")).append("\n")
}
if (offset > 0) {
stringBuilder.append("OFFSET ").append(offset)
}
stringBuilder.toString
}
}
/**
* Represents a SPARQL SELECT query.
*
* @param variables the variables to be returned by the query.
* @param useDistinct indicates if DISTINCT should be used.
* @param fromClause the FROM clause, if any.
* @param whereClause the WHERE clause.
* @param orderBy the variables that the results should be ordered by.
* @param limit the maximum number of result rows to be returned.
* @param offset the offset to be used (limit of the previous query + 1 to do paging).
*/
case class SelectQuery(variables: Seq[SelectQueryColumn], useDistinct: Boolean = true, fromClause: Option[FromClause] = None, whereClause: WhereClause, groupBy: Seq[QueryVariable] = Seq.empty[QueryVariable], orderBy: Seq[OrderCriterion] = Seq.empty[OrderCriterion], limit: Option[Int] = None, offset: Long = 0) extends SparqlGenerator {
override def toSparql: String = {
val stringBuilder = new StringBuilder
stringBuilder.append("SELECT ")
if (useDistinct) {
stringBuilder.append("DISTINCT ")
}
stringBuilder.append(variables.map(_.toSparql).mkString(" ")).append("\n")
.append(fromClause.map(_.toSparql).getOrElse(""))
.append(whereClause.toSparql)
if (groupBy.nonEmpty) {
stringBuilder.append("GROUP BY ").append(groupBy.map(_.toSparql).mkString(" ")).append("\n")
}
if (orderBy.nonEmpty) {
stringBuilder.append("ORDER BY ").append(orderBy.map(_.toSparql).mkString(" ")).append("\n")
}
if (offset > 0) {
stringBuilder.append("OFFSET ").append(offset).append("\n")
}
if (limit.nonEmpty) {
stringBuilder.append(s"LIMIT ${limit.get}").append("\n")
}
stringBuilder.toString
}
}