Skip to content

Add support for null and inclusion query conditions - #28

Merged
byjg merged 2 commits into
masterfrom
5.0
Aug 12, 2025
Merged

Add support for null and inclusion query conditions#28
byjg merged 2 commits into
masterfrom
5.0

Conversation

@byjg

@byjg byjg commented Aug 12, 2025

Copy link
Copy Markdown
Owner

Added new query methods: whereIsNull, whereIsNotNull, and whereIn. These enhancements extend the WhereTrait functionality to handle null checks and inclusion queries. Documentation and tests have been updated to ensure accuracy and reliability.

Description by Korbit AI

What change is being made?

Add new query methods whereIsNull, whereIsNotNull, and whereIn for handling IS NULL, IS NOT NULL, and IN conditions in SQL queries, and implement the setLiteral method to support literal values in UpdateQuery.

Why are these changes being made?

These changes enhance the library's functionality by providing native support for common SQL conditions, leading to more concise and readable query construction. The addition of the setLiteral method allows users to directly embed database functions and expressions in updates, increasing flexibility when performing complex updates involving calculated fields or database functions.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Implemented new methods in `WhereTrait` to support additional query conditions: `whereIsNull`, `whereIsNotNull`, and `whereIn`. Updated documentation and added comprehensive tests to validate functionality.

@korbit-ai korbit-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Functionality Empty IN clause handling ▹ view
Functionality Restrictive Type for SQL Literals ▹ view ✅ Fix detected
Security Unsanitized field names in NULL checks ▹ view
Performance Inefficient Unique Prefix Generation ▹ view ✅ Fix detected
Files scanned
File Path Reviewed
src/WhereTrait.php
src/UpdateQuery.php

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment thread src/WhereTrait.php
Comment on lines +106 to +110
public function whereIn(string $field, array $values): static
{
if (empty($values)) {
return $this;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Empty IN clause handling category Functionality

Tell me more
What is the issue?

The whereIn method silently returns without adding any condition when the values array is empty, which could lead to unexpected query results.

Why this matters

This behavior could inadvertently return all records instead of none when an empty array is provided, potentially exposing data that should be filtered out.

Suggested change ∙ Feature Preview

Modify the method to add a FALSE condition when values array is empty:

public function whereIn(string $field, array $values): static
{
    if (empty($values)) {
        $this->where[] = ['filter' => '1 = 0', 'params' => []];
        return $this;
    }
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/WhereTrait.php
*/
public function whereIsNull(string $field): static
{
$this->where[] = ['filter' => "$field IS NULL", 'params' => []];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unsanitized field names in NULL checks category Security

Tell me more
What is the issue?

The whereIsNull and whereIsNotNull methods directly inject the field name into the SQL without any sanitization.

Why this matters

This could lead to SQL injection if the field parameter contains malicious SQL code, compromising database security.

Suggested change ∙ Feature Preview

Add field name validation or use a prepared statement approach:

private function validateFieldName(string $field): void
{
    if (!preg_match('/^[a-zA-Z0-9_\.]+$/', $field)) {
        throw new InvalidArgumentException('Invalid field name');
    }
}

public function whereIsNull(string $field): static
{
    $this->validateFieldName($field);
    $this->where[] = ['filter' => "$field IS NULL", 'params' => []];
    return $this;
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/WhereTrait.php Outdated
Comment on lines +112 to +113
// Generate a unique prefix for this whereIn call
$uniquePrefix = 'in_' . md5($field . microtime(true)) . '_';

This comment was marked as resolved.

Comment thread src/UpdateQuery.php Outdated
* @param string $value
* @return $this
*/
public function setLiteral(string $field, string $value): UpdateQuery

This comment was marked as resolved.

…d optimized unique prefix generation for `whereIn`.
@byjg
byjg merged commit ba3291d into master Aug 12, 2025
4 checks passed
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.

1 participant