截取一段动态报表的查询

最后更新:2024-09-07 05:30:01 | 状态:未完成

先把查询条件、关联条件保存到配置表中 

也有可能直接由前端提交,大概结构如: 

{
    template:'模板i'
	table:'hr_employee'			     //主表
	datasources:[{                   //数据源
	    id:'数据源主键'
	    code:'数据源编号'
		name:'数据源名称'
		tables:[{
			title:'表名中文'    //数据源-关联表名
			name:'hr_department'
			alias:'别名'
			join:'left'              //连接方式(inner:内连接, left:左连接 ,right:右连接)
			relations:[{            //关联条件
				join:'and'                         //关联方式(可选and,or,ors)
				column:'id'                        //列名
				compare:10                         //比较运算符
				value:null                         //常量值
				relation_table:'hr_employee'      //比较表名
				relation_column:'department_id'   //比较列名
			}] //end-relattions
		}] //end-tables

		,conditions:[{                             //过滤条件
			join:'and'                             //关联方式(可选and,or,ors)
			table:'hr_employee'                    //表名
			column:'type_id'                       //列名
			compare:10                             //比较运算符
			value:100                              //常量值
		}]//end-conditions
	}] //end-datasources
}

下面是基于配置表生成SQL

ConfigStore configs = new DefaultConfigStore();
String masterTableName = datasource.getString("table_name");
String masterTableAlias = datasource.getString( "table_alias");
Schema schema = new Schema();
if(BasicUtil.isEmpty(masterTableAlias)){
	masterTableAlias = masterTableName;
}
DataRow functional = ServiceProxy.service("sys").query("t_functional_uri_table", "++table_name:"+masterTableName, "tenant_id:"+tenant);
if(null != functional){
	String prefix = functional.getString("functional");
	schema.setName(prefix);
}
masterTable = new Table();
masterTable.setSchema(schema);
masterTable.setName(masterTableName);
masterTable.setAlias(masterTableAlias);
log.warn("主表:{}", masterTable);
//TableBuilder实现多表关联
org.anyline.data.param.TableBuilder builder = org.anyline.data.param.TableBuilder.init(masterTable);

//需要查询的列
List<String> columns = new ArrayList<>();
DataSet tables = all_tables.getRows("datasource_id", datasource.getId());
//关联表数据库对应关系
Map subSchemaRelation = new HashMap();
if(tables.size() == 0){
	//如果只有一个表直接查*省了查询表结构的过程
	columns.add("*");
}else{
	//如果有多个表,一般会出现列重名的情况,所以需要一个前缀来区分(这里用表名或别名__)
	List<String> cols = service.columns(masterTable);
	for(String col:cols){
		columns.add(masterTableName+"."+col + " AS " + masterTableAlias + "__" + col);
	}
	for(DataRow table:tables){
		String tableName = table.getString( "name");
		//有别名的用别名,没有别名的用原表名
		String tableAlias = table.getString( "alias", "name");
		//获取子表数据库
		DataRow subFunctional = ServiceProxy.service("sys").query("t_functional_uri_table", "++table_name:"+tableName);
		Schema subSchema = new Schema();
		if(null != subFunctional){
			String prefix = subFunctional.getString("functional");
			subSchema.setName(prefix);
		}
		cols = service.columns(new Table(subSchema, tableName));
		subSchemaRelation.put(tableName,subSchema);
		for(String col:cols){
			//添加前缀 避免列重名
			columns.add(tableAlias+"."+col + " AS " + tableAlias + "__" + col);
		}
	}
}
//每个表参考主表重复上面的过程
for(DataRow table:tables){
	String condition = null;
	String tableName = table.getString("name");
	String tableAlias = table.getString( "alias");
	if(BasicUtil.isEmpty(tableAlias)){
		tableAlias = tableName;
	}
	//最关键的ConfigStore需要熟悉
	ConfigStore conditions = new DefaultConfigStore();
	//与数据源和表相关的 关联条件
	DataSet relations = all_relations.getRows("datasource_id", datasource.getId(), "table_id", table.getId());
	//关联条件
	for(DataRow relation:relations){
		String columnName = relation.getString("column_name");
		String value = relation.getString("val");
		String join = relation.getString("join_code");
		Compare compare = compare(relation.getInt("compare_code", 10));
		String relationTableAlias = relation.getString("relation_table_alias", "relation_table_name");
		DataRow relationAlias = tables.getRow("name",relationTableAlias);
		if(null != relationAlias){
			relationTableAlias = relationAlias.getString("alias", "name");
		}
		String relationColumnNmae = relation.getString("relation_column_name");
		if(BasicUtil.isEmpty(value)) {
			//两个表之间关联
			conditions.condition(join, compare, concat(tableAlias, columnName)
					,  "${"+concat(relationTableAlias, relationColumnNmae)+"}"); //${a.id} 注意这里会原样拼接到SQL
		}else{
			//常量值
			conditions.condition(join, compare, concat(tableAlias, columnName), value);
		}
	}
	//生成ON关联条件
	ConditionChain chain = conditions.getConfigChain().createAutoConditionChain();
	condition = chain.getRunText(false, null, RuntimeHolder.runtime(), true);

	//把占位值添加到SQL主体中
	List<RunValue> vals = chain.getRunValues();
	for(RunValue runValue:vals){
		configs.addStaticValue(runValue.getValue());
	}
	//left join/right join
	Join.TYPE join = Join.TYPE.valueOf(table.getString("join_code").toUpperCase());
	//builder.join(join,  db_prefix+tableName +" as " + tableAlias, condition);
	Schema subSchema = (Schema) subSchemaRelation.get(tableName);
	builder.join(join,  new Table<>(subSchema, tableName).setAlias(tableAlias) , condition);
}
//过滤条件
DataSet filters = all_filters.getRows("datasource_id", datasource.getId());
for(DataRow filter:filters){
	String value = filter.getString("val");
	String join = filter.getString("join_code");
	String tableName = filter.getString("table_name");
	String columnName = filter.getString("column_name");
	Compare compare = compare(filter.getInt("compare_code", 10));
	configs.condition(join, compare, concat(tableName, columnName), value);
}
configs.columns(columns);
configs.and(masterTableAlias+".id", pv);
configs.and(masterTableAlias+".tenant_id", tenant);
//过滤掉删除的条目
for (DataRow table : tables) {
	configs.and(table.getStringWithoutNull("alias","name")+".del_flag","0");
}
DataSet set = service.querys(builder.build(), configs);
首页 最近更新 搜索 提交 回复