Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 4 additions & 51 deletions src/Phaseolies/Database/Entity/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2118,20 +2118,7 @@ public function getModel(): Model
*/
public function whereIn(string $field, array $values): self
{
$this->assertValidIdentifier($field);

if (empty($values)) {
return $this->where($field, '=', 'NULL');
}

if (strpos($field, '.') === false) {
$field = "{$this->table}.{$field}";
}

$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = ['AND', $field, 'IN', $values, "($placeholders)"];

return $this;
return $this->addInCondition('AND', $field, $values, 'IN');
}

/**
Expand All @@ -2143,17 +2130,7 @@ public function whereIn(string $field, array $values): self
*/
public function orWhereIn(string $field, array $values): self
{
$this->assertValidIdentifier($field);

if (empty($values)) {
$this->orWhere($field, '=', 'NULL');
return $this;
}

$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = ['OR', $field, 'IN', $values, "($placeholders)"];

return $this;
return $this->addInCondition('OR', $field, $values, 'IN');
}

/**
Expand All @@ -2167,7 +2144,7 @@ public function orWhereIn(string $field, array $values): self
*/
public function whereNotIn(string $field, array $values): self
{
return $this->addNotInCondition('AND', $field, $values);
return $this->addInCondition('AND', $field, $values, 'NOT IN');
}

/**
Expand All @@ -2179,33 +2156,9 @@ public function whereNotIn(string $field, array $values): self
*/
public function orWhereNotIn(string $field, array $values): self
{
return $this->addNotInCondition('OR', $field, $values);
return $this->addInCondition('OR', $field, $values, 'NOT IN');
}

/**
* Add a NOT IN condition with the given boolean
*
* @param string $boolean
* @param string $field
* @param array $values
* @return self
*/
private function addNotInCondition(string $boolean, string $field, array $values): self
{
$this->assertValidIdentifier($field);

if (strpos($field, '.') === false) {
$field = "{$this->tableReference()}.{$field}";
}

$values = array_values($values);
$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = [$boolean, $field, 'NOT IN', $values, "($placeholders)"];

return $this;
}


/**
* Load one-to-one relationships
*
Expand Down
45 changes: 24 additions & 21 deletions src/Phaseolies/Database/Entity/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,19 +527,7 @@ public function join(string $table, string $first, string $operator, string $sec
*/
public function whereIn(string $field, array $values): self
{
$this->assertValidIdentifier($field);

if (empty($values)) {
return $this->where($field, '=', 'NULL');
}

if (strpos($field, '.') === false) {
$field = "{$this->table}.{$field}";
}

$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = ['AND', $field, 'IN', $values, "($placeholders)"];
return $this;
return $this->addInCondition('AND', $field, $values, 'IN');
}

/**
Expand All @@ -551,16 +539,31 @@ public function whereIn(string $field, array $values): self
*/
public function orWhereIn(string $field, array $values): self
{
$this->assertValidIdentifier($field);
return $this->addInCondition('OR', $field, $values, 'IN');
}

if (empty($values)) {
$this->orWhere($field, '=', 'NULL');
return $this;
}
/**
* Add a WHERE NOT IN condition
*
* @param string $field
* @param array $values
* @return self
*/
public function whereNotIn(string $field, array $values): self
{
return $this->addInCondition('AND', $field, $values, 'NOT IN');
}

$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = ['OR', $field, 'IN', $values, "($placeholders)"];
return $this;
/**
* Add a OR WHERE NOT IN condition
*
* @param string $field
* @param array $values
* @return self
*/
public function orWhereNotIn(string $field, array $values): self
{
return $this->addInCondition('OR', $field, $values, 'NOT IN');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@

trait InteractsWithConditionBinding
{
/**
* Get the name the query's FROM clause refers to its table by:
* the alias for "table as alias" or "table alias", else the table.
*
* @return string
*/
protected function tableReference(): string
{
$table = trim($this->table);

if (preg_match('/^\S+\s+(?:as\s+)?(\S+)$/i', $table, $matches)) {
return $matches[1];
}

return $table;
}

/**
* Add an IN / NOT IN condition with the given boolean.
*
* @param string $boolean
* @param string $field
* @param array $values
* @param string $operator
* @return self
*/
protected function addInCondition(string $boolean, string $field, array $values, string $operator): self
{
$this->assertValidIdentifier($field);

if (strpos($field, '.') === false) {
$field = "{$this->tableReference()}.{$field}";
}

$values = array_values($values);
$placeholders = implode(',', array_fill(0, count($values), '?'));
$this->conditions[] = [$boolean, $field, $operator, $values, "($placeholders)"];

return $this;
}

/**
* Build the WHERE clause SQL string for any query type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,6 @@ protected function compileSoftDeleteConstraint(?string $table = null): ?string
: "{$qualified} IS NULL";
}

/**
* Get the name the query's FROM clause refers to its table by:
* the alias for "table as alias" or "table alias", else the table.
*
* @return string
*/
protected function tableReference(): string
{
$table = trim($this->table);

if (preg_match('/^\S+\s+(?:as\s+)?(\S+)$/i', $table, $matches)) {
return $matches[1];
}

return $table;
}

/**
* Build the default soft delete constraint for a related model used
* inside a hand-written subquery, as an " AND ..." fragment.
Expand Down
Loading
Loading