home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / install / class / dbmanager.php < prev    next >
Encoding:
PHP Script  |  2006-05-27  |  9.8 KB  |  246 lines

  1. <?php
  2. //  ------------------------------------------------------------------------ //
  3. //                XOOPS - PHP Content Management System                      //
  4. //                    Copyright (c) 2000 XOOPS.org                           //
  5. //                       <http://www.xoops.org/>                             //
  6. //  ------------------------------------------------------------------------ //
  7. //  This program is free software; you can redistribute it and/or modify     //
  8. //  it under the terms of the GNU General Public License as published by     //
  9. //  the Free Software Foundation; either version 2 of the License, or        //
  10. //  (at your option) any later version.                                      //
  11. //                                                                           //
  12. //  You may not change or alter any portion of this comment or credits       //
  13. //  of supporting developers from this source code or any supporting         //
  14. //  source code which is considered copyrighted (c) material of the          //
  15. //  original comment or credit authors.                                      //
  16. //                                                                           //
  17. //  This program is distributed in the hope that it will be useful,          //
  18. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  19. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  20. //  GNU General Public License for more details.                             //
  21. //                                                                           //
  22. //  You should have received a copy of the GNU General Public License        //
  23. //  along with this program; if not, write to the Free Software              //
  24. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  25. //  ------------------------------------------------------------------------ //
  26. include_once XOOPS_ROOT_PATH.'/class/logger.php';
  27. include_once XOOPS_ROOT_PATH.'/class/database/databasefactory.php';
  28. include_once XOOPS_ROOT_PATH.'/class/database/'.XOOPS_DB_TYPE.'database.php';
  29. include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php';
  30.  
  31. /**
  32. * database manager for XOOPS installer
  33. *
  34. * @author Haruki Setoyama  <haruki@planewave.org>
  35. * @version $Id: dbmanager.php 506 2006-05-26 23:10:37Z skalpa $
  36. * @access public
  37. **/
  38. class db_manager {
  39.  
  40.     var $s_tables = array();
  41.     var $f_tables = array();
  42.     var $db;
  43.  
  44.     function db_manager(){
  45.         $this->db = XoopsDatabaseFactory::getDatabase();
  46.         $this->db->setPrefix(XOOPS_DB_PREFIX);
  47.         $this->db->setLogger(XoopsLogger::instance());
  48.     }
  49.  
  50.     function isConnectable(){
  51.         return ($this->db->connect(false) != false) ? true : false;
  52.     }
  53.     
  54.     function dbExists(){
  55.         return ($this->db->connect() != false) ? true : false;
  56.     }
  57.     
  58.     function createDB()
  59.     {
  60.         $this->db->connect(false);
  61.     
  62.         $result = $this->db->query("CREATE DATABASE ".XOOPS_DB_NAME);
  63.     
  64.         return ($result != false) ? true : false;
  65.     }
  66.  
  67.     function queryFromFile($sql_file_path){
  68.         $tables = array();
  69.  
  70.         if (!file_exists($sql_file_path)) {
  71.             return false;
  72.         }
  73.         $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
  74.         SqlUtility::splitMySqlFile($pieces, $sql_query);
  75.         $this->db->connect();
  76.         foreach ($pieces as $piece) {
  77.             $piece = trim($piece);
  78.             // [0] contains the prefixed query
  79.             // [4] contains unprefixed table name
  80.             $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix());
  81.             if ($prefixed_query != false ) {
  82.                 $table = $this->db->prefix($prefixed_query[4]);
  83.                 if($prefixed_query[1] == 'CREATE TABLE'){
  84.                     if ($this->db->query($prefixed_query[0]) != false) {
  85.                         if(! isset($this->s_tables['create'][$table])){
  86.                             $this->s_tables['create'][$table] = 1;
  87.                         }
  88.                     }else{
  89.                         if(! isset($this->f_tables['create'][$table])){
  90.                             $this->f_tables['create'][$table] = 1;
  91.                         }
  92.                     }
  93.                 }
  94.                 elseif($prefixed_query[1] == 'INSERT INTO'){
  95.                     if ($this->db->query($prefixed_query[0]) != false) {
  96.                         if(! isset($this->s_tables['insert'][$table])){
  97.                             $this->s_tables['insert'][$table] = 1;
  98.                         }else{
  99.                             $this->s_tables['insert'][$table]++;
  100.                         }
  101.                     }else{
  102.                         if(! isset($this->f_tables['insert'][$table])){
  103.                             $this->f_tables['insert'][$table] = 1;
  104.                         }else{
  105.                              $this->f_tables['insert'][$table]++;
  106.                         }
  107.                     }
  108.                 }elseif($prefixed_query[1] == 'ALTER TABLE'){
  109.                     if ($this->db->query($prefixed_query[0]) != false) {
  110.                         if(! isset($this->s_tables['alter'][$table])){
  111.                             $this->s_tables['alter'][$table] = 1;
  112.                         }
  113.                     }else{
  114.                         if(! isset($this->s_tables['alter'][$table])){
  115.                             $this->f_tables['alter'][$table] = 1;
  116.                         }
  117.                     }
  118.                 }elseif($prefixed_query[1] == 'DROP TABLE'){
  119.                     if ($this->db->query('DROP TABLE '.$table) != false) {
  120.                         if(! isset($this->s_tables['drop'][$table])){
  121.                             $this->s_tables['drop'][$table] = 1;
  122.                         }
  123.                     }else{
  124.                         if(! isset($this->s_tables['drop'][$table])){
  125.                             $this->f_tables['drop'][$table] = 1;
  126.                         }
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.         return true;
  132.     }
  133.  
  134.     function report(){
  135.         $content = "<table align='center'><tr><td align='left'>\n";
  136.         if (isset($this->s_tables['create'])) {
  137.             foreach($this->s_tables['create'] as $key => $val){
  138.                 $content .= _OKIMG.sprintf(_INSTALL_L45, "<b>$key</b>")."<br />\n";
  139.             }
  140.         }
  141.         if (isset($this->s_tables['insert'])) {
  142.             foreach($this->s_tables['insert'] as $key => $val){
  143.                 $content .= _OKIMG.sprintf(_INSTALL_L119, $val, "<b>$key</b>")."<br />\n";
  144.             }
  145.         }
  146.         if (isset($this->s_tables['alter'])) {
  147.             foreach($this->s_tables['alter'] as $key => $val){
  148.                 $content .= _OKIMG.sprintf(_INSTALL_L133, "<b>$key</b>")."<br />\n";
  149.             }
  150.         }
  151.         if (isset($this->s_tables['drop'])) {
  152.             foreach($this->s_tables['drop'] as $key => $val){
  153.                 $content .= _OKIMG.sprintf(_INSTALL_L163, "<b>$key</b>")."<br />\n";
  154.             }
  155.         }
  156.         $content .= "<br />\n";
  157.         if (isset($this->f_tables['create'])) {
  158.             foreach($this->f_tables['create'] as $key => $val){
  159.                 $content .= _NGIMG.sprintf(_INSTALL_L118, "<b>$key</b>")."<br />\n";
  160.             }
  161.         }
  162.         if (isset($this->f_tables['insert'])) {
  163.             foreach($this->f_tables['insert'] as $key => $val){
  164.                 $content .= _NGIMG.sprintf(_INSTALL_L120, $val, "<b>$key</b>")."<br />\n";
  165.             }
  166.         }
  167.         if (isset($this->f_tables['alter'])) {
  168.             foreach($this->f_tables['alter'] as $key => $val){
  169.                 $content .= _NGIMG.sprintf(_INSTALL_L134, "<b>$key</b>")."<br />\n";
  170.             }
  171.         }
  172.         if (isset($this->f_tables['drop'])) {
  173.             foreach($this->f_tables['drop'] as $key => $val){
  174.                 $content .= _NGIMG.sprintf(_INSTALL_L164, "<b>$key</b>")."<br />\n";
  175.             }
  176.         }
  177.         $content .= "</td></tr></table>\n";
  178.         return $content;
  179.     }
  180.  
  181.     function query($sql){
  182.         $this->db->connect();
  183.         return $this->db->query($sql);
  184.     }
  185.  
  186.     function prefix($table){
  187.         $this->db->connect();
  188.         return $this->db->prefix($table);
  189.     }
  190.  
  191.     function fetchArray($ret){
  192.         $this->db->connect();
  193.         return $this->db->fetchArray($ret);
  194.     }
  195.  
  196.     function insert($table, $query){
  197.         $this->db->connect();
  198.         $table = $this->db->prefix($table);
  199.         $query = 'INSERT INTO '.$table.' '.$query;
  200.         if(!$this->db->queryF($query)){
  201.             //var_export($query);
  202.             //echo '<br />' . mysql_error() . '<br />';
  203.             if(!isset($this->f_tables['insert'][$table])){
  204.                 $this->f_tables['insert'][$table] = 1;
  205.             }else{
  206.                 $this->f_tables['insert'][$table]++;
  207.             }
  208.             return false;
  209.         }else{
  210.             if(!isset($this->s_tables['insert'][$table])){
  211.                 $this->s_tables['insert'][$table] = 1;
  212.             }else{
  213.                 $this->s_tables['insert'][$table]++;
  214.             }
  215.             return $this->db->getInsertId();
  216.         }
  217.     }
  218.  
  219.     function isError(){
  220.         return (isset($this->f_tables)) ? true : false;
  221.     }
  222.  
  223.     function deleteTables($tables){
  224.         $deleted = array();
  225.         $this->db->connect();
  226.         foreach ($tables as $key => $val) {
  227.             if(! $this->db->query("DROP TABLE ".$this->db->prefix($key))){
  228.                 $deleted[] = $ct;
  229.             }
  230.         }
  231.         return $deleted;
  232.     }
  233.  
  234.     function tableExists($table){
  235.         $table = trim($table);
  236.         $ret = false;
  237.         if ($table != '') {
  238.             $this->db->connect();
  239.             $sql = 'SELECT * FROM '.$this->db->prefix($table);
  240.             $ret = (false != $this->db->query($sql)) ? true : false;
  241.         }
  242.         return $ret;
  243.     }
  244. }
  245.  
  246. ?>