1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\sql\model\components; |
4
|
|
|
|
5
|
|
|
use mindplay\sql\model\schema\Column; |
6
|
|
|
use mindplay\sql\model\schema\Table; |
7
|
|
|
use mindplay\sql\model\schema\Type; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This trait implements support for the `RETURNING` clause of Postgres `INSERT`, `UPDATE` and `DELETE` queries. |
11
|
|
|
*/ |
12
|
|
|
trait Returning |
13
|
|
|
{ |
14
|
|
|
use Mappers; |
15
|
|
|
|
16
|
|
|
protected ReturnVars $return_vars; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Add all the Columns of a full Table to be selected and returned |
20
|
|
|
* |
21
|
|
|
* @param Table $table Table to select and return |
22
|
|
|
* |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function returningTable(Table $table): static |
26
|
|
|
{ |
27
|
|
|
$this->return_vars->addTable($table); |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add one or more Columns to select and return |
34
|
|
|
* |
35
|
|
|
* @param Column|Column[] $cols one or more Columns to select and return |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
1 |
|
public function returningColumns($cols): static |
40
|
|
|
{ |
41
|
1 |
|
$this->return_vars->addColumns($cols); |
42
|
|
|
|
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add an SQL expression to select and return |
48
|
|
|
* |
49
|
|
|
* @param string $expr return expression |
50
|
|
|
* @param string|null $name return variable name (optional, but usually required) |
51
|
|
|
* @param Type|string|null $type optional Type (or Type class-name) |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function returningValue(string $expr, string|null $name = null, Type|string|null $type = null): static |
56
|
|
|
{ |
57
|
|
|
$this->return_vars->addValue($expr, $name, $type); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @see MapperProvider::getMappers() |
64
|
|
|
*/ |
65
|
1 |
|
public function getMappers(): array |
66
|
|
|
{ |
67
|
1 |
|
return array_merge([$this->return_vars->createTypeMapper()], $this->mappers); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|