public function executeFunction(FunctionJob $function, ExecutionContext $context) { /* @var $argumentValue Value */ $argumentValue = current($function->getArguments()); $beforeSourceRow = $context->getCurrentSourceRow(); $sum = 0; foreach ($context->getCurrentSourceSet() as $row) { $context->setCurrentSourceRow($row); $value = $this->valueResolver->resolveValue($argumentValue, $context); if (is_numeric($value)) { $sum += $value; } } $context->setCurrentSourceRow($beforeSourceRow); return $sum; }
public function executeFunction(FunctionJob $function, ExecutionContext $context) { /* @var $result SelectResult */ $result = $this->resultSet; /* @var $argumentValue Value */ $argumentValue = current($function->getArguments()); $count = 0; $beforeSourceRow = $context->getCurrentSourceRow(); foreach ($context->getCurrentSourceSet() as $row) { $context->setCurrentSourceRow($row); $value = $this->valueResolver->resolveValue($argumentValue); if (!is_null($value)) { $count++; } } $context->setCurrentSourceRow($beforeSourceRow); return $count; }
public function convertSqlToJob(SQLTokenIterator $tokens) { if (!$this->canParseTokens($tokens)) { throw new ErrorException("Tried to convert sql function to job entity when token index is not at function!"); } $tokens->seekIndex($tokens->getExclusiveTokenIndex()); $functionJob = new FunctionJob(); $functionJob->setName($tokens->getCurrentTokenString()); if (!$tokens->seekTokenText('(')) { throw new MalformedSqlException("Missing beginning parenthesis for argument-list in function call!", $tokens); } if (!$tokens->seekTokenText(')')) { do { try { while ($this->parameterConditionParser->canParseTokens($tokens)) { $functionJob->addParameter($this->parameterConditionParser->convertSqlToJob($tokens)); } switch (true) { case $this->valueParser->canParseTokens($tokens): $functionJob->addArgumentValue($this->valueParser->convertSqlToJob($tokens)); break; case $this->selectParser->canParseTokens($tokens): $functionJob->addArgumentValue($this->selectParser->convertSqlToJob($tokens)); break; case $tokens->seekTokenText('*'): $functionJob->addArgumentValue('*'); break; default: throw new MalformedSqlException("Invalid argument defintion in function call!", $tokens); } while ($this->parameterConditionParser->canParseTokens($tokens)) { $functionJob->addParameter($this->parameterConditionParser->convertSqlToJob($tokens)); } } catch (\ErrorException $exception) { throw new MalformedSqlException($exception->getMessage(), $tokens); } } while ($tokens->seekTokenText(',')); if (!$tokens->seekTokenText(')')) { throw new MalformedSqlException("Missing ending parenthesis for argument-list in function call!", $tokens); } } return $functionJob; }
public function executeFunction(FunctionJob $functionJob, ExecutionContext $context) { $functionName = $functionJob->getName(); /* @var $functionResolver FunctionResolverInterface */ $functionResolver = null; if (isset($this->functionOverrides[$functionName])) { $functionResolver = $this->functionOverrides[$functionName]; } else { $functionName = preg_replace("/[^a-zA-Z0-9_]/is", "", $functionName); $functionResolverClassName = ucfirst(strtolower($functionName)) . "Function"; $functionResolverClass = "Addiks\\PHPSQL\\ValueResolver\\FunctionResolver\\{$functionResolverClassName}"; # TODO: This few lines above are bad design, change it! if (!class_exists($functionResolverClass)) { throw new InvalidArgumentException("Function '{$functionName}' does not exist! ({$functionResolverClass})"); } if (!is_subclass_of($functionResolverClass, FunctionInterface::class)) { throw new ErrorException("Function '{$functionName}' does not implement FunctionInterface!"); } $functionResolver = new $functionResolverClass($this->valueResolver); } $returnValue = $functionResolver->executeFunction($functionJob, $context); return $returnValue; }
public function resolveFunction(FunctionJob $functionJob, ExecutionContext $context) { $functionName = $functionJob->getName(); $classNameFunctionPart = str_replace("_", " ", strtolower($functionName)); $classNameFunctionPart = ucwords($classNameFunctionPart); $classNameFunctionPart = str_replace(" ", "", $classNameFunctionPart); $className = "\\Addiks\\PHPSQL\\{{$classNameFunctionPart}}"; if (!class_exists($className)) { throw new ErrorException("Unknown or unimplemented function '{$functionName}' called! (No class '{$className}' found!)"); } /* @var $functionExecuter FunctionResolver */ $functionExecuter = new $className(); $functionExecuter->setValueResolver($this); $functionArguments = array(); foreach ($functionJob->getArguments() as $argument) { if ($argument instanceof Parameter) { $value = $argument->getValue(); } else { $value = $argument; } $value = $this->resolveValue($value, $context); $functionArguments[] = $value; } return $functionExecuter->executeFunction($functionJob, $functionArguments); }