/**
  * step 6 : 数据库更新操作
  *
  * 先执行update.sql,再跳转到update.php
  */
 public function dbAction()
 {
     $step = (int) Wekit::cache()->get('system_upgrade_db_step');
     $step || $this->installService->after($this->localFileList, Wekit::cache()->get('system_upgrade_ftp'), $this->fileList);
     $sqlFile = Wind::getRealPath('PUBLIC:update.sql', true);
     $success = 1;
     if (!file_exists($sqlFile)) {
         Wekit::cache()->set('system_upgrade_step', 6);
         PwSystemHelper::log('no db update', $this->version);
         $this->forwardRedirect(WindUrlHelper::createUrl('appcenter/upgrade/php'));
     }
     $lang = Wind::getComponent('i18n');
     try {
         /* @var $db WindConnection */
         $db = Wind::getComponent('db');
         if (!$step) {
             $sqlArray = PwSystemHelper::sqlParser(WindFile::read($sqlFile), $db->getConfig('charset', '', 'utf8'), $db->getTablePrefix(), $db->getConfig('engine', '', 'MYISAM'));
             WindFile::savePhpData(DATA_PATH . 'upgrade/sql.tmp', $sqlArray);
         } else {
             $sqlArray = (include DATA_PATH . 'upgrade/sql.tmp');
         }
         end($sqlArray);
         if ($step > key($sqlArray)) {
             Wekit::cache()->set('system_upgrade_step', 6);
             PwSystemHelper::log('db update success', $this->version);
             $this->forwardRedirect(WindUrlHelper::createUrl('appcenter/upgrade/php'));
         }
         $sql = $sqlArray[$step];
         if ($sql) {
             foreach ($sql as $v) {
                 if (empty($v)) {
                     continue;
                 }
                 if (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(DROP|ADD)\\s+(KEY|INDEX|UNIQUE)\\s+([\\w\\(\\),`]+)?/i', $v, $matches)) {
                     list($key, $fields) = explode('(', $matches[4]);
                     $fields = trim($fields, '),');
                     list($matches[3]) = explode(' ', $matches[3]);
                     $matches[3] = trim(strtoupper($matches[3]));
                     PwSystemHelper::log($matches[1] . ' ' . str_replace('`', '', $key) . ' ' . ($fields ? str_replace('`', '', $fields) : '') . ' ' . $matches[3], $this->version);
                     PwSystemHelper::alterIndex(array($matches[1], str_replace('`', '', $key), $fields ? str_replace('`', '', $fields) : '', $matches[3], $matches[2]), $db);
                 } elseif (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(CHANGE|DROP|ADD)\\s+`?(\\w+)`?/i', $v, $matches)) {
                     PwSystemHelper::log($matches[1] . ' ' . $matches[3], $this->version);
                     PwSystemHelper::alterField(array($matches[1], $matches[3], $v), $db);
                 } else {
                     PwSystemHelper::log('execute sql ' . $v, $this->version);
                     $db->execute($v);
                 }
             }
         }
     } catch (Exception $e) {
         if ($e instanceof WindForwardException) {
             throw $e;
         }
         $success = 0;
         $this->setOutput(1, 'error');
         PwSystemHelper::log('execute sql failed' . $e->getMessage(), $this->version);
         $this->setOutput($lang->getMessage('APPCENTER:upgrade.db.error', array(implode(';', $sql))), 'msg');
     }
     if ($success) {
         $this->setOutput($lang->getMessage('APPCENTER:upgrade.db.update', array($step, key($sqlArray))), 'msg');
     }
     Wekit::cache()->set('system_upgrade_db_step', ++$step);
 }
Exemple #2
0
 /**
  * 注册数据文件
  *
  * @param PwInstallApplication $install        	
  * @return PwError true
  */
 public function registeData($install)
 {
     try {
         $sqlFile = $install->getTmpPackage() . '/' . self::DB_TABLE;
         if (!is_file($sqlFile)) {
             return true;
         }
         $strSql = WindFile::read($sqlFile);
         /* @var $db WindConnection */
         $db = Wind::getComponent('db');
         $sql = PwApplicationHelper::sqlParser($strSql, $db->getConfig('charset', '', 'utf8'), $db->getTablePrefix(), $db->getConfig('engine', '', 'MYISAM'));
         if ($sql['CREATE']) {
             foreach ($sql['CREATE'] as $table => $statement) {
                 $db->execute($statement);
             }
         }
         $install->setInstallLog('table', $sql['CREATE']);
         foreach ($sql as $option => $statements) {
             if (!in_array($option, array('INSERT', 'UPDATE', 'REPLACE', 'ALTER'))) {
                 continue;
             }
             foreach ($statements as $table => $statement) {
                 if ($option == 'ALTER') {
                     if (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(DROP|ADD)\\s+(KEY|INDEX|UNIQUE)\\s+([\\w\\(\\),`]+)?/i', $statement, $matches)) {
                         list($key, $fields) = explode('(', $matches[4]);
                         $fields = trim($fields, '),');
                         list($matches[3]) = explode(' ', $matches[3]);
                         $matches[3] = trim(strtoupper($matches[3]));
                         PwSystemHelper::alterIndex(array($matches[1], $key, $fields ? $fields : '', $matches[3], $matches[2]), $db);
                     } elseif (preg_match('/^ALTER\\s+TABLE\\s+`?(\\w+)`?\\s+(CHANGE|DROP|ADD)\\s+`?(\\w+)`?/i', $statement, $matches)) {
                         PwSystemHelper::alterField(array($matches[1], $matches[3], $statement), $db);
                     } else {
                         $db->execute($statement);
                     }
                 } else {
                     if ($option == 'INSERT') {
                         $statement = 'REPLACE' . substr($statement, 6);
                     }
                     $db->execute($statement);
                 }
             }
         }
         return true;
     } catch (Exception $e) {
         return new PwError('APPCENTER:install.fail', array('{{error}}' => $e->getMessage()));
     }
     file_put_contents(DATA_PATH . 'tmp/log', 'registedata!', FILE_APPEND);
 }