[ Index ]
 

Code source de vtiger CRM 5.0.2

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

title

Body

[fermer]

/include/database/ -> PearDatabase.php (source)

   1  <?php
   2  /*********************************************************************************
   3   * The contents of this file are subject to the SugarCRM Public License Version 1.1.2
   4   * ("License"); You may not use this file except in compliance with the
   5   * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
   6   * Software distributed under the License is distributed on an  "AS IS"  basis,
   7   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
   8   * the specific language governing rights and limitations under the License.
   9   * The Original Code is:  SugarCRM Open Source
  10   * The Initial Developer of the Original Code is SugarCRM, Inc.
  11   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
  12   * All Rights Reserved.
  13   * Contributor(s): ______________________________________.
  14   ********************************************************************************/
  15  
  16  require_once ('include/logging.php');
  17  include ('adodb/adodb.inc.php');
  18  require_once ("adodb/adodb-xmlschema.inc.php");
  19  
  20  $log =& LoggerManager::getLogger('VT');
  21  
  22  class PearDatabase{
  23      var $database = null;
  24      var $dieOnError = false;
  25      var $dbType = null;
  26      var $dbHostName = null;
  27      var $dbName = null;
  28      var $dbOptions = null;
  29      var $userName=null;
  30      var $userPassword=null;
  31      var $query_time = 0;
  32      var $log = null;
  33      var $lastmysqlrow = -1;
  34      var $enableSQLlog = false;
  35  
  36      function isMySQL() { return dbType=='mysql'; }
  37      function isOracle() { return dbType=='oci8'; }
  38      
  39      function println($msg)
  40      {
  41      require_once ('include/logging.php');
  42      $log1 =& LoggerManager::getLogger('VT');
  43      if(is_array($msg))
  44      {
  45          $log1->info("PearDatabse ->".print_r($msg,true));
  46      }
  47      else
  48      {
  49          $log1->info("PearDatabase ->".$msg);
  50      }
  51      return $msg;
  52      }
  53  
  54      function setDieOnError($value){    
  55      $this->dieOnError = $value;
  56      }
  57      
  58      function setDatabaseType($type){
  59      $this->dbType = $type;
  60      }
  61      
  62      function setUserName($name){
  63      $this->userName = $name;
  64      }
  65      
  66      function setOption($name, $value){
  67      if(isset($this->dbOptions))
  68          $this->dbOptions[$name] = $value;
  69      if(isset($this->database))
  70          $this->database->setOption($name, $value);
  71      }    
  72      
  73      function setUserPassword($pass){
  74      $this->userPassword = $pass;    
  75      }
  76      
  77      function setDatabaseName($db){
  78      $this->dbName = $db;    
  79      }
  80      
  81      function setDatabaseHost($host){
  82      $this->dbHostName = $host;    
  83      }
  84      
  85      function getDataSourceName(){
  86      return     $this->dbType. "://".$this->userName.":".$this->userPassword."@". $this->dbHostName . "/". $this->dbName;
  87      }
  88  
  89      function startTransaction()
  90      {
  91      $this->checkConnection();
  92      $this->println("TRANS Started");
  93      $this->database->StartTrans();
  94      }
  95  
  96      function completeTransaction()
  97      {        
  98      if($this->database->HasFailedTrans()) 
  99          $this->println("TRANS  Rolled Back");
 100      else
 101          $this->println("TRANS  Commited");
 102      
 103      $this->database->CompleteTrans();
 104      $this->println("TRANS  Completed");
 105      }
 106  
 107  /* ADODB converted    
 108   *    function checkError($msg='', $dieOnError=false)
 109   *    {
 110   *    if($this->dbType == "mysql")
 111   *    {
 112   *        if (mysql_errno())
 113   *        {
 114   *        if($this->dieOnError || $dieOnError)
 115   *        {
 116   *            $this->log->fatal("MySQL error ".mysql_errno().": ".mysql_error());    
 117   *            die ($msg."MySQL error ".mysql_errno().": ".mysql_error());
 118   *        } else {
 119   *            $this->log->error("MySQL error ".mysql_errno().": ".mysql_error());    
 120   *        }
 121   *        return true;
 122   *        }
 123   *        return false;
 124   *    }    
 125   *    else
 126   *    {
 127   *        if(!isset($this->database))
 128   *        {
 129   *        $this->log->error("Database Is Not Connected");
 130   *        return true;
 131   *        }
 132   *        if(DB::isError($this->database))
 133   *        {
 134   *        if($this->dieOnError || $dieOnError)
 135   *        {
 136   *            $this->log->fatal($msg.$this->database->getMessage());
 137   *            die ($msg.$this->database->getMessage());    
 138   *        } else {
 139   *            $this->log->error($msg.$this->database->getMessage());        
 140   *        }
 141   *        return true;
 142   *        }
 143   *    }
 144   *    return false;
 145   *   }
 146   */
 147      
 148      function checkError($msg='', $dieOnError=false)
 149      {
 150  /*
 151   *    if($this->database->ErrorNo())
 152   *    {
 153   *        if($this->dieOnError || $dieOnError)
 154   *        {
 155   *        $this->println("ADODB error ".$this->database->ErrorNo());    
 156   *        die ($msg."ADODB error ".$this->database->ErrorNo());
 157   *        } else {
 158   *        $this->log->error("MySQL error ".mysql_errno().": ".mysql_error());
 159   *        }
 160   *        return true;
 161   *    }
 162   */
 163      
 164      if($this->dieOnError || $dieOnError)
 165      {
 166          $this->println("ADODB error ".$msg."->[".$this->database->ErrorNo()."]".$this->database->ErrorMsg());    
 167          die ($msg."ADODB error ".$msg."->".$this->database->ErrorMsg());
 168      }
 169      else
 170      {
 171          $this->println("ADODB error ".$msg."->[".$this->database->ErrorNo()."]".$this->database->ErrorMsg());
 172      }
 173      return false;
 174      }
 175  
 176      function change_key_case($arr)
 177      {
 178      return is_array($arr)?array_change_key_case($arr):$arr;
 179      }
 180  
 181      var $req_flist;    
 182      
 183      /**
 184      * @return void
 185      * @desc checks if a connection exists if it does not it closes the connection
 186       * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
 187       * All Rights Reserved..
 188       * Contributor(s): ______________________________________..
 189      */
 190      function checkConnection(){
 191      global $log;
 192  
 193      if(!isset($this->database))
 194      {
 195          $this->println("TRANS creating new connection");
 196  /*
 197   *        $flist=get_included_files();
 198   *        foreach($flist as $key=>$value)
 199   *        {
 200   *        if(!strstr($value,'\\modules') && !strstr($value,'\\data'))
 201   *        unset($flist[$key]);
 202   *        }
 203   *        $this->println($flist);
 204   */
 205          $this->connect(false);
 206      }
 207      else
 208      {
 209          //$this->println("checkconnect using old connection");
 210      }
 211      }
 212  
 213  /* ADODB converted    
 214   *    function query($sql, $dieOnError=false, $msg='')
 215   *    {
 216   *    $this->println("query ".$sql);
 217   *    $this->log->info('Query:' . $sql);
 218   *    $this->checkConnection();
 219   *    $this->query_time = microtime();
 220   *    if($this->dbType == "mysql")
 221   *    {
 222   *        $result =& mysql_query($sql);
 223   *        $this->lastmysqlrow = -1;     
 224   *    } else {
 225   *        $result =& $this->database->query($sql);
 226   *    }
 227   *    $this->query_time = microtime() - $this->query_time;
 228   *    $this->log->info('Query Execution Time:'.$this->query_time);
 229   *    $this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError);    
 230   *    return $result;
 231   *    }
 232   */
 233  
 234      function query($sql, $dieOnError=false, $msg='')
 235      {
 236      global $log;
 237      //$this->println("ADODB query ".$sql);        
 238      $log->debug('query being executed : '.$sql);
 239      $this->checkConnection();
 240      $result = & $this->database->Execute($sql);
 241      $this->lastmysqlrow = -1;
 242      if(!$result)$this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError);
 243      return $result;        
 244      }
 245  
 246      function getEmptyBlob()
 247      {
 248      //if(dbType=="oci8") return 'empty_blob()';
 249      //else return 'null';
 250      return 'null';
 251      }
 252  
 253      function updateBlob($tablename, $colname, $id, $data)    
 254      {
 255      $this->println("updateBlob t=".$tablename." c=".$colname." id=".$id);
 256      $this->checkConnection();
 257      $result = $this->database->UpdateBlob($tablename, $colname, $data, $id);
 258      $this->println("updateBlob t=".$tablename." c=".$colname." id=".$id." status=".$result);
 259      return $result;
 260      }
 261  
 262      function updateBlobFile($tablename, $colname, $id, $filename)    
 263      {
 264      $this->println("updateBlobFile t=".$tablename." c=".$colname." id=".$id." f=".$filename);
 265      $this->checkConnection();
 266      $result = $this->database->UpdateBlobFile($tablename, $colname, $filename, $id);
 267      $this->println("updateBlobFile t=".$tablename." c=".$colname." id=".$id." f=".$filename." status=".$result);
 268      return $result;
 269      }
 270  
 271  /* ADODB converted
 272   *    function limitQuery($sql,$start,$count, $dieOnError=false, $msg='')
 273   *    {
 274   *    if($this->dbType == "mysql")
 275   *        return $this->query("$sql LIMIT $start,$count", $dieOnError, $msg);
 276   *    $this->log->info('Limit Query:' . $sql. ' Start: ' .$start . ' count: ' . $count);
 277   *    $this->lastsql = $sql;
 278   *    
 279   *    $this->checkConnection();
 280   *    $this->query_time = microtime();
 281   *    $result =& $this->database->limitQuery($sql,$start, $count);
 282   *    $this->query_time = microtime() - $this->query_time;
 283   *    $this->log->info('Query Execution Time:'.$this->query_time);
 284   *    $this->checkError($msg.' Query Failed:' . $sql . '::', $dieOnError);    
 285   *    return $result;
 286   *    }
 287   */
 288      
 289      function limitQuery($sql,$start,$count, $dieOnError=false, $msg='')
 290      {
 291      global $log;
 292      //$this->println("ADODB limitQuery sql=".$sql." st=".$start." co=".$count);
 293      $log->debug(' limitQuery sql = '.$sql .' st = '.$start .' co = '.$count);
 294      $this->checkConnection();
 295      $result =& $this->database->SelectLimit($sql,$count,$start);
 296      if(!$result) $this->checkError($msg.' Limit Query Failed:' . $sql . '::', $dieOnError);
 297      return $result;        
 298      }
 299      
 300  /* ADODB converted
 301   *   function getOne($sql, $dieOnError=false, $msg='')
 302   *   {
 303   *    $this->log->info('Get One:' . $sql);
 304   *    $this->checkConnection();
 305   *    if($this->dbType == "mysql"){
 306   *        $queryresult =& $this->query($sql, $dieOnError, $msg);
 307   *        $result =& mysql_result($queryresult,0);
 308   *    } else {
 309   *        $result =& $this->database->getOne($sql);
 310   *    }
 311   *    $this->checkError($msg.' Get One Failed:' . $sql . '::', $dieOnError);    
 312   *    return $result;
 313   *    }
 314   */
 315  
 316      function getOne($sql, $dieOnError=false, $msg='')
 317      {
 318      $this->println("ADODB getOne sql=".$sql);
 319      $this->checkConnection();
 320      $result =& $this->database->GetOne($sql);
 321      if(!$result) $this->checkError($msg.' Get one Query Failed:' . $sql . '::', $dieOnError);
 322      return $result;        
 323      }
 324  
 325  /* ADODB converted
 326   *    function getFieldsArray(&$result)
 327   *    {
 328   *    $field_array = array();
 329   *
 330   *    if(! isset($result) || empty($result))
 331   *    {
 332   *        return 0;
 333   *    }
 334   *
 335   *    if($this->dbType == "mysql")
 336   *    {
 337   *        $i = 0;
 338   *        while ($i < mysql_num_fields($result)) 
 339   *        {
 340   *        $meta = mysql_fetch_field($result, $i);
 341   *
 342   *        if (!$meta) 
 343   *        {
 344   *            return 0;
 345   *        }
 346   *            
 347   *        array_push($field_array,$meta->name);
 348   *
 349   *        $i++;
 350   *        }
 351   *    }
 352   *    else
 353   *    {
 354   *        $arr = tableInfo($result);
 355   *        foreach ($arr as $index=>$subarr)
 356   *        {
 357   *        array_push($field_array,$subarr['name']);    
 358   *        }
 359   *    }
 360   *
 361   *    return $field_array;
 362   *    }
 363   */
 364  
 365      function getFieldsArray(&$result)
 366      {
 367      //$this->println("ADODB getFieldsArray");
 368      $field_array = array();
 369      if(! isset($result) || empty($result))
 370      {
 371          return 0;
 372      }
 373  
 374      $i = 0;
 375      $n = $result->FieldCount();
 376      while ($i < $n) 
 377      {
 378          $meta = $result->FetchField($i);
 379          if (!$meta) 
 380          {
 381          return 0;
 382          }
 383          array_push($field_array,$meta->name);
 384          $i++;
 385      }
 386  
 387      //$this->println($field_array);
 388      return $field_array;            
 389      }
 390      
 391  /* ADODB Converted
 392   *    function getRowCount(&$result)
 393   *    {
 394   *    if(isset($result) && !empty($result))
 395   *        if($this->dbType == "mysql"){
 396   *        return mysql_numrows($result);
 397   *        } else {
 398   *         return $result->numRows();
 399   *        }
 400   *    return 0;
 401   *    }
 402   */
 403      
 404      function getRowCount(&$result){
 405      global $log;
 406      //$this->println("ADODB getRowCount");
 407      if(isset($result) && !empty($result))
 408          $rows= $result->RecordCount();            
 409      //$this->println("ADODB getRowCount rows=".$rows);    
 410      //$log->debug('getRowCount rows= '.$rows);
 411      return $rows;            
 412      }
 413  
 414      /* ADODB newly added. replacement for mysql_num_rows */
 415      function num_rows(&$result)
 416      {
 417      return $this->getRowCount($result);
 418      }
 419  
 420      /* ADODB newly added. replacement form mysql_num_fields */
 421      function num_fields(&$result)
 422      {
 423      return $result->FieldCount();
 424      }
 425  
 426      /* ADODB newly added. replacement for mysql_fetch_array() */
 427      function fetch_array(&$result)
 428      {
 429      if($result->EOF)
 430      {
 431          //$this->println("ADODB fetch_array return null");
 432          return NULL;
 433      }        
 434      return $this->change_key_case($result->FetchRow());
 435      }
 436  
 437      /* ADODB newly added. replacement for mysql_result() */
 438      function query_result(&$result, $row, $col=0)
 439      {        
 440      //$this->println("ADODB query_result r=".$row." c=".$col);
 441      $result->Move($row);
 442      $rowdata = $this->change_key_case($result->FetchRow());
 443      //$this->println($rowdata);
 444      //Commented strip_selected_tags and added to_html function for HTML tags vulnerability
 445      //$coldata = strip_selected_tags($rowdata[$col],'script');
 446      $coldata = to_html($rowdata[$col]);
 447      //$this->println("ADODB query_result ". $coldata);
 448      return $coldata;
 449      }
 450  
 451  /* ADODB Converted    
 452   *    function getAffectedRowCount(&$result)
 453   *    {
 454   *    if($this->dbType == "mysql"){
 455   *        return mysql_affected_rows();
 456   *    }
 457   *    else {
 458   *        return $result->affectedRows();
 459   *    }
 460   *    return 0;
 461   *    }
 462   */
 463  
 464      function getAffectedRowCount(&$result)
 465      {
 466      global $log;
 467      //$this->println("ADODB getAffectedRowCount");
 468      $log->debug('getAffectedRowCount');
 469      $rows =$this->database->Affected_Rows(); 
 470      //$this->println("ADODB getAffectedRowCount rows=".rows);
 471      $log->debug('getAffectedRowCount rows = '.$rows);
 472      return $rows;
 473      }
 474  
 475  /* ADODB converted
 476   *    function requireSingleResult($sql, $dieOnError=false,$msg='', $encode=true){
 477   *    $result = $this->query($sql, $dieOnError, $msg);
 478   *
 479   *    if($this->getRowCount($result ) == 1)
 480   *        return to_html($result, $encode);
 481   *    $this->log->error('Rows Returned:'. $this->getRowCount($result) .' More than 1 row returned for '. $sql);
 482   *    return '';
 483   *    }
 484   */
 485  
 486      function requireSingleResult($sql, $dieOnError=false,$msg='', $encode=true)
 487      {
 488      $result = $this->query($sql, $dieOnError, $msg);
 489  
 490      if($this->getRowCount($result ) == 1)                
 491          return $result;
 492      $this->log->error('Rows Returned:'. $this->getRowCount($result) .' More than 1 row returned for '. $sql);
 493      return '';
 494      }
 495      
 496  
 497  /* ADODB converted    
 498   *    function fetchByAssoc(&$result, $rowNum = -1, $encode=true)
 499   *    {
 500   *    if(isset($result) && $rowNum < 0)
 501   *    {
 502   *        if($this->dbType == "mysql"){
 503   *        $row = mysql_fetch_assoc($result);
 504   *        
 505   *        if($encode&& is_array($row))
 506   *            return array_map('to_html', $row);    
 507   *        return $row;
 508   *        }
 509   *        $row = $result->fetchRow(DB_FETCHMODE_ASSOC);    
 510   *    }
 511   *    if($this->dbType == "mysql"){
 512   *        if($this->getRowCount($result) > $rowNum){
 513   *        mysql_data_seek($result, $rowNum);    
 514   *        }
 515   *        $this->lastmysqlrow = $rowNum;
 516   *    
 517   *        $row = mysql_fetch_assoc($result);
 518   *        
 519   *        if($encode&& is_array($row))
 520   *        return array_map('to_html', $row);    
 521   *        return $row;
 522   *    }
 523   *    $row = $result->fetchRow(DB_FETCHMODE_ASSOC, $rowNum);
 524   *    if($encode)
 525   *        return array_map('to_html', $row);    
 526   *    return $row;
 527   *    }
 528   */
 529  
 530      function fetchByAssoc(&$result, $rowNum = -1, $encode=true)
 531      {
 532      //$this->println("ADODB fetchByAssoc ".$rowNum." fetch mode=".$adb->database->$ADODB_FETCH_MODE);
 533      if($result->EOF)
 534      {
 535          $this->println("ADODB fetchByAssoc return null");
 536          return NULL;
 537      }
 538      if(isset($result) && $rowNum < 0)
 539      {            
 540          $row = $this->change_key_case($result->GetRowAssoc(false));            
 541          $result->MoveNext();            
 542          //print_r($row);
 543          //$this->println("ADODB fetchByAssoc r< 0 isarray r=".is_array($row)." r1=".is_array($row[1]));            
 544          //$this->println($row);
 545          if($encode&& is_array($row))
 546          return array_map('to_html', $row);
 547          //$this->println("ADODB fetchByAssoc r< 0 not array r1=".$row[1]);            
 548          return $row;            
 549      }
 550  
 551      //$this->println("ADODB fetchByAssoc after if ".$rowNum);    
 552      
 553      if($this->getRowCount($result) > $rowNum)
 554      {
 555          $result->Move($rowNum);                
 556      }
 557  
 558      $this->lastmysqlrow = $rowNum; //srini - think about this
 559      $row = $this->change_key_case($result->GetRowAssoc(false));        
 560      $result->MoveNext();
 561      //print_r($row);        
 562      $this->println($row);
 563              
 564      if($encode&& is_array($row))
 565          return array_map('to_html', $row);    
 566      return $row;
 567      }
 568      
 569  /* ADODB converted
 570   *    function getNextRow(&$result, $encode=true)
 571   *    {
 572   *    if(isset($result)){
 573   *        $row = $result->fetchRow();
 574   *        if($encode&& is_array($row))
 575   *        return array_map('to_html', $row);    
 576   *        return $row;
 577   *    }
 578   *    return null;
 579   *    }
 580   */
 581      
 582      function getNextRow(&$result, $encode=true){
 583      global $log;
 584  
 585      //$this->println("ADODB getNextRow");
 586      $log->info('getNextRow');
 587      if(isset($result)){
 588          $row = $this->change_key_case($result->FetchRow());
 589          if($row && $encode&& is_array($row))
 590          return array_map('to_html', $row);    
 591          return $row;
 592      }
 593      return null;
 594      }
 595  
 596      function fetch_row(&$result, $encode=true)
 597      {
 598      return $this->getNextRow($result);
 599      }
 600  
 601      function field_name(&$result, $col)
 602      {
 603      return $result->FetchField($col);
 604      }
 605      
 606      function getQueryTime(){
 607      return $this->query_time;    
 608      }
 609  
 610  /*
 611   *    function execute($stmt, $data, $dieOnError=false, $msg=''){
 612   *    $this->log->info('Executing:'.$stmt);
 613   *    $this->checkConnection();
 614   *    $this->query_time = microtime();
 615   *    $prepared    = $this->database->prepare($stmt);
 616   *    $result = execute($stmt, $data);
 617   *    $this->query_time = microtime() - $this->query_time;
 618   *    //$this->log->info('Query Execution Time:'.$this->query_time);
 619   *    $this->checkError('Execute Failed:' . $stmt. '::', $dieOnError);
 620   *    return $result;    
 621   *    }
 622   */
 623          
 624      
 625  /* adodb converted
 626   *    function connect($dieOnError = false){
 627   *    $this->println("connect");
 628   *    global $dbconfigoption;
 629   *    if($this->dbType == "mysql" && $dbconfigoption['persistent'] == true){
 630   *        $this->database =@mysql_pconnect($this->dbHostName,$this->userName,$this->userPassword);
 631   *        @mysql_select_db($this->dbName) or die( "Unable to select database");                
 632   *        if(!$this->database){
 633   *        $this->connection = mysql_connect($this->dbHostName,$this->userName,$this->userPassword) or die("Could not connect to server ".$this->dbHostName." as ".$this->userName.".".mysql_error());
 634   *        if($this->connection == false && $dbconfigoption['persistent'] == true){
 635   *            $_SESSION['administrator_error'] = "<B>Severe Performance Degradation: Persistent Database Connections not working.  Please set \$dbconfigoption['persistent'] to false in your config.php file</B>";              
 636   *        }    
 637   *        }
 638   *    }
 639   *    else $this->database = DB::connect($this->getDataSourceName(), $this->dbOptions);
 640   *    if($this->checkError('Could Not Connect:', $dieOnError))
 641   *        $this->log->info("connected to db");
 642   *        
 643   *    }
 644   */
 645      
 646      function connect($dieOnError = false)
 647      {
 648      //$this->println("ADODB connect");
 649      global $dbconfigoption,$dbconfig;
 650      //$this->println("ADODB type=".$this->dbType." host=".$this->dbHostName." dbname=".$this->dbName." user=".$this->userName." password=".$this->userPassword);
 651  
 652  /*
 653   *    $driver='mysql';
 654   *    $server='srinivasan';
 655   *    $user='root';
 656   *    $password='';
 657   *    $database='vtigercrm3_2';
 658   *
 659   *    $this->database = ADONewConnection($driver);
 660   *
 661   *    #$this->database->debug = true;
 662   *    $this->println("ADODB status=".$this->database->PConnect($server, $user, $password, $database));
 663   */
 664  
 665  /*
 666   *    $this->dbHostName="srinivasan:1521";
 667   *    $this->userName="vt4";
 668   *    $this->userPassword="vt4";
 669   *    $this->dbName="srini";
 670   *    $this->dbType="oci8";
 671   */
 672      
 673      if(!isset($this->dbType))
 674      {
 675          $this->println("ADODB Connect : DBType not specified");
 676          return;
 677      }
 678      
 679      $this->database = ADONewConnection($this->dbType);
 680      //$this->database->debug = true;
 681      
 682      $this->database->PConnect($this->dbHostName, $this->userName, $this->userPassword, $this->dbName);
 683      $this->database->LogSQL($this->enableSQLlog);
 684      //$this->database->SetFetchMode(ADODB_FETCH_ASSOC); 
 685      //$this->println("ADODB type=".$this->dbType." host=".$this->dbHostName." dbname=".$this->dbName." user=".$this->userName." password=".$this->userPassword);        
 686      }
 687  
 688  /*
 689   *    function PearDatabase(){            
 690   *    //$this->println("PearDatabase");
 691   *    global $currentModule;
 692   *    $this->log =& LoggerManager::getLogger('PearDatabase_'. $currentModule);
 693   *    $this->resetSettings();
 694   *   }
 695   *
 696   *   function resetSettings(){
 697   *    global $dbconfig, $dbconfigoption;
 698   *    $this->disconnect();
 699   *    $this->setDatabaseType($dbconfig['db_type']);
 700   *    $this->setUserName($dbconfig['db_username']);
 701   *    $this->setUserPassword($dbconfig['db_password']);
 702   *    $this->setDatabaseHost( $dbconfig['db_hostname']);
 703   *    $this->setDatabaseName($dbconfig['db_name']);
 704   *    $this->dbOptions = $dbconfigoption;
 705   *    $this->enableSQLlog = ($dbconfig['log_sql'] == true);
 706   *    //$this->println("resetSettings log=".$this->enableSQLlog);
 707   *    //$this->println($dbconfig);
 708   *    //if($this->dbType != "mysql"){
 709   *    //    require_once( 'DB.php' );    
 710   *    //}
 711   *   }
 712   */
 713  
 714      function PearDatabase($dbtype='',$host='',$dbname='',$username='',$passwd='')
 715      {
 716      //$this->println("PearDatabase");
 717      global $currentModule;
 718      $this->log =& LoggerManager::getLogger('PearDatabase_'. $currentModule);
 719      $this->resetSettings($dbtype,$host,$dbname,$username,$passwd);
 720      }
 721  
 722      function resetSettings($dbtype,$host,$dbname,$username,$passwd)
 723      {
 724      global $dbconfig, $dbconfigoption;
 725          
 726      if($host == '')
 727      {
 728          $this->disconnect();
 729          $this->setDatabaseType($dbconfig['db_type']);
 730          $this->setUserName($dbconfig['db_username']);
 731          $this->setUserPassword($dbconfig['db_password']);
 732          $this->setDatabaseHost( $dbconfig['db_hostname']);
 733          $this->setDatabaseName($dbconfig['db_name']);
 734          $this->dbOptions = $dbconfigoption;
 735          if($dbconfig['log_sql'])
 736          $this->enableSQLlog = ($dbconfig['log_sql'] == true);
 737          //$this->println("resetSettings log=".$this->enableSQLlog);
 738          //$this->println($dbconfig);
 739          /*if($this->dbType != "mysql"){
 740          require_once( 'DB.php' );    
 741          }*/
 742      }
 743      else
 744      {
 745          $this->disconnect();
 746          $this->setDatabaseType($dbtype);
 747          $this->setDatabaseName($dbname);
 748          $this->setUserName($username);
 749          $this->setUserPassword($passwd);
 750          $this->setDatabaseHost( $host);
 751      }
 752      }
 753  
 754      function quote($string){
 755      return $this->database->qstr($string);    
 756      }
 757  
 758  
 759  /* ADODB converted
 760   *    function disconnect() {
 761   *    $this->println("disconnect");
 762   *    if(isset($this->database)){
 763   *        if($this->dbType == "mysql"){
 764   *        mysql_close($this->database);
 765   *        } else {
 766   *        $this->database->disconnect();
 767   *        }
 768   *        unset($this->database);
 769   *    }
 770   *    }
 771   */
 772  
 773      function disconnect() {
 774      $this->println("ADODB disconnect");
 775      if(isset($this->database)){
 776          if($this->dbType == "mysql"){
 777          mysql_close($this->database);
 778          } else {
 779          $this->database->disconnect();
 780          }
 781          unset($this->database);
 782      }
 783      }
 784  
 785      function setDebug($value)
 786      {
 787      $this->database->debug = $value;
 788      }
 789  
 790  
 791      // ADODB newly added methods
 792      function createTables($schemaFile, $dbHostName=false, $userName=false, $userPassword=false, $dbName=false, $dbType=false)
 793      {
 794      $this->println("ADODB createTables ".$schemaFile);
 795      if($dbHostName!=false) $this->dbHostName=$dbHostName;
 796      if($userName!=false) $this->userName=$userPassword;
 797      if($userPassword!=false) $this->userPassword=$userPassword;
 798      if($dbName!=false) $this->dbName=$dbName;
 799      if($dbType!=false) $this->dbType=$dbType;        
 800  
 801      //$db = ADONewConnection($this->dbType);
 802      $this->checkConnection();
 803      $db = $this->database;
 804      //$db->debug = true;
 805  
 806      //$this->println("ADODB createTables connect status=".$db->Connect($this->dbHostName, $this->userName, $this->userPassword, $this->dbName));
 807      $schema = new adoSchema( $db );
 808      //Debug Adodb XML Schema
 809      $sehema->XMLS_DEBUG = TRUE;
 810      //Debug Adodb
 811      $sehema->debug = true;
 812      $sql = $schema->ParseSchema( $schemaFile );
 813  
 814      $this->println("--------------Starting the table creation------------------");
 815      //$this->println($sql);
 816  
 817      //integer ExecuteSchema ([array $sqlArray = NULL], [boolean $continueOnErr = NULL])
 818      $result = $schema->ExecuteSchema( $sql, true );
 819      if($result)
 820      print $db->errorMsg();
 821      // needs to return in a decent way
 822      $this->println("ADODB createTables ".$schemaFile." status=".$result);
 823      return $result;
 824      }
 825  
 826      function createTable($tablename, $flds)
 827      {
 828      $this->println("ADODB createTable table=".$tablename." flds=".$flds);
 829      $this->checkConnection();
 830      //$dict = NewDataDictionary(ADONewConnection($this->dbType));
 831      $dict = NewDataDictionary($this->database);
 832      $sqlarray = $dict->CreateTableSQL($tablename, $flds);
 833      $result = $dict->ExecuteSQLArray($sqlarray);
 834      $this->println("ADODB createTable table=".$tablename." flds=".$flds." status=".$result);
 835      return $result;
 836      }
 837  
 838      function alterTable($tablename, $flds, $oper)
 839      {
 840      $this->println("ADODB alterTableTable table=".$tablename." flds=".$flds." oper=".$oper);
 841      //$dict = NewDataDictionary(ADONewConnection($this->dbType));
 842      $this->checkConnection();
 843      $dict = NewDataDictionary($this->database);
 844      //$sqlarray = new Array(); 
 845      
 846      if($oper == 'Add_Column')
 847      {
 848          $sqlarray = $dict->AddColumnSQL($tablename, $flds);
 849      }
 850      else if($oper == 'Delete_Column')
 851      {
 852          $sqlarray = $dict->DropColumnSQL($tablename, $flds);
 853      }
 854  
 855      $this->println("sqlarray");
 856      $this->println($sqlarray);
 857  
 858      $result = $dict->ExecuteSQLArray($sqlarray);
 859  
 860      $this->println("ADODB alterTableTable table=".$tablename." flds=".$flds." oper=".$oper." status=".$result);
 861      return $result;
 862  
 863      }
 864  
 865      function getColumnNames($tablename)
 866      {
 867      $this->println("ADODB getColumnNames table=".$tablename);    
 868      $this->checkConnection();
 869      $adoflds = $this->database->MetaColumns($tablename);
 870      //$colNames = new Array();
 871      $i=0;
 872      foreach($adoflds as $fld)
 873      {
 874          $colNames[$i] = $fld->name;
 875          $i++;
 876      }
 877      return $colNames;    
 878      }
 879  
 880      function formatString($tablename,$fldname, $str)
 881      {
 882      //$this->println("ADODB formatString table=".$tablename." fldname=".$fldname." str=".$str);
 883      $this->checkConnection();
 884      $adoflds = $this->database->MetaColumns($tablename);
 885      
 886      foreach ( $adoflds as $fld )
 887      {
 888          //$this->println("ADODB formatString adofld =".$fld->name);
 889          if(strcasecmp($fld->name,$fldname)==0)
 890          {
 891          //$this->println("ADODB formatString fldname=".$fldname." fldtype =".$fld->type);
 892  
 893          $fldtype =strtoupper($fld->type);     
 894          if(strcmp($fldtype,'CHAR')==0 || strcmp($fldtype,'VARCHAR') == 0 || strcmp($fldtype,'VARCHAR2') == 0 || strcmp($fldtype,'LONGTEXT')==0 || strcmp($fldtype,'TEXT')==0)
 895          {
 896              //$this->println("ADODB return else normal");
 897              return $this->database->Quote($str);
 898          }
 899          else if(strcmp($fldtype,'DATE') ==0 || strcmp($fldtype,'TIMESTAMP')==0)
 900          {
 901              return $this->formatDate($str);
 902          }
 903          else
 904          {                
 905              return $str;
 906          }
 907          }
 908      }
 909      $this->println("format String Illegal field name ".$fldname);
 910      return $str;
 911      }
 912  
 913      function formatDate($datetime)
 914      {
 915      $this->checkConnection();
 916      //$db = ADONewConnection($this->dbType);
 917      $db = &$this->database;
 918      $date = $db->DBTimeStamp($datetime);
 919      //if($db->dbType=='mysql') return $this->quote($date);
 920      return $date;
 921      }
 922  
 923      function getDBDateString($datecolname)
 924      {
 925      $this->checkConnection();
 926      $db = &$this->database;
 927      $datestr = $db->SQLDate("Y-m-d, H:i:s" ,$datecolname);
 928      return $datestr;    
 929      }
 930  
 931      function getUniqueID($seqname)
 932      {
 933      $this->checkConnection();
 934      return $this->database->GenID($seqname."_seq",1);
 935      }
 936      function get_tables()
 937      {
 938      $this->checkConnection();
 939      $result = & $this->database->MetaTables('TABLES');
 940      $this->println($result);
 941      return $result;        
 942      }
 943  } /* End of class */
 944  
 945  $adb = new PearDatabase();
 946  $adb->connect();
 947  //$adb->database->setFetchMode(ADODB_FETCH_NUM);
 948  
 949  
 950  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7