depage-db v1.4.0
Loading...
Searching...
No Matches
SqlParser.php
Go to the documentation of this file.
1<?php
10
11namespace Depage\Db;
12
14{
15 protected $split = array();
16 protected $hash = false;
17 protected $doubleDash = false;
18 protected $multiLine = false;
19 protected $singleQuote = false;
20 protected $doubleQuote = false;
21 protected $parsedString = '';
22
23 public function parse($line)
24 {
25 $split = $this->split($line);
26 $tidied = $this->tidy($split);
27
28 return $tidied;
29 }
30 public function split($line)
31 {
32 $this->split = array();
33 $this->hash = false;
34 $this->doubleDash = false;
35
36 for ($i = 0; $i < strlen($line); $i++) {
37 $char = $line[$i];
38 $next = (isset($line[$i+1])) ? $line[$i+1] : '';
39 $prev = (isset($line[$i-1])) ? $line[$i-1] : '';
40
41 if ($this->isComment()) {
42 $this->append('comment', $char);
43 if ($this->multiLine && $char == '/' && $prev == '*') {
44 $this->multiLine = false;
45 }
46 } else {
47 if ($this->isString()) {
48 $this->append('string', $char);
49 if ($prev != '\\') {
50 if ($this->singleQuote && $char == '\'') {
51 $this->singleQuote = false;
52 } elseif ($this->doubleQuote && $char == '"') {
53 $this->doubleQuote = false;
54 }
55 }
56 } else {
57 if ($char == '#') {
58 $this->hash = true;
59 } elseif ($char == '-' && $next == '-') {
60 $this->doubleDash = true;
61 } elseif ($char == '/' && $next == '*') {
62 $this->multiLine = true;
63 } elseif ($char == ';') {
64 $this->append('break', $char);
65 } elseif ($char == '\'') {
66 $this->singleQuote = true;
67 $this->append('string', $char);
68 } elseif ($char == '"') {
69 $this->doubleQuote = true;
70 $this->append('string', $char);
71 } else {
72 $this->append('code', $char);
73 }
74 }
75 }
76 }
77
78 return $this->split;
79 }
80 public function tidy($split = array())
81 {
82 $finished = array();
83
84 foreach ($split as $statement) {
85 $type = $statement['type'];
86
87 if ($type == 'code') {
88 $append = preg_replace('/\s+/', ' ', $statement['string']);
89
90 if (substr($this->parsedString, -1) == ' ' && $append[0] == ' ') {
91 $append = ltrim($append);
92 }
93
94 $this->parsedString .= $append;
95 } elseif ($type == 'string') {
96 $this->parsedString .= $statement['string'];
97 } elseif ($type == 'break') {
98 $finished[] = trim($this->parsedString);
99 $this->parsedString = '';
100 }
101 }
102
103 return $finished;
104 }
105
106 protected function append($type, $char)
107 {
108 end($this->split);
109 $index = key($this->split);
110
111 if (
112 $index !== null && $this->split[$index]['type'] == $type
113 ) {
114 $this->split[$index]['string'] .= $char;
115 } else {
116 $this->split[] = array(
117 'type' => $type,
118 'string' => $char,
119 );
120 }
121 }
122 public function isEndOfStatement()
123 {
124 return (trim($this->parsedString) == '');
125 }
126
127 protected function isComment()
128 {
129 return ($this->hash || $this->doubleDash || $this->multiLine);
130 }
131 protected function isString()
132 {
133 return $this->singleQuote || $this->doubleQuote;
134 }
135}
136
137/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
append($type, $char)
tidy($split=array())
Definition SqlParser.php:80