コード例 #1
0
ファイル: Transfer.php プロジェクト: wh1tew0lf/SQLBuilder
 /**
  * Copy tables from fromDB to toDB
  * @param string|array $tables
  * @param array $params
  * @throws Exception
  */
 public function copy($tables, $params = [])
 {
     $tables = !is_array($tables) ? [$tables] : $tables;
     foreach ($tables as $tableName => $tableParams) {
         if (is_int($tableName) && is_string($tableParams)) {
             $fromTable = $tableParams;
             $toTable = $tableParams;
         } elseif (is_string($tableName) && is_string($tableParams)) {
             $fromTable = $tableName;
             $toTable = $tableParams;
         } elseif (is_int($tableName) && is_array($tableParams) && isset($tableParams['sql'])) {
             $select = $tableParams['sql'];
             $fromTable = false;
             $toTable = $tableParams['table'];
         } elseif (is_string($tableName) && is_array($tableParams) && !isset($tableParams['sql'])) {
             $fromTable = $tableName;
             $toTable = $tableParams['table'];
         } else {
             throw new Exception('Incorrect tables');
         }
         if (is_array($tableParams)) {
             $params = array_merge($tableParams, $params);
         }
         if (false !== $fromTable && !$this->fromDB->isTableExists($fromTable)) {
             throw new Exception('Table does not exists at first table');
         }
         $select = isset($select) ? $select : $this->fromDB->getSQLBuilder()->from($fromTable);
         $useType = !(is_array($tableParams) && isset($tableParams['sql']));
         if (is_array($tableParams) && isset($tableParams['sql']) && isset($params['columns'])) {
             $sourceColumns = $params['columns'];
         } elseif (is_array($tableParams) && isset($tableParams['sql']) && !isset($params['columns'])) {
             $sourceColumns = $this->fromDB->extractColumns($select);
             $select->select($this->fromDB->getSelectAll($select));
         } else {
             $sourceColumns = $this->fromDB->getColumns($fromTable);
         }
         $columns = $this->processColumns($sourceColumns, $params);
         $columnsMap = $this->createColumnsMap($sourceColumns, $params);
         if ($this->toDB->isTableExists($toTable)) {
             if ('rewrite' == $this->existsAction) {
                 $this->toDB->dropTable($toTable);
                 $this->toDB->createTable($toTable, $columns);
             } elseif ('truncate' == $this->existsAction && $this->toDB->isTableEqual($toTable, $columns)) {
                 $this->toDB->truncateTable($toTable);
             } elseif ('truncate' == $this->existsAction && !$this->toDB->isTableEqual($toTable, $columns)) {
                 $this->toDB->dropTable($toTable);
                 $this->toDB->createTable($toTable, $columns);
             } elseif ('skip' == $this->existsAction) {
                 echo "TABLE '{$fromTable}' SKIPPED BECAUSE '{$toTable}' EXISTS!\n";
                 continue;
             } elseif ('stop' == $this->existsAction) {
                 throw new \Exception("TABLE '{$fromTable}' DO STOP BECAUSE '{$toTable}' EXISTS!");
             } elseif ('append' == $this->existsAction && $this->toDB->isTableEqual($toTable, $columns)) {
                 //Do nothing
             }
         } else {
             $this->toDB->createTable($toTable, $columns);
         }
         $cursor = $this->fromDB->execute($select->getSQL());
         while ($row = $cursor->fetch(BasePDO::FETCH_ASSOC)) {
             $row = $this->processFields($row, $columnsMap, array_merge($params, ['sourceColumns' => $useType ? $sourceColumns : false]));
             $sqlBuilder = $this->toDB->getSQLBuilder();
             if (method_exists($sqlBuilder, 'insertOnDuplicateUpdate')) {
                 $insertUpdate = $sqlBuilder->insertOnDuplicateUpdate($toTable, array_combine(array_keys($row), array_map(function ($item) {
                     return ":{$item}";
                 }, array_keys($row))));
             } else {
                 $insertUpdate = $sqlBuilder->insert($toTable, array_combine(array_keys($row), array_map(function ($item) {
                     return ":{$item}";
                 }, array_keys($row))));
             }
             //try {
             if (false === $this->toDB->execute($insertUpdate, $row)) {
                 throw new Exception('Row can not be inserted');
             }
             /*
                             } catch (\Exception $e) {
                                 echo $insertUpdate;
                                 var_dump($row);
                                 throw $e;
                             }//*/
         }
         unset($select);
         //for next table
     }
 }