Skip to content

Commit

Permalink
Merge pull request #98 from halillusion/master
Browse files Browse the repository at this point in the history
Added FIND_IN_SET support.
  • Loading branch information
izniburak authored Sep 20, 2021
2 parents 2df484c + e8e6342 commit 604e63e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ $db = new \Buki\Pdox($config);
* [where](#where)
* [grouped](#grouped)
* [in](#in)
* [findInSet](#findinset)
* [between](#between)
* [like](#like)
* [groupBy](#groupby)
Expand Down Expand Up @@ -296,6 +297,30 @@ $db->table('test')->where('active', 1)->orIn('id', [1, 2, 3])->getAll();
# Output: "SELECT * FROM test WHERE active = '1' OR id IN ('1', '2', '3')"
```

### findInSet
```php
$db->table('test')->where('active', 1)->findInSet('selected_ids', 1)->getAll();
# Output: "SELECT * FROM test WHERE active = '1' AND FIND_IN_SET (1, selected_ids)"
```

You can use this method in 4 ways. These;

- findInSet
- orFindInSet
- notFindInSet
- orNotFindInSet

Example:
```php
$db->table('test')->where('active', 1)->notFindInSet('selected_ids', 1)->getAll();
# Output: "SELECT * FROM test WHERE active = '1' AND NOT FIND_IN_SET (1, selected_ids)"

# OR

$db->table('test')->where('active', 1)->orFindInSet('selected_ids', 1)->getAll();
# Output: "SELECT * FROM test WHERE active = '1' OR FIND_IN_SET (1, selected_ids)"
```

### between
```php
$db->table('test')->where('active', 1)->between('age', 18, 25)->getAll();
Expand Down
58 changes: 58 additions & 0 deletions src/Pdox.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,64 @@ public function orNotIn($field, array $keys)
{
return $this->in($field, $keys, 'NOT ', 'OR');
}

/**
* @param string $field
* @param string $key
* @param string $type
* @param string $andOr
*
* @return $this
*/
public function findInSet(string $field, string $key, string $type = '', string $andOr = 'AND')
{
$key = is_numeric($key) ? $key : $this->escape($key);
$where = $type . 'FIND_IN_SET (' . $key . ', '.$field.')';

if ($this->grouped) {
$where = '(' . $where;
$this->grouped = false;
}

$this->where = is_null($this->where)
? $where
: $this->where . ' ' . $andOr . ' ' . $where;

return $this;
}

/**
* @param string $field
* @param string $key
*
* @return $this
*/
public function notFindInSet(string $field, string $key)
{
return $this->findInSet($field, $key, 'NOT ');
}

/**
* @param string $field
* @param string $key
*
* @return $this
*/
public function orFindInSet(string $field, string $key)
{
return $this->findInSet($field, $key, '', 'OR');
}

/**
* @param string $field
* @param string $key
*
* @return $this
*/
public function orNotFindInSet(string $field, string $key)
{
return $this->findInSet($field, $key, 'NOT ', 'OR');
}

/**
* @param string $field
Expand Down

0 comments on commit 604e63e

Please sign in to comment.