added fallback handling when both contains operands are table columns
This commit is contained in:
		
							parent
							
								
									b84930f21a
								
							
						
					
					
						commit
						93d48a85ac
					
				| 
						 | 
					@ -103,16 +103,28 @@ func (f FilterData) resolveTokenizedExpr(expr fexpr.Expr, fieldResolver FieldRes
 | 
				
			||||||
	case fexpr.SignNeq:
 | 
						case fexpr.SignNeq:
 | 
				
			||||||
		return dbx.NewExp(fmt.Sprintf("COALESCE(%s, '') != COALESCE(%s, '')", lName, rName), params), nil
 | 
							return dbx.NewExp(fmt.Sprintf("COALESCE(%s, '') != COALESCE(%s, '')", lName, rName), params), nil
 | 
				
			||||||
	case fexpr.SignLike:
 | 
						case fexpr.SignLike:
 | 
				
			||||||
 | 
							// both sides are columns and therefore wrap the right side with "%" for contains like behavior
 | 
				
			||||||
 | 
							if len(params) == 0 {
 | 
				
			||||||
 | 
								return dbx.NewExp(fmt.Sprintf("%s LIKE ('%%' || %s || '%%')", lName, rName), params), nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// normalize operands and switch sides if the left operand is a number or text
 | 
							// normalize operands and switch sides if the left operand is a number or text
 | 
				
			||||||
		if len(lParams) > 0 {
 | 
							if len(lParams) > 0 {
 | 
				
			||||||
			return dbx.NewExp(fmt.Sprintf("%s LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil
 | 
								return dbx.NewExp(fmt.Sprintf("%s LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return dbx.NewExp(fmt.Sprintf("%s LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil
 | 
							return dbx.NewExp(fmt.Sprintf("%s LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil
 | 
				
			||||||
	case fexpr.SignNlike:
 | 
						case fexpr.SignNlike:
 | 
				
			||||||
 | 
							// both sides are columns and therefore wrap the right side with "%" for not-contains like behavior
 | 
				
			||||||
 | 
							if len(params) == 0 {
 | 
				
			||||||
 | 
								return dbx.NewExp(fmt.Sprintf("%s NOT LIKE ('%%' || %s || '%%')", lName, rName), params), nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// normalize operands and switch sides if the left operand is a number or text
 | 
							// normalize operands and switch sides if the left operand is a number or text
 | 
				
			||||||
		if len(lParams) > 0 {
 | 
							if len(lParams) > 0 {
 | 
				
			||||||
			return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil
 | 
								return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", rName, lName), f.normalizeLikeParams(params)), nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil
 | 
							return dbx.NewExp(fmt.Sprintf("%s NOT LIKE %s", lName, rName), f.normalizeLikeParams(params)), nil
 | 
				
			||||||
	case fexpr.SignLt:
 | 
						case fexpr.SignLt:
 | 
				
			||||||
		return dbx.NewExp(fmt.Sprintf("%s < %s", lName, rName), params), nil
 | 
							return dbx.NewExp(fmt.Sprintf("%s < %s", lName, rName), params), nil
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,6 +32,34 @@ func TestFilterDataBuildExpr(t *testing.T) {
 | 
				
			||||||
				regexp.QuoteMeta("}") +
 | 
									regexp.QuoteMeta("}") +
 | 
				
			||||||
				"$",
 | 
									"$",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							// like with 2 columns
 | 
				
			||||||
 | 
							{"test1 ~ test2", false,
 | 
				
			||||||
 | 
								"^" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("[[test1]] LIKE ('%' || [[test2]] || '%')") +
 | 
				
			||||||
 | 
									"$",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// reversed like with text
 | 
				
			||||||
 | 
							{"'lorem' ~ test1", false,
 | 
				
			||||||
 | 
								"^" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("[[test1]] LIKE {:") +
 | 
				
			||||||
 | 
									".+" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("}") +
 | 
				
			||||||
 | 
									"$",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// not like with 2 columns
 | 
				
			||||||
 | 
							{"test1 !~ test2", false,
 | 
				
			||||||
 | 
								"^" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("[[test1]] NOT LIKE ('%' || [[test2]] || '%')") +
 | 
				
			||||||
 | 
									"$",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// reversed not like with text
 | 
				
			||||||
 | 
							{"'lorem' !~ test1", false,
 | 
				
			||||||
 | 
								"^" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("[[test1]] NOT LIKE {:") +
 | 
				
			||||||
 | 
									".+" +
 | 
				
			||||||
 | 
									regexp.QuoteMeta("}") +
 | 
				
			||||||
 | 
									"$",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		// current datetime constant
 | 
							// current datetime constant
 | 
				
			||||||
		{"test1 > @now", false,
 | 
							{"test1 > @now", false,
 | 
				
			||||||
			"^" +
 | 
								"^" +
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue