loadSingle代码分析
bosJava
14 views
Nov 16, 2024

kd.bos.servicehelper.BusinessDataServiceHelper#loadSingle(java.lang.Object, java.lang.String)

#查询转换

    @KSMethod
public static DynamicObject loadSingle(Object pk, String entityName) {
    // ...
    // 根据实体标识获取实体信息
    DynamicObjectType type = EntityMetadataCache.getDataEntityType(entityName);
    var5 = BusinessDataReader.loadSingle(pk, type);
    // ...
    return var5;
}

  

kd.bos.data.BusinessDataReader#loadSingle(java.lang.Object, kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType)

    public static DynamicObject loadSingle(Object pk, @Nonnull DynamicObjectType type) {
    // ...
    DynamicObject result = (DynamicObject)read(pk, type, Boolean.TRUE, Boolean.FALSE);
    // ...
    return result;
}

  

kd.bos.data.BusinessDataReader#read(java.lang.Object, kd.bos.dataentity.metadata.IDataEntityType, java.lang.Boolean, boolean)

pk: 数据主键

type: 数据类型(表名,数据库路由)

loadReferenceData: 是否加载引用属性数据

selectHeadOnly: 是否只加载主表数据

    private static Object read(Object pk, IDataEntityType type, Boolean loadReferenceData, boolean selectHeadOnly) {
    IDataManager dataManager = DataManagerUtils.getDataManager(type);
    dataManager.setSelectHeadOnly(selectHeadOnly);
    Object obj = dataManager.read(pk);
    if (loadReferenceData && obj != null) {
        loadRefence(new Object[]{obj}, type);
    }
    return obj;
}

  

#加载引用数据

kd.bos.data.BusinessDataReader#loadRefence

    public static void loadRefence(Object[] dataEntitys, IDataEntityType type) {
    CachedLoadReferenceObjectManager manager = new CachedLoadReferenceObjectManager(type, Boolean.FALSE);
    manager.Load(dataEntitys);
}

  

kd.bos.orm.datamanager.LoadReferenceObjectManager#Load(java.lang.Object[])

    public final void Load(Object[] dataEntities) {
    this.DoTasks(this.GetTasks(dataEntities));
}

  

kd.bos.orm.datamanager.LoadReferenceObjectManager#DoTasks(java.util.Collection<kd.bos.dataentity.entity.DataEntityReferenceList>, java.util.HashMap<java.lang.String,java.lang.String>)

    protected void DoTasks(Collection<DataEntityReferenceList> tasks, HashMap<String, String> dictReferenceWhere) {
    dataEntities = this.Read(task.getDataEntityType(), where);
    dataEntities = this.Read(task.getDataEntityType(), oids);
}

  

#加载主数据

kd.bos.orm.dataentity.DataManagerImplement#read(java.lang.Object)

    public Object read(Object oid) {
    ReadWhere where = new ReadWhere(new Object[]{oid});
    ShardingHintContext ctx = ShardingHinter.tryHint(this._dataEntityType, new Object[]{oid});
    result = this.read(where);
    return result[0];
}

  

kd.bos.orm.dataentity.DataManagerImplement#read(kd.bos.orm.datamanager.ReadWhere)

    public Object[] read(ReadWhere where) {
    assert where != null;
    QuickDataSet dataSet = this.readToDataSet(this.getDatabase(), this.getDataEntityTypeMap().getDbTable(), where);
    Object[] entities = this.dataSetToEntities(dataSet);
    this.privacyDataToEntities(entities, this.getDataEntityTypeMap());
    this.handlePrivacyDataEntities(entities, this.getDataEntityTypeMap());
    this.SetEntitySnapshot(entities, dataSet);
    return entities;
}

  

kd.bos.orm.dataentity.DataManagerImplement#readToDataSet

    private QuickDataSet readToDataSet(DbMetadataDatabase database, DbMetadataTable rootTable, ReadWhere where) {
    return this.select(tables, rootTable, where);
}

  

#拼接SQL执行

