1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces numeric expressions for the instance. |
9
|
|
|
*/ |
10
|
|
|
trait NumericTrait { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DB |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `ABS($this)` |
19
|
|
|
* |
20
|
|
|
* @return Numeric |
21
|
|
|
*/ |
22
|
|
|
public function abs () { |
23
|
|
|
return new Numeric($this->db, "ABS({$this})"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* `($this + $arg)` |
28
|
|
|
* |
29
|
|
|
* @param number|ExpressionInterface $arg |
30
|
|
|
* @return Numeric |
31
|
|
|
*/ |
32
|
|
|
public function add ($arg) { |
33
|
|
|
return new Numeric($this->db, "({$this} + {$arg})"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* `CEIL($this)` |
38
|
|
|
* |
39
|
|
|
* @return Numeric |
40
|
|
|
*/ |
41
|
|
|
public function ceil () { |
42
|
|
|
return new Numeric($this->db, "CEIL({$this})"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* `($this / $arg)` |
47
|
|
|
* |
48
|
|
|
* @param number|ExpressionInterface $arg |
49
|
|
|
* @return Numeric |
50
|
|
|
*/ |
51
|
|
|
public function divide ($arg) { |
52
|
|
|
return new Numeric($this->db, "({$this} / {$arg})"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* `FLOOR($this)` |
57
|
|
|
* |
58
|
|
|
* @return Numeric |
59
|
|
|
*/ |
60
|
|
|
public function floor () { |
61
|
|
|
return new Numeric($this->db, "FLOOR({$this})"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* `($this % 2) = 0` |
66
|
|
|
* |
67
|
|
|
* @return Predicate |
68
|
|
|
*/ |
69
|
|
|
public function isEven () { |
70
|
|
|
return new Predicate("({$this} % 2) = 0"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* `$this < 0` |
75
|
|
|
* |
76
|
|
|
* @return Predicate |
77
|
|
|
*/ |
78
|
|
|
public function isNegative () { |
79
|
|
|
return new Predicate("{$this} < 0"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* `($this % 2) <> 0` |
84
|
|
|
* |
85
|
|
|
* @return Predicate |
86
|
|
|
*/ |
87
|
|
|
public function isOdd () { |
88
|
|
|
return new Predicate("({$this} % 2) <> 0"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* `$this > 0` |
93
|
|
|
* |
94
|
|
|
* @return Predicate |
95
|
|
|
*/ |
96
|
|
|
public function isPositive () { |
97
|
|
|
return new Predicate("{$this} > 0"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* `($this % $arg)` |
102
|
|
|
* |
103
|
|
|
* @param number|ExpressionInterface $arg |
104
|
|
|
* @return Numeric |
105
|
|
|
*/ |
106
|
|
|
public function modulo ($arg) { |
107
|
|
|
return new Numeric($this->db, "({$this} % {$arg})"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* `($this * $arg)` |
112
|
|
|
* |
113
|
|
|
* @param number|ExpressionInterface $arg |
114
|
|
|
* @return Numeric |
115
|
|
|
*/ |
116
|
|
|
public function multiply ($arg) { |
117
|
|
|
return new Numeric($this->db, "({$this} * {$arg})"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* `POW($this,$exponent)` |
122
|
|
|
* |
123
|
|
|
* @param number|ExpressionInterface $exponent |
124
|
|
|
* @return Numeric |
125
|
|
|
*/ |
126
|
|
|
public function pow ($exponent) { |
127
|
|
|
return new Numeric($this->db, "POW({$this},{$exponent})"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* `ROUND($this,$decimals)` |
132
|
|
|
* |
133
|
|
|
* @param int $decimals |
134
|
|
|
* @return Numeric |
135
|
|
|
*/ |
136
|
|
|
public function round (int $decimals = 0) { |
137
|
|
|
return new Numeric($this->db, "ROUND({$this},{$decimals})"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* `($this - $arg)` |
142
|
|
|
* |
143
|
|
|
* @param number|ExpressionInterface $arg |
144
|
|
|
* @return Numeric |
145
|
|
|
*/ |
146
|
|
|
public function subtract ($arg) { |
147
|
|
|
return new Numeric($this->db, "({$this} - {$arg})"); |
148
|
|
|
} |
149
|
|
|
} |