/common/libraries/php/storage/mdb2/condition_translator.class.php
PHP | 282 lines | 195 code | 34 blank | 53 comment | 18 complexity | 5045dad9b2a49d38c229de3eef1d8438 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
- <?php
- namespace common\libraries;
-
- use Exception;
-
- /**
- * @package common.libraries.storage.mdb2
- * @author Hans De Bisschop
- */
-
- class Mdb2ConditionTranslator extends ConditionTranslator
- {
-
- /**
- * Translates an aggregate condition to a SQL WHERE clause.
- * @param AggregateCondition $condition The AggregateCondition object.
- * @param array $parameters A reference to the query's parameter list.
- * @param boolean $storage_unit Whether or not to
- * prefix learning
- * object properties
- * to avoid collisions.
- * @return string The WHERE clause.
- */
- function translate_aggregate_condition($aggregate_condition)
- {
- $string = '';
-
- if ($aggregate_condition instanceof AndCondition || $aggregate_condition instanceof OrCondition)
- {
- $condition_translations = array();
- $count = 0;
-
- foreach ($aggregate_condition->get_conditions() as $key => $condition)
- {
- $count ++;
- $translation = $this->translate($condition);
-
- if (! empty($translation))
- {
- $condition_translations[] = $translation;
- }
- }
-
- if (count($condition_translations) > 0)
- {
- $string = '(' . implode($aggregate_condition->get_operator(), $condition_translations) . ')';
- }
- }
- elseif ($aggregate_condition instanceof NotCondition)
- {
- $string .= 'NOT (';
- $string .= $this->translate($aggregate_condition->get_condition());
- $string .= $this->strings[] = ')';
- }
- else
- {
- die('Cannot translate aggregate condition');
- }
-
- return $string;
- }
-
- /**
- * Translates an in condition to a SQL WHERE clause.
- * @param InCondition $condition The InCondition object.
- * @param array $parameters A reference to the query's parameter list.
- * @param boolean $storage_unit Whether or not to
- * prefix learning
- * object properties
- * to avoid collisions.
- * @return string The WHERE clause.
- */
- function translate_in_condition($condition)
- {
- $storage_unit = $this->get_storage_unit();
- $condition_storage_unit = $condition->get_storage_unit();
-
- if (! is_null($condition_storage_unit))
- {
- if ($condition->is_alias())
- {
- $storage_unit = $condition_storage_unit;
- }
- else
- {
- $storage_unit = $this->get_data_manager()->get_alias($condition_storage_unit);
- }
- }
-
- if ($condition instanceof InCondition)
- {
- $name = $condition->get_name();
- $values = $condition->get_values();
-
- if (! is_array($values))
- {
- $values = array($values);
- }
-
- if (count($values) > 0)
- {
- $where_clause = array();
- $where_clause[] = $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' IN (';
-
- $placeholders = array();
- foreach ($values as $value)
- {
- $placeholders[] = $this->get_data_manager()->quote($value);
- }
-
- $where_clause[] = implode(',', $placeholders);
- $where_clause[] = ')';
- return implode('', $where_clause);
- }
- else
- {
- return 'true=false';
- }
- }
- else
- {
- die('Cannot translate in condition');
- }
- }
-
- function translate_subselect_condition($condition)
- {
- if ($condition instanceof SubselectCondition)
- {
- $storage_unit = $this->get_storage_unit();
-
- $name = $condition->get_name();
- $value = $condition->get_value();
- $table = $condition->get_storage_unit_value();
- $name_table = $condition->get_storage_unit_name();
-
- if ($condition->get_data_manager())
- {
- $etable = $condition->get_data_manager()->escape_table_name($table);
- }
- else
- {
- $etable = $this->get_data_manager()->escape_table_name($table);
- }
-
- $sub_condition = $condition->get_condition();
-
- $alias = $this->get_data_manager()->get_alias($table);
- $alias_name = null;
- if ($name_table)
- {
- $alias_name = $this->get_data_manager()->get_alias($name_table);
- }
-
- $this->set_storage_unit($alias);
- $string = $this->get_data_manager()->escape_column_name($name, $alias_name) . ' IN ( SELECT ' . $this->get_data_manager()->escape_column_name($value, $alias) . ' FROM ' . $etable . ' AS ' . $alias;
-
- if ($sub_condition)
- {
- $string .= ' WHERE ';
- $string .= $this->translate($sub_condition);
- }
-
- $string .= ')';
- $this->set_storage_unit($storage_unit);
- }
- else
- {
- die('Cannot translate in condition');
- }
-
- return $string;
- }
-
- /**
- * Translates a simple condition to a SQL WHERE clause.
- * @param Condition $condition The Condition object.
- * @param array $parameters A reference to the query's parameter list.
- * @param boolean $storage_unit Whether or not to
- * prefix learning
- * object properties
- * to avoid collisions.
- * @return string The WHERE clause.
- */
- function translate_simple_condition($condition)
- {
- $storage_unit = $this->get_storage_unit();
- $data_manager = $this->get_data_manager();
-
- $name = $condition->get_name();
- $condition_storage_unit = $condition->get_storage_unit();
-
- if (! is_null($condition_storage_unit))
- {
- if ($condition->is_alias())
- {
- $storage_unit = $condition_storage_unit;
- }
- else
- {
- $storage_unit = $this->get_data_manager()->get_alias($condition_storage_unit);
- }
- }
-
- if ($condition instanceof EqualityCondition)
- {
- $value = $condition->get_value();
-
- if (is_null($value))
- {
- return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' IS NULL';
- }
-
- return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' = ' . $this->get_data_manager()->quote($value);
- }
- elseif ($condition instanceof InequalityCondition)
- {
- $value = $condition->get_value();
-
- switch ($condition->get_operator())
- {
- case InequalityCondition :: GREATER_THAN :
- $operator = '>';
- break;
- case InequalityCondition :: GREATER_THAN_OR_EQUAL :
- $operator = '>=';
- break;
- case InequalityCondition :: LESS_THAN :
- $operator = '<';
- break;
- case InequalityCondition :: LESS_THAN_OR_EQUAL :
- $operator = '<=';
- break;
- default :
- dump($condition);
- die('Unknown operator for inequality condition');
- }
-
- return $this->get_data_manager()->escape_column_name($name, $storage_unit) . ' ' . $operator . ' ' . $this->get_data_manager()->quote($value);
- }
- elseif ($condition instanceof PatternMatchCondition)
- {
- return $this->get_data_manager()->escape_column_name($condition->get_name(), $storage_unit) . ' LIKE ' . $this->get_data_manager()->quote($this->translate_search_string($condition->get_pattern()));
- }
- else
- {
- return $condition; //die('Cannot translate condition');
- }
- }
-
- /**
- * Translates a string with wildcard characters "?" (single character)
- * and "*" (any character sequence) to a SQL pattern for use in a LIKE
- * condition. Should be suitable for any SQL flavor.
- * @param string $string The string that contains wildcard characters.
- * @return string The escaped string.
- */
- function translate_search_string($string)
- {
- /*
- ======================================================================
- * A brief explanation of these regexps:
- * - The first one escapes SQL wildcard characters, thus prefixing
- * %, ', \ and _ with a backslash.
- * - The second one replaces asterisks that are not prefixed with a
- * backslash (which escapes them) with the SQL equivalent, namely a
- * percent sign.
- * - The third one is similar to the second: it replaces question
- * marks that are not escaped with the SQL equivalent _.
- ======================================================================
- */
- return preg_replace(array('/([%\'\\\\_])/e', '/(?<!\\\\)\*/', '/(?<!\\\\)\?/'), array("'\\\\\\\\' . '\\1'",
- '%', '_'), $string);
- }
-
- function render_query($condition, $add_where = true)
- {
- return ($add_where ? ' WHERE ' : '') . $this->translate($condition);
- }
-
- }
- ?>