kd.bos.orm.dataentity.DataManagerImplement#select(java.lang.Iterable<kd.bos.dataentity.metadata.database.DbMetadataTable>, kd.bos.dataentity.metadata.database.DbMetadataTable, kd.bos.orm.datamanager.ReadWhere)

    public final QuickDataSet select(Iterable<DbMetadataTable> tablesSchema, DbMetadataTable rootTable, ReadWhere where) {
    Collection<DbMetadataTable> tables = (Collection)tablesSchema;
    ArrayList<SelectSql> selectSQLAll = new ArrayList(tables.size());
    List<String> rootJoinTempTableStrList = new ArrayList(2);
    List<PKTempTable> tempTableList = new ArrayList();
    SqlBuilder whereSql = this.getRootTableWhereSQl(rootTable, where, rootJoinTempTableStrList, tempTableList);
    Iterator var9 = tablesSchema.iterator();
    while(var9.hasNext()) {
        DbMetadataTable tableSchema = (DbMetadataTable)var9.next();
        DbMetadataTable stopJoinTable = null;
        List<String> joinTempTableStrList = new ArrayList(2);
        if (tableSchema != rootTable) {
            SqlBuilder whereSqlChild = this.getChildTableWhereSQl(tableSchema, rootTable, where, whereSql, joinTempTableStrList, tempTableList);
            if (whereSqlChild != whereSql) {
                stopJoinTable = rootTable;
                whereSql = whereSqlChild;
            } else {
                joinTempTableStrList.addAll(rootJoinTempTableStrList);
            }
        } else {
            joinTempTableStrList.addAll(rootJoinTempTableStrList);
        }
        SelectSql tupSQL = this.getSelectSQL(tableSchema, stopJoinTable);
        SqlBuilder selectSql = new SqlBuilder();
        selectSql.append("/*ORM*/ ", new Object[0]).append(tupSQL.SelectSqlStr, new Object[0]);
        Iterator var15 = joinTempTableStrList.iterator();
        while(var15.hasNext()) {
            String join = (String)var15.next();
            selectSql.append(join, new Object[0]);
        }
        if (!whereSql.isEmpty()) {
            selectSql.append(" WHERE ", new Object[0]);
            selectSql.appendSqlBuilder(whereSql);
            if (tupSQL.SelectWhere.length() > 0) {
                selectSql.append(" AND ", new Object[0]).append(tupSQL.SelectWhere, tupSQL.SelectParams != null ? tupSQL.SelectParams.toArray() : null);
            }
        } else if (tupSQL.SelectWhere.length() > 0) {
            selectSql.append(" WHERE ", new Object[0]).append(tupSQL.SelectWhere, tupSQL.SelectParams != null ? tupSQL.SelectParams.toArray() : null);
        }
        String sqlCount = tupSQL.CountSql;
        SqlBuilder countBuild = new SqlBuilder();
        if (sqlCount.length() > 0 && !whereSql.isEmpty()) {
            sqlCount = String.format(sqlCount, " WHERE ");
            countBuild.append("/*ORM*/ ", new Object[0]).append(sqlCount, new Object[0]).appendSqlBuilder(whereSql).append(tupSQL.CountWhere.length() > 0 ? " AND " + tupSQL.CountWhere : " ", tupSQL.CountParams != null ? tupSQL.CountParams.toArray() : null).append(StringUtils.isBlank(tupSQL.CountGroupBySqlpart) ? " " : tupSQL.CountGroupBySqlpart, new Object[0]);
        }
        selectSql.append(this.getSortSQL(tableSchema), new Object[0]);
        tupSQL.SelectSqlBuild = selectSql;
        tupSQL.CountSqlBuild = countBuild;
        selectSQLAll.add(tupSQL);
    }
    return this.executeReader(tablesSchema, rootTable, selectSQLAll, where, tempTableList);
}

  

