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 ImportedKeyInfo {
026:
027: /** ctor/constructor taking a connection object and a tablename.
028: */
029: public ImportedKeyInfo(Connection conn, String tableName) {
030: try {
031: DatabaseMetaData dma = conn.getMetaData();
032: ResultSet rs = dma.getImportedKeys(null, "", tableName);
033: List l = ResultSetUtil.getMappedDataString(rs);
034: rs.close();
035: setInfo(l);
036: } catch (Exception exc) {
037: System.err.println(P + " L EXC:" + exc.toString());
038: exc.printStackTrace();
039: }
040: }
041:
042: /** takes a list containing a map object for each key.
043: *
044: * It sorts the keys as per KEY_SEQ since the javadoc says its per
045: * COLUMN_NAME which sucks. Also mysql driver returns each key_seq
046: * as 0. mysql returns them as per defined order, so not a worry.
047: * To check how oracle works.
048: */
049: private void setInfo(List /*<Map>*/l) {
050: _list = l;
051: }
052:
053: /** returns keys as a comma delimited string.
054: */
055: public String getKeysAsString() {
056: if (_list == null)
057: return null;
058: String a[] = getKeysAsArray();
059: if (a.length == 1)
060: return a[0];
061: return ArrayUtil.join(a, ',');
062: }
063:
064: /** returns keys as a string array.
065: */
066: public String[] getKeysAsArray() {
067: if (_list == null)
068: return null;
069:
070: String a[] = new String[_list.size()];
071: for (int i = 0; i < _list.size(); i++) {
072: Map m = (Map) _list.get(i);
073: a[i] = (String) m.get(COLUMN_NAME);
074: }
075: return a;
076: }
077:
078: /** returns the number of columns that make this key.
079: */
080: public int getKeyCount() {
081: if (_list == null)
082: return 0;
083: return _list.size();
084: }
085:
086: /** returns a List of Map objects each containing a key.
087: * Use the names given in the javadoc as keys in the Map.
088: * COLUMN_NAME and KEY_SEQ are two of them.
089: * @see java.sql.DatabaseMetaData#getPrimaryKeys()
090: */
091: public List getInfo() {
092: return _list;
093: }
094:
095: ////// START INSTANCE VARIABLES //////
096:
097: List _list;
098: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
099: static final String P = "ImportedKeyInfo"; // used in exception strings
100: public final static String COLUMN_NAME = "COLUMN_NAME";
101: public final static String KEY_SEQ = "KEY_SEQ";
102:
103: } // end of class
|