Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit 9c71759

Browse files
author
Daniel Opitz
committed
Added function expression
1 parent 3a116dd commit 9c71759

File tree

4 files changed

+107
-51
lines changed

4 files changed

+107
-51
lines changed

src/Database/FunctionBuilder.php

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,94 +31,88 @@ public function __construct(Connection $db)
3131
*
3232
* @param string $field Field name
3333
*
34-
* @return RawExp Expression
34+
* @return FunctionExpression Expression
3535
*/
36-
public function sum(string $field): RawExp
36+
public function sum(string $field): FunctionExpression
3737
{
38-
$expression = sprintf('SUM(%s)', $this->db->getQuoter()->quoteName($field));
38+
$quoter = $this->db->getQuoter();
39+
$expression = sprintf('SUM(%s)', $quoter->quoteName($field));
3940

40-
return new RawExp($expression);
41+
return new FunctionExpression($expression, $quoter);
4142
}
4243

4344
/**
4445
* Calculate an average. The arguments will be treated as literal values.
4546
*
4647
* @param string $field Field name
4748
*
48-
* @return RawExp Expression
49+
* @return FunctionExpression Expression
4950
*/
50-
public function avg(string $field): RawExp
51+
public function avg(string $field): FunctionExpression
5152
{
52-
$expression = sprintf('AVG(%s)', $this->db->getQuoter()->quoteName($field));
53+
$quoter = $this->db->getQuoter();
54+
$expression = sprintf('AVG(%s)', $quoter->quoteName($field));
5355

54-
return new RawExp($expression);
56+
return new FunctionExpression($expression, $quoter);
5557
}
5658

5759
/**
5860
* Calculate the min of a column. The arguments will be treated as literal values.
5961
*
6062
* @param string $field Field name
6163
*
62-
* @return RawExp Expression
64+
* @return FunctionExpression Expression
6365
*/
64-
public function min(string $field): RawExp
66+
public function min(string $field): FunctionExpression
6567
{
66-
$expression = sprintf('MIN(%s)', $this->db->getQuoter()->quoteName($field));
68+
$quoter = $this->db->getQuoter();
69+
$expression = sprintf('MIN(%s)', $quoter->quoteName($field));
6770

68-
return new RawExp($expression);
71+
return new FunctionExpression($expression, $quoter);
6972
}
7073

7174
/**
7275
* Calculate the max of a column. The arguments will be treated as literal values.
7376
*
7477
* @param string $field Field name
7578
*
76-
* @return RawExp Expression
79+
* @return FunctionExpression Expression
7780
*/
78-
public function max(string $field): RawExp
81+
public function max(string $field): FunctionExpression
7982
{
80-
$expression = sprintf('MAX(%s)', $this->db->getQuoter()->quoteName($field));
83+
$quoter = $this->db->getQuoter();
84+
$expression = sprintf('MAX(%s)', $quoter->quoteName($field));
8185

82-
return new RawExp($expression);
86+
return new FunctionExpression($expression, $quoter);
8387
}
8488

8589
/**
8690
* Calculate the count. The arguments will be treated as literal values.
8791
*
8892
* @param string $field Field name (Default is *)
93+
* @param string|null $alias Alias
8994
*
90-
* @return RawExp Expression
95+
* @return FunctionExpression Expression
9196
*/
92-
public function count(string $field = '*'): RawExp
97+
public function count(string $field = '*', string $alias = null): RawExp
9398
{
94-
$expression = sprintf('COUNT(%s)', $this->db->getQuoter()->quoteName($field));
99+
$quoter = $this->db->getQuoter();
100+
$expression = sprintf('COUNT(%s)', $quoter->quoteName($field));
95101

96-
return new RawExp($expression);
97-
}
98-
99-
/**
100-
* Calculate the count. The arguments will be treated as literal values.
101-
*
102-
* @param string $field Field name (Default is *)
103-
* @param string ...$fields Field names
104-
*
105-
* @return RawExp Expression
106-
*/
107-
public function concat(string $field, string ...$fields): RawExp
108-
{
109-
$names = $this->db->getQuoter()->quoteNames(array_merge([$field], $fields));
110-
$expression = sprintf('CONCAT(%s)', implode(', ', $names));
102+
if ($alias !== null) {
103+
$expression .= sprintf(' %s AS %s', $expression, $quoter->quoteName($alias));
104+
}
111105

112-
return new RawExp($expression);
106+
return new FunctionExpression($expression, $quoter);
113107
}
114108

