/** * Return the column name of a property. If property not exists just return $name * * @param $query * @param $columns * @return array */ public static function parseColumns($query, $columns) { if ($columns instanceof AnDomainResourceColumn) { return array($columns); } settype($columns, 'array'); $array = array(); foreach ($columns as $key => $column) { $result = strpos($column, ' ') !== false ? null : AnDomainQueryHelper::parseColumn($query, $column); $cols = $result['columns']; if (!isset($result['property'])) { $array[$key] = $column; } elseif (!is_array($cols)) { $array[$key] = $cols; } else { $key = 10000; foreach ($cols as $col) { $array[$key++] = $col; } } } return $array; }
/** * Builds a query into a final query statement. * * @param AnDomainQuery $query Query object * @param string $string A String object * * @return string */ public function parseMethods($query, $string) { //replaces any @col(\w+) pattern with the correct column name if (strpos($string, '@col(')) { $matches = array(); if (preg_match_all('/@col\\((.*?)\\)/', $string, $matches)) { $description = $query->getRepository()->getDescription(); $replaces = array(); foreach ($matches[1] as $match) { $result = AnDomainQueryHelper::parseColumn($query, $match); if (empty($result['columns'])) { $replaces[] = $match; } else { $replaces[] = (string) $result['columns']; } } $string = str_replace($matches[0], $replaces, $string); } } if (strpos($string, '@quote(')) { $matches = array(); $replaces = array(); if (preg_match_all('/@quote\\((.*?)\\)/', $string, $matches)) { foreach ($matches[1] as $match) { $replaces[] = $this->_store->quoteValue($match); } $string = str_replace($matches[0], $replaces, $string); } } if (strpos($string, '@instanceof(')) { $matches = array(); $replaces = array(); if (preg_match_all('/\\!?@instanceof\\((.*?)\\)/', $string, $matches)) { foreach ($matches[1] as $i => $match) { $operand = ''; if ($matches[0][$i][0] == '!') { $operand = 'NOT '; } $type_col = $query->getRepository()->getDescription()->getInheritanceColumn(); $classes = explode(',', $match); $statements = array(); foreach ($classes as $class) { $class = $this->_store->quoteValue($class); $statements[] = $operand . "FIND_IN_SET({$class},{$type_col})"; } if ($operand == 'NOT ') { $operand = ' AND '; } else { $operand = ' OR '; } if (count($statements) == 1) { $statements = implode($operand, $statements); } else { $statements = '(' . implode($operand, $statements) . ')'; } $replaces[] = $statements; } $string = str_replace($matches[0], $replaces, $string); } } if (strpos($string, '@remove_from_set(')) { $matches = array(); $replaces = array(); if (preg_match_all('/@remove_from_set\\((.*?)\\)/', $string, $matches)) { foreach ($matches[1] as $i => $match) { list($set, $item) = explode(',', $match); $set = trim($set); $item = trim($item); $replaces[] = "TRIM(BOTH ',' FROM REPLACE(concat(',',{$set},','),CONCAT(',',{$item},','),','))"; } $string = str_replace($matches[0], $replaces, $string); } } if (strpos($string, '@set_length(')) { $matches = array(); $replaces = array(); if (preg_match_all('/@set_length\\((.*?)\\)/', $string, $matches)) { foreach ($matches[1] as $i => $match) { $replaces[] = "LENGTH({$match}) - LENGTH(REPLACE({$match}, ',', '')) + 1"; } $string = str_replace($matches[0], $replaces, $string); } } return $string; }