|
2 | 2 | namespace Vimeo\MysqlEngine\Processor\Expression; |
3 | 3 |
|
4 | 4 | use Vimeo\MysqlEngine\FakePdoInterface; |
| 5 | +use Vimeo\MysqlEngine\Processor\ProcessorException; |
5 | 6 | use Vimeo\MysqlEngine\Processor\QueryResult; |
6 | 7 | use Vimeo\MysqlEngine\Processor\Scope; |
7 | | -use Vimeo\MysqlEngine\Processor\ProcessorException; |
8 | 8 | use Vimeo\MysqlEngine\Query\Expression\ColumnExpression; |
| 9 | +use Vimeo\MysqlEngine\Query\Expression\ConstantExpression; |
9 | 10 | use Vimeo\MysqlEngine\Query\Expression\Expression; |
10 | 11 | use Vimeo\MysqlEngine\Query\Expression\FunctionExpression; |
11 | 12 | use Vimeo\MysqlEngine\Query\Expression\IntervalOperatorExpression; |
@@ -235,6 +236,19 @@ public static function getColumnSchema( |
235 | 236 | return Evaluator::getColumnSchema($expr->args[0], $scope, $columns); |
236 | 237 |
|
237 | 238 | case 'ROUND': |
| 239 | + $precision = 0; |
| 240 | + |
| 241 | + if (isset($expr->args[1])) { |
| 242 | + /** @var ConstantExpression $arg */ |
| 243 | + $arg = $expr->args[1]; |
| 244 | + |
| 245 | + $precision = (int)$arg->value; |
| 246 | + } |
| 247 | + |
| 248 | + if ($precision === 0) { |
| 249 | + return new Column\IntColumn(false, 10); |
| 250 | + } |
| 251 | + |
238 | 252 | return Evaluator::getColumnSchema($expr->args[0], $scope, $columns); |
239 | 253 |
|
240 | 254 | case 'DATEDIFF': |
@@ -1239,24 +1253,31 @@ private static function sqlDay( |
1239 | 1253 |
|
1240 | 1254 | /** |
1241 | 1255 | * @param array<string, mixed> $row |
| 1256 | + * |
| 1257 | + * @return float|int |
1242 | 1258 | */ |
1243 | 1259 | private static function sqlRound( |
1244 | 1260 | FakePdoInterface $conn, |
1245 | 1261 | Scope $scope, |
1246 | 1262 | FunctionExpression $expr, |
1247 | 1263 | array $row, |
1248 | 1264 | QueryResult $result |
1249 | | - ) : float { |
| 1265 | + ) { |
1250 | 1266 | $args = $expr->args; |
1251 | 1267 |
|
1252 | | - if (\count($args) !== 2) { |
1253 | | - throw new ProcessorException("MySQL ROUND() function must be called with one arguments"); |
| 1268 | + if (\count($args) !== 1 && \count($args) !== 2) { |
| 1269 | + throw new ProcessorException("MySQL ROUND() function must be called with one or two arguments"); |
| 1270 | + } |
| 1271 | + |
| 1272 | + $number = (float)Evaluator::evaluate($conn, $scope, $args[0], $row, $result); |
| 1273 | + |
| 1274 | + if (!isset($args[1])) { |
| 1275 | + return \round($number); |
1254 | 1276 | } |
1255 | 1277 |
|
1256 | | - $first = Evaluator::evaluate($conn, $scope, $args[0], $row, $result); |
1257 | | - $second = Evaluator::evaluate($conn, $scope, $args[1], $row, $result); |
| 1278 | + $precision = (int)Evaluator::evaluate($conn, $scope, $args[1], $row, $result); |
1258 | 1279 |
|
1259 | | - return \round($first, $second); |
| 1280 | + return \round($number, $precision); |
1260 | 1281 | } |
1261 | 1282 |
|
1262 | 1283 | private static function getPhpIntervalFromExpression( |
|
0 commit comments