eZ components PHP Cross Reference Developer Tools

Source: /Database/src/sqlabstraction/implementations/utilities_oracle.php - 139 lines - 4307 bytes - Summary - Text - Print

Description: File containing the ezcDbUtilities class.

   1  <?php
   2  /**
   3   * File containing the ezcDbUtilities class.
   4   *
   5   * @package Database
   6   * @version 1.4.7
   7   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   * @access private
  10   */
  11  
  12  /**
  13   * Various database methods.
  14   *
  15   * This implementation is oracle specific.
  16   *
  17   * This class inherits most of its database handling functionality from
  18   * PDO ({@link http://php.net/PDO}) -- an object-oriented database abstraction
  19   * layer that is going to become a new standard for PHP applications.
  20   *
  21   * @package Database
  22   * @todo this class must be renamed
  23   * @access private
  24   * @version 1.4.7
  25   */
  26  class ezcDbUtilitiesOracle extends ezcDbUtilities
  27  {
  28      /**
  29       * Constructs a new db util using the db handler $db.
  30       *
  31       * @param ezcDbHandler $db
  32       */
  33      public function __construct( $db )
  34      {
  35          parent::__construct( $db );
  36      }
  37  
  38      /**
  39       * Remove all tables from the database.
  40       */
  41      public function cleanup()
  42      {
  43          $this->db->beginTransaction();
  44  
  45          // drop tables
  46          $rslt = $this->db->query( "SELECT lower(table_name) FROM user_tables" );
  47          $rslt->setFetchMode( PDO::FETCH_NUM );
  48          $rows = $rslt->fetchAll();
  49          unset( $rslt );
  50          foreach ( $rows as $row )
  51          {
  52              $table = $row[0];
  53              $this->db->exec( "DROP TABLE $table" );
  54          }
  55  
  56          // drop sequences
  57          $rslt = $this->db->query( "SELECT LOWER(sequence_name) FROM user_sequences" );
  58          $rslt->setFetchMode( PDO::FETCH_NUM );
  59          $rows = $rslt->fetchAll();
  60          foreach ( $rows as $row )
  61          {
  62              $seq = $row[0];
  63              $this->db->exec( "DROP SEQUENCE $seq" );
  64          }
  65  
  66          // FIXME: drop triggers?
  67  
  68          $this->db->commit();
  69      }
  70  
  71  
  72      /**
  73       * Creates a new temporary table and returns the name.
  74       *
  75       * @throws ezcDbException::GENERIC_ERROR in case of inability to generate
  76       *         a unique temporary table name.
  77       * @see ezcDbHandler::createTemporaryTable()
  78       *
  79       *
  80       * @param   string $tableNamePattern  Name of temporary table user wants
  81       *                                    to create.
  82       * @param   string $tableDefinition Definition for the table, i.e.
  83       *                                  everything that goes between braces after
  84       *                                  CREATE TEMPORARY TABLE clause.
  85       * @return string                  Table name, that might have been changed
  86       *                                  by the handler to guarantee its uniqueness.
  87       * @todo move out
  88       */
  89      public function createTemporaryTable( $tableNamePattern, $tableDefinition )
  90      {
  91          if ( strpos( $tableNamePattern, '%' ) === false )
  92          {
  93              $tableName = $tableNamePattern;
  94          }
  95          else // generate unique table name with the given pattern
  96          {
  97              $maxTries = 10;
  98              do
  99              {
 100                  $num = rand( 10000000, 99999999 );
 101                  $tableName = strtoupper( str_replace( '%', $num, $tableNamePattern ) );
 102                  $query = "SELECT count(*) AS cnt FROM user_tables WHERE table_name='$tableName'";
 103                  $cnt = (int) $this->db->query( $query )->fetchColumn( 0 );
 104                  $maxTries--;
 105              } while ( $cnt > 0 && $maxTries > 0 );
 106  
 107              if ( $maxTries == 0 )
 108              {
 109                  throw ezcDbException(
 110                      ezcDbException::GENERIC_ERROR,
 111                      "Tried to generate an uninque temp table name for {$maxTries} time with no luck."
 112                  );
 113              }
 114          }
 115  
 116          $this->db->exec( "CREATE GLOBAL TEMPORARY TABLE $tableName ($tableDefinition)" );
 117          return $tableName;
 118      }
 119  
 120      /**
 121       * Drop specified temporary table
 122       * in a portable way.
 123       *
 124       * Developers should use this method instead of dropping temporary
 125       * tables with the appropriate SQL queries
 126       * to maintain inter-DBMS portability.
 127       *
 128       * @see createTemporaryTable()
 129       *
 130       * @param   string  $tableName Name of temporary table to drop.
 131       * @return void
 132       */
 133      public function dropTemporaryTable( $tableName )
 134      {
 135          $this->db->exec( "TRUNCATE TABLE $tableName" );
 136          $this->db->exec( "DROP TABLE $tableName" );
 137      }
 138  }
 139  ?>

title

Description

title

Description

title

Description

title

title

Body