-
Notifications
You must be signed in to change notification settings - Fork 7
/
fieldtop.php
252 lines (215 loc) · 8.28 KB
/
fieldtop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
include('fieldtop_config.php');
if (php_sapi_name() == 'cli') $mode='cli';
else $mode='html';
$o = new DBOverflowCheck($mode);
$o->connectDB($userPass['host'],$userPass['user'],$userPass['pass'],'information_schema');
$o->check();
$o->show($mode);
class DBOverflowCheck {
public $dbh = null; // Database connection
public $maxAllowed = null; // MySql field type limits
public $columnData = null; // Analysis result
public $proneToUnderflow = array(
'tinyint',
'smallint',
'mediumint',
'int',
'bigint',
);
public $textTypes = array(
'tinytext',
'text',
'mediumtext',
'longtext',
'varchar',
'char',
);
public $numericTypes = array(
'tinyint',
'smallint',
'mediumint',
'int',
'bigint',
'utinyint',
'usmallint',
'umediumint',
'uint',
'ubigint',
);
function __construct() {
# for numeric types
# http://dev.mysql.com/doc/refman/5.7/en/integer-types.html
# - the signed types min=-max
# - unsigned types min=0
#
# for text types
# http://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html#idm140434164775232
# http://stackoverflow.com/a/13506920/827519
# http://stackoverflow.com/a/13932834/827519
$this->maxAllowed = array(
# numeric types
'tinyint' => "127",
'utinyint' => "255",
'smallint' => "32767",
'usmallint' => "65535",
'mediumint' => "8388607",
'umediumint' => "16777215",
'int' => "2147483647",
'uint' => "4294967295",
'bigint' => "9223372036854775807",
'ubigint' => "18446744073709551615",
# text types
'char' => 255,
'varchar' => 65535,
'tinytext' => (1<<8),
'text' => (1<<16),
'mediumtext' => (1<<24),
'longtext' => (1<<32),
);
}
function __destruct() {
$dbh = null;
}
# get all column metadata
function getColumnMetadata() {
$query = file_get_contents('fieldtop.sql');
$sth = $this->dbh->query($query);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth = null;
return $result;
}
function connectDB($host, $user, $password, $dbName) {
$this->dbh = new PDO("mysql:host=$host;dbname=$dbName", $user, $password);
}
# gets column minimum and maximum values
function getMinMax($dbName,$tableName,$columnName,$dataType) {
$query="";
if(in_array($dataType, $this->numericTypes)) {
$query="
SELECT
MIN(`$columnName`) AS _min,
MAX(`$columnName`) AS _max
FROM $dbName.$tableName ;
";
} else if(in_array($dataType, $this->textTypes)) {
$query="
SELECT
MIN(CHAR_LENGTH(`$columnName`)) AS _min,
MAX(CHAR_LENGTH(`$columnName`)) AS _max
FROM $dbName.$tableName ;
";
} else {
throw new Exception('getMinMax was not designed for this data type');
};
$sth = $this->dbh->query($query);
$result = $sth->fetch(PDO::FETCH_ASSOC);
$sth = null;
return $result;
}
# extract column attributes
function extractColumnWidth($columnType) {
# this regex extracts the width of the column data type
$regexColumnWidth = '/^.*\(([0-9]+(?:,[0-9]+)?)\).*$/';
$regexSplitValues = '/[\s,\(\)]+/';
preg_match($regexColumnWidth,$columnType,$rawWidthAttribs);
# preg_match will put the entire string in [0] and the matches
# will start at [1]
if($rawWidthAttribs === NULL || count($rawWidthAttribs) < 2)
return null;
$widthAttribs = preg_split($regexSplitValues,$rawWidthAttribs[0]);
return $widthAttribs;
}
# checks all columns for overflows
function check() {
$metadata = $this->getColumnMetadata();
# put together all the required data
$this->columnData = array();
foreach($metadata as $idx => $cdata ) {
$signed = $cdata['signed'];
$dataType = $cdata['DATA_TYPE'];
$widthAttribs = $this->extractColumnWidth($cdata['COLUMN_TYPE']);
if(!$signed)
$dataType = "u$dataType";
if(!array_key_exists($dataType, $this->maxAllowed))
continue;
$minmax = $this->getMinMax($cdata['TABLE_SCHEMA'],$cdata['TABLE_NAME'],$cdata['COLUMN_NAME'],$dataType);
$minUsed = $minmax['_min'];
$maxUsed = $minmax['_max'];
$maxValueAllowed = $this->maxAllowed[$dataType];
$minValueAllowed = -$this->maxAllowed[$dataType];
# for text fields, use width attribute in the column definition
# as the maximum allowed
if(in_array($dataType, $this->textTypes) && $widthAttribs !== NULL) {
$maxValueAllowed = $widthAttribs[1];
};
$toOverflow = round($maxUsed/$maxValueAllowed,4) * 100.0;
$toUnderflow = round($minUsed/$minValueAllowed,4) * 100.0;
# if toUnderflow is negative, the min value must be
# positive, and that's a very low risk of underflow.
# to make the report more consistent and clear,
# we set this to 0
if($toUnderflow <= 0) {
$toUnderflow = 0;
};
# analog situation for toOverflow
if($toOverflow <= 0) {
$toOverflow = 0;
};
$column_data = array(
'dataType' => $dataType,
'maxAllowed' => $maxValueAllowed,
'minAllowed' => $minValueAllowed,
'toOverflow' => $toOverflow,
'toUnderflow' => $toUnderflow,
'TABLE_SCHEMA' => $cdata['TABLE_SCHEMA'],
'TABLE_NAME' => $cdata['TABLE_NAME'],
'COLUMN_NAME' => $cdata['COLUMN_NAME'],
'widthAttribs' => $widthAttribs,
);
array_push($this->columnData, $column_data);
};
# sort them by closeness maximum value
uasort(
$this->columnData,
function($a,$b) {
return $a['toOverflow'] < $b['toOverflow'];
}
);
}
function show($mode)
{
if ($mode=='html')
{
print('<style>td {padding-right: 2em} tr:hover {background: #ffff80} th {text-align: left} </style><table>');
print('<tr><th>Field</th><th>Type</th><th>Min</th><th>Max</th></tr>');
}
if ($mode=='cli')
{
printf("%-60s %8s %9s\n", 'Field','Min','Max');
}
foreach($this->columnData as $idx => $cdata) {
$formattedName = sprintf('%s.%s.%s',$cdata['TABLE_SCHEMA'],$cdata['TABLE_NAME'],$cdata['COLUMN_NAME']);
$toOverflow = $cdata['toOverflow'];
$toUnderflow = $cdata['toUnderflow'];
$dataType = $cdata['dataType'];
if ($mode=='cli' ) $this->showFieldCli ($formattedName,$toOverflow,$toUnderflow,$dataType);
if ($mode=='html') $this->showFieldHtml($formattedName,$toOverflow,$toUnderflow,$dataType);
};
if ($mode=='html') print '</table>';
}
function showFieldCli($formattedName,$toOverflow,$toUnderflow,$dataType)
{
$toOverflow = sprintf("%3.4f", $toOverflow);
$toUnderflow = sprintf("%3.4f", $toUnderflow);
if(!in_array($dataType, $this->proneToUnderflow)) $toUnderflow='n/a';
printf("%-60s %8s %8s%% %8s%%\n", $formattedName, $dataType, $toUnderflow, $toOverflow);
}
function showFieldHtml($formattedName,$toOverflow,$toUnderflow,$dataType)
{
$toOverflow = sprintf("%3.4f", $toOverflow);
$toUnderflow = sprintf("%3.4f", $toUnderflow);
if(!in_array($dataType, $this->proneToUnderflow)) $toUnderflow='-';
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $formattedName, $dataType, $toUnderflow, $toOverflow);
}
}