1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / bookmark / ColumnMetaData.java
1 /*
2  * Created on 2/04/2003
3  *
4  */
5 package net.sourceforge.phpdt.sql.view.bookmark;
6
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.Vector;
10
11 import org.eclipse.jface.resource.ImageDescriptor;
12 import org.eclipse.swt.graphics.Image;
13
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;
18
19 /**
20  * 
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.
23  */
24 public class ColumnMetaData implements TreeNode {
25         
26         StringMatrix matrix = null;
27         TreeNode parent = null;
28         int primaryKeyOrder = 0;
29
30         public ColumnMetaData( StringMatrix metaData , TreeNode parent) {
31                         this.matrix = metaData;
32                         this.parent = parent;
33                 }
34
35         /**
36          * @return the display size of the column
37          */
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);
42         }
43
44         // Is a node that has no children
45         public Object[] getChildren() {
46                 return null;
47         }
48         public boolean hasChildren() {
49                 return false;
50         }
51         
52
53         /**
54          * @return if the column is nullable (can have nulls). Values as constants in ResultSetMetaData
55          */
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$
59                 else return false;
60         }
61
62         /**
63          * @return the order of the column in the primary key of the table 
64          * (0 is is not part of it)
65          */
66         public int getPrimaryKeyOrder() {
67                 return primaryKeyOrder;
68         }
69         /**
70          * @param i : The order of the column in the primary key of the table
71          * (0 if not part of it)
72          */
73         public void setPrimaryKeyOrder(int i) {
74                 primaryKeyOrder = i;
75         }
76
77         /**
78          * @return the name of the column
79          */
80         public String getName() {
81                 return matrix.get("COLUMN_NAME", 0); //$NON-NLS-1$
82         }
83
84         /**
85          * @return the precision of the column (digits left of the digital point)
86          */
87         public int getPrecision() {
88                 return getDisplaySize();
89         }
90
91         /**
92          * @return the scale of the column (digits right of the digital point)
93          */
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);
98         }
99
100         /**
101          * @return the type of the column (as a string)
102          */
103         public String getTypeName() {
104                 return matrix.get("TYPE_NAME", 0); //$NON-NLS-1$
105         }
106
107
108
109         /* (non-Javadoc)
110          * @see java.lang.Object#toString()
111          */
112         public String toString() {
113                 String sDesc =  getName() + " " + getTypeName(); //$NON-NLS-1$
114                 if (isNumeric())
115                 {
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$
119                 } else
120                 {
121                         if (getDisplaySize() > 0) sDesc += "(" + Integer.toString(getDisplaySize()) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
122                 }
123                 return sDesc;
124         }
125
126         /**
127          * @return an Image object to appear in the view
128          * @throws MalformedURLException
129          */
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();
137                 } else
138                         return null;
139         }
140         public boolean isReal() {
141                 int type = getType();
142                 return SQLHelper.isReal(type);
143         }
144
145         public boolean isNumeric() {
146                 int type = getType();
147                 return SQLHelper.isNumeric(type);
148         }
149
150         /**
151          * @return the type of the column, as a numeric constant definde in java.sql.
152          */
153         public int getType() {
154                 String result = matrix.get("DATA_TYPE", 0); //$NON-NLS-1$
155                 int type = -1;
156                 if (result != null)  type = Integer.parseInt(result);
157                 return type;
158         }
159
160         public String getTable(){
161                 return matrix.get("TABLE_NAME", 0); //$NON-NLS-1$
162         }
163
164         /**
165          * @return
166          */
167         public Object getParent() {
168                 return parent;
169         }
170
171         /**
172          * @param node
173          */
174         public void setParent(TreeNode node) {
175                 parent = node;
176         }
177
178         /* (non-Javadoc)
179          * @see net.sourceforge.phpdt.view.bookmark.TreeNode#getMetaData()
180          */
181         public ObjectMetaData getMetaData() {
182                 return null;
183         }
184         /**
185          * @return a Vector of ColumnMetaData objects representing all the columns of
186          * the Object. 
187          */
188         public static Vector getColumnsMetaData(ObjectMetaData metadata, TreeNode node)
189         {
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()));
195                         ret.add(columnMD);              
196                 }
197                 
198                 return ret;
199                 
200         }
201
202 }