c# 递归应用 完成js文件自动引用
背景:
两张表,分别是 :sys_tbl,和 sys_field,其中:sys_tbl 是系统所有表的信息,包含两个字段 :code(表名),name(表描述信息);sys_fld 是记录第张表中的字段 的名称(field)和描述信息(table) ,
截图如下:
sys_tbl

其中,字段 名称包含对其它名称(对象) 的引用,写法为:表名(去除下划线)_引用字段 ,如:einventory_cinvcode,就是对表 e_inventory 中字段 cinvcode的引用。
每张表都在系统 中对应有不同功能的js文件(模块)如: 编辑窗体(dialog类型),跨域选择窗体(field)类型等
需求:
在一个综合的页面中,会对其它对象进行引用(依赖),而这种引用最终体现在对js文件的包含。同时被引用的js 文件(一级引用)还有自已关联对象(二级引用).....如此下去,直到N 级引用。
所以现在需要逐层找到对象的引用,直到最后不依赖任何对象为止,并把这些js文件生成引用包含路径(不重复)
分析:
1、返回结果类型:
得到这些js引用,不能重复,但是对象之间引用是可以存在交叉的,在整个引用链条上,同一个对象会被 多个对象引用,所以简单的字符串数组是避免不了重复的。但是在C#中的HashSet可以做到不重复,同时这个去重工作是自动完成的,所以存结果如果是简单的value类型,用 HashSet就可以,但是因为字段中表的信息已经被格式化过,所以还要把格式化之前的表名称取出与字段对应,所以需要一个key-value类型的数据 ,那就是 Dictionary<string, string> 了。
2、算法选择
因为查找引用,是层层进行的,而且下一层引用的要用到上一层的引用结果,所以这里选择递归算法
代码实现:
声明一个全局变量,记录所有的表与字段的对应的数据,因为需要确保数据key 唯一性所以选择Dicctionary类型
/// <summary>
/// define the gloable parameter to save the rel obj data
/// </summary>
public static Dictionary<string, string> deepRef = new Dictionary<string, string>();
入口函数,完成准备工作,(数据库连接,参数准备)
/// <summary>
/// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
/// </summary>
/// <param name="headField">表字段列表</param>
/// <returns></returns>
public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField)
{
HashSet<string> refset = new HashSet<string>();
// HashSet<string> refset_result = new HashSet<string>();
foreach (var item in headField)
{
if (!item.controlType.StartsWith("hz_"))// is not the field of reference
{
continue;
}
string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field] refset.Add(fieldarr[0]);//the first prefix }
dataOperate dao = new dataOperate();
dao.DBServer = "info";
SqlConnection conn = dao.createCon();
try
{
if (refset.Count > 0)
{ if (conn.State != ConnectionState.Open)
conn.Open();//open connection
deepRef = new Dictionary<string, string>();//clear the relation obj data
getRef(conn, refset); }
return deepRef;
}
catch (Exception)
{ throw;
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
} }
递归函数,最终完成在数据库中获取字段依赖的对象的获取
/// <summary>
/// get the relation obj
/// </summary>
/// <param name="conn"></param>
/// <param name="ref_field"></param>
/// <returns></returns>
public static HashSet<string> getRef(SqlConnection conn, HashSet<string> ref_field)
{
HashSet<string> refset = new HashSet<string>(); string refstr = string.Join("','", ref_field);
if (refstr != "")
{
refstr = "'" + refstr + "'"; string sql = "SELECT dbo.GetSplitOfIndex(b.[field],'_',1) as refcode,a.[code] FROM ( select replace(code,'_','') as uncode,* from [SevenWOLDev].[dbo].[sys_tbl_view] ) as a inner join [SevenWOLDev].[dbo].[sys_fld_view] as b on a.uncode=dbo.GetSplitOfIndex(b.[field],'_',1) where replace([table],'_','') in (" + refstr + ") and uitype='ref'";
//get dataset relation obj
DataSet ds = dataOperate.getDataset(conn, sql);
if (ds != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
//rel ref exists
for (int k = 0; k < dt.Rows.Count; k++)
{
string vt = dt.Rows[k].ItemArray[0].ToString();
string vv = dt.Rows[k].ItemArray[1].ToString();
refset.Add(vt);//save current ref
if(!deepRef.ContainsKey(vt))
deepRef.Add(vt, vv);// save all ref }
if (refset.Count > 0)// get the ref successfully
{
//recursion get
getRef(conn, refset);
} } } }
else
{
//no ref
}
return refset;
}
对函数进行调用,并组织出js文件引用路径
Dictionary<string,string> deepRef = ExtjsFun.getRelRef(HeadfieldSetup);
if (deepRef != null)
{
foreach (var s in deepRef)
{
string tem_module = "";
tem_module = s.Value;
string[] moduleArr = tem_module.Split('_');
if (tem_module.IndexOf("_") >= 0)
tem_module = moduleArr[1];//module name for instance: p_machine ,the module is “machine” unless not included the underscore
string fieldkind = "dialog";
jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
fieldkind = "field";
jslist.Add(MyComm.getFirstUp(tem_module) + "/" + ExtjsFun.GetJsModuleName(tem_module, s.Value, fieldkind) + ".js");
}
}
最终结果 如下:
<script src="../../dept/demoApp/scripts/Sprice/SpriceE_spriceGridfield.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Org/OrgE_orgDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Supplier/SupplierOa_supplierField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Unit/UnitE_unitDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Unit/UnitE_unitField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Wh/WhWh_whDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Wh/WhWh_whField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Mold/MoldP_moldDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Mold/MoldP_moldField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Suppliercategory/SuppliercategoryOa_suppliercategoryField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Bank/BankOa_bankDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Bank/BankOa_bankField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Machine/MachineP_machineDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Machine/MachineP_machineField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Inventory/InventoryE_inventoryDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Basedocment/BasedocmentOa_basedocmentField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Basedoctype/BasedoctypeOa_basedoctypeField.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Tax/TaxE_taxDialog.js" type="text/javascript"></script>
<script src="../../dept/demoApp/scripts/Tax/TaxE_taxField.js" type="text/javascript"></script>
完事代码如下:
/// <summary>
/// define the gloable parameter to save the rel obj data
/// </summary>
public static Dictionary<string, string> deepRef = new Dictionary<string, string>();
/// <summary>
/// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
/// </summary>
/// <param name="headField">表字段列表</param>
/// <returns></returns>
public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField)
{
HashSet<string> refset = new HashSet<string>();
// HashSet<string> refset_result = new HashSet<string>();
foreach (var item in headField)
{
if (!item.controlType.StartsWith("hz_"))// is not the field of reference
{
continue;
}
string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field] refset.Add(fieldarr[0]);//the first prefix }
dataOperate dao = new dataOperate();
dao.DBServer = "info";
SqlConnection conn = dao.createCon();
try
{
if (refset.Count > 0)
{ if (conn.State != ConnectionState.Open)
conn.Open();//open connection
deepRef = new Dictionary<string, string>();//clear the relation obj data
getRef(conn, refset); }
return deepRef;
}
catch (Exception)
{ throw;
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
} } /// <summary>
/// get the relation obj
/// </summary>
/// <param name="conn"></param>
/// <param name="ref_field"></param>
/// <returns></returns>
public static HashSet<string> getRef(SqlConnection conn, HashSet<string> ref_field)
{
HashSet<string> refset = new HashSet<string>(); string refstr = string.Join("','", ref_field);
if (refstr != "")
{
refstr = "'" + refstr + "'"; string sql = "SELECT dbo.GetSplitOfIndex(b.[field],'_',1) as refcode,a.[code] FROM ( select replace(code,'_','') as uncode,* from [SevenWOLDev].[dbo].[sys_tbl_view] ) as a inner join [SevenWOLDev].[dbo].[sys_fld_view] as b on a.uncode=dbo.GetSplitOfIndex(b.[field],'_',1) where replace([table],'_','') in (" + refstr + ") and uitype='ref'";
//get dataset relation obj
DataSet ds = dataOperate.getDataset(conn, sql);
if (ds != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
//rel ref exists
for (int k = 0; k < dt.Rows.Count; k++)
{
string vt = dt.Rows[k].ItemArray[0].ToString();
string vv = dt.Rows[k].ItemArray[1].ToString();
refset.Add(vt);//save current ref
if(!deepRef.ContainsKey(vt))
deepRef.Add(vt, vv);// save all ref }
if (refset.Count > 0)// get the ref successfully
{
//recursion get
getRef(conn, refset);
} } } }
else
{
//no ref
}
return refset;
}
c# 递归应用 完成js文件自动引用的更多相关文章
- JavaScript进阶(二)在一个JS文件中引用另一个JS文件
在一个JS文件中引用另一个JS文件 转载地址:http://blog.csdn.net/zndxlxm/article/details/7875787 方法一 在调用文件的顶部加入下例代码 ...
- JS文件中引用另一个JS文件
1.生产项目上遇到一个Bug,需要修改JS文件,添加Jquery代码,但是原来的页面没有添加对Jquery文件的引用,无法修改原来的页面(自动生成的HTML) 这就需要在JS文件中添加对Jquery文 ...
- JS中关于JS文件的引用以及问题
问题描述: 由于JSP中JS函数比较多,因此打算新建一个JS文件在JSP中引用JS文件,现在出现如下问题,JS如何引用时正确的,JS引用之后出现乱码如何解决? 问题解决: (1)JS ...
- Js- 在一个JS文件中引用另一个JS文件
在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></script> ...
- JavaScript笔记01——数据存储(包括.js文件的引用)
While, generally speaking, HTML is for content and CSS is for presentation, JavaScript is for intera ...
- 在一个JS文件中引用另一个JS文件
方法一,在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></scrip ...
- 当vue.js与其他js文件同时引用导致页面不显示的问题
作为一个萌新,昨天学习的过程中遇到了vuejs与其他js在共同页面时引用时冲突的问题 具体如下 虽然注意到了前后顺序,但是页面还是出不来东西 我知道现实开发中可能不是这么引用,但是学习中是这么引入的, ...
- 上传css,js文件并引用
今天在做单页面的简历,由于css样式跟js代码过多,所以就想着可不可以把css文件跟js文件上传到网上,然后引用. 一开始的想法是我上传到gitee上,但是从gitee服务器返回的Header上加了C ...
- 批处理/命令行合并js,递归合并子目录js文件
for /r %%i in (*.js) do type "%%i">>xxx-all.js java -jar yuicompressor.jar --type js ...
- js文件中引用其他js文件
这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...
随机推荐
- nodejs发布cesium问题,其他电脑访问发布
在电脑上安装nodejs后在选择的cesium文件中,按住shift和鼠标右键,打开powershell,输入命令行hs -p 1212,完成cesium的发布,出现两个网址,127.0.0.1:12 ...
- 2022-4-7内部群每日三题-清辉PMP
1.公司聘用一名项目经理来协调一个期限紧迫的敏捷项目,项目经理和敏捷团队都由一位项目组合经理管理,该项目组合经理倾向于根据需要将开发人员重新分配给其他紧急事项,当项目经理与其接洽时,项目组合经理坚持认 ...
- 4. 模板解析,生成render函数,渲染页面
解析模板,生成render函数,执行render函数,实现视图渲染 1.模板转化成ast语法树 2.ast语法树生成render函数 3.执行render函数生成虚拟dom 4.执行_update方法 ...
- Java构造器详解
java 构造器详解 一个构造器即使什么都不写 ,他也会默认存在一个构造器. 构造器的作用; ①:使用new关键字.本质是在调用构造器 ②:用来初始化值 定义了一个有参构造之后,如果想使用无参构造,显 ...
- Win10服务主机本地系统磁盘占用过高解决
前言:发现电脑卡,磁盘被本地系统占用好多.把尝试过的方法都发一下. 1.尝试用了网上的关闭家庭组: win+R打开运行输入:services.msc 打开Windows服务管理器.找到HomeGro ...
- Oracle数据库简单常用语句
简单常用语句: 登录超级用户 sqlplus / as sysdba; 登录普通用户 connect username/password; 显示当前用户名 show user; 查询所有用户名 sel ...
- sqlmap-1.6.12.11
Usage: sqlmap.py [options] 选项: -h, --help 显示基本帮助信息并退出 -hh 显示高级帮助信息并退出 --version 显示程序的版本号并退出 -v VERBO ...
- L2 Cracia Final Update1 OpCodz
[87] Gracia Final Update 1 Client 00 SendLogOut 01 RequestAttack 03 RequestStartPledgeWar 04 Request ...
- 20211306 实验四 Python综合实践
学号 20211306 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 2113 姓名: 丁文博 学号:20211306 实验教师:王志强 实验日期 ...
- python读取Excel文件的操作
①通过xlutils在已有表中写数据(这种方法会改变excel的样式) import xlrd,xlwt from xlutils.copy import copy 将已存在的Excel表格赋值给变量 ...