install/includes/sql_parse.php
changeset 526 b2fb50d572c7
parent 525 3f2dfdb99be4
child 527 21e11f564463
equal deleted inserted replaced
525:3f2dfdb99be4 526:b2fb50d572c7
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.1
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  * Installation package
       
     8  * sql_parse.php - SQL query splitter and templater
       
     9  *
       
    10  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    11  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    15  */
       
    16 
       
    17 /**
       
    18  * Parses a full file of SQL into individual queries. Also includes substitution (template) functions.
       
    19  * @package Enano
       
    20  * @subpackage Installer
       
    21  * @author Dan Fuhry
       
    22  */
       
    23 
       
    24 class SQL_Parser
       
    25 {
       
    26   /**
       
    27    * The SQL to be parsed.
       
    28    * @var string
       
    29    * @access private
       
    30    */
       
    31   
       
    32   private $sql_string;
       
    33   
       
    34   /**
       
    35    * Parsed SQL array
       
    36    * @var array
       
    37    * @access private
       
    38    */
       
    39   
       
    40   private $sql_array;
       
    41   
       
    42   /**
       
    43    * Template variables.
       
    44    * @var array
       
    45    * @access private
       
    46    */
       
    47   
       
    48   private $tpl_strings;
       
    49   
       
    50   /**
       
    51    * Constructor.
       
    52    * @param string If this contains newlines, it will be treated as the target SQL. If not, will be treated as a filename.
       
    53    */
       
    54   
       
    55   public function __construct($sql)
       
    56   {
       
    57     if ( strpos($sql, "\n") )
       
    58     {
       
    59       $this->sql_string = $sql;
       
    60     }
       
    61     else
       
    62     {
       
    63       if ( file_exists($sql) )
       
    64       {
       
    65         $this->sql_string = @file_get_contents($sql);
       
    66         if ( empty($this->sql_string) )
       
    67         {
       
    68           throw new Exception('SQL file is blank or permissions are bad');
       
    69         }
       
    70       }
       
    71       else
       
    72       {
       
    73         throw new Exception('SQL file doesn\'t exist');
       
    74       }
       
    75     }
       
    76     $this->sql_array = false;
       
    77     $this->tpl_strings = array();
       
    78   }
       
    79   
       
    80   /**
       
    81    * Sets template variables.
       
    82    * @param array Associative array of template variables to assign
       
    83    */
       
    84   
       
    85   public function assign_vars($vars)
       
    86   {
       
    87     if ( !is_array($vars) )
       
    88       return false;
       
    89     $this->tpl_strings = array_merge($this->tpl_strings, $vars);
       
    90   }
       
    91   
       
    92   /**
       
    93    * Internal function to parse the SQL.
       
    94    * @access private
       
    95    */
       
    96   
       
    97   private function parse_sql()
       
    98   {
       
    99     $this->sql_array = $this->sql_string;
       
   100     foreach ( $this->tpl_strings as $key => $value )
       
   101     {
       
   102       $this->sql_array = str_replace("{{{$key}}}", $value, $this->sql_array);
       
   103     }
       
   104     
       
   105     // Build an array of queries
       
   106     $this->sql_array = explode("\n", $this->sql_array);
       
   107     
       
   108     foreach ( $this->sql_array as $i => $sql )
       
   109     {
       
   110       $query =& $this->sql_array[$i];
       
   111       $t = trim($query);
       
   112       if ( empty($t) || preg_match('/^(\#|--)/i', $t) )
       
   113       {
       
   114         unset($this->sql_array[$i]);
       
   115         unset($query);
       
   116       }
       
   117     }
       
   118     unset($query);
       
   119     
       
   120     $this->sql_array = array_values($this->sql_array);
       
   121     $this->sql_array = implode("\n", $this->sql_array);
       
   122     $this->sql_array = explode(";\n", $this->sql_array);
       
   123     
       
   124     foreach ( $this->sql_array as $i => $sql )
       
   125     {
       
   126       $query =& $this->sql_array[$i];
       
   127       if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' )
       
   128       {
       
   129         $query .= ';';
       
   130       }
       
   131     }
       
   132     unset($query);
       
   133   }
       
   134   
       
   135   /**
       
   136    * Returns the parsed array of SQL queries.
       
   137    * @param bool Optional. Defaults to false. If true, a parse is performed even if it already happened.
       
   138    * @return array
       
   139    */
       
   140   
       
   141   public function parse($force_reparse = false)
       
   142   {
       
   143     if ( !$this->sql_array || $force_reparse )
       
   144       $this->parse_sql();
       
   145     return $this->sql_array;
       
   146   }
       
   147 }
       
   148 
       
   149 ?>