DbAliasConst.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package jnpf.database.constant;
  2. import com.google.common.collect.ImmutableMap;
  3. import jnpf.constant.MsgCode;
  4. import jnpf.exception.DataException;
  5. import lombok.AllArgsConstructor;
  6. import java.util.Map;
  7. import java.util.Optional;
  8. /**
  9. * 字段别名特殊标识
  10. *
  11. * @author JNPF开发平台组 YanYu
  12. * @version v3.4.6
  13. * @copyrignt 引迈信息技术有限公司
  14. * @date 2023-02-11
  15. */
  16. public class DbAliasConst {
  17. /**
  18. * 允空
  19. */
  20. public static final String NULL = "NULL";
  21. /**
  22. * 非空
  23. */
  24. public static final String NOT_NULL = "NOT NULL";
  25. /**
  26. * 允空
  27. * 0:空值 NULL、1:非空值 NOT NULL
  28. */
  29. public static final NumFieldAttr<String> ALLOW_NULL = new NumFieldAttr<>(ImmutableMap.of(
  30. 1, NULL,
  31. 0, NOT_NULL,
  32. -1, NOT_NULL
  33. ));
  34. /**
  35. * 主键
  36. * 0:非主键、1:主键
  37. */
  38. public static final NumFieldAttr<Boolean> PRIMARY_KEY = new NumFieldAttr<>(ImmutableMap.of(
  39. 1, true,
  40. 0, false,
  41. -1, false
  42. ));
  43. public static final NumFieldAttr<Boolean> AUTO_INCREMENT = new NumFieldAttr<>(ImmutableMap.of(
  44. 1, true,
  45. 0, false,
  46. -1, false
  47. ));
  48. /**
  49. * 数值对应字段属性
  50. * @param <T>
  51. */
  52. @AllArgsConstructor
  53. public static class NumFieldAttr<T>{
  54. private Map<Integer, T> config;
  55. /**
  56. * 获取标识
  57. */
  58. public T getSign(Integer i) {
  59. return config.get(i == null ? -1 : i);
  60. }
  61. /**
  62. * 获取数值
  63. */
  64. public Integer getNum(T sign) throws DataException {
  65. if(sign == null){
  66. return 0;
  67. }
  68. Optional<Map.Entry<Integer, T>> first = config.entrySet().stream().filter(map -> map.getValue().equals(sign)).findFirst();
  69. if(first.isPresent()){
  70. return first.get().getKey();
  71. }else {
  72. throw new DataException(MsgCode.DB012.get());
  73. }
  74. }
  75. }
  76. }