PostgreSqlColumnSelector.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.gitee.gen.gen.postgresql;
  2. import com.gitee.gen.gen.ColumnDefinition;
  3. import com.gitee.gen.gen.ColumnSelector;
  4. import com.gitee.gen.gen.GeneratorConfig;
  5. import org.apache.commons.lang.StringUtils;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import static com.gitee.gen.util.FieldUtil.convertString;
  9. /**
  10. * @author tanghc
  11. */
  12. public class PostgreSqlColumnSelector extends ColumnSelector {
  13. private static final PostgreSqlTypeFormatter SQL_TYPE_FORMATTER = new PostgreSqlTypeFormatter();
  14. public PostgreSqlColumnSelector(GeneratorConfig dataBaseConfig) {
  15. super(dataBaseConfig);
  16. }
  17. private static final String SHOW_COLUMN_SQL = "SELECT " +
  18. " pg_attribute.attname AS colname, " +
  19. " atttypid::regtype AS type, " +
  20. " numeric_scale as SCALE, " +
  21. " col_description ( pg_attribute.attrelid, pg_attribute.attnum ) AS cmt, " +
  22. " pg_attribute.attnum = pg_constraint.conkey [ 1 ] AS is_pk, " +
  23. "CASE WHEN POSITION ( 'nextval' IN column_default ) > 0 THEN 1 ELSE 0 END AS is_identity " +
  24. "FROM " +
  25. " pg_constraint " +
  26. " INNER JOIN pg_class ON pg_constraint.conrelid = pg_class.oid " +
  27. " INNER JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid " +
  28. " INNER JOIN pg_type ON pg_type.oid = pg_attribute.atttypid " +
  29. " INNER JOIN information_schema.COLUMNS C ON C.TABLE_NAME = pg_class.relname " +
  30. " AND C.COLUMN_NAME = pg_attribute.attname " +
  31. "WHERE " +
  32. " pg_class.relname = '%s' and pg_constraint.contype = 'p' " +
  33. " AND pg_attribute.attnum > 0";
  34. @Override
  35. protected String getColumnInfoSQL(String tableName) {
  36. return String.format(SHOW_COLUMN_SQL, tableName);
  37. }
  38. /*
  39. "colname" "type" "cmt" "is_pk" "is_identity"
  40. "id" "integer" "自增主键" "t" "1"
  41. "user_id" "integer" "用户id" "f" "0"
  42. "city" "character varying" "城市" "f" "0"
  43. "address" "character varying" "街道" "f" "0"
  44. "status" "character varying" "类型" "f" "0"
  45. "create_time" "timestamp without time zone" "添加时间" "f" "0"
  46. "update_time" "timestamp without time zone" "修改时间" "f" "0"
  47. */
  48. @Override
  49. protected ColumnDefinition buildColumnDefinition(Map<String, Object> rowMap) {
  50. Set<String> columnSet = rowMap.keySet();
  51. for (String columnInfo : columnSet) {
  52. rowMap.put(columnInfo.toUpperCase(), rowMap.get(columnInfo));
  53. }
  54. ColumnDefinition columnDefinition = new ColumnDefinition();
  55. columnDefinition.setColumnName(convertString(rowMap.get("COLNAME")));
  56. boolean isIdentity = "1".equals(convertString(rowMap.get("IS_IDENTITY")));
  57. columnDefinition.setIsIdentity(isIdentity);
  58. boolean isPk = (Boolean) rowMap.get("IS_PK");
  59. columnDefinition.setIsPk(isPk);
  60. String type = convertString(rowMap.get("TYPE"));
  61. // 如果是number
  62. if (StringUtils.containsIgnoreCase(type, "numeric")) {
  63. // 有精度则为decimal,否则是int
  64. Object scaleCol = rowMap.get("SCALE");
  65. if (scaleCol == null) {
  66. scaleCol = 0;
  67. }
  68. String scale = String.valueOf(scaleCol);
  69. type = "0".equals(scale) ? "integer" : "decimal";
  70. }
  71. columnDefinition.setType(SQL_TYPE_FORMATTER.format(type));
  72. columnDefinition.setComment(convertString(rowMap.get("CMT")));
  73. return columnDefinition;
  74. }
  75. }