method getConditions documentation in bbn\Db\Languages\Sql
Returns a string with the conditions for the ON, WHERE, or HAVING part of the query if there is, empty otherwise.
function(array $conditions, array $cfg = [], bool $is_having = false, int $indent = 0)
{
$res = '';
if (isset($conditions['conditions'], $conditions['logic'])) {
$logic = isset($conditions['logic']) && ($conditions['logic'] === 'OR') ? 'OR' : 'AND';
foreach ($conditions['conditions'] as $key => $f) {
if (\is_array($f) && isset($f['logic']) && isset($f['conditions'])) {
if ($tmp = $this->getConditions($f, $cfg, $is_having, $indent + 2)) {
$res .= (empty($res) ? '(' : PHP_EOL . str_repeat(' ', $indent) . "$logic (") .
$tmp . PHP_EOL . str_repeat(' ', $indent) . ")";
}
}
elseif (isset($f['operator'], $f['field'])) {
$field = $f['field'];
if (!array_key_exists('value', $f)) {
$f['value'] = false;
}
$is_number = false;
$is_null = true;
$is_uid = false;
$is_date = false;
$is_bool = false;
$model = null;
if ($is_having) {
$res .= PHP_EOL . str_repeat(' ', $indent) . (empty($res) ? '' : "$logic ") . $field . ' ';
}
elseif (isset($cfg['available_fields'][$field])) {
$table = $cfg['tables_full'][$cfg['available_fields'][$field]] ?? false;
$column = $this->colSimpleName($cfg['fields'][$field] ?? $field);
if ($table && $column && isset($cfg['models'][$table]['fields'][$column])) {
$model = $cfg['models'][$table]['fields'][$column];
$res .= PHP_EOL .
str_repeat(' ', $indent) .
(empty($res) ? '' : "$logic ") .
(
!empty($cfg['available_fields'][$field]) ?
$this->colFullName(
$cfg['fields'][$field] ?? $field,
$cfg['available_fields'][$field],
true
)
: $this->colSimpleName($column, true)
) . ' ';
}
else {
// Remove the alias from where and join but not in having except if it's a count
if (!$is_having && empty($table) && isset($cfg['fields'][$field])) {
$field = $cfg['fields'][$field];
// Same for exp in case it's an alias
if (!empty($f['exp']) && isset($cfg['fields'][$f['exp']])) {
$f['exp'] = $cfg['fields'][$f['exp']];
}
}
$res .= (empty($res) ? '' : PHP_EOL . str_repeat(' ', $indent) . $logic . ' ') . $field . ' ';
}
if (!empty($model)) {
$is_null = (bool)$model['null'];
if ($model['type'] === 'binary') {
$is_number = true;
if (($model['maxlength'] === 16) && !empty($model['key'])) {
$is_uid = true;
}
}
elseif (\in_array($model['type'], self::$numeric_types, true)) {
$is_number = true;
}
elseif (\in_array($model['type'], self::$date_types, true)) {
$is_date = true;
}
}
elseif ($f['value'] && Str::isUid($f['value'])) {
$is_uid = true;
}
elseif (\is_int($f['value']) || \is_float($f['value'])) {
$is_number = true;
}
}
else {
$res .= (empty($res) ? '' : PHP_EOL . str_repeat(' ', $indent) . $logic . ' ') . $field . ' ';
}
if (empty($f['exp']) && isset($f['value']) && in_array($f['value'], [1, 0, true, false], true)) {
// Always use LIKE as booleans and 1 and 0 are interpreted badly by MySQL
$is_bool = true;
}
switch (strtolower($f['operator'])) {
case '=':
if ($is_uid && $is_bool) {
$res .= isset($f['exp']) ? 'LIKE ' . $f['exp'] : 'LIKE ?';
}
else {
$res .= isset($f['exp']) ? '= ' . $f['exp'] : '= ?';
}
break;
case '!=':
if (isset($f['exp'])) {
$res .= '!= ' . $f['exp'];
}
else {
$res .= '!= ?';
}
break;
case 'like':
if (isset($f['exp'])) {
$res .= 'LIKE ' . $f['exp'];
}
else {
$res .= 'LIKE ?';
}
break;
case 'not like':
if (isset($f['exp'])) {
$res .= 'NOT LIKE ' . $f['exp'];
}
else {
$res .= 'NOT LIKE ?';
}
break;
case 'eq':
case 'is':
if ($is_uid && $is_bool) {
$res .= isset($f['exp']) ? 'LIKE ' . $f['exp'] : 'LIKE ?';
}
elseif ($is_uid) {
$res .= isset($f['exp']) ? '= ' . $f['exp'] : '= ?';
}
else {
$res .= isset($f['exp']) ? '= ' . $f['exp'] : ($is_number ? '= ?' : 'LIKE ?');
}
break;
case 'neq':
case 'isnot':
if ($is_uid) {
$res .= isset($f['exp']) ? '!= ' . $f['exp'] : '!= ?';
}
else {
$res .= isset($f['exp']) ? '!= ' . $f['exp'] : ($is_number ? '!= ?' : 'NOT LIKE ?');
}
break;
case 'doesnotcontains':
case 'doesnotcontain':
$res .= 'NOT LIKE ' . ($f['exp'] ?? '?');
break;
case 'endswith':
case 'startswith':
case 'contains':
$res .= 'LIKE ' . ($f['exp'] ?? '?');
break;
case 'gte':
case '>=':
if (isset($f['exp'])) {
$res .= '>= ' . $f['exp'];
}
else {
$res .= '>= ?';
}
break;
case 'gt':
case '>':
if (isset($f['exp'])) {
$res .= '> ' . $f['exp'];
}
else {
$res .= '> ?';
}
break;
case 'lte':
case '<=':
if (isset($f['exp'])) {
$res .= '<= ' . $f['exp'];
}
else {
$res .= '<= ?';
}
break;
case 'lt':
case '<':
if (isset($f['exp'])) {
$res .= '< ' . $f['exp'];
}
else {
$res .= '< ?';
}
break;
/** @todo Check if it is working with an array */
case 'isnull':
$res .= 'IS NULL';
break;
case 'isnotnull':
$res .= 'IS NOT NULL';
break;
case 'isempty':
$res .= $is_number ? '= 0' : "LIKE ''";
break;
case 'isnotempty':
$res .= $is_number ? '!= 0' : "NOT LIKE ''";
break;
case 'doesnotcontain':
$res .= $is_number ? '!= ?' : 'NOT LIKE ?';
break;
case 'contains':
$res .= 'LIKE ?';
break;
default:
$res .= $is_uid && $is_bool ? 'LIKE ?' : '= ?';
break;
}
}
}
}
if (!empty($res)) {
return str_replace(PHP_EOL . PHP_EOL, PHP_EOL, $res . PHP_EOL);
}
return $res;
}
Returns a string with the conditions for the ON, WHERE, or HAVING part of the query if there is, empty otherwise. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2023
BBN Solutions