keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table);
342 | boolean isEmpty = keyColumnMap == null || keyColumnMap.isEmpty();
343 | String alias = isEmpty ? null : keyColumnMap.get(key);
344 | if (alias == null) {
345 | if (isEmpty == false && throwWhenNoKey) {
346 | throw new NullPointerException(table + ":{} 中不允许传 " + key + " !");
347 | }
348 | return key;
349 | }
350 |
351 | return alias;
352 | }
353 |
354 | /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。
355 | * @param key
356 | * @param table
357 | * @param method
358 | * @return
359 | */
360 | public static String compatOutputKey(String key, String table, RequestMethod method) {
361 | return compatOutputKey(key, table, method, null);
362 | }
363 |
364 | /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。
365 | * @param key
366 | * @param table
367 | * @param method
368 | * @param version
369 | * @return
370 | * @see 先提前配置 {@link #VERSIONED_COLUMN_KEY_MAP},然后在 {@link AbstractSQLExecutor} 的子类重写 {@link AbstractSQLExecutor#getKey } 并调用这个方法,例如
371 | *
372 | protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
373 | int columnIndex, Map childMap) throws Exception {
374 | return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod(), version);
375 | }
376 | *
377 | */
378 | public static String compatOutputKey(String key, String table, RequestMethod method, Integer version) {
379 | Map columnKeyMap = getClosestValue(VERSIONED_COLUMN_KEY_MAP, version, table);
380 | String alias = columnKeyMap == null || columnKeyMap.isEmpty() ? null : columnKeyMap.get(key);
381 | return alias == null ? key : alias;
382 | }
383 |
384 | public static T getClosestValue(SortedMap> versionedMap, Integer version, String table) {
385 | boolean isEmpty = versionedMap == null || versionedMap.isEmpty();
386 |
387 | Map map = isEmpty || version == null ? null : versionedMap.get(version);
388 | T m = map == null ? null : map.get(table);
389 | if (isEmpty == false && m == null) {
390 | Set>> set = versionedMap.entrySet();
391 |
392 | T lm = null;
393 | for (Entry> entry : set) {
394 | Map val = entry.getValue();
395 | m = val == null ? null : val.get(table);
396 | if (m == null) {
397 | continue;
398 | }
399 |
400 | if (version == null || version == 0) {
401 | // versionedMap.put(null, val);
402 | return m;
403 | }
404 |
405 | Integer key = entry.getKey();
406 | if (key == null) {
407 | lm = m;
408 | map = val;
409 | continue;
410 | }
411 |
412 | if (version >= key) {
413 | versionedMap.put(version, val);
414 | return m;
415 | }
416 |
417 | break;
418 | }
419 |
420 | if (lm != null) {
421 | m = lm;
422 | }
423 |
424 | if (map != null) {
425 | versionedMap.put(version, map);
426 | }
427 | }
428 |
429 | return m;
430 | }
431 |
432 |
433 | /**把多个表名相关属性拼接成一个表名
434 | * @param database
435 | * @param schema
436 | * @param datasource
437 | * @param table
438 | * @return
439 | */
440 | public static String concat(String database, String schema, String datasource, String table) {
441 | return database + "-" + schema + "-" + datasource + "-" + table;
442 | }
443 |
444 |
445 | }
446 |
--------------------------------------------------------------------------------