EASY markup interpretation to influence conditions-behavior

@param      $string
@param bool $set
@param bool $prepared

@return array|bool|string
@throws DbException
17695
protected function operandi($string, $set = false, $prepared = false) {
        if(empty($string) && $string !== "0") {
            return ($set ? ' = NULL' : ' IS NULL');
        }

        $firstLetter = strtolower(substr($string, 0, 1));
        switch($firstLetter) {
            case '>':
            case '<':
                $return = ' ' . $firstLetter . ' "' . intval(substr($string, 1)) . '"';
                break;
            case '.':
                $return = ' = NOW()';
                break;
            case '$':
                $rest = substr($string, 1);
                if($prepared){
                    $return = ' = UNHEX(?)';
                    $this->addExclusion($rest, 's');
                } else {
                    $return = ' = UNHEX("'.$rest.'")';
                }
                break;
            case '!':
                if(strtolower($string) == '!null' || strlen($string) == 1){
                    if($set){
                        $this->formatError(
                            [$string], 'Cannot set "NOT NULL" as value for "' . substr($string, 1) . '"'
                        );
                    }
                    $return = ' IS NOT NULL ';
                } else {
                    if($set){
                        $this->formatError([$string], 'Cannot use "!= ' . substr($string, 1) . '" to set a value');
                    }
                    $return = ' != "' . substr($string, 1) . '"';
                }
                break;
            case '{':
                $return = ' ' . substr($string, 1, -1);
                break;
            case '^':
                $return = ($set ? ' = NULL' : ' IS NULL');
                break;
            default:
                if(strtolower($string) == 'null'){
                    $return = ($set ? ' = NULL' : ' IS NULL');
                } elseif($prepared){
                    $return = ' = ? ';
                    $this->addExclusion($string);
                } else {
                    $return = ' = "' . $string . '"';
                }
                break;
        }
        return $return;
    }
1136303
public static function sql_prepare($str, $used_in_like = false)
    {
        if (!self::getInstance()->pdo_db) {
            self::getInstance()->connect();
        }

        if (is_array($str)) {
            foreach ($str as &$v) {
                $v = self::sql_prepare($v);
            }
        } else {
            $str = substr(self::getInstance()->pdo_db->quote(trim((string)$str)), 1, -1);

            if ($used_in_like) {
                $str = str_replace(['_', '%'], ['\_', '\%'], $str);
            }
        }

        return $str;
    }
857478
@Override
    public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
    {
        checkClosed();
        throw SQLError.noSupport();
    }
1309086
private int bindStatement(PreparedStatement stmt, int index, ExistsCriteria crit, ClassDescriptor cld) throws SQLException
    {
        Query subQuery = (Query) crit.getValue();

        // if query has criteria, bind them
        if (subQuery.getCriteria() != null && !subQuery.getCriteria().isEmpty())
        {
            return bindStatement(stmt, subQuery.getCriteria(), cld.getRepository().getDescriptorFor(subQuery.getSearchClass()), index);

            // otherwise, just ignore it
        }
        else
        {
            return index;
        }
    }
813652
private int bindStatement(PreparedStatement stmt, int index, InCriteria crit, ClassDescriptor cld) throws SQLException
    {
        if (crit.getValue() instanceof Collection)
        {
            Collection values = (Collection) crit.getValue();
            Iterator iter = values.iterator();

            while (iter.hasNext())
            {
                index = bindStatementValue(stmt, index, crit.getAttribute(), iter.next(), cld);
            }
        }
        else
        {
            index = bindStatementValue(stmt, index, crit.getAttribute(), crit.getValue(), cld);
        }
        return index;
    }
363998
private static String modify(final String str, final String[] set, final boolean expect) {
        final CharSet chars = CharSet.getInstance(set);
        final StringBuilder buffer = new StringBuilder(str.length());
        final char[] chrs = str.toCharArray();
        for (final char chr : chrs) {
            if (chars.contains(chr) == expect) {
                buffer.append(chr);
            }
        }
        return buffer.toString();
    }
666269
@Override
	public ResultSet doQuery(final SqlContext sqlContext, final PreparedStatement preparedStatement,
			final ResultSet resultSet) throws SQLException {
		if (getFilters().isEmpty()) {
			return resultSet;
		}
		ResultSet rs = resultSet;
		for (final SqlFilter filter : getFilters()) {
			rs = filter.doQuery(sqlContext, preparedStatement, rs);
		}
		return rs;
	}
802986
@Override
	public ResultSet doQuery(final SqlContext sqlContext, final PreparedStatement preparedStatement,
			final ResultSet resultSet) throws SQLException {
		return resultSet;
	}
