Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping in LIKE expressions #87

Merged
merged 2 commits into from
Jul 25, 2023

Conversation

awrichar
Copy link
Contributor

@awrichar awrichar commented Jul 6, 2023

Explicitly specify '[' as the escape character in all cases, and use it to escape the wildcards '%' and '_' in LIKE expressions. Works for Postgres and SQLite.

Fixes #83

@codecov-commenter
Copy link

Codecov Report

Merging #87 (2cf8fb0) into main (da6668c) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main       #87   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           63        63           
  Lines         4778      4808   +30     
=========================================
+ Hits          4778      4808   +30     
Impacted Files Coverage Δ
pkg/dbsql/filter_sql.go 100.00% <100.00%> (ø)

@awrichar awrichar force-pushed the like-escape branch 2 times, most recently from 5610083 to 70ea3b0 Compare July 6, 2023 19:19
Comment on lines +36 to +50
// Split a map into a list of maps with a single entry each
func splitMap[T ~map[string]interface{}](m T) (exprs []T) {
for key, val := range m {
exprs = append(exprs, T{key: val})
}
return exprs
}

// Convert a list of Sqlizer operations to sq.And
func toAnd[T sq.Sqlizer](ops []T) (and sq.And) {
for _, op := range ops {
and = append(and, op)
}
return and
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be better ways to do this, but I wanted a way to add ESCAPE to every LIKE condition without writing the iterating logic 4 times below.

Note that this logic likely isn't even used in practice, as we only construct single-element maps anyway when using filterOp.

Explicitly specify '[' as the escape character in all cases, and use it to escape
the wildcards '%' and '_' in LIKE expressions. Works for Postgres and SQLite.

Fixes hyperledger#83

Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
@awrichar awrichar force-pushed the like-escape branch 2 times, most recently from 27e3ccc to 296d3c8 Compare July 6, 2023 19:23
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
Copy link
Contributor

@peterbroadhurst peterbroadhurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving approval with one thought on letting the linter have its opinion :)

return and
}

func (lk LikeEscape) ToSql() (sql string, args []interface{}, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think letting the linter be on this one with ToSQL is most consistent, rather than the linter override.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... the method has to be called ToSql in order to implement sq.Sqlizer. I think that means I have to add an inline override (which I could not for the life of me figure out how to do) or a global override.

@peterbroadhurst peterbroadhurst merged commit 33b402c into hyperledger:main Jul 25, 2023
@peterbroadhurst peterbroadhurst deleted the like-escape branch July 25, 2023 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Underscores in LIKE expression are not properly escaped
3 participants