Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Where group #186

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

Merged
merged 11 commits into from
Apr 24, 2020
Merged

Where group #186

merged 11 commits into from
Apr 24, 2020

Conversation

dpDesignz
Copy link
Contributor

@dpDesignz dpDesignz commented Apr 21, 2020

Resolves #164

UPDATE: There are no longer breaking changes

Example code from pdo\pdo_mysqlTest.php file:

$db->selecting(
    'unit_test', 
    '*', 
    where(
        eq('active', '1'),
        whereGroup(
           like('test_key', '%1%', _OR),
           like('test_key', '%3%')
        )
    )
);

or another example without the function

$db->selecting(
    'unit_test', 
    '*', 
    where(
        eq('active', '1'),
        like('test_key', '%1%', _OR, '('),
        like('test_key', '%3%', null, ')')
    )
);

The combiner will add itself at the end of the group.

Created the initial function but came across an issue where the $whereClause doesn't work in the $where method when using an OR combiner, so need to fix that first
Methods and functions complete but failed testing in phpunit so needing to fix
PHPUnit tests succeeded.
Cleaned up trailing combiner in tests
@@ -126,110 +127,110 @@ function createCertificate(
/**
* Creates an equality comparison expression with the given arguments.
*/
function eq($x, $y, $and = null, ...$args)
function eq($x, $y, $and = null, $group = null, ...$args)
Copy link

Choose a reason for hiding this comment

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

Method eq has 5 arguments (exceeds 4 allowed). Consider refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

All these bring in another required field? Whereas ...$args already covers that. You should try adding additional logic in the other methods instead to get the same results you are after.

The logic could be some kind of token/trigger word, to extract to get the desired action.
This is far beyond a breaking change, unnecessary, the logic should include a way for the current code to continue to work. The current tests should still pass.

return $expression;
}

/**
* Creates a non equality comparison expression with the given arguments.
*/
function neq($x, $y, $and = null, ...$args)
function neq($x, $y, $and = null, $group = null, ...$args)
Copy link

Choose a reason for hiding this comment

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

Method neq has 5 arguments (exceeds 4 allowed). Consider refactoring.

return $expression;
}

/**
* Creates the other non equality comparison expression with the given arguments.
*/
function ne($x, $y, $and = null, ...$args)
function ne($x, $y, $and = null, $group = null, ...$args)
Copy link

Choose a reason for hiding this comment

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

Method ne has 5 arguments (exceeds 4 allowed). Consider refactoring.

return $expression;
}

/**
* Creates a lower-than comparison expression with the given arguments.
*/
function lt($x, $y, $and = null, ...$args)
function lt($x, $y, $and = null, $group = null, ...$args)
Copy link

Choose a reason for hiding this comment

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

Method lt has 5 arguments (exceeds 4 allowed). Consider refactoring.

return $expression;
}

/**
* Creates a lower-than-equal comparison expression with the given arguments.
*/
function lte($x, $y, $and = null, ...$args)
function lte($x, $y, $and = null, $group = null, ...$args)
Copy link

Choose a reason for hiding this comment

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

Method lte has 5 arguments (exceeds 4 allowed). Consider refactoring.

@TheTechsTech
Copy link
Contributor

you will need to come up with a solution for the bc, to have tests pass as is.

Changed from breaking changes to use the existing extra `$args`. All existing PHPUnit tests run without failing.
@@ -485,6 +493,19 @@ function replace($table = '', $keyValue)
: false;
}

function flattenWhereConditions($whereConditions)
Copy link

Choose a reason for hiding this comment

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

Function flattenWhereConditions has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

This function should be in the class method being used in. It's a private helper function that should not be called directly by user/developer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point. I have moved this to the ezQuery file as a private method.

lib/ezQuery.php Outdated
}
}

public function whereGroup(...$whereConditions)
Copy link

Choose a reason for hiding this comment

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

Function whereGroup has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

What you think about just calling it group or grouping?
The only place it can be used in is with where already.
Repeating the same word within the same function call trying to stay away from, and keep naming somewhat similar to regular SQL dialect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Have updated the code and tests. I didn't want to call it group as that felt too close to groupBy for me and I thought it might get confused, but I like grouping.

Incorrectly made a modification to the return of the `where` method instead of the `whereGroup` method
@dpDesignz
Copy link
Contributor Author

you will need to come up with a solution for the bc, to have tests pass as is.

Thanks, I didn't even really think of using the existing $args, that makes way more sense. It took me most of the day to work out how it worked in the first place. Definitely learned a lot. 😄

Updated the code to use the existing options to avoid breaking changes.

@TheTechsTech
Copy link
Contributor

You will need to re-sync with master branch, the CI travis and appveyor systems and github see a conflicting file, and Travis still has a fail.

Synced with master branch to reduce conflicts
Synced with master branch to reduce conflicts
@dpDesignz
Copy link
Contributor Author

You will need to re-sync with master branch, the CI travis and appveyor systems and github see a conflicting file, and Travis still has a fail.

Thanks. Still learning how this all works with open source and larger projects. I've only really used GitHub for small personal projects until recently.

Both CI checks passed :)

Changed the method from `whereGroup` to `grouping` and moved the flatten function to be a private method in the `ezQuery` file as per @techno-express recommendations.
}
}

public function grouping(...$whereConditions)
Copy link

Choose a reason for hiding this comment

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

Function grouping has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

Added a brief example in the readme file underneath the shortcut methods. Will add a detailed example in the wiki once approved.
@TheTechsTech TheTechsTech merged commit c843017 into ezSQL:master Apr 24, 2020
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.

Grouping WHERE conditions with ezSQL v4 PDO
2 participants