1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DbSqlLiteral extends DbSqlAware { |
4
|
|
|
|
5
|
|
|
const SQL_LITERAL_ALIAS_NONE = null; |
6
|
|
|
const SQL_LITERAL_ALIAS_AUTO = ''; |
7
|
1 |
|
|
8
|
1 |
|
public $literal = ''; |
9
|
1 |
|
|
10
|
|
|
/** |
11
|
|
|
* DbSqlLiteral constructor. |
12
|
|
|
* |
13
|
|
|
* @param db_mysql|null $db |
14
|
|
|
* @param string $literal |
15
|
|
|
*/ |
16
|
|
|
public function __construct($db = null, $literal = '') { |
17
|
|
|
parent::__construct($db); |
18
|
|
|
$this->literal = $literal; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Renders single argument function |
23
|
|
|
* - supports autobuild alias |
24
|
|
|
* - supports direct alias |
25
|
|
|
* - supports dotted field names like 'table.field' |
26
|
|
|
* - supports all fields with '*' and 'table.*' |
27
|
|
|
* |
28
|
|
|
* @param string $functionName |
29
|
|
|
* @param string $field |
30
|
|
|
* - '*' - all fields |
31
|
|
|
* - string - quoted as field name |
32
|
|
|
* @param null|string $alias |
33
|
|
|
* - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias |
34
|
|
|
* - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field |
35
|
|
|
* - other value: using supplied alias |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
protected function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
40
|
|
|
if ($alias === self::SQL_LITERAL_ALIAS_AUTO) { |
41
|
|
|
$alias = $this->makeAliasFromField($functionName, $field); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->literal = strtoupper($functionName) . '(' . $this->makeFieldFromString($field) . ')'; |
45
|
|
|
|
46
|
|
|
if ($alias) { |
|
|
|
|
47
|
|
|
$this->literal .= ' AS `' . $alias . '`'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $field |
55
|
|
|
* @param null|string $alias |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
* |
59
|
|
|
* @see buildSingleArgument |
60
|
|
|
*/ |
61
|
|
|
public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
62
|
|
|
$this->buildSingleArgument('max', $field, $alias); |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function __toString() { |
72
|
|
|
return $this->literal; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: