📄 sqldescription.java
字号:
ResultSet rs = null; try { st = conn.prepareStatement(sql); for (int i=0; i < entries.size(); i++) insertIntoStatement(st, i+1, (ClientProperty)entries.get(i)); rs = st.executeQuery(); if (!rs.next()) throw new Exception("When deleting '" + row.toXml("") + "' for '" + toXml("") + "' the statement '" + sql + "' returned an emtpy result set. Can not determine what to do"); long entriesFound = rs.getLong(1); if (entriesFound == 0) { log.warning("no entries found for the statement '" + sql + "' I will not do anything"); return 0; } if (entriesFound > 1) log.warning("" + entriesFound + " entries found matching the statement '" + sql + "'. I will continue by updating these entries. You probably will get " + entriesFound + " warnings of entries not found in this same transaction. You can then ignore these"); } finally { if (rs != null) { try { rs.close(); rs = null; } catch (Exception ex) { } } if (st != null) { try { st.close(); st = null; } catch (Exception ex) { } } } } sql = "DELETE FROM " + getCompleteTableName() + whereSt; st = conn.prepareStatement(sql); for (int i=0; i < entries.size(); i++) insertIntoStatement(st, i+1, (ClientProperty)entries.get(i)); return st.executeUpdate(); } catch (Throwable ex) { log.severe(" Entry '" + row.toXml("", true, false, true) + "' caused a (throwable) exception. Statement was '" + sql + "': " + ex.getMessage()); if (ex instanceof Exception) throw (Exception)ex; else throw new Exception(ex); } finally { if (st != null) st.close(); } } /** * Returns the number of entries inserted * @param conn * @param row * @return * @throws Exception */ public int insert(Connection conn, SqlRow row) throws Exception { PreparedStatement st = null; int ret = 0; String sql = null; try { ArrayList entries = new ArrayList(); String insertSt = createInsertStatement(row, entries); sql = "INSERT INTO " + getCompleteTableName() + insertSt; st = conn.prepareStatement(sql); //StringBuffer buf = new StringBuffer(512); for (int i=0; i < entries.size(); i++) { insertIntoStatement(st, i+1, (ClientProperty)entries.get(i)); //if (i>0) buf.append(", "); //buf.append((ClientProperty)entries.get(i)); } //log.info("Writing: " + buf.toString()); ret = st.executeUpdate(); return ret; } catch (SQLException ex) { // unique constraint for Oracle: TODO implement also for other DB if (ex.getMessage().indexOf("ORA-00001") > -1) { log.severe("Entry '" + row.toXml("", true, false, true) + "' exists already. Will ignore it an continue"); return 0; } else { log.severe(" Entry '" + row.toXml("", true, false, true) + "' caused an exception. Statement was *" + sql + "': " + ex.getMessage()); throw ex; } } catch (Throwable ex) { log.severe(" Entry '" + row.toXml("", true, false, true) + "' caused a (throwable) exception. Statement was '" + sql + "': " + ex.getMessage()); if (ex instanceof Exception) throw (Exception)ex; else throw new Exception(ex); } finally { if (st != null) st.close(); } } public String getCommand() { return this.command; } public void setCommand(String command) { this.command = command; } public String getIdentity() { return this.identity; } public void setIdentity(String identity) { this.identity = identity; } public synchronized void addColumn(SqlColumn column) { if (this.hasAddedStatements) throw new IllegalStateException("SqlDescription.addColumnDescription: can not add columns now since prepared statements have already been created"); this.columnList.add(column); this.pkKnown = false; } public SqlColumn[] getColumns() { return (SqlColumn[])this.columnList.toArray(new SqlColumn[this.columnList.size()]); } public SqlColumn getColumn(String colName) { for (int i=0; i < this.columnList.size(); i++) { SqlColumn tmp = (SqlColumn)this.columnList.get(i); if (tmp == null) continue; String tmpColName = tmp.getColName(); if (tmpColName == null) continue; if (tmpColName.equalsIgnoreCase(colName)) return tmp; } return null; } public int getNumOfColumns() { return this.columnList.size(); } /** * Gets the column at position given by pos. Note that the first position is 1 (not 0). * @param pos * @return * @throws IllegalArgumentException if the number is less than 1 or bigger than the size of the list or if for some reason the entry has not been found. */ public SqlColumn getColumnAtPosition(int pos) { if (pos < 1 || pos > this.columnList.size()) throw new IllegalArgumentException("getColumnAtPosition has wrong argument '" + pos + "' must be in the range [1.." + this.columnList.size() + "] (means inclusive)"); SqlColumn col = (SqlColumn)this.columnList.get(pos-1); int p = col.getPos(); // fast find if (p == (pos-1)) return col; StringBuffer buf = new StringBuffer(); for (int i=0; i < this.columnList.size(); i++) { col = (SqlColumn)this.columnList.get(i); p = col.getPos(); buf.append(p).append(" "); if (p == pos) return col; } throw new IllegalArgumentException("getColumnAtPosition: The position '" + pos + "' has not been found among the ones processed which are '" + buf.toString() + "'"); } public String toXml(String extraOffset) { return toXml(extraOffset, false); } final String toXml(String extraOffset, boolean doTruncate) { StringBuffer sb = new StringBuffer(256); if (extraOffset == null) extraOffset = ""; String offset = Constants.OFFSET + extraOffset; if (this.updateStatementTxt != null) sb.append(offset).append("<!-- update: ").append(this.updateStatementTxt).append("-->"); if (this.insertStatementTxt != null) sb.append(offset).append("<!-- insert: ").append(this.insertStatementTxt).append("-->"); if (this.deleteStatementTxt != null) sb.append(offset).append("<!-- delete: ").append(this.deleteStatementTxt).append("-->"); sb.append(offset).append("<").append(DESC_TAG).append(">"); sb.append(offset + " ").append("<").append(COMMAND_TAG).append(">").append(this.command).append("</").append(COMMAND_TAG).append(">"); sb.append(offset + " ").append("<").append(IDENT_TAG).append(">").append(this.identity).append("</").append(IDENT_TAG).append(">"); for (int i=0; i < this.columnList.size(); i++) { SqlColumn desc = (SqlColumn)this.columnList.get(i); sb.append(desc.toXml(extraOffset + " ")); if (doTruncate && sb.length() > SqlInfo.MAX_BUF_SIZE) { sb.append(" ..."); return sb.toString(); } } Iterator iter = this.attributeKeys.iterator(); while (iter.hasNext()) { Object key = iter.next(); ClientProperty prop = (ClientProperty)this.attributes.get(key); sb.append(prop.toXml(extraOffset + " ", SqlInfoParser.ATTR_TAG)); if (doTruncate && sb.length() > SqlInfo.MAX_BUF_SIZE) { sb.append(" ..."); return sb.toString(); } } sb.append(offset).append("</").append(DESC_TAG).append(">"); return sb.toString(); } public static String getDifferences(SqlDescription description1, SqlDescription description2) { if (description1 == null || description2 == null) return ""; Map map1 = new HashMap(); Map map2 = new HashMap(); SqlColumn[] cols = description1.getColumns(); for (int i=0; i < cols.length; i++) { String name = /*cols[i].getCatalog() + "." +*/ cols[i].getSchema() + "." + cols[i].getTable() + "." + cols[i].getColName(); map1.put(name, cols[i]); } cols = description2.getColumns(); for (int i=0; i < cols.length; i++) { String name = /*cols[i].getCatalog() + "." +*/ cols[i].getSchema() + "." + cols[i].getTable() + "." + cols[i].getColName(); map2.put(name, cols[i]); } // scan map1 Map ret = new TreeMap(); String[] keys = (String[])map1.keySet().toArray(new String[map1.size()]); for (int i=0; i < keys.length; i++) { SqlColumn[] val = new SqlColumn[2]; SqlColumn val1 = (SqlColumn)map1.get(keys[i]); SqlColumn val2 = (SqlColumn)map2.remove(keys[i]); if (val2 == null) { // then the column has been deleted val[0] = val1; val[1] = null; ret.put(keys[i], val); } else { // check if it has changed if(!val1.isSame(val2)) { ret.put(keys[i], new SqlColumn[] { val1, val2 }); } } } // and now scam map2 for still remaining (new) columns keys = (String[])map2.keySet().toArray(new String[map2.size()]); for (int i=0; i < keys.length; i++) { SqlColumn[] val = new SqlColumn[2]; val[0] = null; val[1] = (SqlColumn)map2.get(keys[i]); ret.put(keys[i], val); } // and now build the output string ... StringBuffer buf = new StringBuffer(1024); buf.append("<descDiff>\n"); Iterator iter = ret.keySet().iterator(); while (iter.hasNext()) { String name = (String)iter.next(); buf.append("\n\n\n <!-- column ").append(name); SqlColumn[] val = (SqlColumn[])ret.get(name); if (val[0] == null) { // new column buf.append(" NEW -->\n"); buf.append(val[1].toXml(" ")); } else if (val[1] == null) { // deleted column buf.append(" DELETED -->\n"); buf.append(val[0].toXml(" ")); } else { // modified column buf.append(" MODIFIED -->\n"); buf.append(val[0].toXml(" ")); buf.append("\n"); buf.append(val[1].toXml(" ")); } } buf.append("</descDiff>\n"); return buf.toString(); } public static void main(String[] args) { try { String propFile = System.getProperty("properties", null); Properties props = null; if (propFile == null) { props = System.getProperties(); System.err.println("not using any properties file"); } else { System.err.println("Using properties file '" + propFile + "'"); props = new Properties(System.getProperties()); props.load(new FileInputStream(propFile)); } I_Info info = new PropertiesInfo(props); if (args.length < 2) { System.err.println("Usage: " + SqlDescription.class.getName() + " oldFile.xml newFile.xml"); System.exit(-1); } String oldFile = args[0]; String newFile = args[1]; BufferedReader reader = new BufferedReader(new FileReader(oldFile)); String line = null; StringBuffer buf = new StringBuffer(1024); while ( (line = reader.readLine()) != null) { buf.append(line).append("\n"); } String oldData = buf.toString(); reader.close(); reader = new BufferedReader(new FileReader(newFile)); buf = new StringBuffer(1024); while ( (line = reader.readLine()) != null) { buf.append(line).append("\n"); } String newData = buf.toString(); reader.close(); SqlInfoParser parser = new SqlInfoParser(); parser.init(info); SqlInfo oldInfo = parser.parse(oldData); SqlInfo newInfo = parser.parse(newData); String ret = SqlDescription.getDifferences(oldInfo.getDescription(), newInfo.getDescription()); System.out.println(ret); } catch (Throwable ex) { ex.printStackTrace(); } } /** * @param column the name of the column to check or null if a global check for the table. * * @return true if the specified column is configured as searchable false otherwise. If you * passed null as the column name, then true is returned if at least one column is searchable, * false otherwise. * */ public boolean isColumnSearchConfigured(String column) { if (this.configuredSearchableColumns == null) { synchronized (this) { if (this.configuredSearchableColumns == null) { SearchableConfig searchableConfig = (SearchableConfig)this.info.getObject(SearchableConfig.NAME); String catalog = getCatalog(); String table = this.identity; if (searchableConfig != null) this.configuredSearchableColumns = searchableConfig.getSearchableColumnNames(catalog, getSchema(), table); if (this.configuredSearchableColumns == null) this.configuredSearchableColumns = new HashSet(); else { // do the complete check here if (this.hasPk()) log.warning("The table '" + getCompleteTableName() + "' has primary keys defined. You configured explicitly searchable columns which overwrite the PK defaults. I hope you know what you are doing"); String[] columns = (String[])this.configuredSearchableColumns.toArray(new String[this.configuredSearchableColumns.size()]); for (int i=0; i < columns.length; i++) { SqlColumn col = getColumn(columns[i]); if (col == null) log.warning("The column '" + columns[i] + "' was not found on table '" + getCompleteTableName() + "' but you have configured it to be searchable: is this really correct ? Are you maybe adding the table later ?"); else { if (!col.isSearchable()) { log.warning("The column '" + columns[i] + "' is not searchable. Can not be used to search: will remove it from the searchable list of columns: please update your configuration file"); this.configuredSearchableColumns.remove(columns[i]); } } } } } } } if (column == null) // we have forced an initialization even with null. return this.configuredSearchableColumns.size() > 0; return this.configuredSearchableColumns.contains(column); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -