|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* QueryBuilderFilter.php |
|
4
|
|
|
* |
|
5
|
|
|
* -Description- |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @link http://librenms.org |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @author Tony Murray <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace app; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use Cache; |
|
30
|
|
|
use DB; |
|
31
|
|
|
|
|
32
|
|
|
class QueryBuilderFilter |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function generateFilter() |
|
38
|
|
|
{ |
|
39
|
|
|
//TODO use variable for cache time or check if it should be invalidated |
|
40
|
|
|
// Cache::forget('query_builder_filter'); |
|
41
|
|
|
|
|
42
|
|
|
return Cache::remember('query_builder_filter', 30, function() { |
|
43
|
|
|
$filter = []; |
|
44
|
|
|
$schema = DB::getDoctrineSchemaManager(); |
|
45
|
|
|
|
|
46
|
|
|
// Doctrine DBAL has issues with enums, pretend they are strings |
|
47
|
|
|
$schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
|
48
|
|
|
|
|
49
|
|
|
$validTypes = ['string', 'integer', 'double', 'date', 'time', 'datetime', 'boolean']; |
|
|
|
|
|
|
50
|
|
|
$ignoreTypes = ['blob', 'binary']; |
|
51
|
|
|
|
|
52
|
|
|
$tables = $schema->listTables(); |
|
53
|
|
|
foreach ($tables as $table) { |
|
54
|
|
|
$columns = $schema->listTableColumns($table->getName()); |
|
55
|
|
|
$tableName = $table->getName(); |
|
56
|
|
|
|
|
57
|
|
|
// only allow tables with direct association to device_id for now |
|
58
|
|
|
if (!$table->hasColumn('device_id')) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($columns as $column) { |
|
63
|
|
|
$item = []; |
|
64
|
|
|
$type = $column->getType()->getName(); |
|
65
|
|
|
$name = $column->getName(); |
|
66
|
|
|
|
|
67
|
|
|
switch ($type) { |
|
68
|
|
|
case 'text': |
|
69
|
|
|
// $item['input'] = 'textarea'; |
|
|
|
|
|
|
70
|
|
|
case 'string': |
|
71
|
|
|
$item['type'] = 'string'; |
|
72
|
|
|
break; |
|
73
|
|
|
|
|
74
|
|
|
case 'integer': |
|
75
|
|
|
case 'smallint': |
|
76
|
|
|
case 'bigint': |
|
77
|
|
|
$item['type'] = 'integer'; |
|
78
|
|
|
break; |
|
79
|
|
|
|
|
80
|
|
|
case 'double': |
|
81
|
|
|
case 'float': |
|
82
|
|
|
case 'decimal': |
|
83
|
|
|
$item['type'] = 'double'; |
|
84
|
|
|
break; |
|
85
|
|
|
|
|
86
|
|
|
case 'date': |
|
87
|
|
|
$item['type'] = 'date'; |
|
88
|
|
|
break; |
|
89
|
|
|
|
|
90
|
|
|
case 'time': |
|
91
|
|
|
$item['type'] = 'time'; |
|
92
|
|
|
break; |
|
93
|
|
|
|
|
94
|
|
|
case 'datetime': |
|
95
|
|
|
$item['type'] = 'datetime'; |
|
96
|
|
|
break; |
|
97
|
|
|
|
|
98
|
|
|
case 'boolean': |
|
99
|
|
|
$item['type'] = 'boolean'; |
|
100
|
|
|
break; |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!isset($item['type'])) { |
|
105
|
|
|
if (!in_array($type, $ignoreTypes)) { |
|
106
|
|
|
dd($type); |
|
107
|
|
|
} |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
// ignore device id columns, except in the devices table |
|
113
|
|
|
if ($name == 'device_id') { |
|
114
|
|
|
if ($tableName != 'devices') { |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$item['id'] = $tableName.'.'.$name; |
|
120
|
|
|
$filter[] = $item; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
return json_encode($filter); |
|
124
|
|
|
}); |
|
125
|
|
|
} |
|
126
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.