-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlParser.php
More file actions
153 lines (137 loc) · 4.31 KB
/
SqlParser.php
File metadata and controls
153 lines (137 loc) · 4.31 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
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
<?php
/**
* @file framework/Db/SqlParser.php
*
* depage database module
*
* @author Frank Hellenkamp [jonas@depage.net]
* @author Sebastian Reinhold [sebastian@bitbernd.de]
*/
namespace Depage\Db;
class SqlParser
{
// {{{ variables
protected $split = array();
protected $hash = false;
protected $doubleDash = false;
protected $multiLine = false;
protected $singleQuote = false;
protected $doubleQuote = false;
protected $parsedString = '';
// }}}
// {{{ parse
public function parse($line)
{
$split = $this->split($line);
$tidied = $this->tidy($split);
return $tidied;
}
// }}}
// {{{ split
public function split($line)
{
$this->split = array();
$this->hash = false;
$this->doubleDash = false;
for ($i = 0; $i < strlen($line); $i++) {
$char = $line[$i];
$next = (isset($line[$i+1])) ? $line[$i+1] : '';
$prev = (isset($line[$i-1])) ? $line[$i-1] : '';
if ($this->isComment()) {
$this->append('comment', $char);
if ($this->multiLine && $char == '/' && $prev == '*') {
$this->multiLine = false;
}
} else {
if ($this->isString()) {
$this->append('string', $char);
if ($prev != '\\') {
if ($this->singleQuote && $char == '\'') {
$this->singleQuote = false;
} elseif ($this->doubleQuote && $char == '"') {
$this->doubleQuote = false;
}
}
} else {
if ($char == '#') {
$this->hash = true;
} elseif ($char == '-' && $next == '-') {
$this->doubleDash = true;
} elseif ($char == '/' && $next == '*') {
$this->multiLine = true;
} elseif ($char == ';') {
$this->append('break', $char);
} elseif ($char == '\'') {
$this->singleQuote = true;
$this->append('string', $char);
} elseif ($char == '"') {
$this->doubleQuote = true;
$this->append('string', $char);
} else {
$this->append('code', $char);
}
}
}
}
return $this->split;
}
// }}}
// {{{ tidy
public function tidy($split = array())
{
$finished = array();
foreach ($split as $statement) {
$type = $statement['type'];
if ($type == 'code') {
$append = preg_replace('/\s+/', ' ', $statement['string']);
if (substr($this->parsedString, -1) == ' ' && $append[0] == ' ') {
$append = ltrim($append);
}
$this->parsedString .= $append;
} elseif ($type == 'string') {
$this->parsedString .= $statement['string'];
} elseif ($type == 'break') {
$finished[] = trim($this->parsedString);
$this->parsedString = '';
}
}
return $finished;
}
// }}}
// {{{ append
protected function append($type, $char)
{
end($this->split);
$index = key($this->split);
if (
$index !== null && $this->split[$index]['type'] == $type
) {
$this->split[$index]['string'] .= $char;
} else {
$this->split[] = array(
'type' => $type,
'string' => $char,
);
}
}
// }}}
// {{{ isEndOfStatment
public function isEndOfStatement()
{
return (trim($this->parsedString) == '');
}
// }}}
// {{{ isComment
protected function isComment()
{
return ($this->hash || $this->doubleDash || $this->multiLine);
}
// }}}
// {{{ isString
protected function isString()
{
return $this->singleQuote || $this->doubleQuote;
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */