View Javadoc
1   package net.sumaris.core.dao.technical.schema;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Core
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  
26  import java.sql.DatabaseMetaData;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.util.Objects;
30  import java.util.StringTokenizer;
31  
32  /**
33   * A database column metadata
34   */
35  public class SumarisColumnMetadata {
36  
37  	protected final String catalog;
38  	protected final String schema;
39  	protected final String table;
40  	protected final String name;
41  	protected final boolean nullable;
42  
43  	protected final String defaultValue;
44  
45  	// From JDBC meta
46  	protected final int columnSize;
47  	protected final int decimalDigits;
48  	protected final int typeCode;
49  	protected final String typeName;
50  	protected final boolean isNullable;
51  	protected final String description;
52  
53  	public SumarisColumnMetadata(ResultSet rs) throws SQLException {
54  		this(rs, null);
55  	}
56  
57  	public SumarisColumnMetadata(ResultSet rs, String defaultValue) throws SQLException {
58  		this.defaultValue = defaultValue;
59  
60  		// Add additional info from JDBC meta
61  		// (see https://docs.oracle.com/javase/8/docs/api/java/sql/DatabaseMetaData.html#getColumns-java.lang.String-java.lang.String-java.lang.String-java.lang.String)
62  		this.catalog = rs.getString("TABLE_CAT");
63  		this.schema = rs.getString("TABLE_SCHEM");
64  		this.table = rs.getString("TABLE_NAME");
65  		this.name = rs.getString("COLUMN_NAME");
66  		this.nullable = rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable;
67  		this.columnSize = rs.getInt("COLUMN_SIZE");
68  		this.decimalDigits = rs.getInt("DECIMAL_DIGITS");
69  		this.typeCode = rs.getInt("DATA_TYPE");
70  		this.isNullable = "YES".equalsIgnoreCase(rs.getString("IS_NULLABLE"));
71  		this.typeName = (new StringTokenizer(rs.getString("TYPE_NAME"), "() ")).nextToken();
72  		this.description = rs.getString("REMARKS");
73  	}
74  
75  	public int hashCode() {
76  		return this.name.hashCode();
77  	}
78  
79  	public String getName() {
80  		return this.name;
81  	}
82  
83  	public String getNullable() {
84  		return nullable ? "YES": "NO";
85  	}
86  
87  	public boolean isNullable() {
88  		return nullable;
89  	}
90  
91  	public String getTypeName() {
92  		return typeName;
93  	}
94  
95  	public int getColumnSize() {
96  		return columnSize;
97  	}
98  
99  	public int getDecimalDigits() {
100 		return decimalDigits;
101 	}
102 
103 	public int getTypeCode() {
104 		return typeCode;
105 	}
106 
107 	public String getDefaultValue() {
108 		return defaultValue;
109 	}
110 
111 	public String getDescription() {
112 		return description;
113 	}
114 
115 
116 	public String toString() {
117 		return new StringBuilder()
118 				.append(catalog).append('.')
119 				.append(schema).append('.')
120 				.append(table).append('.')
121 				.append(name)
122 				.toString();
123 	}
124 
125 	public boolean equals(Object other) {
126 
127 		if (this == other) return true;
128 		if ( !(other instanceof SumarisColumnMetadata) ) return false;
129 
130 		final SumarisColumnMetadata./../net/sumaris/core/dao/technical/schema/SumarisColumnMetadata.html#SumarisColumnMetadata">SumarisColumnMetadata bean = (SumarisColumnMetadata) other;
131 
132 		if (!Objects.equals(bean.catalog, catalog) ||
133 			!Objects.equals(bean.schema, schema) ||
134 			!Objects.equals(bean.table, table) ||
135 			!Objects.equals(bean.name, name)) return false;
136 
137 		return true;
138 	}
139 
140 
141 }