View Javadoc

1   package org.paneris.jal.model;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   import java.sql.ResultSetMetaData;
6   import java.sql.SQLException;
7   import java.sql.Statement;
8   
9   /**
10   * A SQLMetaData object holds the jdbc sql metadta for a column (FieldMetaData).
11   */
12  
13  public class SQLMetaData extends Object {
14  
15      private DBConnectionManager  connMgr = DBConnectionManager.getInstance();
16  
17      /**
18       * Is the column automatically numbered, thus read-only?
19       */
20      boolean AutoIncrement;
21  
22      /**
23       * Does a column's case matter?
24       */
25      boolean CaseSensitive;
26  
27      /**
28       * Can the column be used in a where clause?
29       */
30      boolean Searchable;
31  
32      /**
33       * Is the column a cash value?
34       */
35      boolean Currency;
36  
37      /**
38       * Can you put a NULL in this column?
39       */
40      int Nullable;
41  
42      /**
43       * Is the column a signed number?
44       */
45      boolean Signed;
46  
47      /**
48       * What's the column's normal max width in chars?
49       */
50      int ColumnDisplaySize;
51  
52      /**
53       * What's the suggested column title for use in printouts and
54       * displays?
55       */
56      String ColumnLabel;
57  
58      /**
59       * What's a column's name?
60       */
61      String ColumnName;
62  
63      /**
64       * What's a column's table's schema?
65       */
66      String SchemaName;
67  
68      /**
69       * What's a column's number of decimal digits?
70       */
71      int Precision;
72  
73      /**
74       * What's a column's number of digits to right of the decimal point?
75       */
76        int Scale;
77  
78      /**
79       * What's a column's SQL type?
80       * @see java.sql.Types
81       */
82        public int ColumnType;
83  
84      /**
85       * What's a column's data source specific type name?
86       */
87        public String ColumnTypeName;
88  
89      /**
90       * Is a column definitely not writable?
91       */
92        boolean ReadOnly;
93  
94      /**
95       * Is it possible for a write on the column to succeed?
96       */
97        boolean Writable;
98  
99      /**
100      * Will a write on the column definitely succeed?
101      */
102       boolean DefinitelyWritable;
103 
104     /**
105      * constructor takes a resultset and a column number
106      */
107     public SQLMetaData(String db, Integer fn) {
108         Connection conn = connMgr.getConnection("SQLMetaData",db);
109         String sqlString = "";
110         try {
111             Statement s = conn.createStatement();
112             sqlString = "SELECT datadictionary.fieldname, datadictionarytables.tablename FROM datadictionary, datadictionarytables WHERE datadictionary.tablename = datadictionarytables.id and datadictionary.id = " + fn;
113             ResultSet rs = s.executeQuery(sqlString);
114             rs.next();
115             try {
116                 String fieldname = rs.getString(1);
117                 String tablename = rs.getString(2);
118                 try {
119                     sqlString = "SELECT " + fieldname + " FROM " + tablename + " WHERE id=0";
120                     rs = s.executeQuery(sqlString);
121                     ResultSetMetaData md = rs.getMetaData();
122                     try {
123                         AutoIncrement = md.isAutoIncrement(1);
124                         CaseSensitive = md.isCaseSensitive(1);
125                         Searchable = md.isSearchable(1);
126                         Currency = md.isCurrency(1);
127                         Nullable = md.isNullable(1);
128                         Signed = md.isSigned(1);
129                         ColumnDisplaySize = md.getColumnDisplaySize(1);
130                         ColumnLabel = md.getColumnLabel(1);
131                         ColumnName = md.getColumnName(1);
132                         SchemaName  = md.getSchemaName(1);
133                         try {
134                            Precision = md.getPrecision(1);
135                         } catch (SQLException e) {
136                            Precision = 0;
137                         }
138                         try {
139                            Scale = md.getScale(1);
140                         } catch (SQLException e) {
141                            Scale = 0;
142                         }
143                     ColumnType = md.getColumnType(1);
144                         ColumnTypeName = md.getColumnTypeName(1);
145                     ReadOnly = md.isReadOnly(1);
146                         Writable = md.isWritable(1);
147                     DefinitelyWritable = md.isDefinitelyWritable(1);
148                     } catch (Exception e) {
149                         System.err.println(e.toString());
150                     }
151                 } catch (Exception e) {
152                     System.err.println("failed get resultsetmetadata, sqlString: " + sqlString + "  Error: " + e.toString());
153                 }
154             } catch (Exception e) {
155                 System.err.println("failed get fieldname / tablename, sqlString: " + sqlString + "  Error: " + e.toString());
156             }
157         } catch (Exception e) {
158             System.err.println("failed to create results set, sqlString: " + sqlString + "  Error: " + e.toString());
159         }
160         connMgr.freeConnection(db, conn);
161     }
162 }
163