📄 tabletowatchinfo.java
字号:
if (this.replKey > -1L) { if (!isFirst) buf.append(VAL_SEP); buf.append(SEQUENCE_KEY).append("=").append(this.replKey); } return buf.toString(); } /** * Parses the value and fills the object appropriately. The * syntax to be parsed is of the kind: <br/> * action=IDU,trigger=TRIGGER_NAME,sequence=100 * Defaults are action=IDU, triggerName=null (will be * assigned by the application, sequenceNr=-1, will be assigned * by the application. * @param val * @throws Exception */ private final void parseValue(String val) throws Exception { if (val == null || val.trim().length() < 1) { this.actions = "IDU"; this.trigger = null; this.replKey = -1L; return; } Map map = StringPairTokenizer.parseToStringStringPairs(val.trim(), ",", "="); this.actions = (String)map.get(ACTION_KEY); if (this.actions == null) { if (map.containsKey(ACTION_KEY)) this.actions = ""; else this.actions = "IDU"; } this.trigger = (String)map.get(TRIGGER_KEY); String tmp = (String)map.get(SEQUENCE_KEY); if (tmp == null || tmp.trim().length() < 1) this.replKey = -1; else { try { this.replKey = Long.parseLong(tmp.trim()); } catch (Throwable ex) { String txt = "The string '" +tmp + "' could not be parsed as a long for 'sequence'. The complete value was '" + val; throw new Exception(txt); } } } /** * Parses the data which is passed as a key/value pair. * @param key * @param value * @throws Exception */ public void assignFromInfoPair(String key, String value) throws Exception { parseKey(key); parseValue(value); } /** * @return Returns the catalog. */ public String getCatalog() { if (this.catalog == null || this.catalog.trim().length() < 1) return EMPTY; return catalog; } /** * @param catalog The catalog to set. */ public void setCatalog(String catalog) { this.catalog = catalog; } /** * @return Returns the replicate. */ public boolean isReplicate() { if (this.actions == null) return false; return this.actions.length() > 0; } /** * @param replicate The replicate to set. */ public void setActions(String actions) { if (actions != null) { this.actions = actions.trim().toUpperCase(); } else this.actions = ""; } /** * @return Returns the replKey. */ public long getReplKey() { return replKey; } /** * @param replKey The replKey to set. */ public void setReplKey(long replKey) { this.replKey = replKey; } /** * @return Returns the schema. */ public String getSchema() { if (this.schema == null || this.schema.trim().length() < 1) return EMPTY; return schema; } /** * @param schema The schema to set. */ public void setSchema(String schema) { this.schema = schema; } /** * @return Returns the status. */ public String getStatus() { return status; } /** * @param status The status to set. */ public void setStatus(String status) { this.status = status; } /** * @return Returns the table. */ public String getTable() { return table; } /** * @param table The table to set. */ public void setTable(String table) { this.table = table; } /** * @return Returns the trigger. */ public String getTrigger() { return trigger; } /** * @param trigger The trigger to set. */ public void setTrigger(String trigger) { this.trigger = trigger; } /** * First checks if the entry exists already. If it exists, it is first removed, otherwise it is * just added. * * @param replPrefix * @param dbPool * @param conn * @throws Exception */ public void store(String replPrefix, I_DbPool dbPool, Connection conn) throws Exception { String selectSql = "SELECT * FROM " + replPrefix + "tables WHERE CATALOGNAME='" + getCatalog() + "' AND SCHEMANAME='" + getSchema() + "' AND TABLENAME='" + getTable() + "'"; log.info("executing '" + selectSql + "'"); Statement st = null; try { st = conn.createStatement(); ResultSet rs = st.executeQuery(selectSql); if (rs.next()) { String delSql = "DELETE FROM " + replPrefix + "tables WHERE CATALOGNAME='" + getCatalog() + "' AND SCHEMANAME='" + getSchema() + "' AND TABLENAME='" + getTable() + "'"; log.info("executing '" + delSql + "'"); dbPool.update(conn, delSql); } } finally { if (st != null) { st.close(); } } String sql = "INSERT INTO " + replPrefix + "tables VALUES ('" + getCatalog() + "','" + getSchema() + "','" + getTable() + "','" + getActions() + "', 'CREATING'," + getReplKey() + ",'" + getTrigger() + "'," + getDebug() + ")"; log.info("Inserting the statement '" + sql + "' for '" + this + "'"); dbPool.update(conn, sql); } /** * * @param rs * @param tableToWatch can be null. If not it will be the instance returned (after having filled it of course). * @return * @throws SQLException */ private static TableToWatchInfo get(ResultSet rs, TableToWatchInfo tableToWatch) throws SQLException { if (!rs.next()) return null; String catalog = rs.getString(1); String schema = rs.getString(2); String table = rs.getString(3); String actions = rs.getString(4); String status = rs.getString(5); long replKey = rs.getLong(6); String triggerName = rs.getString(7); long debug = rs.getInt(8); if (tableToWatch == null) tableToWatch = new TableToWatchInfo(catalog, schema, table); else { tableToWatch.setCatalog(catalog); tableToWatch.setSchema(schema); tableToWatch.setTable(table); } tableToWatch.setActions(actions); tableToWatch.setStatus(status); tableToWatch.setReplKey(replKey); tableToWatch.setDebug((int)debug); tableToWatch.setTrigger(triggerName); return tableToWatch; } /** * * @param conn * @param tableName The name of the table from which to retrieve the information * @param catalog * @param schema * @param table * @param tableToWatch can be null. If it is not null, it will be changed appropriately and returned. * @return * @throws Exception */ public static TableToWatchInfo get(Connection conn, String tableName, String catalog, String schema, String table , TableToWatchInfo tableToWatch) throws Exception { Statement st = null; ResultSet rs = null; if (catalog == null) catalog = EMPTY; if (schema == null) schema = EMPTY; try { st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + tableName + " WHERE catalogname='" + catalog + "' AND schemaname='" + schema + "' AND tablename='" + table + "'"); return get(rs, tableToWatch); } finally { if (rs != null) try { rs.close(); } catch (Exception e) {} if (st != null) try { st.close(); } catch (Exception e) {} } } /** * Gets the entire configuration information of the configuration table specified in the * argument list. * Never throws exception nor returns null. * @param conn * @param tableName * @return */ public static TableToWatchInfo[] getAll(Connection conn, String confTableName) { if (conn == null || confTableName == null || confTableName.length() < 1) return new TableToWatchInfo[0]; Statement st = null; ResultSet rs = null; try { ArrayList list = new ArrayList(); st = conn.createStatement(); rs = st.executeQuery("SELECT * from " + confTableName); TableToWatchInfo tmp = null; while ( (tmp=get(rs, null)) != null) list.add(tmp); return (TableToWatchInfo[])list.toArray(new TableToWatchInfo[list.size()]); } catch (Exception ex) { ex.printStackTrace(); return new TableToWatchInfo[0]; } finally { if (rs != null) try { rs.close(); } catch (Exception e) {} if (st != null) try { st.close(); } catch (Exception e) {} } } public String getActions() { if (this.actions == null) return "IDU"; return this.actions; } public String toXml() { StringBuffer buf = new StringBuffer(); buf.append("<tableToWatch "); if (this.catalog != null) buf.append(" catalog='" + this.catalog + "'"); if (this.schema != null) buf.append(" schema='" + this.schema + "'"); if (this.table != null) buf.append(" table='" + this.table + "'"); if (this.status != null) buf.append(" status='" + this.status + "'"); buf.append(" replKey='" + this.replKey + "'"); if (this.trigger != null) buf.append(" trigger='" + this.trigger + "'"); if (this.actions != null) buf.append(" actions='" + this.actions + "'"); if (this.debug > 0) buf.append(" debug='" + this.debug + "'"); buf.append(" doReplicate='" + isReplicate() + "' />"); return buf.toString(); } public int getDebug() { return (int)this.debug; } public void setDebug(int debug) { this.debug = debug; } public void storeStatus(String replicationPrefix, I_DbPool dbPool) throws Exception { String sql = "UPDATE " + replicationPrefix + "tables SET status='" + this.status + "' WHERE catalogname='" + this.catalog + "' AND schemaname='" + this.schema + "' AND tablename='" + this.table + "'"; dbPool.update(sql); } public void removeFromDb(String replicationPrefix, I_DbPool dbPool) throws Exception { String sql = "DELETE FROM " + replicationPrefix + "tables WHERE catalogname='" + this.catalog + "' AND schemaname='" + this.schema + "' AND tablename='" + this.table + "'"; dbPool.update(sql); } /** * Checks if the status is OK. To return true the status flag in the tables table must * be OK, the trigger must exist and no exception shall be thrown by requesting the * existence of the trigger. * * @param dbSpecific * @param conn * @return */ public boolean isStatusOk(I_DbSpecific dbSpecific, Connection conn) { if (this.status == null) { log.warning("The status flag for trigger '" + this.trigger + "' on table '" + this.table + "' is null"); return false; } try { boolean ret = dbSpecific.triggerExists(conn, this); if (!ret) { log.warning("The trigger '" + this.trigger + "' for table '" + this.table + "' does not exist and status is '" + getStatus() + "'"); return ret; } } catch (Throwable ex) { ex.printStackTrace(); return false; } boolean ret = getStatus().equals(TableToWatchInfo.STATUS_OK); if (!ret) log.warning("The status flag for trigger '" + this.trigger + "' on table '" + this.table + "' is not OK, it is '" + getStatus() + "'"); return ret; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -