5 package net.sourceforge.phpdt.sql.view.bookmark;
7 import java.net.MalformedURLException;
9 import java.util.Vector;
11 import org.eclipse.jface.resource.ImageDescriptor;
12 import org.eclipse.swt.graphics.Image;
14 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
15 import net.sourceforge.phpdt.sql.sql.SQLHelper;
16 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
17 import net.sourceforge.phpdt.sql.sql.metadata.StringMatrix;
21 * Class that holds the MetaData for a Column in a table or view.
22 * primaryKeyOrder holds value 0 or the sequence number of the column in the primary key.
24 public class ColumnMetaData implements TreeNode {
26 StringMatrix matrix = null;
27 TreeNode parent = null;
28 int primaryKeyOrder = 0;
30 public ColumnMetaData( StringMatrix metaData , TreeNode parent) {
31 this.matrix = metaData;
36 * @return the display size of the column
38 public int getDisplaySize() {
39 String result = matrix.get("COLUMN_SIZE", 0); //$NON-NLS-1$
40 if (result == null) return 0;
41 else return Integer.parseInt(result);
44 // Is a node that has no children
45 public Object[] getChildren() {
48 public boolean hasChildren() {
54 * @return if the column is nullable (can have nulls). Values as constants in ResultSetMetaData
56 public boolean getIsNullable() {
57 String result = matrix.get("IS_NULLABLE", 0); //$NON-NLS-1$
58 if (result != null && result == "YES") return true; //$NON-NLS-1$
63 * @return the order of the column in the primary key of the table
64 * (0 is is not part of it)
66 public int getPrimaryKeyOrder() {
67 return primaryKeyOrder;
70 * @param i : The order of the column in the primary key of the table
71 * (0 if not part of it)
73 public void setPrimaryKeyOrder(int i) {
78 * @return the name of the column
80 public String getName() {
81 return matrix.get("COLUMN_NAME", 0); //$NON-NLS-1$
85 * @return the precision of the column (digits left of the digital point)
87 public int getPrecision() {
88 return getDisplaySize();
92 * @return the scale of the column (digits right of the digital point)
94 public int getScale() {
95 String result = matrix.get("DECIMAL_DIGITS", 0); //$NON-NLS-1$
96 if (result == null) return 0;
97 else return Integer.parseInt(result);
101 * @return the type of the column (as a string)
103 public String getTypeName() {
104 return matrix.get("TYPE_NAME", 0); //$NON-NLS-1$
110 * @see java.lang.Object#toString()
112 public String toString() {
113 String sDesc = getName() + " " + getTypeName(); //$NON-NLS-1$
116 if (getPrecision() > 0 || getScale() > 0) sDesc += "(" + Integer.toString(getPrecision()); //$NON-NLS-1$
117 if (getScale() > 0) sDesc += "," + Integer.toString(getScale()); //$NON-NLS-1$
118 if (getPrecision() > 0 || getScale() > 0) sDesc += ")"; //$NON-NLS-1$
121 if (getDisplaySize() > 0) sDesc += "(" + Integer.toString(getDisplaySize()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
127 * @return an Image object to appear in the view
128 * @throws MalformedURLException
130 public Image getImage() throws MalformedURLException {
131 // We'll return an icon if it's part of the primary key
132 if (getPrimaryKeyOrder() > 0) {
133 URL installURL = PHPEclipseSQLPlugin.getDefault().getDescriptor().getInstallURL();
134 URL url = new URL(installURL, "icons/key.gif"); //$NON-NLS-1$
135 ImageDescriptor descriptor = ImageDescriptor.createFromURL(url);
136 return descriptor.createImage();
140 public boolean isReal() {
141 int type = getType();
142 return SQLHelper.isReal(type);
145 public boolean isNumeric() {
146 int type = getType();
147 return SQLHelper.isNumeric(type);
151 * @return the type of the column, as a numeric constant definde in java.sql.
153 public int getType() {
154 String result = matrix.get("DATA_TYPE", 0); //$NON-NLS-1$
156 if (result != null) type = Integer.parseInt(result);
160 public String getTable(){
161 return matrix.get("TABLE_NAME", 0); //$NON-NLS-1$
167 public Object getParent() {
174 public void setParent(TreeNode node) {
179 * @see net.sourceforge.phpdt.view.bookmark.TreeNode#getMetaData()
181 public ObjectMetaData getMetaData() {
185 * @return a Vector of ColumnMetaData objects representing all the columns of
188 public static Vector getColumnsMetaData(ObjectMetaData metadata, TreeNode node)
190 StringMatrix columns = metadata.getColumns();
191 Vector ret = new Vector(columns.size(),1);
192 for (int i = 0; i< columns.size(); i++){
193 ColumnMetaData columnMD = new ColumnMetaData(columns.rowMatrix(i), node);
194 columnMD.setPrimaryKeyOrder(metadata.getPrimaryKeyOrder(columnMD.getName()));