#获取查询后返回的行

    List<QuickRow> rows = (List)this.doExecute(selectInfo.SelectSqlBuild, where, tableSchema, rootTable, action);

  
    protected QuickDataSet executeReader(Iterable<DbMetadataTable> tablesSchema, DbMetadataTable rootTable, List<SelectSql> selectSqls, ReadWhere where, List<PKTempTable> tempTableList) {
    List<QuickRow> rows = (List)this.doExecute(selectInfo.SelectSqlBuild, where, tableSchema, rootTable, action);
    return ds;
}

  

#执行SQL

    private <T> T doExecute(SqlBuilder builder, ReadWhere where, DbMetadataTable tableSchema, DbMetadataTable rootTable, ResultSetHandler<T> action) {
    return DB.query(this.dbRoute, builder, action);
}

  
    public static <T> T query(DBRoute dbRoute, SqlBuilder sb, ResultSetHandler<T> rh) {
    DBLimiter.checkCount();
    return executeSqlBuilder(dbRoute, sb, (t3) -> {
        return ((BaseDB)t3.getV2()).query(dbRoute, ((SqlObject)t3.getV3()).getSql(), ((SqlObject)t3.getV3()).getParams(), rh, (TraceSpan)t3.getV1(), ((SqlObject)t3.getV3()).getTempTableReleaseList());
    }, true);
}

  
    <T> T query(DBRoute dbRoute, String sql, Object[] params, ResultSetHandler<T> rh, TraceSpan ts, List<AutoCloseable> tempTableList) {
    QueryResult<T> ret = this.query(dbRoute, this.getConnection(dbRoute, sql, ts), true, sql, rh, true, params);
    if (ret.getResource() != null) {
        ret.getResource().closeWithConnection();
    }
    this.releaseTempTableList(tempTableList);
    return ret.getResult();
}

  
    <T> QueryResult<T> query(DBRoute dbRoute, DelegateConnection con, boolean close, String sql, ResultSetHandler<T> rsh, boolean convertResultSet, Object... params) {
    // 查询获取Java原生ResultSet
    java.sql.ResultSet rs = ExtXDB.query(ctx, sql, params);
    result = rsh.handle(rs);
}

  
    public static ResultSet query(ExtContext ctx, String sql, Object... params) throws SQLException {
    return xdb.query(sql, params);
}

  

#查询分片

    public ResultSet query(String sql, Object... params) throws SQLException {
    // 根据SQL分片
    ShardingResult sr = engine.sharding(sql, params);
    // 事务
    boolean writtenInTX = ExtContext.writtenInTX(sql);
    // 保存分片查询结果
    List<Future<Pair<ShardingResult, ResultSet>>> listFS = new ArrayList(2);
    List<Pair<ShardingResult, ResultSet>> rss = new ArrayList();
    // 是否需要对分片结果进行合并
    if (!sr.withUnion()) {
                return this.doQuery(sr, originalSQL, engine, autoCloseSet, queryTimeoutSeconds, (ParallelTag)null, mc, writtenInTX);
            } else {
                // 多线程查询分片
                Future<Pair<ShardingResult, ResultSet>> f = ParallelExecutor.submit(() -> {
            if (config.isEnableParallelExecute()) {
                ParallelExecutor.setupParallelThreadContext(tag, pec, mc);
            }
            Pair var11;
            try {
                ResultSet rs = this.doQuery(cur, originalSQL, engine, autoCloseSet, queryTimeoutSeconds, tag, mc, writtenInTX);
                var11 = new Pair(cur, rs);
            } finally {
                sp.release();
                if (config.isEnableParallelExecute()) {
                    ParallelExecutor.clearParallelThreadContext(tag);
                }
            }
            return var11;
        });
        listFS.add(f);
    }
    // 合并
    Iterator var23 = listFS.iterator();
    while(var23.hasNext()) {
        Future<Pair<ShardingResult, ResultSet>> f = (Future)var23.next();
        rss.add(f.get());
    }
    // 数据合并
    return MergeManager.union(engine.getShardingConfigProvider(), sr.getStatementInfo(), rss, sr.getParentStmt(), sr.getSelectFeature(), sr.getUnionRootSelectFeature(), mc);
}

  
Total PV : 0|UV : 0
Current Online:1
From :