/** * 把一个表的数据转换到另外一个表中 * * @param string $srcTableName 源表名 * @param array $condArray * array( * array('supplier_id = ?', $supplier_id) * array('is_on_sale = ?', 1) * array('supplier_price > ? or supplier_price < ?', $priceMin, $priceMax) * ) * @param array $optionArray 排序分页等条件 ,格式例如 array('order' => 'goods_id asc', 'offset' => 100, 'limit' => '10') * @param string $dstTableName 目的表名 * @param array $columnMap 列对应,格式如 array('id' => 'goods_id' , 'name' => 'goods_name') * @param array $srcValueConvertFuncArray * 对源数据做装换,例如: array('id' => function($srcValue, $srcRecord){ return $srcValue + 1;} , ...),用闭包函数做转化 * @param callable $recordPreFunc 在查询到 src 的记录之后调用 ,函数原型 function(&$srcRecord){ ... } * @param callable $recordPostFunc 在 src 已经往 dst 赋值完成之后,dst 还没有写入数据库之前调用,函数原型 function(&$srcRecord, &dstRecord){...} * */ public function convertTable($srcTableName, $condArray, $optionArray, $dstTableName, $columnMap, $srcValueConvertFuncArray, $recordPreFunc = null, $recordPostFunc = null) { $srcFieldArray = array(); $dstFieldArray = array(); foreach ($columnMap as $srcField => $dstField) { $srcFieldArray[] = $srcField; $dstFieldArray[] = $dstField; } $srcTable = new SrcDataMapper($srcTableName); // 构造查询条件 $filter = null; if (!empty($condArray)) { $filter = QueryBuilder::buildAndFilter($condArray); } // 获得总数 $totalRecordCount = $srcTable->count($filter); printLog('begin convertTable ' . $srcTableName . '-->' . $dstTableName . ' totalRecordCount:' . $totalRecordCount, self::$loggerSource); $recordLeft = $totalRecordCount; // 剩余多少记录需要处理 $queryOffset = 0; // 查询的起始位置 while ($recordLeft > 0) { // 这次需要处理多少记录 $processCount = $recordLeft > $this->batchProcessCount ? $this->batchProcessCount : $recordLeft; // 记录处理进度 printLog('totalRecordCount:' . $totalRecordCount . ' recordLeft:' . $recordLeft . ' processCount:' . $processCount, self::$loggerSource); $recordLeft -= $processCount; // 从源表查询数据 $srcField = '*'; if (!empty($srcFieldArray)) { $srcField = implode(',', $srcFieldArray); } // 处理查询的起始位置 $optionArray = array_merge($optionArray, array('offset' => $queryOffset, 'limit' => $processCount)); $queryOffset += $processCount; // 查询数据 $srcRecordList = $srcTable->select($srcField, $filter, $optionArray); // 转换数据到目标表中 foreach ($srcRecordList as $srcRecordItem) { // PreFunc 处理 if ($recordPreFunc) { $recordPreFunc($srcRecordItem); } $dstTable = new DstDataMapper($dstTableName); //字段复制 foreach ($columnMap as $srcField => $dstField) { // 无意义的字段,不复制 if (null == $dstField) { continue; } if (isset($srcValueConvertFuncArray) && isset($srcValueConvertFuncArray[$srcField])) { if (!is_callable($srcValueConvertFuncArray[$srcField])) { printLog($srcField . ' value convert is not function ', self::$loggerSource, \Core\Log\Base::ERROR); continue; } // 做数据转化 $dstTable->{$dstField} = $srcValueConvertFuncArray[$srcField]($srcRecordItem[$srcField], $srcRecordItem); } else { $dstTable->{$dstField} = $srcRecordItem[$srcField]; } } // postFunc 处理 if ($recordPostFunc) { $recordPostFunc($srcRecordItem, $dstTable); } $dstTable->save(); unset($dstTable); // 及时释放数据,优化内存使用 } unset($srcRecordList); // 及时释放数据,优化内存使用 gc_collect_cycles(); // 主动做一次垃圾回收 } printLog('finish convertTable ' . $srcTableName . '-->' . $dstTableName . ' totalRecordCount:' . $totalRecordCount, self::$loggerSource); }
/** * 清除一些无关的表 */ private function clearTable() { $tableMigrate = new TableMigrate(); $table = new DstDataMapper('account_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('admin_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('affiliate_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('auction_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('auto_manage'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('back_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('back_order'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('booking_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('brand'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('comment'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('delivery_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('delivery_order'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('email_list'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('email_sendlist'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('error_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('feedback'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('goods_activity'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('goods_article'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('group_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('keywords'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('link_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('order_action'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('order_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('order_info'); $tableMigrate->clearTable($table); $srcOrder = new SrcDataMapper('cartinfo'); list($result) = $srcOrder->select('max(id) as mvalue', null, null, 0); $autoIncValue = $result->getAdhocValue('mvalue') + 1; $tableMigrate->setAutoIncValue($table, $autoIncValue); unset($srcOrder); unset($table); $table = new DstDataMapper('order_goods'); $tableMigrate->clearTable($table); $srcOrder = new SrcDataMapper('order'); list($result) = $srcOrder->select('max(id) as mvalue', null, null, 0); $autoIncValue = $result->getAdhocValue('mvalue') + 1; $tableMigrate->setAutoIncValue($table, $autoIncValue); unset($srcOrder); unset($table); $table = new DstDataMapper('order_refer'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('pack'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('package_goods'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); $table = new DstDataMapper('pay_log'); $tableMigrate->clearTable($table); $tableMigrate->setAutoIncValue($table, 1); unset($table); unset($tableMigrate); }