Example #1
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);
 }