Table.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.xjrsoft.common.mybatis;
  2. import cn.hutool.db.meta.Column;
  3. import java.io.Serializable;
  4. import java.util.*;
  5. /**
  6. * 数据库表信息
  7. *
  8. * @author loolly
  9. */
  10. public class Table implements Serializable, Cloneable {
  11. /**
  12. * table所在的schema
  13. */
  14. private String schema;
  15. /**
  16. * tables所在的catalog
  17. */
  18. private String catalog;
  19. /**
  20. * 表名
  21. */
  22. private String tableName;
  23. /**
  24. * 注释
  25. */
  26. private String comment;
  27. /**
  28. * 主键字段名列表
  29. */
  30. private Set<String> pkNames = new LinkedHashSet<>();
  31. private final Map<String, Column> columns = new LinkedHashMap<>();
  32. public static Table create(String tableName) {
  33. return new Table(tableName);
  34. }
  35. // ----------------------------------------------------- Constructor start
  36. /**
  37. * 构造
  38. *
  39. * @param tableName 表名
  40. */
  41. public Table(String tableName) {
  42. this.setTableName(tableName);
  43. }
  44. // ----------------------------------------------------- Constructor end
  45. // ----------------------------------------------------- Getters and Setters start
  46. /**
  47. * 获取 schema
  48. *
  49. * @return schema
  50. * @since 5.4.3
  51. */
  52. public String getSchema() {
  53. return schema;
  54. }
  55. /**
  56. * 设置schema
  57. *
  58. * @param schema schema
  59. * @return this
  60. * @since 5.4.3
  61. */
  62. public Table setSchema(String schema) {
  63. this.schema = schema;
  64. return this;
  65. }
  66. /**
  67. * 获取catalog
  68. *
  69. * @return catalog
  70. * @since 5.4.3
  71. */
  72. public String getCatalog() {
  73. return catalog;
  74. }
  75. /**
  76. * 设置catalog
  77. *
  78. * @param catalog catalog
  79. * @return this
  80. * @since 5.4.3
  81. */
  82. public Table setCatalog(String catalog) {
  83. this.catalog = catalog;
  84. return this;
  85. }
  86. /**
  87. * 获取表名
  88. *
  89. * @return 表名
  90. */
  91. public String getTableName() {
  92. return tableName;
  93. }
  94. /**
  95. * 设置表名
  96. *
  97. * @param tableName 表名
  98. */
  99. public void setTableName(String tableName) {
  100. this.tableName = tableName;
  101. }
  102. /**
  103. * 获取注释
  104. *
  105. * @return 注释
  106. */
  107. public String getComment() {
  108. return comment;
  109. }
  110. /**
  111. * 设置注释
  112. *
  113. * @param comment 注释
  114. * @return this
  115. */
  116. public Table setComment(String comment) {
  117. this.comment = comment;
  118. return this;
  119. }
  120. /**
  121. * 获取主键列表
  122. *
  123. * @return 主键列表
  124. */
  125. public Set<String> getPkNames() {
  126. return pkNames;
  127. }
  128. /**
  129. * 给定列名是否为主键
  130. *
  131. * @param columnName 列名
  132. * @return 是否为主键
  133. * @since 5.4.3
  134. */
  135. public boolean isPk(String columnName) {
  136. return getPkNames().contains(columnName);
  137. }
  138. /**
  139. * 设置主键列表
  140. *
  141. * @param pkNames 主键列表
  142. */
  143. public void setPkNames(Set<String> pkNames) {
  144. this.pkNames = pkNames;
  145. }
  146. // ----------------------------------------------------- Getters and Setters end
  147. /**
  148. * 设置列对象
  149. *
  150. * @param column 列对象
  151. * @return 自己
  152. */
  153. public Table setColumn(Column column) {
  154. this.columns.put(column.getName(), column);
  155. return this;
  156. }
  157. /**
  158. * 获取某列信息
  159. *
  160. * @param name 列名
  161. * @return 列对象
  162. * @since 4.2.2
  163. */
  164. public Column getColumn(String name) {
  165. return this.columns.get(name);
  166. }
  167. /**
  168. * 获取所有字段元信息
  169. *
  170. * @return 字段元信息集合
  171. * @since 4.5.8
  172. */
  173. public Collection<Column> getColumns() {
  174. return this.columns.values();
  175. }
  176. /**
  177. * 添加主键
  178. *
  179. * @param pkColumnName 主键的列名
  180. * @return 自己
  181. */
  182. public Table addPk(String pkColumnName) {
  183. this.pkNames.add(pkColumnName);
  184. return this;
  185. }
  186. }