001: /*
002: * $Author$
003: * $Id$
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package db;
013:
014: import java.util.*;
015: import util.*;
016: import java.sql.*;
017:
018: /**
019: * Creates an object containing a tables primay key info, in a more
020: * usable manner.
021: * @author rahul kumar <rahul_kumar@yahoo.com>
022: * @see XXX
023: */
024:
025: public class PrimaryKeyInfo {
026:
027: /** ctor/constructor taking a connection object and a tablename.
028: */
029: public PrimaryKeyInfo(Connection conn, String tableName) {
030: try {
031: DatabaseMetaData dma = conn.getMetaData();
032: ResultSet rs = dma.getPrimaryKeys(null, "", tableName);
033: ResultSetMetaData rsmd = rs.getMetaData();
034: int numCols = rsmd.getColumnCount();
035:
036: String label, value;
037:
038: List /*<Map>*/l = new ArrayList();
039: int i;
040: while (rs.next()) {
041: Map /*<String>,<String> */map = new LinkedHashMap(
042: numCols);
043: for (i = 1; i <= numCols; i++) {
044: label = rsmd.getColumnLabel(i).toString();
045: value = rs.getObject(i).toString();
046: map.put(label, value);
047: System.out.println(" putting " + label + ":"
048: + value);
049: }
050: l.add(map);
051: }
052: rs.close();
053: setInfo(l);
054: } catch (Exception exc) {
055: System.err.println(P + " L EXC:" + exc.toString());
056: exc.printStackTrace();
057: }
058: }
059:
060: /** takes a list containing a map object for each key.
061: *
062: * It sorts the keys as per KEY_SEQ since the javadoc says its per
063: * COLUMN_NAME which sucks. Also mysql driver returns each key_seq
064: * as 0. mysql returns them as per defined order, so not a worry.
065: * To check how oracle works.
066: */
067: private void setInfo(List /*<Map>*/l) {
068: _list = l;
069: Comparator c = new Comparator() {
070: public int compare(Object a, Object b) {
071: int ai = Integer.parseInt((String) ((Map) a)
072: .get(KEY_SEQ));
073: int bi = Integer.parseInt((String) ((Map) b)
074: .get(KEY_SEQ));
075: return ai - bi;
076: }
077: };
078: Collections.sort(_list, c);
079: }
080:
081: /** returns keys as a comma delimited string.
082: */
083: public String getKeysAsString() {
084: if (_list == null)
085: return null;
086: String a[] = getKeysAsArray();
087: if (a.length == 1)
088: return a[0];
089: return ArrayUtil.join(a, ',');
090: }
091:
092: /** returns keys as a string array.
093: */
094: public String[] getKeysAsArray() {
095: if (_list == null)
096: return null;
097:
098: String a[] = new String[_list.size()];
099: for (int i = 0; i < _list.size(); i++) {
100: Map m = (Map) _list.get(i);
101: a[i] = (String) m.get(COLUMN_NAME);
102: }
103: return a;
104: }
105:
106: /** returns the number of columns that make this key.
107: */
108: public int getKeyCount() {
109: if (_list == null)
110: return 0;
111: return _list.size();
112: }
113:
114: /** returns a List of Map objects each containing a key.
115: * Use the names given in the javadoc as keys in the Map.
116: * COLUMN_NAME and KEY_SEQ are two of them.
117: * @see java.sql.DatabaseMetaData#getPrimaryKeys()
118: */
119: public List getInfo() {
120: return _list;
121: }
122:
123: ////// START INSTANCE VARIABLES //////
124:
125: List _list;
126: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
127: static final String P = "PrimaryKeyInfo"; // used in exception strings
128: public final static String COLUMN_NAME = "COLUMN_NAME";
129: public final static String KEY_SEQ = "KEY_SEQ";
130:
131: } // end of class
|