1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove

File ColumnMeta.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
80% of files have more coverage

Code metrics

12
17
3
2
159
62
9
0.53
5.67
1.5
3

Classes

Class Line # Actions
ColumnMeta 48 17 0% 9 15
0.5312553.1%
ColumnMeta.Property 59 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 128 tests. .

Source view

1    package guru.mikelue.foxglove;
2   
3    import java.sql.JDBCType;
4    import java.util.EnumSet;
5    import java.sql.DatabaseMetaData;
6   
7    import org.apache.commons.lang3.builder.EqualsBuilder;
8    import org.apache.commons.lang3.builder.ToStringBuilder;
9    import org.apache.commons.lang3.builder.ToStringStyle;
10   
11    import static java.util.stream.Collectors.joining;
12   
13    /**
14    * Metadata of a column.
15    *
16    * <p>The information could come from:
17    *
18    * <ul>
19    * <li>{@link DatabaseMetaData#getColumns(String, String, String, String)}</li>
20    * <li>{@link java.sql.ResultSet#getMetaData()}</li>
21    * </ul>
22    *
23    * <hr>
24    *
25    * <h2>From {@link DatabaseMetaData#getColumns(String, String, String, String)}</h2>
26    *
27    * The columns mapping to this object are:
28    *
29    * <ul>
30    * <li><code>COLUMN_NAME</code> - for {@link #name()}</li>
31    * <li><code>TYPE_NAME</code> - for {@link #typeName()}</li>
32    * <li><code>DATA_TYPE</code> - for {@link #jdbcType()}</li>
33    * <li><code>NULLABLE</code> - for {@link Property#NULLABLE}</li>
34    * <li><code>COLUMN_DEF</code> - for {@link Property#DEFAULT_VALUE}</li>
35    * <li><code>IS_AUTOINCREMENT</code> - for {@link Property#AUTO_INCREMENT}</li>
36    * <li><code>IS_GENERATEDCOLUMN</code> - for {@link Property#GENERATED}</li>
37    * <li><code>COLUMN_SIZE</code> - for {@link #size()}</li>
38    * <li><code>DECIMAL_DIGITS</code> - for {@link #decimalDigits()}</li>
39    * </ul>
40    *
41    * @param name The name of the column
42    * @param properties The true/false properties of this column
43    * @param typeName The name of type for the column
44    * @param jdbcType The corresponding {@link JDBCType} of the column
45    * @param size The size of the column
46    * @param decimalDigits The number of decimal digits of the column(as scale of SQL standard)
47    */
 
48    public record ColumnMeta(
49    String name,
50    EnumSet<Property> properties,
51    String typeName, JDBCType jdbcType,
52    int size, int decimalDigits
53    ) {
54    /**
55    * The properties used for auto-generating of a column.
56    *
57    * This type is used with {@link EnumSet} usually.
58    */
 
59    public enum Property {
60    /**
61    * Indicates whether the column allows null value.
62    *
63    * This value corresponds to NULLABLE defined by {@link java.sql.DatabaseMetaData#getColumns(String, String, String, String)}.
64    *
65    * For "columnNullable" or "unknown" value of NULLABLE, this value is put into the set.
66    */
67    NULLABLE,
68    /**
69    * Indicates whether the column has default value.
70    *
71    * This value corresponds to COLUMN_DEF defined by {@link java.sql.DatabaseMetaData#getColumns(String, String, String, String)}.
72    *
73    * For not-null value of COLUMN_DEF, this value is put into the set.
74    */
75    DEFAULT_VALUE,
76    /**
77    * Indicates whether the column is auto-increment.
78    *
79    * This value corresponds to IS_AUTOINCREMENT defined by {@link java.sql.DatabaseMetaData#getColumns(String, String, String, String)}.
80    *
81    * For "YES" value of IS_AUTOINCREMENT, this value is put into the set.
82    */
83    AUTO_INCREMENT,
84    /**
85    * Indicates whether the column is generated by database.
86    *
87    * This value corresponds to IS_GENERATEDCOLUMN defined by {@link java.sql.DatabaseMetaData#getColumns(String, String, String, String)}.
88    *
89    * For "YES" value of IS_GENERATEDCOLUMN, this value is put into the set.
90    */
91    GENERATED,
92    }
93   
94    /**
95    * Only use name of column for hash code.
96    *
97    * @return Hash code of column name
98    */
 
99  52238 toggle @Override
100    public int hashCode()
101    {
102  52238 return name.hashCode();
103    }
104   
105    /**
106    * Only use name and {@link JDBCType} of column for equality check.
107    *
108    * @param obj The other object to be checked
109    *
110    * @return True if the other object is a {@link ColumnMeta} and has same name
111    */
 
112  0 toggle @Override
113    public boolean equals(Object obj)
114    {
115  0 if (obj == null) { return false; }
116  0 if (obj == this) { return true; }
117  0 if (obj.getClass() != getClass()) {
118  0 return false;
119    }
120   
121  0 ColumnMeta rhs = (ColumnMeta) obj;
122  0 return new EqualsBuilder()
123    .append(name(), rhs.name())
124    .isEquals();
125    }
126   
127    /**
128    * Builds complete properties and descriptive information of column metadata.
129    *
130    * @return The string representation of this column
131    */
 
132  815 toggle @Override
133    public String toString()
134    {
135  815 var builder = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
136    .append("name", '"' + name + '"')
137    .append(" JDBC<" + jdbcType + ">")
138    .append(" (" + typeName + ")");
139   
140  815 if (size > 0) {
141  804 builder.append(" #" + size);
142    }
143   
144  815 if (decimalDigits > 0) {
145  258 builder.append(" .[" + decimalDigits + "]");
146    }
147   
148  815 if (!properties.isEmpty()) {
149  439 builder.append(
150    " " +
151    properties.stream()
152    .map(p -> "<" + p.name() + ">")
153    .collect(joining(", "))
154    );
155    }
156   
157  815 return builder.toString();
158    }
159    }