Skip to content

Empty array in whereEquals() causes SQL syntax error #13

@paigeruten

Description

@paigeruten

Hello, thank you for your work on this excellent library! There's just this one annoying case that keeps coming up ever since we started using it. Here's an example of where it might happen:

$user_ids = getUserIdsByAccountId($account_id);
$settings = getSettingsByUserIds($user_ids);

function getSettingsByUserIds(array $user_ids): array {
    return Select::new($conn)
        ->columns('*')
        ->from('settings')
        ->whereEquals(['user_id' => $user_ids])
        ->fetchAll();
}

This causes an SQL syntax error if there happens to be no users in that account (i.e. $user_ids is an empty array). It produces the query SELECT * FROM settings WHERE user_id IN ().

So we have to keep writing lines like if ($user_ids === []) { return []; } everywhere to handle this case.

I'm wondering if it would make sense for Atlas to handle this case by mapping it to a false condition: SELECT * FROM settings WHERE false? Then no rows are returned, which to me is the expected result of an empty array condition. You could also see it as making up for a shortcoming in SQL's syntax.

What do you think? (Sorry if this has already been considered before.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions