1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2014 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Db; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Represents an string that won't be escaped in queries. |
12
|
|
|
*/ |
13
|
|
|
class Literal { |
14
|
|
|
/// Properties /// |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array An array that maps driver names to literals. |
18
|
|
|
*/ |
19
|
|
|
protected $driverValues = []; |
20
|
|
|
|
21
|
|
|
/// Methods /// |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initialize an instance of the {@link Literal} class. |
25
|
|
|
* |
26
|
|
|
* @param string|array $value Either a string default value or an array in the form |
27
|
|
|
* `['driver' => 'literal']` to specify different literals for different database drivers. |
28
|
|
|
*/ |
29
|
48 |
|
public function __construct($value) { |
30
|
48 |
|
if (is_string($value)) { |
31
|
48 |
|
$this->driverValues['default'] = $value; |
32
|
|
|
} elseif (is_array($value)) { |
|
|
|
|
33
|
|
|
foreach ($value as $key => $value) { |
34
|
|
|
$this->driverValues[$this->driverKey($key)] = $value; |
35
|
|
|
} |
36
|
|
|
} |
37
|
48 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the literal value. |
41
|
|
|
* |
42
|
|
|
* @param Db $db The database driver getting the value. |
43
|
|
|
* @param mixed ...$args Arguments to pass into the literal. This uses **sprintf()** so arguments must already be escaped. |
44
|
|
|
* @return string Returns the value for the specific driver, the default literal, or "null" if there is no default. |
45
|
|
|
*/ |
46
|
48 |
|
public function getValue(Db $db, ...$args) { |
47
|
48 |
|
$driver = $this->driverKey($db); |
48
|
|
|
|
49
|
48 |
|
if (isset($this->driverValues[$driver])) { |
50
|
|
|
return sprintf($this->driverValues[$driver], ...$args); |
51
|
48 |
|
} elseif (isset($this->driverValues['default'])) { |
52
|
48 |
|
return sprintf($this->driverValues['default'], ...$args); |
53
|
|
|
} else { |
54
|
|
|
throw new \InvalidArgumentException("No literal for driver '$driver'.", 500); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the literal value. |
60
|
|
|
* |
61
|
|
|
* @param string $value The new value to set. |
62
|
|
|
* @param string|object $driver The name of the database driver to set the value for. |
63
|
|
|
* @return Literal Returns $this for fluent calls. |
64
|
|
|
*/ |
65
|
|
|
public function setValue($value, $driver = 'default') { |
66
|
|
|
$driver = $this->driverKey($driver); |
67
|
|
|
$this->driverValues[$driver] = $value; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Normalize the driver name for the drivers array. |
73
|
|
|
* |
74
|
|
|
* @param string|object $key The name of the driver or an instance of a database driver. |
75
|
|
|
* @return string Returns the driver name normalized. |
76
|
|
|
*/ |
77
|
48 |
|
protected function driverKey($key) { |
78
|
48 |
|
if (is_object($key)) { |
79
|
48 |
|
$key = get_class($key); |
80
|
|
|
} |
81
|
48 |
|
$key = strtolower(basename($key)); |
82
|
|
|
|
83
|
48 |
|
if (preg_match('`([az]+)(Db)?$`', $key, $m)) { |
84
|
|
|
$key = $m; |
85
|
|
|
} |
86
|
48 |
|
return $key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create and return a {@link Literal} object. |
91
|
|
|
* |
92
|
|
|
* @param string|array $value The literal value(s) as passed to {@link Literal::__construct()}. |
93
|
|
|
* @return Literal Thew new literal value. |
94
|
|
|
*/ |
95
|
|
|
public static function value($value) { |
96
|
|
|
$literal = new Literal($value); |
97
|
|
|
return $literal; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create and return a {@link Literal} object that will query the current unix timesatmp. |
102
|
|
|
* |
103
|
|
|
* @return Literal Returns the timestamp expression. |
104
|
|
|
*/ |
105
|
|
|
public static function timestamp() { |
106
|
|
|
$literal = new Literal([ |
107
|
|
|
MySqlDb::class => 'unix_timestamp()', |
|
|
|
|
108
|
|
|
SqliteDb::class => "date('now', 'unixepoch')", |
|
|
|
|
109
|
|
|
// 'posgresql' => 'extract(epoch from now())', |
110
|
|
|
'default' => time() |
111
|
|
|
]); |
112
|
|
|
return $literal; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|