コード例 #1
0
ファイル: Db.php プロジェクト: xinhaiz/PHPORBuilder
 public function __construct(\Lib\DbConfig $dbConfig)
 {
     $maxTry = 3;
     $index = 1;
     \Lib\State::notice('Connecting to MySQL...');
     while ($index <= $maxTry) {
         \Lib\State::notice('Try ' . $index . '...');
         $index++;
         try {
             $pdo = new \PDO($dbConfig->getDsn(), $dbConfig->getUsername(), $dbConfig->getPasswd());
         } catch (\Exception $e) {
             \Lib\State::warning($e->getMessage());
             if ($index <= $maxTry) {
                 \Lib\State::notice('Try to connect again after 3 seconds');
                 sleep(3);
             }
             continue;
         }
         break;
     }
     if (!isset($pdo) || !$pdo instanceof \PDO) {
         \Lib\State::notice($dbConfig->toString());
         \Lib\State::error('MySQL connection failed.');
     }
     $options = $dbConfig->getOptions();
     if (!empty($options)) {
         foreach ($options as $option) {
             $pdo->query($option);
         }
     }
     \Lib\State::notice('connect successed');
     $this->_db = $pdo;
     $this->_dbname = $dbConfig->getDbname();
 }
コード例 #2
0
ファイル: Build.php プロジェクト: xinhaiz/PHPORBuilder
 /**
  * 流程处理
  *
  * @return boolean
  * @throws \Lib\Exception
  */
 public function process()
 {
     if ($this->_state === false) {
         if (\Lib\Params::getInstance()->showHelp() === true) {
             $this->getHelp();
         }
         return false;
     }
     $db = $this->getDbResponse();
     $op = \Lib\Options::getInstance();
     if (empty($this->_dbname)) {
         \Lib\State::error('The database is not specified');
     }
     \Lib\State::notice('Scanning the database table...');
     $tables = $op->getTable();
     if (empty($tables)) {
         $tables = $db->findTables();
     } else {
         foreach ($tables as $table) {
             if ($db->isExistTable($table) === false) {
                 \Lib\State::warning('Unkown table \'' . $table . '\'');
             }
         }
     }
     if (empty($tables)) {
         \Lib\State::warning('Not found any tables');
     }
     \Lib\State::notice('Found ' . sizeof($tables) . ' table(s)');
     $modelFile = \Model\File::getInstance();
     $modelContents = \Model\Content::getInstance();
     $replaceArr = $op->getReplace() ?: [];
     foreach ($tables as $table) {
         $tableName = \Lib\Func::uc($table);
         $className = $tableName;
         if (!empty($replaceArr['source']) && !empty($replaceArr['target'])) {
             $className = str_ireplace($replaceArr['source'], ucfirst($replaceArr['target']), $className);
         }
         if (preg_match('/^[0-9]+/', $tableName)) {
             $tableName = ltrim(preg_replace('/^[0-9]+/', '', $tableName), '_');
         }
         \Lib\State::notice('-----------------');
         \Lib\State::notice('Processing [' . $table . ']');
         $modelContents->setTableInfo($db->findTableInfo($table));
         $modelContents->setClassName($className);
         $modelContents->setTableName($tableName);
         $modelContents->setColumns($db->findCols($table));
         $modelContents->build();
         \Lib\State::notice('Done');
         $modelFile->setFileName($className)->build();
         $modelContents->reset();
         $modelFile->reset();
     }
     return true;
 }