65 public $mStatus = EASYLEX_SQL_UNKNOWN;
74 public $mActiveToken =
'';
76 public $mActiveQuoteMark =
null;
78 public function setBuffer($buffer)
81 for ($i = 0; $i < strlen($buffer); $i++) {
82 $this->mBuffer[$i] = $buffer[$i];
88 public function parse()
90 while ($this->mIndex <= count($this->mBuffer)) {
91 if ($this->mIndex == count($this->mBuffer)) {
93 $type = EASYLEX_SQL_UNKNOWN;
95 $ch = $this->mBuffer[$this->mIndex];
96 $type = $this->_getChrType($ch);
99 switch ($this->mStatus) {
100 case EASYLEX_SQL_UNKNOWN:
101 $this->_parseUnknown($ch, $type);
104 case EASYLEX_SQL_DIGIT:
105 $this->_parseDigit($ch, $type);
108 case EASYLEX_SQL_LETTER:
109 $this->_parseLetter($ch, $type);
112 case EASYLEX_SQL_STRING_LITERAL:
113 $this->_parseStringLiteral($ch, $type);
116 case EASYLEX_SQL_STRING_LITERAL_ESCAPE:
117 $this->_parseStringLiteralEscape($ch, $type);
120 case EASYLEX_SQL_OPEN_PARENTHESIS:
121 $this->_parseOpenParenthesis($ch, $type);
124 case EASYLEX_SQL_CLOSE_PARENTHESIS:
125 $this->_parseCloseParenthesis($ch, $type);
128 case EASYLEX_SQL_SEPARATER:
129 $this->_parseSeparater($ch, $type);
132 case EASYLEX_SQL_MARK:
133 $this->_parseMark($ch, $type);
136 case EASYLEX_SQL_SEMICOLON:
137 $this->_parseSemicolon($ch, $type);
140 case EASYLEX_SQL_COMMA:
141 $this->_parseComma($ch, $type);
155 public function loadFile($path, $preprocess =
true)
157 if (!file_exists($path)) {
161 $fp = fopen($path,
'rb');
167 while ($str = fgets($fp)) {
169 $str = preg_replace(
"/^\s*\#.*/",
'', $str);
174 $this->setBuffer($t_buff);
180 public function _getChrType($ch)
182 if (preg_match(
"/\s/", $ch)) {
183 return EASYLEX_SQL_SEPARATER;
187 return EASYLEX_SQL_OPEN_PARENTHESIS;
191 return EASYLEX_SQL_CLOSE_PARENTHESIS;
195 return EASYLEX_SQL_SEMICOLON;
199 return EASYLEX_SQL_COMMA;
202 if (preg_match(
'/[0-9]/', $ch)) {
203 return EASYLEX_SQL_DIGIT;
206 if (preg_match(
"/[!=<>%\*]/", $ch)) {
207 return EASYLEX_SQL_MARK;
210 return EASYLEX_SQL_LETTER;
213 public function _parseUnknown($ch, $type)
215 $this->mStatus = $type;
216 $this->mActiveToken .= $ch;
219 if (
"'" == $ch ||
'"' == $ch ||
'`' == $ch) {
220 $this->mStatus = EASYLEX_SQL_STRING_LITERAL;
221 $this->mActiveQuoteMark = $ch;
225 public function _parseDigit($ch, $type)
227 if (EASYLEX_SQL_DIGIT == $type) {
228 $this->mActiveToken .= $ch;
230 } elseif (EASYLEX_SQL_LETTER == $type) {
231 $this->mStatus = EASYLEX_SQL_LETTER;
232 $this->mActiveToken .= $ch;
235 $this->_createToken();
239 public function _parseLetter($ch, $type)
241 if (EASYLEX_SQL_LETTER == $type || EASYLEX_SQL_DIGIT == $type) {
242 $this->mActiveToken .= $ch;
245 $this->_createToken();
249 public function _parseStringLiteral($ch, $type)
251 $this->mActiveToken .= $ch;
255 $this->mStatus = EASYLEX_SQL_STRING_LITERAL_ESCAPE;
256 } elseif ($ch == $this->mActiveQuoteMark) {
257 $this->_createToken();
261 public function _parseStringLiteralEscape($ch, $type)
263 $this->mStatus = EASYLEX_SQL_STRING_LITERAL;
266 public function _parseOpenParenthesis($ch, $type)
268 $this->_createToken();
271 public function _parseCloseParenthesis($ch, $type)
273 $this->_createToken();
276 public function _parseSeparater($ch, $type)
278 if (EASYLEX_SQL_SEPARATER == $type) {
279 $this->mActiveToken .= $ch;
283 $this->mStatus = EASYLEX_SQL_UNKNOWN;
284 $this->mActiveToken =
'';
288 public function _parseSemicolon($ch, $type)
290 $this->_createToken();
293 public function _parseMark($ch, $type)
295 if (EASYLEX_SQL_MARK == $type) {
296 $this->mActiveToken .= $ch;
299 $this->_createToken();
303 public function _parseComma($ch, $type)
305 $this->_createToken();
308 public function _createToken($type =
null, $value =
null)
310 if (
null === $type) {
311 $type = $this->mStatus;
314 if (
null === $value) {
315 $value = $this->mActiveToken;
318 $token =
new EasyLex_SQLToken($type, $value);
319 $this->mTokens[] = &$token;
321 $this->mStatus = EASYLEX_SQL_UNKNOWN;
322 $this->mActiveToken =
'';
338 foreach (array_keys($this->mTokens) as $key) {
339 if (EASYLEX_SQL_OPEN_PARENTHESIS == $this->mTokens[$key]->mType) {
341 } elseif (EASYLEX_SQL_CLOSE_PARENTHESIS == $this->mTokens[$key]->mType) {
345 $t_tokens[] = &$this->mTokens[$key];
347 if (EASYLEX_SQL_SEMICOLON == $this->mTokens[$key]->mType && 0 == $depth) {
354 if (count($t_tokens) > 0) {
362 public function getSQL()
367 foreach ($lines as $line) {
369 foreach ($line as $token) {
370 $t_arr[] = $token->getOutputValue();
372 $sqls[] = implode(
' ', $t_arr);