diff -r 299a90e28abc -r 87e08a6e4fec install/includes/sql_parse.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/install/includes/sql_parse.php Wed Jan 16 13:55:49 2008 -0500 @@ -0,0 +1,149 @@ +sql_string = $sql; + } + else + { + if ( file_exists($sql) ) + { + $this->sql_string = @file_get_contents($sql); + if ( empty($this->sql_string) ) + { + throw new Exception('SQL file is blank or permissions are bad'); + } + } + else + { + throw new Exception('SQL file doesn\'t exist'); + } + } + $this->sql_array = false; + $this->tpl_strings = array(); + } + + /** + * Sets template variables. + * @param array Associative array of template variables to assign + */ + + public function assign_vars($vars) + { + if ( !is_array($vars) ) + return false; + $this->tpl_strings = array_merge($this->tpl_strings, $vars); + } + + /** + * Internal function to parse the SQL. + * @access private + */ + + private function parse_sql() + { + $this->sql_array = $this->sql_string; + foreach ( $this->tpl_strings as $key => $value ) + { + $this->sql_array = str_replace("{{{$key}}}", $value, $this->sql_array); + } + + // Build an array of queries + $this->sql_array = explode("\n", $this->sql_array); + + foreach ( $this->sql_array as $i => $sql ) + { + $query =& $this->sql_array[$i]; + $t = trim($query); + if ( empty($t) || preg_match('/^(\#|--)/i', $t) ) + { + unset($this->sql_array[$i]); + unset($query); + } + } + unset($query); + + $this->sql_array = array_values($this->sql_array); + $this->sql_array = implode("\n", $this->sql_array); + $this->sql_array = explode(";\n", $this->sql_array); + + foreach ( $this->sql_array as $i => $sql ) + { + $query =& $this->sql_array[$i]; + if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' ) + { + $query .= ';'; + } + } + unset($query); + } + + /** + * Returns the parsed array of SQL queries. + * @param bool Optional. Defaults to false. If true, a parse is performed even if it already happened. + * @return array + */ + + public function parse($force_reparse = false) + { + if ( !$this->sql_array || $force_reparse ) + $this->parse_sql(); + return $this->sql_array; + } +} + +?>