115109
/**
116110
* Returns a Expression representing a call that will return the current date and time (ISO).
117111
*
118-
* @return RawExp Expression
112+
* @return FunctionExpression Expression
119113
*/
120-
public function now(): RawExp
114+
public function now(): FunctionExpression
121115
{
122-
return new RawExp('NOW()');
116+
return new FunctionExpression('NOW()', $this->db->getQuoter());
123117
}
124118
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Odan\Database;
4+
5+
/**
6+
* Contains methods related to generating Expression objects
7+
* with most commonly used SQL functions.
8+
* This acts as a factory for RawExp objects.
9+
*/
10+
class FunctionExpression extends RawExp
11+
{
12+
/**
13+
* @var Quoter
14+
*/
15+
protected $quoter;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param string $value
21+
* @param Quoter $quoter
22+
*/
23+
public function __construct(string $value, Quoter $quoter)
24+
{
25+
parent::__construct($value);
26+
$this->quoter = $quoter;
27+
}
28+
29+
/**
30+
* Alias.
31+
*
32+
* @param string|null $alias Alias
33+
*
34+
* @return $this
35+
*/
36+
public function alias(string $alias = null)
37+
{
38+
$clone = clone $this;
39+
$clone->value = sprintf('%s AS %s', $clone->value, $this->quoter->quoteName($alias));
40+
41+
return $clone;
42+
}
43+
}

src/Database/RawExp.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,41 @@
22

33
namespace Odan\Database;
44

5+
/**
6+
* Raw Expression.
7+
*/
58
class RawExp
69
{
10+
/**
11+
* @var string
12+
*/
713
protected $value;
814

9-
public function __construct($value)
15+
/**
16+
* Constructor.
17+
*
18+
* @param string $value
19+
*/
20+
public function __construct(string $value)
1021
{
1122
$this->value = $value;
1223
}
1324

25+
/**
26+
* To string.
27+
*
28+
* @return string
29+
*/
1430
public function __toString()
1531
{
1632
return $this->getValue();
1733
}
1834

35+
/**
36+
* Get value.
37+
*
38+
* @return string
39+
*/
1940
public function getValue()
2041
{
2142
return $this->value;

tests/FunctionBuilderTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Odan\Database\Test;
66

77
use Odan\Database\FunctionBuilder;
8+
use Odan\Database\RawExp;
89

910
/**
1011
* @coversDefaultClass \Odan\Database\FunctionBuilder
@@ -33,6 +34,10 @@ public function testSum()
3334

3435
$this->assertEquals('SUM(`field`)', $func->sum('field'));
3536
$this->assertEquals('SUM(`table`.`field`)', $func->sum('table.field'));
37+
38+
$query = $this->getConnection()->select()->from('payments');
39+
$query->columns($query->func()->count('amount'));
40+
$this->assertEquals('SELECT COUNT(`amount`) FROM `payments`;', $query->build());
3641
}
3742

3843
/**
@@ -85,19 +90,10 @@ public function testCount()
8590

8691
$this->assertEquals('COUNT(*)', $func->count());
8792
$this->assertEquals('COUNT(`field`)', $func->count('field'));
88-
}
89-
90-
/**
91-
* Test.
92-
*
93-
* @return void
94-
*/
95-
public function testConcat()
96-
{
97-
$func = $this->getConnection()->select()->func();
9893

99-
$this->assertEquals('CONCAT(`field`)', $func->concat('field'));
100-
$this->assertEquals('CONCAT(`field`, `field2`, `field3`)', $func->concat('field', 'field2', 'field3'));
94+
$query = $this->getConnection()->select()->from('users');
95+
$query->columns($query->func()->count());
96+
$this->assertEquals('SELECT COUNT(*) FROM `users`;', $query->build());
10197
}
10298

10399
/**
@@ -109,6 +105,8 @@ public function testNow()
109105
{
110106
$func = $this->getConnection()->select()->func();
111107

108+
$this->assertInstanceOf(RawExp::class, $func->now());
112109
$this->assertEquals('NOW()', $func->now());
110+
$this->assertEquals('NOW() AS `alias_field`', $func->now()->alias('alias_field')->getValue());
113111
}
114112
}

0 commit comments

Comments
 (0)