{$message}"); fclose($file); return true; } else return false; } date_default_timezone_set('Asia/Shanghai'); /** * @var IPDOHelper $db */ $db = new PDODelegator(DATABASE_TYPE); if (!$db->Connect(MASTER_HOSTNAME, MASTER_HOSTPORT, MASTER_DATABASE, MASTER_USERNAME, MASTER_PASSWORD, MASTER_CHARSET)) { $log = JsonObjectToJsonString($db->GetErrors()); OutputDebugMessage($log); die($log); } $db->BeginTransaction(); /// 启动事务 $sync_date = date('Y-m-d', strtotime(isset($_REQUEST['sync_date']) ? $_REQUEST['sync_date'] : '-1 days')); // 前一天 try { // 1、从数据库获取自动执行脚本 if ($commands = $db->request('select `sql`, `params`, `table` from ct_report_ext_sql where status = ?;', 1)) { $commands = json_decode(json_encode($commands), true); } else { throw new Exception('从数据库获取自动执行脚本失败', -1); } // 2、循环执行脚本 foreach ($commands as $key => $value) { // 删除前一天原有的数据 if (!$db->execute(/** @lang text */ 'delete from `' . $value['table'] . '` where date_format(`time`, \'%Y-%m-%d\') = ?;', $sync_date) ) throw new Exception($db->geterrorinfo() . '(' . __LINE__ . ')', intval($db->geterrorcode())); // 插入前一天的数据 if (!$db->execute($value['sql'], $sync_date)) throw new Exception($db->geterrorinfo() . '(' . __LINE__ . ')', intval($db->geterrorcode())); } $db->Commit(); /// 提交事务 $log = 'success!'; } catch (Exception $Exception) /// 异常处理 { $db->Rollback(); /// 事务回滚 $log = JsonObjectToJsonString(array('code' => $Exception->getCode(), 'info' => $Exception->getMessage(),)); OutputDebugMessage($log); } die($log);