-
Notifications
You must be signed in to change notification settings - Fork 37
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
Closes #148 Refactored Where structure to a binary tree for lazy evaluation #149
Conversation
* ``` | ||
* WHERE (data1 = 'johnson' AND data1 = 'colorado') | ||
* ``` | ||
* ### Binary tree structure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh boy! I happen to have built myself a binary tree 😁 Those interview leet code questions coming in to save the day!!!
*/ | ||
private fun where(field: Field, operator: String, value: Any?, options: String? = null): String { | ||
var where = "${field.columnName} $operator ${value.toSqlString()}" | ||
class Where<out T : Field> private constructor( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary constructor is private. Two secondary constructors are exposed to uphold the integrity of the binary tree structure.
options = options | ||
) | ||
} else if (lhs is WhereHolder && rhs is WhereHolder) { | ||
// Recursive case. Traverse tree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who knew that recursion would actually come in handy once in a while 🤣
// If there are mutable property values, then this will be evaluated at the time of invocation | ||
// and will not mutate along with the mutable property values (e.g. a mutable list). I don't | ||
// think consumers expect this to mutate anyways if they happen to save a reference to it. | ||
private val evaluatedWhereString: String by unsafeLazy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is where the transformation into a string occurs. Should be self-explanatory 😁
private val evaluatedWhereString: String by unsafeLazy { | ||
|
||
var whereString = when (operator) { | ||
is Operator.Combine -> "($lhs) $operator ($rhs)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the current structure, the Combine
operators (AND, OR) imply that the lhs
and rhs
are both un-evaluated Where
statements. So, this is recursion once again 😁
Most of the details are in #148.
The gist of this change is that the intrinsic value an instance of
Where
is now lazily evaluated. Previously,Where
only held a reference to the evaluated string. Now, it holds a reference to the left-hand-side, operator, and right-hand-side in a binary tree structure.This will allow us to support things that need
Where
to be mutated, such as #148 and #142.