TypeFormatter.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.gitee.gen.gen;
  2. import org.apache.commons.lang.StringUtils;
  3. import java.util.List;
  4. /**
  5. * 将各数据库类型格式化成统一的类型
  6. * @see TypeEnum
  7. * @author tanghc
  8. */
  9. public interface TypeFormatter {
  10. default String format(String columnType) {
  11. if (isBit(columnType)) {
  12. return TypeEnum.BIT.getType();
  13. }
  14. if (isBoolean(columnType)) {
  15. return TypeEnum.BOOLEAN.getType();
  16. }
  17. if (isTinyint(columnType)) {
  18. return TypeEnum.TINYINT.getType();
  19. }
  20. if (isSmallint(columnType)) {
  21. return TypeEnum.SMALLINT.getType();
  22. }
  23. if (isInt(columnType)) {
  24. return TypeEnum.INT.getType();
  25. }
  26. if (isLong(columnType)) {
  27. return TypeEnum.BIGINT.getType();
  28. }
  29. if (isFloat(columnType)) {
  30. return TypeEnum.FLOAT.getType();
  31. }
  32. if (isDouble(columnType)) {
  33. return TypeEnum.DOUBLE.getType();
  34. }
  35. if (isDecimal(columnType)) {
  36. return TypeEnum.DECIMAL.getType();
  37. }
  38. if(isJsonb(columnType)){
  39. return TypeEnum.JSONB.getType();
  40. }
  41. if (isVarchar(columnType)) {
  42. return TypeEnum.VARCHAR.getType();
  43. }
  44. if (isDatetime(columnType)) {
  45. return TypeEnum.DATETIME.getType();
  46. }
  47. if (isBlob(columnType)) {
  48. return TypeEnum.BLOB.getType();
  49. }
  50. return TypeEnum.VARCHAR.getType();
  51. }
  52. default boolean contains(List<String> columnTypes, String type) {
  53. for (String columnType : columnTypes) {
  54. if (StringUtils.containsIgnoreCase(type, columnType)) {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. boolean isBit(String columnType);
  61. boolean isBoolean(String columnType);
  62. boolean isTinyint(String columnType);
  63. boolean isSmallint(String columnType);
  64. boolean isInt(String columnType);
  65. boolean isLong(String columnType);
  66. boolean isFloat(String columnType);
  67. boolean isDouble(String columnType);
  68. boolean isDecimal(String columnType);
  69. boolean isVarchar(String columnType);
  70. boolean isDatetime(String columnType);
  71. boolean isBlob(String columnType);
  72. boolean isJsonb(String columnType);
  73. }