-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhereParser.class.php
executable file
·67 lines (63 loc) · 2.65 KB
/
WhereParser.class.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
<?php
class WhereParser{
public static function parse($whereClause){
$results = array();
$inQuote = false;
$quoteChar = '';
$quotation = '';
$result = array();
$quoteChars = array('\'', '"');
$breakingChars = array(' ', '=', '>', '<', '!');
for($lcv=0; $lcv<strlen($whereClause); $lcv++){
if($inQuote){
if($whereClause[$lcv] == $quoteChar){ //close a quote
$result[sizeof($result)-1] .= '\''.$quotation.'\'';
$inQuote = false;
$quotation = '';
}else{
$quotation .= $whereClause[$lcv];
}
}else{
if(!isset($result[0])) $result[0] = ''; //init a new subject if we have nothing
if($whereClause[$lcv] == ' ' ||
(!isset($result[1]) && in_array($whereClause[$lcv], $breakingChars)) ||
(isset($result[1]) && !isset($result[2]) && !in_array($whereClause[$lcv], $breakingChars))
){
if(isset($result[2]) && strtolower(substr($whereClause, $lcv, 4)) == 'and '){
$results[] = $result;
$result = array();
$lcv += 3; //skip the 'and' chars
continue;
}
if(isset($result[0]) && $result[0] == '') continue; //don't advance if we have nothing yet (leading spaces)
if(!isset($result[1])){
$result[1] = '';
}else if(!isset($result[2])){
$result[2] = '';
}
}
if(in_array($whereClause[$lcv], $quoteChars)){ //open a quote
$quoteChar = $whereClause[$lcv];
$inQuote = true;
$quotation = '';
}else{
$result[sizeof($result)-1] .= $whereClause[$lcv];
}
}
}
if($quotation != '') $result[2] = $quotation;
$results[] = $result;
return $results;
}
public static function construct($discriminants){
if(gettype($discriminants) == "string") return $discriminants;
if(count($discriminants) == 3 && gettype($discriminants[0]) == "string" && gettype($discriminants[1]) == "string"){
return $discriminants[0].' '.$discriminants[1].' '.$discriminants[2];
}
$result = '';
foreach($discriminants as $discriminant){
$result .= WhereParser::construct($discriminant);
}
return $result;
}
}