|
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;
|
@@ -226,6 +227,19 @@ public static function getColumnSchema(
|
226 | 227 | return Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
|
227 | 228 |
|
228 | 229 | case 'ROUND':
|
| 230 | + $precision = 0; |
| 231 | + |
| 232 | + if (isset($expr->args[1])) { |
| 233 | + /** @var ConstantExpression $arg */ |
| 234 | + $arg = $expr->args[1]; |
| 235 | + |
| 236 | + $precision = (int)$arg->value; |
| 237 | + } |
| 238 | + |
| 239 | + if ($precision === 0) { |
| 240 | + return new Column\IntColumn(false, 10); |
| 241 | + } |
| 242 | + |
229 | 243 | return Evaluator::getColumnSchema($expr->args[0], $scope, $columns);
|
230 | 244 |
|
231 | 245 | case 'DATEDIFF':
|
@@ -1186,24 +1200,31 @@ private static function sqlDay(
|
1186 | 1200 |
|
1187 | 1201 | /**
|
1188 | 1202 | * @param array<string, mixed> $row
|
| 1203 | + * |
| 1204 | + * @return float|int |
1189 | 1205 | */
|
1190 | 1206 | private static function sqlRound(
|
1191 | 1207 | FakePdoInterface $conn,
|
1192 | 1208 | Scope $scope,
|
1193 | 1209 | FunctionExpression $expr,
|
1194 | 1210 | array $row,
|
1195 | 1211 | QueryResult $result
|
1196 |
| - ) : float { |
| 1212 | + ) { |
1197 | 1213 | $args = $expr->args;
|
1198 | 1214 |
|
1199 |
| - if (\count($args) !== 2) { |
1200 |
| - throw new ProcessorException("MySQL ROUND() function must be called with one arguments"); |
| 1215 | + if (\count($args) !== 1 && \count($args) !== 2) { |
| 1216 | + throw new ProcessorException("MySQL ROUND() function must be called with one or two arguments"); |
| 1217 | + } |
| 1218 | + |
| 1219 | + $number = (float)Evaluator::evaluate($conn, $scope, $args[0], $row, $result); |
| 1220 | + |
| 1221 | + if (!isset($args[1])) { |
| 1222 | + return \round($number); |
1201 | 1223 | }
|
1202 | 1224 |
|
1203 |
| - $first = Evaluator::evaluate($conn, $scope, $args[0], $row, $result); |
1204 |
| - $second = Evaluator::evaluate($conn, $scope, $args[1], $row, $result); |
| 1225 | + $precision = (int)Evaluator::evaluate($conn, $scope, $args[1], $row, $result); |
1205 | 1226 |
|
1206 |
| - return \round($first, $second); |
| 1227 | + return \round($number, $precision); |
1207 | 1228 | }
|
1208 | 1229 |
|
1209 | 1230 | private static function getPhpIntervalFromExpression(
|
|
0 commit comments