src/wcmf/lib/util/DBUtil.php
public static function executeScript($file, $initSection) {
    $logger = LogManager::getLogger(__CLASS__);
    if (file_exists($file)) {
      $logger->info('Executing SQL script '.$file.' ...');

      // find init params
      $config = ObjectFactory::getInstance('configuration');
      if (($connectionParams = $config->getSection($initSection)) === false) {
        throw new ConfigurationException("No '".$initSection."' section given in configfile.");
      }
      // connect to the database
      $conn = self::createConnection($connectionParams);

      $logger->debug('Starting transaction ...');
      $conn->beginTransaction();

      $exception = null;
      $fh = fopen($file, 'r');
      if ($fh) {
        while (!feof($fh)) {
          $command = fgets($fh, 8192);
          if (strlen(trim($command)) > 0) {
            $logger->debug('Executing command: '.preg_replace('/[\n]+$/', '', $command));
            try {
              $conn->query($command);
            }
            catch(\Exception $ex) {
              $exception = $ex;
              break;
            }
          }
        }
        fclose($fh);
      }
      if ($exception == null) {
        $logger->debug('Execution succeeded, committing ...');
        $conn->commit();
      }
      else {
        $logger->error('Execution failed. Reason'.$exception->getMessage());
        $logger->debug('Rolling back ...');
        $conn->rollBack();
      }
      $logger->debug('Finished SQL script '.$file.'.');
    }
    else {
      $logger->error('SQL script '.$file.' not found.');
    }
  }
public static function executeScript($file, $initSection) {
    $logger = LogManager::getLogger(__CLASS__);
    if (file_exists($file)) {
      $logger->info('Executing SQL script '.$file.' ...');

      // find init params
      $config = ObjectFactory::getInstance('configuration');
      if (($connectionParams = $config->getSection($initSection)) === false) {
        throw new ConfigurationException("No '".$initSection."' section given in configfile.");
      }
      // connect to the database
      $conn = self::createConnection($connectionParams);

      $logger->debug('Starting transaction ...');
      $conn->beginTransaction();

      $exception = null;
      $fh = fopen($file, 'r');
      if ($fh) {
        while (!feof($fh)) {
          $command = fgets($fh, 8192);
          if (strlen(trim($command)) > 0) {
            $logger->debug('Executing command: '.preg_replace('/[\n]+$/', '', $command));
            try {
              $conn->query($command);
            }
            catch(\Exception $ex) {
              $exception = $ex;
              break;
            }
          }
        }
        fclose($fh);
      }
      if ($exception == null) {
        $logger->debug('Execution succeeded, committing ...');
        $conn->commit();
      }
      else {
        $logger->error('Execution failed. Reason'.$exception->getMessage());
        $logger->debug('Rolling back ...');
        $conn->rollBack();
      }
      $logger->debug('Finished SQL script '.$file.'.');
    }
    else {
      $logger->error('SQL script '.$file.' not found.');
    }
  }
public static function executeScript ( $ file , $ initSection ) { $ logger = LogManager :: getLogger ( __CLASS__ ) ; if ( file_exists ( $ file ) ) { $ logger -> info ( 'Executing SQL script ' . $ file . ' ...' ) ; // find init params $ config = ObjectFactory :: getInstance ( 'configuration' ) ; if ( ( $ connectionParams = $ config -> getSection ( $ initSection ) ) === false ) { throw new ConfigurationException ( "No '" . $ initSection . "' section given in configfile." ) ; } // connect to the database $ conn = self :: createConnection ( $ connectionParams ) ; $ logger -> debug ( 'Starting transaction ...' ) ; $ conn -> beginTransaction ( ) ; $ exception = null ; $ fh = fopen ( $ file , 'r' ) ; if ( $ fh ) { while ( ! feof ( $ fh ) ) { $ command = fgets ( $ fh , 8192 ) ; if ( strlen ( trim ( $ command ) ) > 0 ) { $ logger -> debug ( 'Executing command: ' . preg_replace ( '/[\n]+$/' , '' , $ command ) ) ; try { $ conn -> query ( $ command ) ; } catch ( \ Exception $ ex ) { $ exception = $ ex ; break ; } } } fclose ( $ fh ) ; } if ( $ exception == null ) { $ logger -> debug ( 'Execution succeeded, committing ...' ) ; $ conn -> commit ( ) ; } else { $ logger -> error ( 'Execution failed. Reason' . $ exception -> getMessage ( ) ) ; $ logger -> debug ( 'Rolling back ...' ) ; $ conn -> rollBack ( ) ; } $ logger -> debug ( 'Finished SQL script ' . $ file . '.' ) ; } else { $ logger -> error ( 'SQL script ' . $ file . ' not found.' ) ; } }
Execute a sql script. Execution is done inside a transaction, which is rolled back in case of failure.
@param $file The filename of the sql script
@param $initSection The name of the configuration section that defines the database connection
@return Boolean whether execution succeeded or not.
Execute a sql script . Execution is done inside a transaction which is rolled back in case of failure .