92373
public function query($sql, $bind = [], $cache = false) {
        $items = null;
        $dbCache = null;
        if ($cache) {
            /* @var $dbCache DBCache */
            $dbCache = Ioc::get(DBCache::class);
            $items = $dbCache->queryCache($sql, $bind);
        }
        if (!$items) {
            $items = [];
            $this->execute($sql, $bind, false);
            $procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
            if ($procedure) {
                do {
                    $result = $this->PDOStatement->fetchAll(PDO::FETCH_ASSOC);
                    if ($result) {
                        $items[] = $result;
                    }
                } while ($this->PDOStatement->nextRowset());

            } else {
                $items = $this->PDOStatement->fetchAll(PDO::FETCH_ASSOC);
            }
            $this->PDOStatement = null;
            if ($cache) {
                $dbCache->saveCache($sql, $bind, $items);
            }
        }
        return $items;
    }
749097
@SuppressWarnings("unchecked")
	public static List<String> allStrings(PreparedStatement stmt) throws SQLException {
		return (List<String>) all(stmt, String.class);
	}
1149531
@Override
    public PoolablePreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
        return prepareStatement(sql, autoGeneratedKeys, -1, -1, -1);
    }
1040598
public function subset( \PhpSets\Set $set ) {
        $iterator = $set->entries();

        // iterate through $set and return false is an uncommon value is present
        while ( $iterator->valid() ) {
            if ( ! $this->has( $iterator->current() ) ) {
                return false;
            }
            $iterator->next();
        }

        $iterator->rewind();

        return true;
    }
594620
private function prepareCondition($condition)
    {
        if (!is_array($condition)) {
            $condition = ['in' => [$condition]];
        }

        $conditionKeys = array_keys($condition);

        if (is_integer(current($conditionKeys))) {
            $condition = ['in' => $condition];
        }

        foreach ($condition as $key => $value) {
            if (!isset($this->mappedConditions[$key]) && !in_array($key, $this->rangeConditions)) {
                throw new \LogicException("Condition {$key} is not supported.");
            }

            if (isset($this->mappedConditions[$key])) {
                $condition[$this->mappedConditions[$key]] = $value;
                unset($condition[$key]);
            }

            if (in_array($key, $this->negativeConditions)) {
                $condition['negative'] = true;
            }
        }

        return $condition;
    }
1339964
public static PreparedStatement prepareStatement(Connection conn, String sql, Collection<Object> params) throws SQLException {
		return prepareStatement(conn, sql, params.toArray(new Object[params.size()]));
	}
1296718
public function isConditionalClause($string) {
        $matched= false;
        if (is_string($string)) {
            if (!static::isValidClause($string)) {
                throw new xPDOException("SQL injection attempt detected: {$string}");
            }
            foreach ($this->_operators as $operator) {
                if (strpos(strtoupper($string), $operator) !== false) {
                    $matched= true;
                    break;
                }
            }
        }
        return $matched;
    }
1258917
public function setGetfield($string)
	{
		if (!is_null($this->getPostfields()))
		{
			throw new \Exception('You can only choose get OR post / post fields.');
		}
		$getfields = preg_replace('/^\?/', '', explode('&', $string));
		$params = array();
		foreach ($getfields as $field)
		{
			if ($field !== '')
			{
				list($key, $value) = explode('=', $field);
				$params[$key] = $value;
			}
		}
		$this->getfield = '?' . http_build_query($params, '', '&');
		return $this;
	}
728119
public int execSelectCount(String preparedSql, Object[] searchKeys) {
    log("#execSelectCount preparedSql=" + preparedSql + " searchKeys=" + searchKeys);
    int retVal = 0;

    PreparedStatement preparedStmt = null;
    ResultSet rs = null;

    final Connection conn = createConnection();

    try {

      preparedStmt = conn.prepareStatement(preparedSql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

      final StringBuilder logSb = new StringBuilder();

      if (searchKeys != null) {
        logSb.append("/ ");
        for (int i = 0; i < searchKeys.length; i++) {

          setObject((i + 1), preparedStmt, searchKeys[i]);

          logSb.append("key(" + (i + 1) + ")=" + searchKeys[i]);
          logSb.append(" ");
        }
      }

      log("#execSelectCount SQL=" + preparedSql + " " + logSb.toString());

      // execute SQL
      rs = preparedStmt.executeQuery();

      while (rs.next()) {
        retVal = rs.getInt(1);
      }

    } catch (Exception e) {
      loge("#execSelectCount", e);

    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (preparedStmt != null) {
          preparedStmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {

        loge("#execSelectCount", e);
      }
    }
    return retVal;

  }