[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Agora1-4/ecrire/install/ -> SQLScript.php (source)

   1  <?php
   2  /*****************************************************
   3  * This file is part of Agora, web based content management system.
   4  *
   5  * Agora is free software; you can redistribute it and/or modify
   6  * it under the terms of the GNU General Public License as published by
   7  * the Free Software Foundation; version 2 of the License.
   8  *
   9  * Agora is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU General Public License for more details (file "COPYING").
  13  *
  14  * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière.
  15  * List of authors detailed in "copyright_fr.html" file.
  16  * E-mail : agora@sig.premier-ministre.gouv.fr
  17  * Web site : http://www.agora.gouv.fr
  18  *****************************************************/
  19  // API for formatted SQL script execution through DB
  20  //
  21  
  22  //Using PEAR for simple error handling
  23  
  24  require_once ("PEAR.php");
  25  
  26  // {{{ constants
  27  
  28  // script read modes
  29  define('BLOCKMODE', 1);
  30  define('LINEMODE', 2);
  31  
  32  define('INVALIDFILETYPE', -1);
  33  // }}}
  34  
  35  /**
  36   * SQLScript is a helper class to execute
  37   * specially formatted SQL scripts. It offers two main methods
  38   * one executing [action] formatted blocks, the other one
  39   * executing a SQL file line by line
  40   *
  41   * @auhthor Antoine Angénieux <aangenieux@clever-age.com>
  42   * @version 1.0
  43   */
  44  
  45  class SQLScript {
  46  
  47      // {{{ properties
  48  
  49      /**
  50       * _sqlFile is the filename of the sql script to execute
  51       * @var        String
  52       * @access    private
  53       */
  54      var $_sqlFile;
  55  
  56      /**
  57       * _fileType specifies whether to execute a block formatted script
  58       * or a line separated script. Its value should be either
  59       * BLOCKMODE or LINEMODE
  60       * @var        int
  61       * @access    private
  62       */
  63      var $_fileType;
  64  
  65      // {{{ constructor
  66  
  67  	function SQLScript ($sqlFile, $fileType = BLOCKMODE) {
  68          if (!($fileType == BLOCKMODE || $fileType == LINEMODE)) {
  69              die ("<br>Filetype specified is neither BLOCKMODE nor LINEMODE ! fileType = $fileType");
  70          }
  71  
  72          $this->_sqlFile = $sqlFile;
  73          $this->_fileType = $fileType;
  74      }
  75  
  76      // }}}
  77  
  78      // {{{ destructor
  79  
  80  	function _SQLScript () {
  81          $this->_sqlFile = null;
  82      }
  83  
  84      // }}}
  85  
  86      // {{{ getSqlFile()
  87  
  88      /**
  89       * returns the SQL file name
  90       * @return    String
  91       * @access    public
  92       */
  93  	function getSqlFile () {
  94          return $this->_sqlFile;
  95      }
  96  
  97      // }}}
  98  
  99      // {{{ getFileType()
 100  
 101      /**
 102       * returns the file type
 103       * @return    int
 104       * @access    public
 105       */
 106  
 107  	function getFileType () {
 108          return $this->_fileType;
 109      }
 110  
 111      // }}}
 112  
 113      // {{{ setSqlFile()
 114  
 115      /**
 116       * sets the SQL file name
 117       * @var        String
 118       * @access    public
 119       */
 120  	function setSqlFile ($sqlFile) {
 121          $this->_sqlFile = $sqlFile;
 122      }
 123  
 124      // }}}
 125  
 126      // {{{ setFileType()
 127  
 128      /**
 129       * sets the file type
 130       * @var    int
 131       * @access    public
 132       */
 133  
 134  	function setFileType ($fileType) {
 135          if (!($fileType == BLOCKMODE || $fileType == LINEMODE)) {
 136              die ("<br>Filetype specified is neither BLOCKMODE nor LINEMODE ! fileType = $fileType");
 137          }
 138  
 139          $this->_fileType = $fileType;
 140      }
 141  
 142      // }}}
 143  
 144      // {{{ executeScript()
 145  
 146      /**
 147       * Execute the specified script
 148       * @param    $db DB or MDB reference to an open connection
 149       * @return    int    1 on success, other if an error occured (see constants for error codes)
 150       * @access    public
 151       */
 152  
 153  	function executeScript (&$dbInstance) {
 154          switch ($this->_fileType) {
 155              case BLOCKMODE:
 156                  $queries = $this->_buildBlockMode();
 157                  break;
 158  
 159              case LINEMODE:
 160                  $queries = $this->_buildLineMode();
 161                  break;
 162          }
 163  
 164          reset ($queries);
 165  
 166          while (list(, $query) = each($queries)) {
 167              if (trim($query) != '') {
 168                  $res = $this->_executeQuery($dbInstance, $query);
 169                  if (PEAR::isError($res)) {
 170                      echo ("<br>Error executing query [$query]. Quitting.<br>");
 171                      die ($res->getMessage());
 172                  }
 173              }
 174          }
 175  
 176          return 1;
 177      }
 178  
 179      // }}}
 180  
 181      /**
 182       * Builds the specified script in block mode
 183       * @param    $db DB or MDB reference to an open connection
 184       * @return    array
 185       * @access    protected
 186       */
 187  
 188      // {{{ _buildBlockMode()
 189  
 190      function &_buildBlockMode () {
 191          $allBlocks = implode(' ', file($this->_sqlFile));
 192  
 193          if (!is_string($allBlocks)) {
 194              die ('<br>File ' . $this->_sqlFile . ' cannot be loaded. It probably does not exist.');
 195          }
 196  
 197          $queries = explode('[action]', $allBlocks);
 198          return $queries;
 199      }
 200  
 201      // }}}
 202  
 203      // {{{ _buildLineMode()
 204  
 205      /**
 206       * builds the specified script in line mode
 207       * @param    $db DB or MDB reference to an open connection
 208       * @return    array
 209       * @access    protected
 210       */
 211  
 212      function &_buildLineMode () {
 213          $queries = file($this->_sqlFile);
 214  
 215          if (!is_array($queries)) {
 216              die ('<br>File ' . $this->_sqlFile . ' cannot be loaded. It probably does not exist.');
 217          }
 218          else {
 219              return $queries;
 220          }
 221      }
 222  
 223      // }}}
 224  
 225      // {{{ _executeQuery()
 226  
 227      /**
 228       * Execute the passed query. Subclasses can override this method
 229       * in order to be compliant with the BD object passed, or to do
 230       * any processing prior to executing the query.
 231       * @param    $db DB or MDB reference to an open connection
 232       * @param    $query String representing the SQL query to execute
 233       */
 234  
 235  	function _executeQuery (&$dbInstance, $query) {
 236          return $dbInstance->query($query);
 237      }
 238  
 239  // }}}
 240  
 241  }
 242  ?>


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7