20 * @author Dan Fuhry |
20 * @author Dan Fuhry |
21 */ |
21 */ |
22 |
22 |
23 class SQL_Parser |
23 class SQL_Parser |
24 { |
24 { |
25 /** |
25 /** |
26 * The SQL to be parsed. |
26 * The SQL to be parsed. |
27 * @var string |
27 * @var string |
28 * @access private |
28 * @access private |
29 */ |
29 */ |
30 |
30 |
31 private $sql_string; |
31 private $sql_string; |
32 |
32 |
33 /** |
33 /** |
34 * Parsed SQL array |
34 * Parsed SQL array |
35 * @var array |
35 * @var array |
36 * @access private |
36 * @access private |
37 */ |
37 */ |
38 |
38 |
39 private $sql_array; |
39 private $sql_array; |
40 |
40 |
41 /** |
41 /** |
42 * Template variables. |
42 * Template variables. |
43 * @var array |
43 * @var array |
44 * @access private |
44 * @access private |
45 */ |
45 */ |
46 |
46 |
47 private $tpl_strings; |
47 private $tpl_strings; |
48 |
48 |
49 /** |
49 /** |
50 * Constructor. |
50 * Constructor. |
51 * @param string If this contains newlines, it will be treated as the target SQL. If not, will be treated as a filename. |
51 * @param string If this contains newlines, it will be treated as the target SQL. If not, will be treated as a filename. |
52 * @param string If true, force as raw SQL, i.e. don't treat as a filename no matter what |
52 * @param string If true, force as raw SQL, i.e. don't treat as a filename no matter what |
53 */ |
53 */ |
54 |
54 |
55 public function __construct($sql, $force_file = false) |
55 public function __construct($sql, $force_file = false) |
56 { |
56 { |
57 if ( strpos($sql, "\n") || $force_file ) |
57 if ( strpos($sql, "\n") || $force_file ) |
58 { |
58 { |
59 $this->sql_string = $sql; |
59 $this->sql_string = $sql; |
60 } |
60 } |
61 else |
61 else |
62 { |
62 { |
63 if ( file_exists($sql) ) |
63 if ( file_exists($sql) ) |
64 { |
64 { |
65 $this->sql_string = @file_get_contents($sql); |
65 $this->sql_string = @file_get_contents($sql); |
66 if ( empty($this->sql_string) ) |
66 if ( empty($this->sql_string) ) |
67 { |
67 { |
68 throw new Exception('SQL file is blank or permissions are bad'); |
68 throw new Exception('SQL file is blank or permissions are bad'); |
69 } |
69 } |
70 } |
70 } |
71 else |
71 else |
72 { |
72 { |
73 throw new Exception('SQL file doesn\'t exist'); |
73 throw new Exception('SQL file doesn\'t exist'); |
74 } |
74 } |
75 |
75 |
76 } |
76 } |
77 $this->sql_array = false; |
77 $this->sql_array = false; |
78 $this->tpl_strings = array(); |
78 $this->tpl_strings = array(); |
79 |
79 |
80 // convert \r\n in the schema to \n, in case some FTP client or zip utility ran unix2dos for us |
80 // convert \r\n in the schema to \n, in case some FTP client or zip utility ran unix2dos for us |
81 // thanks to InvisGhost for reporting this error |
81 // thanks to InvisGhost for reporting this error |
82 $this->sql_string = str_replace("\r\n", "\n", $this->sql_string); |
82 $this->sql_string = str_replace("\r\n", "\n", $this->sql_string); |
83 } |
83 } |
84 |
84 |
85 /** |
85 /** |
86 * Sets template variables. |
86 * Sets template variables. |
87 * @param array Associative array of template variables to assign |
87 * @param array Associative array of template variables to assign |
88 */ |
88 */ |
89 |
89 |
90 public function assign_vars($vars) |
90 public function assign_vars($vars) |
91 { |
91 { |
92 if ( !is_array($vars) ) |
92 if ( !is_array($vars) ) |
93 return false; |
93 return false; |
94 $this->tpl_strings = array_merge($this->tpl_strings, $vars); |
94 $this->tpl_strings = array_merge($this->tpl_strings, $vars); |
95 } |
95 } |
96 |
96 |
97 /** |
97 /** |
98 * Internal function to parse the SQL. |
98 * Internal function to parse the SQL. |
99 * @access private |
99 * @access private |
100 */ |
100 */ |
101 |
101 |
102 private function parse_sql() |
102 private function parse_sql() |
103 { |
103 { |
104 $this->sql_array = $this->sql_string; |
104 $this->sql_array = $this->sql_string; |
105 foreach ( $this->tpl_strings as $key => $value ) |
105 foreach ( $this->tpl_strings as $key => $value ) |
106 { |
106 { |
107 $this->sql_array = str_replace("{{{$key}}}", $value, $this->sql_array); |
107 $this->sql_array = str_replace("{{{$key}}}", $value, $this->sql_array); |
108 } |
108 } |
109 |
109 |
110 // Strip out comments |
110 // Strip out comments |
111 $this->sql_array = explode("\n", $this->sql_array); |
111 $this->sql_array = explode("\n", $this->sql_array); |
112 |
112 |
113 foreach ( $this->sql_array as $i => $sql ) |
113 foreach ( $this->sql_array as $i => $sql ) |
114 { |
114 { |
115 $query =& $this->sql_array[$i]; |
115 $query =& $this->sql_array[$i]; |
116 $t = trim($query); |
116 $t = trim($query); |
117 if ( preg_match('/^(\#|--)/i', $t) ) |
117 if ( preg_match('/^(\#|--)/i', $t) ) |
118 { |
118 { |
119 unset($this->sql_array[$i]); |
119 unset($this->sql_array[$i]); |
120 unset($query); |
120 unset($query); |
121 } |
121 } |
122 } |
122 } |
123 unset($query); |
123 unset($query); |
124 |
124 |
125 $this->sql_array = array_values($this->sql_array); |
125 $this->sql_array = array_values($this->sql_array); |
126 $this->sql_array = implode("\n", $this->sql_array); |
126 $this->sql_array = implode("\n", $this->sql_array); |
127 $this->sql_array = explode(";\n", trim($this->sql_array)); |
127 $this->sql_array = explode(";\n", trim($this->sql_array)); |
128 |
128 |
129 foreach ( $this->sql_array as $i => $sql ) |
129 foreach ( $this->sql_array as $i => $sql ) |
130 { |
130 { |
131 $query =& $this->sql_array[$i]; |
131 $query =& $this->sql_array[$i]; |
132 $query = trim($query); |
132 $query = trim($query); |
133 if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' ) |
133 if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' ) |
134 { |
134 { |
135 $query .= ';'; |
135 $query .= ';'; |
136 } |
136 } |
137 } |
137 } |
138 unset($query); |
138 unset($query); |
139 } |
139 } |
140 |
140 |
141 /** |
141 /** |
142 * Returns the parsed array of SQL queries. |
142 * Returns the parsed array of SQL queries. |
143 * @param bool Optional. Defaults to false. If true, a parse is performed even if it already happened. |
143 * @param bool Optional. Defaults to false. If true, a parse is performed even if it already happened. |
144 * @return array |
144 * @return array |
145 */ |
145 */ |
146 |
146 |
147 public function parse($force_reparse = false) |
147 public function parse($force_reparse = false) |
148 { |
148 { |
149 if ( !$this->sql_array || $force_reparse ) |
149 if ( !$this->sql_array || $force_reparse ) |
150 $this->parse_sql(); |
150 $this->parse_sql(); |
151 return $this->sql_array; |
151 return $this->sql_array; |
152 } |
152 } |
153 } |
153 } |
154 |
154 |
155 ?> |
155 ?> |