001: // FIXME CONCURRENT ACCESS TO DRIVER, URL ETC...??
002: package org.julp;
003:
004: import java.util.*;
005: import java.sql.Connection;
006:
007: ;
008: /**
009: * Retrieves and caches PrimaryKeys from DatabaseMetaData or user input
010: */
011:
012: public class PKCache implements java.io.Serializable, Cloneable {
013:
014: private PKCache() {
015: if (Boolean.getBoolean("debug-julp")
016: || Boolean.getBoolean("debug-" + getClass().getName())) {
017: setDebug(true);
018: }
019: }
020:
021: private static PKCache instance = null;
022: /* Key for each table */
023: protected Map pkCache = new HashMap();
024: protected boolean debug = false;
025: private static final String DOT = ".";
026:
027: public static PKCache getInstance() {
028: if (instance == null) {
029: instance = new PKCache();
030: }
031: return instance;
032: }
033:
034: /*
035: *This method allows to set columns for a table PrimaryKey without Database connection, etc.
036: *
037: * Make sure to use format: catalog + DOT + schema + DOT + table + DOT + column
038: * or schema + DOT + table + DOT + column
039: */
040: public synchronized void setPrimaryKey(String catalog,
041: String schema, String table, List pk) {
042: String tableId = null;
043: if (catalog != null && catalog.trim().length() != 0) {
044: tableId = catalog + DOT + schema + DOT + table;
045: } else if (schema != null && schema.trim().length() != 0) {
046: tableId = schema + DOT + table;
047: } else if (table != null && table.trim().length() != 0) {
048: tableId = table;
049: } else {
050: throw new IllegalArgumentException(
051: "setPrimaryKey(): missing all table identifiers");
052: }
053: if (pk == null || pk.size() == 0) {
054: throw new IllegalArgumentException(
055: "setPrimaryKey(): PK list is empty");
056: }
057: if (debug)
058: System.out.println("julp ============= "
059: + new java.util.Date() + " " + this .getClass()
060: + "::" + this + "::setPrimaryKey()::tableId: "
061: + tableId + " pk: " + pk);
062: pkCache.put(tableId, pk);
063: }
064:
065: public synchronized List getPrimaryKey(String catalog,
066: String schema, String table) {
067: if (debug)
068: System.out.println("julp ============= "
069: + new java.util.Date() + " " + this .getClass()
070: + "::" + this + "::getPrimaryKey()::catalog: "
071: + catalog + "::schema: " + schema + "::table: "
072: + table);
073: String tableId = null;
074: //Build table id
075: if (catalog != null && catalog.trim().length() != 0) {
076: tableId = catalog.trim() + DOT + schema.trim() + DOT
077: + table.trim();
078: } else if (schema != null && schema.trim().length() != 0) {
079: tableId = schema.trim() + DOT + table.trim();
080: } else if (table != null && table.trim().length() != 0) {
081: tableId = table.trim();
082: } else {
083: throw new IllegalArgumentException(
084: "getPrimaryKey(): All table identifiers are missing");
085: }
086: if (this .pkCache.containsKey(tableId)) {
087: return (List) this .pkCache.get(tableId); //it's already cached
088: } else {
089: return null;
090: }
091: }
092:
093: public synchronized List getPrimaryKey(Connection connection,
094: String catalog, String schema, String table) {
095: if (debug)
096: System.out.println("julp ============= "
097: + new java.util.Date() + " " + this .getClass()
098: + "::" + this + "::getPrimaryKey()::catalog: "
099: + catalog + "::schema: " + schema + "::table: "
100: + table);
101: String tableId = null;
102: //Build table id
103: if (catalog != null && catalog.trim().length() != 0) {
104: tableId = catalog.trim() + DOT + schema.trim() + DOT
105: + table.trim();
106: } else if (schema != null && schema.trim().length() != 0) {
107: tableId = schema.trim() + DOT + table.trim();
108: } else if (table != null && table.trim().length() != 0) {
109: tableId = table.trim();
110: } else {
111: throw new IllegalArgumentException(
112: "getPrimaryKey(): All table identifiers are missing");
113: }
114: List pkFields = (List) this .pkCache.get(tableId);
115: if (debug)
116: System.out.println("julp ============= "
117: + new java.util.Date() + " " + this .getClass()
118: + "::" + this + "::getPrimaryKey()::tableId: "
119: + tableId + " pkFields: " + pkFields);
120: if (pkFields != null) {
121: return pkFields; //it's already cached
122: } else {
123: pkFields = new ArrayList();
124: }
125: java.sql.ResultSet pkInfo = null;
126: try {
127: java.sql.DatabaseMetaData dbmd = connection.getMetaData();
128: if (debug)
129: System.out.println("julp ============= "
130: + new java.util.Date() + " " + this .getClass()
131: + "::" + this
132: + "::getPrimaryKey()::DatabaseMetaData: "
133: + dbmd.getDatabaseProductName() + "::catalog: "
134: + catalog + "::schema: " + schema + "::table: "
135: + table);
136: pkInfo = dbmd.getPrimaryKeys(catalog, schema, table);
137: if (debug)
138: System.out.println("julp ============= "
139: + new java.util.Date() + " " + this .getClass()
140: + "::" + this + "::getPrimaryKey()::pkInfo: "
141: + pkInfo);
142: while (pkInfo.next()) {
143: if (debug)
144: System.out.println("julp ============= "
145: + new java.util.Date() + " "
146: + this .getClass() + "::" + this
147: + "::getPrimaryKey()::pkInfo.next()");
148: String columnName = pkInfo.getString(4);
149: pkFields.add(tableId + DOT + columnName);
150: }
151: if (debug)
152: System.out.println("julp ============= "
153: + new java.util.Date() + " " + this .getClass()
154: + "::" + this + "::getPrimaryKey()::pkFields: "
155: + pkFields);
156: if (pkFields.isEmpty()) {
157: throw new java.sql.SQLException(
158: "Cannot retrieve PrimaryKey info from DatabaseMetaData");
159: }
160: } catch (java.sql.SQLException sqle) {
161: throw new RuntimeException(sqle);
162: } catch (Exception e) {
163: throw new RuntimeException(e);
164: } finally {
165: try {
166: if (pkInfo != null)
167: pkInfo.close();
168: } catch (java.sql.SQLException sqle) {
169: sqle.printStackTrace();
170: throw new RuntimeException(sqle);
171: }
172: }
173: pkCache.put(tableId, pkFields);
174: return pkFields;
175: }
176:
177: public boolean isDebug() {
178: return debug;
179: }
180:
181: public void setDebug(boolean debug) {
182: this.debug = debug;
183: }
184: }
|