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

Closes #148 Refactored Where structure to a binary tree for lazy evaluation #149

Merged
merged 1 commit into from
Dec 25, 2021

Conversation

vestrel00
Copy link
Owner

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.

* ```
* WHERE (data1 = 'johnson' AND data1 = 'colorado')
* ```
* ### Binary tree structure
Copy link
Owner Author

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(
Copy link
Owner Author

@vestrel00 vestrel00 Dec 25, 2021

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.
Copy link
Owner Author

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 {
Copy link
Owner Author

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)"
Copy link
Owner Author

@vestrel00 vestrel00 Dec 25, 2021

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 😁

@vestrel00 vestrel00 merged commit 562ac1a into main Dec 25, 2021
@vestrel00 vestrel00 deleted the 148-where-lazy-evaluation branch December 25, 2021 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor This may involve a refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant