View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Genjava-Core nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.jdbc;
33  
34  import java.sql.DatabaseMetaData;
35  import java.sql.Connection;
36  import java.sql.ResultSet;
37  import java.sql.ResultSetMetaData;
38  import java.sql.SQLException;
39  import java.sql.SQLWarning;
40  
41  import java.util.ArrayList;
42  import java.util.HashMap;
43  import java.util.Map;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  public final class JdbcW {
48  
49      // assumes next() has been called already
50      static Object[] resultSetToArray(ResultSet rs) throws SQLException {
51          ResultSetMetaData meta = rs.getMetaData();
52          int sz = meta.getColumnCount();
53          Object[] objs = new Object[sz];
54          for(int i=0; i<sz; i++) {
55              Object obj = rs.getObject(i+1);
56              if(rs.wasNull()) {
57                  obj = null;
58              }
59              objs[i] = obj;
60          }
61          return objs;
62      }
63  
64      static public String[] getColumnNames(ResultSet rs) throws SQLException {
65          ResultSetMetaData rsmd = rs.getMetaData();
66          int sz = rsmd.getColumnCount();
67          String[] names = new String[sz];
68          for(int i=0; i<sz; i++) {
69              names[i] = rsmd.getColumnName(i+1);
70          }
71          return names;
72      }
73  
74      static public int getColumnType(String column, Connection conn) throws SQLException {
75          DatabaseMetaData meta = conn.getMetaData();
76          String catalog = null;
77          String schema = null;
78          String table = null;
79          String columnName = null;
80          String[] args = StringUtils.split(column, ".");
81          if(args.length == 4) {
82              catalog = args[0];
83              schema = args[1];
84              table = args[2];
85              columnName = args[3];
86          } else
87          if(args.length == 3) {
88              schema = args[0];
89              table = args[1];
90              columnName = args[2];
91          } else {
92              table = args[0];
93              columnName = args[1];
94          }
95          ResultSet rs = meta.getColumns(catalog, schema, table, columnName);
96          if(rs.next()) {
97              return rs.getInt(5);
98          } else {
99              return -1;
100         }
101     }
102 
103     static public String[] getPrimaryKeys(String tablename, Connection conn) throws SQLException {
104         DatabaseMetaData meta = conn.getMetaData();
105         String catalog = null;
106         String schema = null;
107         String table = null;
108         String[] args = StringUtils.split(tablename, ".");
109         if(args.length == 3) {
110             catalog = args[0];
111             schema = args[1];
112             table = args[2];
113         } else
114         if(args.length == 2) {
115             schema = args[0];
116             table = args[1];
117         } else {
118             table = args[0];
119         }
120         ResultSet rs = meta.getPrimaryKeys(catalog, schema, table);
121         ArrayList list = new ArrayList();
122         while(rs.next()) {
123             rs.getObject(1);
124             rs.getObject(2);
125             rs.getObject(3);
126             list.add(rs.getObject(4));
127         }
128         return (String[])list.toArray(new String[0]);
129     }
130 
131     static public Object getAutoIncrement(String tablename, Connection conn, String pkey) throws SQLException {
132         DatabaseMetaData meta = conn.getMetaData();
133         ResultSet rs = meta.getPrimaryKeys(null, null, tablename);
134         while(rs.next()) {
135             rs.getObject(1);
136             rs.getObject(2);
137             rs.getObject(3);
138             String pk = rs.getString(4);
139             if(pkey.equals(pk)) {
140                 return rs.getObject(5);
141             }
142         }
143 
144         return null;
145     }
146 
147     static public Map getAutoIncrements(String tablename, Connection conn) {
148         try {
149             DatabaseMetaData meta = conn.getMetaData();
150             ResultSet rs = meta.getPrimaryKeys(null, null, tablename);
151             HashMap map = new HashMap();
152             while(rs.next()) {
153                 map.put(rs.getObject(4), rs.getObject(5));
154             }
155             return map;
156         } catch(SQLException sqle) {
157             sqle.printStackTrace();
158             return null;
159         }
160     }
161 
162     static public void printWarnings(SQLWarning warning) {
163         if(warning == null) {
164             return;
165         }
166         System.err.println( formatWarnings(warning) );
167     }
168 
169     static public String formatWarnings(SQLWarning warning) {
170         StringBuffer buffer = new StringBuffer();
171         while(warning != null) {
172             buffer.append("SQLWarning: ");
173             buffer.append( warning.getMessage() );
174             buffer.append("\nSQL State: ");
175             buffer.append( warning.getSQLState( ));
176             buffer.append("\nErrorCode: ");
177             buffer.append( warning.getErrorCode( ));
178             buffer.append("\n");
179             warning = warning.getNextWarning();
180         }
181         return buffer.toString();
182     }
183 
184 }
185