文件
AiXun-DataGov/script/datagov/BiOtherMysqlProbe.java
2026-11-20 15:32:57 +08:00

30 行
3.4 KiB
Java

package cn.aixunkeji.datagov.dataproduct.bi;
import cn.aixunkeji.datagov.integration.api.tablefile.AnalyticsQuery;
import cn.aixunkeji.datagov.integration.tablefile.AnalyticsSqlCompiler;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
/** Executes no database access. Uses the already-built production compiler and Owner result decoder. */
public final class BiOtherMysqlProbe {
private static String encode(String value){return Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));}
private static String decode(String value){return "-".equals(value)?null:new String(Base64.getDecoder().decode(value),StandardCharsets.UTF_8);}
public static void main(String[] args) throws ReflectiveOperationException {
String mode=args[0],aggregate=args[1];int top=Integer.parseInt(args[2]);
if(!Set.of("AVG","COUNT_DISTINCT","SUM").contains(aggregate)||top<1||top>2)throw new IllegalArgumentException();
if("compile".equals(mode)){
var plan=new AnalyticsQuery.Plan(List.of(),List.of("category"),List.of(new AnalyticsQuery.Measure("amount",aggregate,"value")),List.of(new AnalyticsQuery.Filter("tenant","EQ",List.of("fixture' OR 1=1 --"))),List.of(),10,false,new AnalyticsQuery.CategoryRemainder(top));
var compiled=AnalyticsSqlCompiler.compileForSource(plan,List.of("category","amount","tenant"),"MYSQL","aixun_datagov_system",null,"bi_other_fixture");
System.out.println(encode(compiled.sql()));System.out.println(String.join(",",compiled.parameters().stream().map(BiOtherMysqlProbe::encode).toList()));return;
}
if(!"decode".equals(mode))throw new IllegalArgumentException();
var rows=new ArrayList<Map<String,String>>();try(var input=new Scanner(System.in,StandardCharsets.UTF_8)){while(input.hasNextLine()){var values=input.nextLine().split("\\t",-1);if(values.length!=3)throw new IllegalArgumentException();var row=new LinkedHashMap<String,String>();row.put("category",decode(values[0]));row.put("value",decode(values[1]));row.put(AnalyticsQuery.CATEGORY_LEVEL,decode(values[2]));rows.add(row);}}
var plan=new BiQuerySource.Plan(List.of("category","amount"),List.of("category"),List.of(new BiQuerySource.Measure("amount",aggregate,"value")),List.of(),List.of(),10,false,new BiQuerySource.CategoryLimit(top));
// Source-file launch uses a distinct classloader. Reflection reaches the fixed package-private Owner method without changing production visibility.
var decoder=Class.forName("cn.aixunkeji.datagov.dataproduct.bi.BiCategoryResults").getDeclaredMethod("decode",BiQuerySource.Plan.class,BiQuerySource.Result.class);decoder.setAccessible(true);
try{var result=(BiQuerySource.Result)decoder.invoke(null,plan,new BiQuerySource.Result("SUCCEEDED",List.of("category","value",AnalyticsQuery.CATEGORY_LEVEL),List.copyOf(rows),false,LocalDateTime.now(),"Synthetic MySQL observation"));System.out.println("SUCCEEDED\t"+result.rows().size()+"\t"+Objects.toString(result.categoryRemainder().otherRowIndex(),"NONE"));}
catch(java.lang.reflect.InvocationTargetException error){var cause=error.getCause();if(!cause.getClass().getName().equals("cn.aixunkeji.datagov.dataproduct.bi.BiException"))throw error;var code=cause.getClass().getDeclaredField("code");code.setAccessible(true);System.out.println("REJECTED\t"+code.get(cause));}
}
}