Additional ideas for the DaoHelpers query/sort utilities #62
sleberknight
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
These are some leftover ideas from
DaoHelpers
while I was re-implementing it with some improvements and additional features.Allow caller to specify the connector?
Currently we use "order by" as the initial "connector" and a comma as the sort field connector, e.g. given the base query
and sort fields
lastName
andfirstName
, the resulting query will be:This works fine for SQL, HQL, etc. which is the primary use case for
DaoHelpers
. So, is there any reason to permit other values?The
addSort
method is currentlyprivate
but if we wanted to make itpublic
to allow callers to add individual sorts, we'd probably want to restrict the values. We could create anenum
, for example:and then use this instead of a
String
argument, which would prevent possible SQL injections.Possible overloads to accept
String
query object, and return a newString
Currently all the methods accept a
StringBuilder
, and they mutate it. Would it be worth adding a bunch of new methods that acceptString query
and returnString
such that the originalquery
is not modified and the returned value is the original query plus the order clause?Add "quiet" methods that are no-ops if given any disallowed sort fields?
This would be yet more methods that are similar to the existing ones, but would never throw exceptions. Instead, if given disallowed sort fields they could log a warning and simply be a no-op.
Add option to NOT have
AllowedFields
?This would effectively let callers add anything, i.e. no security at all. For our own use cases, we always require that the sort fields be validated against a whitelist of allowed fields, so I'm leaning towards "no" on this one. Would we really want to allow no security?
Add "parameter object with builder" to allow building what you want to sort?
Instead of all the existing method overloads, we could change the entire thing to be a builder which lets you define how you want to sort things. This would let the caller specify a
String
or aStringBuilder
for thequery
, though in that case the builder would have to be "smart" and branch such that the terminal method is eithervoid
or returns aString
depending on whether the builder received aStringBuilder
orString
for the query. Examples:The class of the object returned by
DaoHelpers.sortBuilder()
could actually be a standalone class, e.g. aSortBuilder
class.More utilities to add pagination to query
This could be simple and allow you to add a
limit
and/oroffset
to the query, as one simple example.Reconsider the package and class names
While
AllowedFields
probably makes sense as a class name, consider whether there is a better name thanDaoHelpers
and whether there is a better package name thandao
. I only used them because that's what they are called in the service where I found the original versions. The versions here add enhancements to the original and more functionality.Beta Was this translation helpful? Give feedback.
All reactions