Skip to content
Permalink
Browse files

Merge pull request #59 from shadowhand/fix/fn-params

Allow fn() to be used with parameters
  • Loading branch information...
shadowhand committed May 1, 2018
2 parents 91c932a + 617cf17 commit 211d4d806df21a691d83347b0f1627412fb0c004
Showing with 44 additions and 2 deletions.
  1. +11 −0 docs/cheatsheet.md
  2. +2 −2 src/functions.php
  3. +31 −0 tests/Issues/ParamsInsideFunctionsTest.php
@@ -116,6 +116,17 @@ fn('COUNT', 'users.id')
fn('CONCAT', 'first_name', 'last_name')
```

_By default functions assume identifiers as parameters, use `param()` for scalar values._


```php
use function Latitude\QueryBuilder\fn;
use function Latitude\QueryBuilder\param;
// POINT(?, ?)
fn('POINT', param(1), param(2))
```

## [](#ordering)Ordering

```php
@@ -13,9 +13,9 @@ function alias($field, string $alias): ExpressionInterface
return express('%s AS %s', identify($field), identify($alias));
}
function fn(string $function, string ...$fields): ExpressionInterface
function fn(string $function, ...$replacements): ExpressionInterface
{
return express("$function(%s)", listing(identifyAll($fields)));
return express("$function(%s)", listing(identifyAll($replacements)));
}
function literal($value): StatementInterface
@@ -0,0 +1,31 @@
<?php
namespace Latitude\QueryBuilder\Issues;
use Latitude\QueryBuilder\TestCase;
use function Latitude\QueryBuilder\fn;
use function Latitude\QueryBuilder\param;
/**
* @link https://github.com/shadowhand/latitude/issues/57
*/
class ParamsInsideFunctionsTest extends TestCase
{
public function testFnColumns()
{
$expr = fn('COUNT', 'id');
$this->assertSql('COUNT(id)', $expr);
$this->assertParams([], $expr);
}
public function testFnParams()
{
$expr = fn('POINT', param(1), param(2));
$this->assertSql('POINT(?, ?)', $expr);
$this->assertParams([1, 2], $expr);
}
}

0 comments on commit 211d4d8

Please sign in to comment.
You can’t perform that action at this time.