-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlSyntaxCheck.php
More file actions
115 lines (87 loc) · 2.45 KB
/
Copy pathsqlSyntaxCheck.php
File metadata and controls
115 lines (87 loc) · 2.45 KB
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
<?php
$error = 0;
$sql = "INSERT INTO ( id , value , id ) VALUES ( '{datetime:?1}' , '{string:?2}' , '{ float:?3 }' )";
$sql = config_parse($sql,array('2010-01-10 00:59:49' , 'value' , '1'));
echo "SQL1 $sql\n";
$sql = preg_replace_callback('~{( ?[a-z]+):([a-z0-9A-Z: _-]+ ?)}~','sql_syntax_check',$sql);
if($error > 0) echo "SQL Parse Error Occurred\n";
else echo "SQL2 $sql\n";
function sql_syntax_check($matches) {
global $error;
if(count($matches) === 3) {
$type = trim($matches[1]);
$check = trim($matches[2]);
switch(trim($type)) {
case 'int':
if(is_numeric($check) || (string) $check === (string) (int)$check) {
return sprintf('%i',$check);
}
break;
case 'float':
if (is_numeric($check)) {
return sprintf("%f",$check);;
}
break;
case 'string':
return $check;
break;
case 'date':
if (preg_match('~^([0-9]{4})-([0-1]{2}?)-([0-3]{2}?)$~', $check, $date_matches) === 1) {
return sprintf('%04d-%02d-%02d', $date_matches[1], $date_matches[2], $date_matches[3]);
}
break;
case 'datetime':
if (preg_match('~^([0-9]{4})-([0-1]{2}?)-([0-3]{2}?) ?([0-9]{2}):([0-9]{2}):([0-9]{2})$~', $check, $date_matches) === 1) {
return sprintf('%04d-%02d-%02d %02d:%02d:%02d', $date_matches[1], $date_matches[2], $date_matches[3], $date_matches[4], $date_matches[5], $date_matches[6]);
}
break;
default:
break;
}
}
$error++;
return FALSE;
}
function config_parse ( $sql , $replacements ) {{{
if($sql === FALSE) {
return FALSE;
}
$pattern = '/\?[0-9]{0,3}/';
preg_match_all($pattern,$sql,$matches);
if(count($replacements) != count($matches[0])) {
return FALSE;
}
for($i=0;$i<count($matches[0]);$i++) {
if ($i < 9) {
$values[$i] = '/\\'.$matches[0][$i].'(?![0-9])/'; // Rewrite what we need to replace a little
} else {
$values[$i] = '/\\'.$matches[0][$i].'/'; // Rewrite what we need to replace a little
}
}
$ret = preg_replace($values,$replacements,$sql);
return $ret;
}}}
function array_parse ( $config ) {{{
/*
{ { blue green red }
{ blue green red }
{ blue green red } }
*/
// {\s*.*?(?:\s*.)*.\s\}
/*
$value = "{
{ d , a , c , b }
{ b , d , a , c , b }
{ a , c , b }
}";
$values = explode(PHP_EOL, $value);
print_r($values);
for($i=0;$i<sizeof($values);$i++) {
if (preg_match('/^\{\s*.*?(?:\s*,\s*.*?)*\s*\}$/', trim($values[$i]))) {
$data[$i] = array_map('trim', explode(',', trim(trim($values[$i]), '{}')));
}
}
print_r($data);
*/
}}}
?>