5 package com.quantum.sql.metadata;
7 import java.util.Vector;
9 import com.quantum.util.StringMatrix;
13 * Class to hold the Metadata of a database element
16 public class ObjectMetaData {
17 private StringMatrix columns = new StringMatrix();
18 private StringMatrix primaryKeys = new StringMatrix();
19 private StringMatrix foreignKeys = new StringMatrix();
20 private StringMatrix indexInfo = new StringMatrix();
21 private StringMatrix bestRowId = new StringMatrix();
25 * Gives the order of the column in the primary key
27 * @return the order of the column in the primary key, 0 if it's not part of it.
29 public int getPrimaryKeyOrder(String column){
30 if (primaryKeys.size() == 0) return 0;
31 StringMatrix keyColumns = primaryKeys.select("COLUMN_NAME", column); //$NON-NLS-1$
32 if (keyColumns != null && keyColumns.size() > 0) {
33 String index = keyColumns.get("KEY_SEQ", 0); // We suppose there is only a primary key //$NON-NLS-1$
34 if (index != null ) return Integer.parseInt(index);
40 * Gives the type of the column
42 * @return the type of the column using the values defined in java.sql.Types
44 public int getColumnType(String column){
45 StringMatrix selectCol = columns.select("COLUMN_NAME", column); //$NON-NLS-1$
46 if (selectCol != null && selectCol.size() > 0) {
47 String type = selectCol.get("DATA_TYPE", 0); // It should be only one column //$NON-NLS-1$
48 if (type != null ) return Integer.parseInt(type);
55 * Returns a String with the names of the columns, separated by commas
57 public String getColumnsString() {
58 String result = ""; //$NON-NLS-1$
59 Vector columnNames = columns.getColumn("COLUMN_NAME"); //$NON-NLS-1$
60 for (int i = 0; i < columnNames.size(); i++) {
61 if (i > 0) result += ", "; //$NON-NLS-1$
62 result += (String) columnNames.get(i);
68 * Returns a vector of Strings with the names of the columns
70 public Vector getColumnNamesVector() {
71 Vector result = new Vector();
72 Vector columnNames = columns.getColumn("COLUMN_NAME"); //$NON-NLS-1$
73 for (int i = 0; i < columnNames.size(); i++) {
74 result.add((String) columnNames.get(i));
79 // Inmediate getters and setters
84 public void setColumns(StringMatrix matrix) {
90 public void setForeignKeys(StringMatrix matrix) {
97 public void setPrimaryKeys(StringMatrix matrix) {
103 public void setBestRowId(StringMatrix matrix) {
110 public void setIndexInfo(StringMatrix matrix) {
117 public StringMatrix getColumns() {
124 public StringMatrix getForeignKeys() {
131 public StringMatrix getPrimaryKeys() {
139 public StringMatrix getIndexInfo() {
145 public StringMatrix getBestRowId() {
153 public void dropColumn(String columnName) {
154 columns.dropMatching("COLUMN_NAME", columnName); //$NON-NLS-1$
157 // Common Object interface
160 * @see java.lang.Object#clone()
162 public Object clone() {
163 ObjectMetaData result = new ObjectMetaData();
164 result.columns = (StringMatrix) columns.clone();
165 result.primaryKeys = (StringMatrix) primaryKeys.clone();
166 result.indexInfo = (StringMatrix) indexInfo.clone();
167 result.foreignKeys = (StringMatrix) foreignKeys.clone();
172 * @see java.lang.Object#equals(java.lang.Object)
174 public boolean equals(Object obj) {
175 if (!(obj instanceof ObjectMetaData)) return false;
176 ObjectMetaData obMd = (ObjectMetaData) obj;
177 return (columns.equals(obMd.columns) &&
178 primaryKeys.equals(obMd.primaryKeys) &&
179 indexInfo.equals(obMd.indexInfo) &&
180 foreignKeys.equals(obMd.foreignKeys) );
184 * @see java.lang.Object#toString()
186 public String toString() {
187 return ( columns.toString() +
188 foreignKeys.toString() +
189 indexInfo.toString() +
190 primaryKeys.toString());
193 public String getQualifiedTableName(){
194 if (columns.size() < 1) return "";
195 String result = columns.get("TABLE_NAME", 0);
196 String schema = columns.get("TABLE_SCHEM", 0);
197 if (schema != null && schema.length() > 0)
198 result = schema + "." + result;