@@ -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}
0 commit comments