/** * If the $method is one of the MySQL. * * @see KObject::__call() */ public function __call($method, $arguments) { if (isset($arguments[0]) && $this->_parent_query->getRepository()->getDescription()->getProperty($method)) { $condition = isset($arguments[1]) ? $arguments[1] : 'AND'; $this->where($method, '=', $arguments[0], $condition); return $this; } return parent::__call($method, $arguments); }
/** * Fetch a result from a store. * * @param AnDomainQuery $query Query object * @param int $mode Fetch Mode * * @return mixed */ public function fetch($query, $mode) { $context = $this->getCommandContext(); $context->mode = $mode; $context->query = $query; $context->repository = $query->getRepository(); if ($this->getCommandChain()->run('before.fetch', $context) !== false) { $modes = array(AnDomain::FETCH_ROW => KDatabase::FETCH_ARRAY, AnDomain::FETCH_ROW_LIST => KDatabase::FETCH_ARRAY_LIST, AnDomain::FETCH_ENTITY => KDatabase::FETCH_ARRAY, AnDomain::FETCH_ENTITY_SET => KDatabase::FETCH_ARRAY_LIST, AnDomain::FETCH_ENTITY_LIST => KDatabase::FETCH_ARRAY_LIST, AnDomain::FETCH_VALUE => KDatabase::FETCH_FIELD, AnDomain::FETCH_VALUE_LIST => KDatabase::FETCH_FIELD_LIST); $mode = $modes[$mode]; $context['data'] = $this->_adapter->select(to_str($query), $mode); $this->getCommandChain()->run('after.fetch', $context); } return $context->data; }
/** * Adds a relationship to query. * * @param AnDomainQuery $query Query Object * @param string $relationship Relationship name */ public static function addRelationship($query, $relationship) { $property = $query->getRepository()->getDescription()->getProperty($relationship); switch (true) { case $property->isManyToOne(): return self::_parseManyToOne($query, $property); case $property->isManyToMany(): return self::_parseManyToMany($query, $property); case $property->isOneToMany(): return self::_parseOneToMany($query, $property); } }
/** * 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; }