解析标准树状文件为sql语句及代码解析(c++版本,txt,mysql)

如需Python版本,请跳转:解析标准树状文件为sql语句及代码解析(python版本,txt,mysql)

使用场景

解析标准化的树状文件为数据库tree表insert语句,涉及:

  • 自动编号指标id(NodeCode);
  • 获取节点间父子关系(ParentNode);
  • 所属层级(LevelNumber);
  • 是否为叶节点(NoteCategory:1中间节点,2叶子结点);
  • 设置同级节点间顺序(BrotherNo)
目标效果

输入:树状文件片段(txt为例)

----Oil Tankers
------Trade & Demand
--------Monthly Global Seaborne Trade Indicators (Monthly)
----------Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr 
----------Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr 3mma
----------Monthly Global Seaborne Crude Oil Trade Indicator Volume Index
----------Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr 
----------Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr 3mma 
----------Monthly Global Seaborne Oil Products Trade Indicator Volume Index 
----------Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr 
----------Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr 3mma 
----------Monthly Global Seaborne Total Oil Trade Indicator Volume Index 
------Port Call Activity
--------Country
----------Russia Port Calls - Product Tankers 25,000+ dwt (Beta) 
----------Russia Port Calls - Product Tankers 25,000+ dwt (Beta) DWT million
----------Russia Port Calls - Crude Tankers (Beta) 
----------Russia Port Calls - Crude Tankers (Beta) DWT million 

输出:自动生成的入库语句(mysql语句为例)

UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701001, 701001, 'Oil Tankers', 'Oil Tankers', 'Oil Tankers', 701, 1, 3, 2, 1, now(), 1655703762299, 3502, NULL);
UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701001 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701002, 701002, 'Trade & Demand', 'Trade & Demand', 'Trade & Demand', 701001, 1, 4, 2, 1, now(), 1655703762305, 3502, NULL);
UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701002 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701003, 701003, 'Monthly Global Seaborne Trade Indicators (Monthly)', 'Monthly Global Seaborne Trade Indicators (Monthly)', 'Monthly Global Seaborne Trade Indicators (Monthly', 701002, 1, 5, 2, 1, now(), 1655703762312, 3502, NULL);
UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701003 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701004, 701004, 'Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Crude Oil Trade Indicator', 701003, 1, 6, 2, 1, now(), 1655703762321, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701005, 701005, 'Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr 3mma', 'Monthly Global Seaborne Crude Oil Trade Indicator % Yr/Yr 3mma', 'Monthly Global Seaborne Crude Oil Trade Indicator', 701003, 2, 6, 2, 1, now(), 1655703762329, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701006, 701006, 'Monthly Global Seaborne Crude Oil Trade Indicator Volume Index', 'Monthly Global Seaborne Crude Oil Trade Indicator Volume Index', 'Monthly Global Seaborne Crude Oil Trade Indicator', 701003, 3, 6, 2, 1, now(), 1655703762336, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701007, 701007, 'Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Oil Products Trade Indica', 701003, 4, 6, 2, 1, now(), 1655703762346, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701008, 701008, 'Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr 3mma ', 'Monthly Global Seaborne Oil Products Trade Indicator % Yr/Yr 3mma ', 'Monthly Global Seaborne Oil Products Trade Indica', 701003, 5, 6, 2, 1, now(), 1655703762356, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701009, 701009, 'Monthly Global Seaborne Oil Products Trade Indicator Volume Index ', 'Monthly Global Seaborne Oil Products Trade Indicator Volume Index ', 'Monthly Global Seaborne Oil Products Trade Indica', 701003, 6, 6, 2, 1, now(), 1655703762369, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701010, 701010, 'Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr ', 'Monthly Global Seaborne Total Oil Trade Indicator', 701003, 7, 6, 2, 1, now(), 1655703762378, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701011, 701011, 'Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr 3mma ', 'Monthly Global Seaborne Total Oil Trade Indicator % Yr/Yr 3mma ', 'Monthly Global Seaborne Total Oil Trade Indicator', 701003, 8, 6, 2, 1, now(), 1655703762385, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701012, 701012, 'Monthly Global Seaborne Total Oil Trade Indicator Volume Index ', 'Monthly Global Seaborne Total Oil Trade Indicator Volume Index ', 'Monthly Global Seaborne Total Oil Trade Indicator', 701003, 9, 6, 2, 1, now(), 1655703762395, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701013, 701013, 'Port Call Activity', 'Port Call Activity', 'Port Call Activity', 701001, 2, 4, 2, 1, now(), 1655703762407, 3502, NULL);
UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701013 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701014, 701014, 'Country', 'Country', 'Country', 701013, 1, 5, 2, 1, now(), 1655703762417, 3502, NULL);
UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = 701014 ;
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701015, 701015, 'Russia Port Calls - Product Tankers 25,000+ dwt (Beta) ', 'Russia Port Calls - Product Tankers 25,000+ dwt (Beta) ', 'Russia Port Calls - Product Tankers 25,000+ dwt (', 701014, 1, 6, 2, 1, now(), 1655703762426, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701016, 701016, 'Russia Port Calls - Product Tankers 25,000+ dwt (Beta) DWT million', 'Russia Port Calls - Product Tankers 25,000+ dwt (Beta) DWT million', 'Russia Port Calls - Product Tankers 25,000+ dwt (', 701014, 2, 6, 2, 1, now(), 1655703762436, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701017, 701017, 'Russia Port Calls - Crude Tankers (Beta) ', 'Russia Port Calls - Crude Tankers (Beta) ', 'Russia Port Calls - Crude Tankers (Beta) ', 701014, 3, 6, 2, 1, now(), 1655703762449, 3502, NULL);
INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)VALUES (701018, 701018, 'Russia Port Calls - Crude Tankers (Beta) DWT million ', 'Russia Port Calls - Crude Tankers (Beta) DWT million ', 'Russia Port Calls - Crude Tankers (Beta) DWT mill', 701014, 4, 6, 2, 1, now(), 1655703762457, 3502, NULL);
代码解析(c++)
#include<iostream>
#include<fstream>
#include<string>
#include<sys/timeb.h>
#include<chrono>
#include<windows.h>

