1 | <?php |
||
6 | class DbSqlLiteral extends DbSqlAware { |
||
7 | |||
8 | const SQL_LITERAL_ALIAS_NONE = null; |
||
9 | const SQL_LITERAL_ALIAS_AUTO = ''; |
||
10 | |||
11 | /** |
||
12 | * ALWAYS STRING! |
||
13 | * |
||
14 | * @var string $literal |
||
15 | */ |
||
16 | public $literal = ''; |
||
17 | |||
18 | /** |
||
19 | * DbSqlLiteral constructor. |
||
20 | * |
||
21 | * @param db_mysql|null $db |
||
22 | * @param string $literal |
||
23 | */ |
||
24 | 1 | public function __construct($db = null, $literal = '') { |
|
|
|||
25 | 1 | parent::__construct($db); |
|
26 | 1 | } |
|
27 | |||
28 | /** |
||
29 | * @param mixed $value |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function literal($value) { |
||
34 | $this->literal = (string)$value; |
||
35 | |||
36 | return $this; |
||
37 | } |
||
38 | // |
||
39 | // public static function __callStatic($name, $arguments) { |
||
40 | // // method_exists(get_called_class(), $name) |
||
41 | // return call_user_func_array(array(static::build(), '_' .$name), $arguments); |
||
42 | // } |
||
43 | |||
44 | /** |
||
45 | * Renders single argument function |
||
46 | * - supports autobuild alias |
||
47 | * - supports direct alias |
||
48 | * - supports dotted field names like 'table.field' |
||
49 | * - supports all fields with '*' and 'table.*' |
||
50 | * |
||
51 | * @param string $functionName |
||
52 | * @param string $field |
||
53 | * - '*' - all fields |
||
54 | * - string - quoted as field name |
||
55 | * @param null|string $alias |
||
56 | * - DbSqlLiteral::SQL_LITERAL_ALIAS_NONE: no alias |
||
57 | * - DbSqlLiteral::SQL_LITERAL_ALIAS_AUTO: alias from $field |
||
58 | * - other value: using supplied alias |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | 12 | public function buildSingleArgument($functionName, $field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
|
63 | 12 | if ($alias === self::SQL_LITERAL_ALIAS_AUTO) { |
|
64 | 4 | $alias = $this->aliasFromField($functionName, $field); |
|
65 | 4 | } |
|
66 | |||
67 | 12 | $this->literal = strtoupper($functionName) . '(' . $this->quoteField($field) . ')'; |
|
68 | |||
69 | 12 | if (self::SQL_LITERAL_ALIAS_NONE !== $alias && !empty($alias)) { |
|
70 | 8 | $this->literal .= ' AS `' . $alias . '`'; |
|
71 | 8 | } |
|
72 | |||
73 | 12 | return $this; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $field |
||
78 | * @param null|string $alias |
||
79 | * |
||
80 | * @return static |
||
81 | * |
||
82 | * @see buildSingleArgument |
||
83 | */ |
||
84 | 12 | public function max($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
|
85 | 12 | return $this->buildSingleArgument('max', $field, $alias); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $field |
||
90 | * @param null|string $alias |
||
91 | * |
||
92 | * @return $this |
||
93 | * |
||
94 | * @see buildSingleArgument |
||
95 | */ |
||
96 | public function count($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
||
97 | return $this->buildSingleArgument('count', $field, $alias); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $field |
||
102 | * @param null|string $alias |
||
103 | * |
||
104 | * @return $this |
||
105 | * |
||
106 | * @see buildSingleArgument |
||
107 | */ |
||
108 | public function sum($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
||
111 | |||
112 | /** |
||
113 | * @param string $field |
||
114 | * @param null|string $alias |
||
115 | * |
||
116 | * @return $this |
||
117 | * |
||
118 | * @see buildSingleArgument |
||
119 | */ |
||
120 | public function isNull($field = '*', $alias = self::SQL_LITERAL_ALIAS_NONE) { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 12 | public function __toString() { |
|
143 | |||
144 | } |
||
145 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.