Add in/inArray; expand Field ctr func names
This commit is contained in:
179
src/Field.php
179
src/Field.php
@@ -27,7 +27,7 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound
|
||||
* @param string $qualifier A table qualifier for the `data` column
|
||||
*/
|
||||
public function __construct(public string $fieldName = '', public Op $op = Op::EQ, public mixed $value = '',
|
||||
public function __construct(public string $fieldName = '', public Op $op = Op::Equal, public mixed $value = '',
|
||||
public string $paramName = '', public string $qualifier = '') { }
|
||||
|
||||
/**
|
||||
@@ -39,10 +39,10 @@ class Field
|
||||
public function appendParameter(array $existing): array
|
||||
{
|
||||
switch ($this->op) {
|
||||
case Op::EX:
|
||||
case Op::NEX:
|
||||
case Op::Exists:
|
||||
case Op::NotExists:
|
||||
break;
|
||||
case Op::BT:
|
||||
case Op::Between:
|
||||
$existing["{$this->paramName}min"] = $this->value[0];
|
||||
$existing["{$this->paramName}max"] = $this->value[1];
|
||||
break;
|
||||
@@ -68,16 +68,17 @@ class Field
|
||||
};
|
||||
$fieldPath = match ($mode) {
|
||||
Mode::PgSQL => match (true) {
|
||||
$this->op === Op::BT => is_numeric($this->value[0]) ? "($fieldName)::numeric" : $fieldName,
|
||||
is_numeric($this->value) => "($fieldName)::numeric",
|
||||
default => $fieldName,
|
||||
$this->op === Op::Between => is_numeric($this->value[0]) ? "($fieldName)::numeric" : $fieldName,
|
||||
is_numeric($this->value) => "($fieldName)::numeric",
|
||||
default => $fieldName,
|
||||
},
|
||||
default => $fieldName,
|
||||
};
|
||||
$criteria = match ($this->op) {
|
||||
Op::EX, Op::NEX => '',
|
||||
Op::BT => " {$this->paramName}min AND {$this->paramName}max",
|
||||
default => " $this->paramName",
|
||||
Op::Exists, Op::NotExists => '',
|
||||
Op::Between => " {$this->paramName}min AND {$this->paramName}max",
|
||||
Op::In => "TODO",
|
||||
default => " $this->paramName",
|
||||
};
|
||||
return $fieldPath . ' ' . $this->op->toSQL() . $criteria;
|
||||
}
|
||||
@@ -90,9 +91,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function equal(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::Equal, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an equals (=) field criterion _(alias for `Field.equal`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for which equality will be checked
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function EQ(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::EQ, $value, $paramName);
|
||||
return self::equal($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,9 +117,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function greater(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::Greater, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a greater than (>) field criterion _(alias for `Field.greater`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for the greater than comparison
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function GT(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::GT, $value, $paramName);
|
||||
return self::greater($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,9 +143,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function greaterOrEqual(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::GreaterOrEqual, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a greater than or equal to (>=) field criterion _(alias for `Field.greaterOrEqual`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for the greater than or equal to comparison
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function GE(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::GE, $value, $paramName);
|
||||
return self::greaterOrEqual($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,9 +169,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function less(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::Less, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a less than (<) field criterion _(alias for `Field.less`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for the less than comparison
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function LT(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::LT, $value, $paramName);
|
||||
return self::less($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,9 +195,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function lessOrEqual(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::LessOrEqual, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a less than or equal to (<=) field criterion _(alias for `Field.lessOrEqual`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for the less than or equal to comparison
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function LE(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::LE, $value, $paramName);
|
||||
return self::lessOrEqual($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,9 +221,22 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function notEqual(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::NotEqual, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a not equals (<>) field criterion _(alias for `Field.notEqual`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $value The value for the not equals comparison
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function NE(string $fieldName, mixed $value, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::NE, $value, $paramName);
|
||||
return self::notEqual($fieldName, $value, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,9 +248,49 @@ class Field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function between(string $fieldName, mixed $minValue, mixed $maxValue, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::Between, [$minValue, $maxValue], $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a BETWEEN field criterion _(alias for `Field.between`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the value will be compared
|
||||
* @param mixed $minValue The lower value for range
|
||||
* @param mixed $maxValue The upper value for the range
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function BT(string $fieldName, mixed $minValue, mixed $maxValue, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::BT, [$minValue, $maxValue], $paramName);
|
||||
return self::between($fieldName, $minValue, $maxValue, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an IN field criterion
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the values will be compared
|
||||
* @param mixed[] $values The potential matching values for the field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function in(string $fieldName, array $values, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::In, $values, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an IN ARRAY field criterion
|
||||
*
|
||||
* @param string $fieldName The name of the field against which the values will be compared
|
||||
* @param mixed[] $values The potential matching values for the field
|
||||
* @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank)
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function inArray(string $fieldName, array $values, string $paramName = ''): self
|
||||
{
|
||||
return new self($fieldName, Op::InArray, $values, $paramName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,9 +299,20 @@ class Field
|
||||
* @param string $fieldName The name of the field for which existence will be checked
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function exists(string $fieldName): self
|
||||
{
|
||||
return new self($fieldName, Op::Exists, '', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an exists (IS NOT NULL) field criterion _(alias for `Field.exists`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field for which existence will be checked
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function EX(string $fieldName): self
|
||||
{
|
||||
return new self($fieldName, Op::EX, '', '');
|
||||
return self::exists($fieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,8 +321,19 @@ class Field
|
||||
* @param string $fieldName The name of the field for which non-existence will be checked
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function notExists(string $fieldName): self
|
||||
{
|
||||
return new self($fieldName, Op::NotExists, '', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a not exists (IS NULL) field criterion _(alias for `Field.notExists`)_
|
||||
*
|
||||
* @param string $fieldName The name of the field for which non-existence will be checked
|
||||
* @return self The field with the requested criterion
|
||||
*/
|
||||
public static function NEX(string $fieldName): self
|
||||
{
|
||||
return new self($fieldName, Op::NEX, '', '');
|
||||
return self::notExists($fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
44
src/Op.php
44
src/Op.php
@@ -9,28 +9,32 @@ declare(strict_types=1);
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
/**
|
||||
* The types of logical operations allowed for JSON fields
|
||||
* The types of comparison operators allowed for JSON fields
|
||||
*/
|
||||
enum Op
|
||||
{
|
||||
/** Equals (=) */
|
||||
case EQ;
|
||||
case Equal;
|
||||
/** Greater Than (>) */
|
||||
case GT;
|
||||
case Greater;
|
||||
/** Greater Than or Equal To (>=) */
|
||||
case GE;
|
||||
case GreaterOrEqual;
|
||||
/** Less Than (<) */
|
||||
case LT;
|
||||
case Less;
|
||||
/** Less Than or Equal To (<=) */
|
||||
case LE;
|
||||
case LessOrEqual;
|
||||
/** Not Equal to (<>) */
|
||||
case NE;
|
||||
case NotEqual;
|
||||
/** Between (BETWEEN) */
|
||||
case BT;
|
||||
case Between;
|
||||
/** In (IN) */
|
||||
case In;
|
||||
/** In Array (PostgreSQL - ?|, SQLite - EXISTS / json_each / IN) */
|
||||
case InArray;
|
||||
/** Exists (IS NOT NULL) */
|
||||
case EX;
|
||||
case Exists;
|
||||
/** Does Not Exist (IS NULL) */
|
||||
case NEX;
|
||||
case NotExists;
|
||||
|
||||
/**
|
||||
* Get the SQL representation of this operator
|
||||
@@ -40,15 +44,17 @@ enum Op
|
||||
public function toSQL(): string
|
||||
{
|
||||
return match ($this) {
|
||||
Op::EQ => "=",
|
||||
Op::GT => ">",
|
||||
Op::GE => ">=",
|
||||
Op::LT => "<",
|
||||
Op::LE => "<=",
|
||||
Op::NE => "<>",
|
||||
Op::BT => "BETWEEN",
|
||||
Op::EX => "IS NOT NULL",
|
||||
Op::NEX => "IS NULL",
|
||||
Op::Equal => "=",
|
||||
Op::Greater => ">",
|
||||
Op::GreaterOrEqual => ">=",
|
||||
Op::Less => "<",
|
||||
Op::LessOrEqual => "<=",
|
||||
Op::NotEqual => "<>",
|
||||
Op::Between => "BETWEEN",
|
||||
Op::In => "IN",
|
||||
Op::InArray => "?|",
|
||||
Op::Exists => "IS NOT NULL",
|
||||
Op::NotExists => "IS NULL",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ class Query
|
||||
*/
|
||||
public static function whereById(string $paramName = ':id', mixed $docId = null): string
|
||||
{
|
||||
return self::whereByFields([Field::EQ(Configuration::$idField, $docId ?? '', $paramName)]);
|
||||
return self::whereByFields([Field::equal(Configuration::$idField, $docId ?? '', $paramName)]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user