-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMysqlQueryFingerprint.php
53 lines (40 loc) · 1.47 KB
/
MysqlQueryFingerprint.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
<?php
class MysqlQueryFingerprint
{
/**
* From : http://code.google.com/p/maatkit/source/browse/trunk/mk-query-digest/mk-query-digest?r=3968#1518
* With the great help of : http://www.cs.wcupa.edu/~rkline/perl2php/
* @param type $query
*/
public static function generate($query)
{
//One line comments
$query = preg_replace('/(?:--|#)[^\'"\r\n]*(?=[\r\n]|\Z)/', '', $query);
//Multi line comments
$query = preg_replace('#/\*[^!].*?\*/#sm', '', $query);
//Quoted strings
$query = preg_replace("/\\[\"']/", "", $query);
//Quoted strings
$query = preg_replace('/".*?"/s', "?", $query);
//Quoted strings
$query = preg_replace("/'.*?'/s", "?", $query);
//Everything which look like a number
$query = preg_replace("/[0-9+-][0-9a-f.xb+-]*/", "?", $query);
$query = preg_replace("/[xb.+-]\?/", "?", $query);
//Delete leading and trealing whitespaces
$query = trim($query);
//Collapses whitespaces
//TODO: wtf ?
$query = preg_replace("/[ \n\t\r\f]/s", " ", $query);
//Makes string lowercase
$query = strtolower($query);
//Collapses IN and VALUES
$query = preg_replace('/\b(in|values?)(?:[\s,]*\([\s?,]*\))+/x', "$1(?+)", $query);
//Collapses UNION
$query = preg_replace('/\b(select\s.*?)(?:(\sunion(?:\sall)?)\s\1)+/x', '$1', $query);
//Collapses LIMIT
$query = preg_replace('/\blimit \?(?:, ?\?| offset \?)?/', 'limit ?', $query);
return $query;
}
}
?>