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

Commit f596b2b

Browse files
author
Daniel Opitz
committed
Added raw method to select query
1 parent 50815e3 commit f596b2b

5 files changed

Lines changed: 45 additions & 26 deletions

File tree

docs/selects.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ To create a raw expression, you may use the RawExp value object:
118118

119119
```php
120120
$users = $db->select()
121-
->columns(new RawExp('count(*) as user_count'), 'status')
121+
->columns(new RawExp('count(*) AS user_count'), 'status')
122122
->from('payments')
123123
->where('status', '<>', 1)
124124
->groupBy('status')
@@ -127,7 +127,19 @@ $users = $db->select()
127127
```
128128

129129
```sql
130-
SELECT count(*) as user_count, `status` FROM `payments` WHERE `status` <> 1 GROUP BY `status`;
130+
SELECT count(*) AS user_count, `status` FROM `payments` WHERE `status` <> 1 GROUP BY `status`;
131+
```
132+
133+
Creating raw expressions with the function builder:
134+
135+
```php
136+
$query = $db->select();
137+
$query->columns($query->raw('count(*) AS user_count'), 'status');
138+
$query->from('payments');
139+
```
140+
141+
```sql
142+
SELECT count(*) AS user_count,`status` FROM `payments`;
131143
```
132144

133145
#### Aggregates

src/Database/FunctionBuilder.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,4 @@ public function now(): FunctionExpression
115115
{
116116
return new FunctionExpression('NOW()', $this->db->getQuoter());
117117
}
118-
119-
/**
120-
* Returns a Raw Expression.
121-
*
122-
* @param string $value A raw value. Be careful!
123-
* @return RawExp Raw Expression
124-
*/
125-
public function raw(string $value): RawExp
126-
{
127-
return new RawExp($value);
128-
}
129118
}

src/Database/SelectQuery.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,18 @@ public function offset(float $offset): self
454454
return $this;
455455
}
456456

457+
/**
458+
* Returns a Raw Expression.
459+
*
460+
* @param string $value A raw value. Be careful!
461+
*
462+
* @return RawExp Raw Expression
463+
*/
464+
public function raw(string $value): RawExp
465+
{
466+
return new RawExp($value);
467+
}
468+
457469
/**
458470
* Executes an SQL statement, returning a result set as a PDOStatement object.
459471
*

tests/FunctionBuilderTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,4 @@ public function testNow()
109109
$this->assertEquals('NOW()', $func->now());
110110
$this->assertEquals('NOW() AS `alias_field`', $func->now()->alias('alias_field')->getValue());
111111
}
112-
113-
/**
114-
* Test.
115-
*
116-
* @return void
117-
*/
118-
public function testRaw()
119-
{
120-
$func = $this->getConnection()->select()->func();
121-
122-
$this->assertInstanceOf(RawExp::class, $func->raw(''));
123-
$this->assertEquals('value', $func->raw('value')->getValue());
124-
}
125112
}

tests/SelectQueryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,25 @@ public function testLimit()
552552
$this->assertSame('SELECT `id` FROM `test` LIMIT 10;', $select->build());
553553
}
554554

555+
/**
556+
* Test.
557+
*
558+
* @return void
559+
*/
560+
public function testRaw()
561+
{
562+
$query = $this->getConnection()->select();
563+
564+
$this->assertInstanceOf(RawExp::class, $query->raw(''));
565+
$this->assertEquals('value', $query->raw('value')->getValue());
566+
567+
$query = $this->getConnection()->select();
568+
$query->columns($query->raw('count(*) AS user_count'), 'status');
569+
$query->from('payments');
570+
571+
$this->assertEquals('SELECT count(*) AS user_count,`status` FROM `payments`;', $query->build());
572+
}
573+
555574
/**
556575
* Set Up.
557576
*

0 commit comments

Comments
 (0)