1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces text related expressions for the instance. |
9
|
|
|
*/ |
10
|
|
|
trait TextTrait { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DB |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return Text |
19
|
|
|
*/ |
20
|
|
|
public function getHex () { |
21
|
|
|
return new Text($this->db, "HEX({$this})"); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns the number of characters (not bytes) in the string. |
26
|
|
|
* |
27
|
|
|
* @see getSize() |
28
|
|
|
* |
29
|
|
|
* @return Numeric |
30
|
|
|
*/ |
31
|
|
|
public function getLength () { |
32
|
|
|
switch ($this->db->getDriver()) { |
33
|
|
|
case 'sqlite': |
34
|
|
|
return new Numeric($this->db, "LENGTH(CAST({$this} AS TEXT))"); |
35
|
|
|
default: |
36
|
|
|
return new Numeric($this->db, "CHAR_LENGTH({$this})"); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* `LOWER($this)` |
42
|
|
|
* |
43
|
|
|
* @return Text |
44
|
|
|
*/ |
45
|
|
|
public function getLower () { |
46
|
|
|
return new Text($this->db, "LOWER({$this})"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* A substring's position (1-based). |
51
|
|
|
* |
52
|
|
|
* The position is `0` if the substring isn't found. |
53
|
|
|
* |
54
|
|
|
* @param string $substring |
55
|
|
|
* @return Numeric |
56
|
|
|
*/ |
57
|
|
|
public function getPosition (string $substring) { |
58
|
|
|
$substring = $this->db->quote($substring); |
59
|
|
|
switch ($this->db->getDriver()) { |
60
|
|
|
case 'sqlite': |
61
|
|
|
return new Numeric($this->db, "INSTR({$this},{$substring})"); |
62
|
|
|
default: |
63
|
|
|
return new Numeric($this->db, "LOCATE({$substring},{$this})"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* `REPLACE($this,$search,$replace)` |
69
|
|
|
* |
70
|
|
|
* @param string $search |
71
|
|
|
* @param string $replace |
72
|
|
|
* @return Text |
73
|
|
|
*/ |
74
|
|
|
public function getReplacement (string $search, string $replace) { |
75
|
|
|
$search = $this->db->quote($search); |
76
|
|
|
$replace = $this->db->quote($replace); |
77
|
|
|
return new Text($this->db, "REPLACE({$this},{$search},{$replace})"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The number of bytes in the string. |
82
|
|
|
* |
83
|
|
|
* @return Numeric |
84
|
|
|
*/ |
85
|
|
|
public function getSize () { |
86
|
|
|
switch ($this->db->getDriver()) { |
87
|
|
|
case 'sqlite': |
88
|
|
|
return new Numeric($this->db, "LENGTH(CAST({$this} AS BLOB))"); |
89
|
|
|
default: |
90
|
|
|
return new Numeric($this->db, "LENGTH({$this})"); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* `SUBSTR($this,$start)` or `SUBSTR($this,$start,$length)` |
96
|
|
|
* |
97
|
|
|
* @param int $start 1-based, can be negative to start from the right. |
98
|
|
|
* @param int $length |
99
|
|
|
* @return Text |
100
|
|
|
*/ |
101
|
|
|
public function getSubstring (int $start, int $length = null) { |
102
|
|
|
if (isset($length)) { |
103
|
|
|
return new Text($this->db, "SUBSTR({$this},{$start},{$length})"); |
104
|
|
|
} |
105
|
|
|
return new Text($this->db, "SUBSTR({$this},{$start})"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* `UPPER($this)` |
110
|
|
|
* |
111
|
|
|
* @return Text |
112
|
|
|
*/ |
113
|
|
|
public function getUpper () { |
114
|
|
|
return new Text($this->db, "UPPER({$this})"); |
115
|
|
|
} |
116
|
|
|
} |