Skip to content

Commit c60da68

Browse files
author
arutyunyan
committed
select raw within suql syntax
1 parent cb031a6 commit c60da68

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/syntax/RawSuQL.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use core\SuQLObject;
4+
5+
class RawSuQL extends SuQLObject implements SuQLInterface
6+
{
7+
protected $driver = 'mysql';
8+
9+
public function query()
10+
{
11+
return 'main';
12+
}
13+
14+
public function table()
15+
{
16+
return null;
17+
}
18+
19+
public function getRawSql()
20+
{
21+
return parent::getSQL([$this->query()]);
22+
}
23+
24+
public static function find()
25+
{
26+
$instance = new static();
27+
$instance->addSelect($instance->query());
28+
return $instance;
29+
}
30+
31+
public function field($raw)
32+
{
33+
$this->getQuery($this->query())->addField($this->table(), $raw);
34+
return $this;
35+
}
36+
}

src/syntax/SuQL.php

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public function field($name, $modifiers = [], $visible = true)
108108
return $this;
109109
}
110110

111+
public function raw($field)
112+
{
113+
$this->getQuery($this->query())->addField(null, $field);
114+
return $this;
115+
}
116+
111117
public function join($model)
112118
{
113119
$this->currentModel = $model;

tests/SuQLObjectTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function testSelect1(): void
5454
// Select raw expression
5555
$this->osuql->addSelect('main');
5656
$this->osuql->getQuery('main')->addField(null, "2 * 2");
57-
$this->osuql->getQuery('main')->addField(null, "'project' as f");
58-
$this->assertEquals($this->osuql->getSQL('all'), "select 2 * 2, 'project' as f");
57+
$this->osuql->getQuery('main')->addField(null, "'Yuriy' as author");
58+
$this->assertEquals($this->osuql->getSQL('all'), "select 2 * 2, 'Yuriy' as author");
5959
$this->assertNull($this->osuql->getSQL(['main']));
6060
}
6161

tests/SuQLTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public function testSelect(): void
2828
User::find()->select(['id' => 'uid', 'name' => 'uname'])->getRawSql(),
2929
'select users.id as uid, users.name as uname from users'
3030
);
31+
32+
// Select raw expression
33+
$this->assertEquals(
34+
RawSuQL::find()->field('2 * 2')->field("'Yuriy' as author")->getRawSql(),
35+
"select 2 * 2, 'Yuriy' as author"
36+
);
37+
38+
// Select raw within a real model
39+
$this->assertEquals(
40+
User::find()->field('id')->raw('2 * 2')->getRawSql(),
41+
'select users.id, 2 * 2 from users'
42+
);
3143
}
3244

3345
public function testJoin(): void

0 commit comments

Comments
 (0)