added "new PHP project" shortcut to PHP perspective
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / sql / SQLMetaDataResults.java
1 package com.quantum.sql;
2
3 import java.sql.ResultSet;
4 import java.sql.ResultSetMetaData;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Vector;
9
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.Entity;
12
13
14 /**
15  * @author BC
16  */
17 public class SQLMetaDataResults extends SQLResultSetResults {
18
19         /**
20          * @param query
21          */
22         private SQLMetaDataResults(String query, Bookmark bookmark, Entity entity) {
23                 super(query, bookmark, entity);
24         }
25
26         static SQLResultSetResults create(Bookmark bookmark, ResultSet set, String query, Entity entity) throws SQLException {
27
28                 SQLMetaDataResults results = new SQLMetaDataResults(query, bookmark, entity);
29                 results.parseResultSet(set);
30                 
31                 return results;
32         }
33         
34         /**
35          * @param set
36          * @param results
37          * @param metaData
38          * @param columnCount
39          * @throws SQLException
40          */
41         protected void parseResultSet(ResultSet set) throws SQLException {
42
43                 List columns = new ArrayList();
44                 columns.add(new Column("ColumnName", "", 0)); //$NON-NLS-1$
45                 columns.add(new Column("Type Name", "", 0)); //$NON-NLS-1$
46                 columns.add(new Column("Size", "", 0)); //$NON-NLS-1$
47                 columns.add(new Column("Scale", "", 0)); //$NON-NLS-1$
48                 columns.add(new Column("Nullable", "", 0)); //$NON-NLS-1$
49                 columns.add(new Column("AutoIncrement", "", 0)); //$NON-NLS-1$
50                 columns.add(new Column("Type", "", 0)); //$NON-NLS-1$
51                 setColumns((Column[]) columns.toArray(new Column[columns.size()]));
52                 
53                 ResultSetMetaData metaData = set.getMetaData();
54                 
55                 List rowList = new ArrayList();
56                 for (int i = 1; i <= metaData.getColumnCount(); i++) {
57                         Vector row = new Vector();
58                         row.addElement(metaData.getColumnName(i));
59                         row.addElement(metaData.getColumnTypeName(i));
60                         long precision = 0;
61                         try {
62                                 precision = metaData.getPrecision(i);
63             } catch (Throwable t) {
64                 // Do nothing. An exception can be generated by some very large row sizes, like BLOB in Oracle.
65                 // Then getPrecision() will generate an exception. So we ignore it and go on.
66             }
67                         if (precision == 0) {
68                                 precision = metaData.getColumnDisplaySize(i);
69                         }
70                         int scale = metaData.getScale(i);
71                         
72                         row.addElement(new Long(precision));
73                         row.addElement(new Integer(scale));
74                         
75                         int nullable = metaData.isNullable(i);
76                         if (nullable == ResultSetMetaData.columnNoNulls) {
77                                 row.addElement("Not Null"); //$NON-NLS-1$
78                         } else if (nullable == ResultSetMetaData.columnNullable) {
79                                 row.addElement("Nullable"); //$NON-NLS-1$
80                         } else if (
81                                 nullable == ResultSetMetaData.columnNullableUnknown) {
82                                 row.addElement("Nullable"); //$NON-NLS-1$
83                         } else {
84                                 row.addElement("<Error>"); //$NON-NLS-1$
85                         }
86                         row.addElement(
87                                 (metaData.isAutoIncrement(i)
88                                         ? Boolean.TRUE
89                                         : Boolean.FALSE)
90                                         .toString());
91                         rowList.add(new Row(row));
92                         row.addElement(new Integer(metaData.getColumnType(i)));
93                 }
94                 setRows((Row[]) rowList.toArray(new Row[rowList.size()]));
95         }
96
97         public boolean isMetaData() {
98                 return true;
99         }
100 }