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