/* * Created on 2/04/2003 * */ package net.sourceforge.phpdt.sql.view.bookmark; import java.net.MalformedURLException; import java.net.URL; import java.util.Vector; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin; import net.sourceforge.phpdt.sql.sql.SQLHelper; import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData; import net.sourceforge.phpdt.sql.sql.metadata.StringMatrix; /** * * Class that holds the MetaData for a Column in a table or view. * primaryKeyOrder holds value 0 or the sequence number of the column in the primary key. */ public class ColumnMetaData implements TreeNode { StringMatrix matrix = null; TreeNode parent = null; int primaryKeyOrder = 0; public ColumnMetaData( StringMatrix metaData , TreeNode parent) { this.matrix = metaData; this.parent = parent; } /** * @return the display size of the column */ public int getDisplaySize() { String result = matrix.get("COLUMN_SIZE", 0); //$NON-NLS-1$ if (result == null) return 0; else return Integer.parseInt(result); } // Is a node that has no children public Object[] getChildren() { return null; } public boolean hasChildren() { return false; } /** * @return if the column is nullable (can have nulls). Values as constants in ResultSetMetaData */ public boolean getIsNullable() { String result = matrix.get("IS_NULLABLE", 0); //$NON-NLS-1$ if (result != null && result == "YES") return true; //$NON-NLS-1$ else return false; } /** * @return the order of the column in the primary key of the table * (0 is is not part of it) */ public int getPrimaryKeyOrder() { return primaryKeyOrder; } /** * @param i : The order of the column in the primary key of the table * (0 if not part of it) */ public void setPrimaryKeyOrder(int i) { primaryKeyOrder = i; } /** * @return the name of the column */ public String getName() { return matrix.get("COLUMN_NAME", 0); //$NON-NLS-1$ } /** * @return the precision of the column (digits left of the digital point) */ public int getPrecision() { return getDisplaySize(); } /** * @return the scale of the column (digits right of the digital point) */ public int getScale() { String result = matrix.get("DECIMAL_DIGITS", 0); //$NON-NLS-1$ if (result == null) return 0; else return Integer.parseInt(result); } /** * @return the type of the column (as a string) */ public String getTypeName() { return matrix.get("TYPE_NAME", 0); //$NON-NLS-1$ } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { String sDesc = getName() + " " + getTypeName(); //$NON-NLS-1$ if (isNumeric()) { if (getPrecision() > 0 || getScale() > 0) sDesc += "(" + Integer.toString(getPrecision()); //$NON-NLS-1$ if (getScale() > 0) sDesc += "," + Integer.toString(getScale()); //$NON-NLS-1$ if (getPrecision() > 0 || getScale() > 0) sDesc += ")"; //$NON-NLS-1$ } else { if (getDisplaySize() > 0) sDesc += "(" + Integer.toString(getDisplaySize()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ } return sDesc; } /** * @return an Image object to appear in the view * @throws MalformedURLException */ public Image getImage() throws MalformedURLException { // We'll return an icon if it's part of the primary key if (getPrimaryKeyOrder() > 0) { URL installURL = PHPEclipseSQLPlugin.getDefault().getDescriptor().getInstallURL(); URL url = new URL(installURL, "icons/key.gif"); //$NON-NLS-1$ ImageDescriptor descriptor = ImageDescriptor.createFromURL(url); return descriptor.createImage(); } else return null; } public boolean isReal() { int type = getType(); return SQLHelper.isReal(type); } public boolean isNumeric() { int type = getType(); return SQLHelper.isNumeric(type); } /** * @return the type of the column, as a numeric constant definde in java.sql. */ public int getType() { String result = matrix.get("DATA_TYPE", 0); //$NON-NLS-1$ int type = -1; if (result != null) type = Integer.parseInt(result); return type; } public String getTable(){ return matrix.get("TABLE_NAME", 0); //$NON-NLS-1$ } /** * @return */ public Object getParent() { return parent; } /** * @param node */ public void setParent(TreeNode node) { parent = node; } /* (non-Javadoc) * @see net.sourceforge.phpdt.view.bookmark.TreeNode#getMetaData() */ public ObjectMetaData getMetaData() { return null; } /** * @return a Vector of ColumnMetaData objects representing all the columns of * the Object. */ public static Vector getColumnsMetaData(ObjectMetaData metadata, TreeNode node) { StringMatrix columns = metadata.getColumns(); Vector ret = new Vector(columns.size(),1); for (int i = 0; i< columns.size(); i++){ ColumnMetaData columnMD = new ColumnMetaData(columns.rowMatrix(i), node); columnMD.setPrimaryKeyOrder(metadata.getPrimaryKeyOrder(columnMD.getName())); ret.add(columnMD); } return ret; } }