using namespace std;

int main() {
	
	// 读取树文件片段
	ifstream ReadF("D:\\cpp\\tree.txt");
	string data;

	/*  
		设置两个一维数组
        parent_indicatorId[n] 记录在n级别上的父指标id
        brotherNo[n] 记录在n级别上存在的兄弟节点个数
    */
	int parent_indicatorId[] = {-1,150000000,701,0,0,0,0,0};
	int brotherNo[] = {-1,1,0,0,0,0,0,0};

	// 设置初始的父节点级别 
	int parent_level = 2;
	
	// 设置生成指标的初始id,后续id在此基础上递增
	int cur_indicatorId = 701001;
	int cur_level;
	int cur_brotherNo;
	int cur_parent_indicatorId;
	
	// 设置工作游标,用于记录 当前最新指标id、当前指标级别、当前兄弟节点个数
	int last_indicatorId = parent_indicatorId[parent_level];
	int last_level_cursor = parent_level;
	int last_brotherNo_cursor = brotherNo[parent_level];
	
    // 设置sql输出位置
	ofstream outfile;
	outfile.open("D:\\cpp\\sql.txt", ofstream::out);

	// 逐行处理并记录数据
	while(getline(ReadF, data)) {
        // 通过标准树状txt每行起始的‘-’数量,计算所属等级
		cur_level = data.find_first_not_of("-")/2+1;
		
		// 当前节点,默认设置成叶子节点
		int noteCategory = 2;
		
		// 通过当前级别,和上次级别,判断是兄弟节点、子节点、非子节点三种情况
		if(cur_level == last_level_cursor){
			// 兄弟节点
			brotherNo[cur_level]++;
			cur_brotherNo = brotherNo[cur_level];
		}else if(last_level_cursor < cur_level){
			// 子节点
			brotherNo[cur_level] = 1;
			cur_brotherNo = brotherNo[cur_level];
			// 由于当前节点为上次节点的子节点,判定上次节点为非叶子节点
			string update_sql = "UPDATE c_in_indicatortree set NoteCategory = 1 where NodeCode = " + to_string(last_indicatorId) + " ;";
			cout << update_sql << endl;
			outfile << update_sql << endl;
		}else{
			// 非子节点
			brotherNo[last_level_cursor] = 0;
			brotherNo[cur_level]++;
			cur_brotherNo = brotherNo[cur_level];
		}
        
        // 从一维数组获取指标父节点,并更新当前级别父指标为当前指标
		cur_parent_indicatorId = parent_indicatorId[cur_level-1];
		parent_indicatorId[cur_level] = cur_indicatorId;
		        
		// 指标名称截取
		string cur_indicatorName = data.substr(data.find_first_not_of("-"), data.size() - 1);
		// 指标简称、拼音简称等生成
		string nodeAbbr = cur_indicatorName.substr(0, 99);
		string chiSpelling = cur_indicatorName.substr(0, 49);
        
        // 获取当前时间戳
		chrono::time_point<chrono::system_clock, chrono::milliseconds> tpMicro= chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now());
    	time_t timeStamp = tpMicro.time_since_epoch().count();
    
    	// 生成sql
		string insert_sql = "INSERT INTO `caldb`.`c_in_indicatortree` (`ID`, `NodeCode`, `NodeName`, `NodeAbbr`, `ChiSpelling`, `ParentNode`, `BrotherNo`, `LevelNumber`, `NoteCategory`, `NodeState`, `UpdateTime`, `JSID`, `Edition`, `IndiRemark`)";
		insert_sql+= "VALUES ("+to_string(cur_indicatorId)+", "+to_string(cur_indicatorId)+", '"+cur_indicatorName+"', '"+nodeAbbr+"', '"+chiSpelling+"', "+to_string(cur_parent_indicatorId)+", "+to_string(cur_brotherNo)+", "+to_string(cur_level)+", "+to_string(noteCategory)+", 1, now(), "+to_string(timeStamp)+", 3502, NULL);";
		
		// 输出到控制台和文件
		cout << insert_sql << endl;
		outfile << insert_sql << endl;

		// 进入下一行前,更新上次指标id和上次指标级别,并自增指标id
		last_indicatorId = cur_indicatorId;
		last_level_cursor = cur_level;
        cur_indicatorId++;
       
		// 5毫秒休眠,避免毫秒级时间戳冲突
		Sleep(5);
	}
	
	outfile.close();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值