@@ -11,7 +11,7 @@ package io.github.jan.supabase.postgrest.query.filter
1111 */
1212data class FilterOperation (val column : String , val operator : FilterOperator , val value : Any? )
1313
14- fun FilterOperation.escapedValue (): String =
14+ fun FilterOperation.escapedValue (isInLogicalExpression : Boolean ): String =
1515 when (operator ) {
1616 FilterOperator .EQ ,
1717 FilterOperator .NEQ ,
@@ -23,8 +23,15 @@ fun FilterOperation.escapedValue(): String =
2323 FilterOperator .ILIKE ,
2424 FilterOperator .MATCH ,
2525 FilterOperator .IMATCH ,
26- FilterOperator .IS ->
27- escapeValue(value)
26+ FilterOperator .IS ,
27+ FilterOperator .FTS ,
28+ FilterOperator .WFTS ,
29+ FilterOperator .PHFTS ,
30+ FilterOperator .PLFTS ->
31+ if (isInLogicalExpression)
32+ escapeValue(value)
33+ else
34+ value.toString()
2835 FilterOperator .IN ->
2936 if (value is List <* >) {
3037 encodeAsList(value)
@@ -35,9 +42,15 @@ fun FilterOperation.escapedValue(): String =
3542 FilterOperator .CD ,
3643 FilterOperator .OV ->
3744 when (value) {
38- is List <* > -> encodeOverlapAsArray(value)
39- is Pair <* , * > -> encodeOverlapAsRange(value)
40- else -> escapeValue(value)
45+ is List <* > ->
46+ encodeOverlapAsArray(value)
47+ is Pair <* , * > ->
48+ encodeOverlapAsRange(value)
49+ else ->
50+ if (isInLogicalExpression)
51+ escapeValue(value)
52+ else
53+ value.toString()
4154 }
4255 FilterOperator .SL ,
4356 FilterOperator .SR ,
@@ -49,35 +62,30 @@ fun FilterOperation.escapedValue(): String =
4962 is List <* > -> encodeAsRange(value)
5063 else -> escapeValue(value)
5164 }
52- FilterOperator .FTS ,
53- FilterOperator .WFTS ,
54- FilterOperator .PHFTS ,
55- FilterOperator .PLFTS ->
56- escapeValue(value) // Do these need special handling?
5765 }
5866
5967private fun encodeAsList (values : List <* >): String =
6068 values.joinToString(" ," , prefix = " (" , postfix = " )" ) { escapeValue(it) }
6169
6270private fun encodeAsRange (range : Pair <* , * >): String =
63- " (${escapeValue( range.first) } ,${escapeValue( range.second) } )"
71+ " (${range.first} ,${range.second} )"
6472
6573private fun encodeAsRange (range : List <* >): String =
66- " (${escapeValue( range[0 ]) } ,${escapeValue( range[1 ]) } )"
74+ " (${range[0 ]} ,${range[1 ]} )"
6775
6876private fun encodeOverlapAsRange (range : Pair <* , * >): String =
69- " [${escapeValue( range.first) } ,${escapeValue( range.second) } ]"
77+ " [${range.first} ,${range.second} ]"
7078
7179private fun encodeOverlapAsArray (values : List <* >): String =
72- values.joinToString(" ," , prefix = " {" , postfix = " }" ) { escapeValue(it) }
80+ values.joinToString(" ," , prefix = " {" , postfix = " }" )
7381
7482private val quotedCharacters = listOf (" ," , " ." , " :" , " (" , " )" )
7583
7684internal fun escapeValue (value : Any? ): String {
7785 val asString = value.toString()
7886 .replace(" \\ " , " \\\\ " )
7987 .replace(" \" " , " \\\" " )
80- return if (value !is Number && quotedCharacters.any { asString.contains(it) }) {
88+ return if (quotedCharacters.any { asString.contains(it) }) {
8189 " \" $asString \" "
8290 } else {
8391 asString
0 commit comments