📄 netboxinfo.java
字号:
public static boolean put(String key, String var, String val) { return put(null, key, var, Collections.singletonList(val)); } /** * Add the values for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. A default netboxid * must be set before this method is called. * * @param key The key to insert values for * @param var The variable to insert values for * @param vals The values to insert * @return true if previous vals did not exist. */ public static boolean put(String key, String var, String[] vals) { return put(null, key, var, Arrays.asList(vals)); } /** * Add the values for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. A default netboxid * must be set before this method is called. * * @param key The key to insert values for * @param var The variable to insert values for * @param vals The values to insert * @return true if previous vals did not exist. */ public static boolean put(String key, String var, List vals) { return put(null, key, var, vals); } /** * Add the value for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. If a default netboxid * is set netboxid is also allowed to be null. * * @param netboxid The netboxid to insert values for * @param key The key to insert values for * @param var The variable to insert values for * @param val The value to insert * @return true if previous vals did not exist. */ public static boolean put(String netboxid, String key, String var, String val) { return put(netboxid, key, var, Collections.singletonList(val)); } /** * Add the values for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. If a default netboxid * is set netboxid is also allowed to be null. * * @param netboxid The netboxid to insert values for * @param key The key to insert values for * @param var The variable to insert values for * @param vals The values to insert * @return true if previous vals did not exist. */ public static boolean put(String netboxid, String key, String var, String[] vals) { return put(netboxid, key, var, Arrays.asList(vals)); } /** * Add the values for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. If a default netboxid * is set netboxid is also allowed to be null. * * @param netboxid The netboxid to insert values for * @param key The key to insert values for * @param var The variable to insert values for * @param vals List of String objects; the values to insert * @return true if previous vals did not exist. */ public static boolean put(String netboxid, String key, String var, List vals) { return putError(netboxid, key, var, vals.iterator()); } // Check for errors private static boolean putError(String netboxid, String key, String var, Iterator newVals) { if (netboxid == null) netboxid = (String)netboxidMap.get(Thread.currentThread()); if (netboxid == null || var == null) throw new NullPointerException("Netboxid and var are not allowed to be null"); try { return putNoError(netboxid, key, var, newVals); } catch (SQLException se) { try { Database.rollback(); } catch (SQLException expr) { Log.d("NETBOXINFO", "PUT_ERROR", "SQLException when rolling back: " + expr); } throw new RuntimeException("Got SQLException, aborting put: " + se.getMessage()); }} // Does not check for errors private static boolean putNoError(String netboxid, String key, String var, Iterator newVals) throws SQLException { ResultSet rs = getVals(netboxid, key, var); Map valMap = new HashMap(); while (rs.next()) { valMap.put(rs.getString("val"), rs.getString("netboxinfoid")); } try { Database.beginTransaction(); if (valMap.size() == 0) { // Var is new, simply insert new records insertVals(netboxid, key, var, newVals); Database.commit(); return true; } else { // Var exists, try to update before delete Set newValSet = new HashSet(); while (newVals.hasNext()) { newValSet.add(newVals.next()); } // Remove all equal values (the intersection) from both sets // since we don't need to update those Set intersection = new HashSet(newValSet); intersection.retainAll(valMap.keySet()); newValSet.removeAll(intersection); valMap.keySet().removeAll(intersection); // All remaining values in valMap should no longer be present; if there are // any values left in newValMap, update rows from valMap for (Iterator newValIt = newValSet.iterator(), valIt = valMap.values().iterator(); newValIt.hasNext() && valIt.hasNext();) { String newVal = (String)newValIt.next(); String netboxinfoid = (String)valIt.next(); newValIt.remove(); valIt.remove(); String[] set = { "val", newVal }; String[] where = { "netboxinfoid", netboxinfoid }; Database.update("netboxinfo", set, where); } // Now either newValSet or valMap (or both) are empty; in the // first case the remaning entries from valMap are deleted, in // the second the remaining entries in newValSet are inserted. if (!newValSet.isEmpty()) { insertVals(netboxid, key, var, newValSet.iterator()); } if (!valMap.isEmpty()) { for (Iterator valIt = valMap.values().iterator(); valIt.hasNext();) { Database.update("DELETE FROM netboxinfo WHERE netboxinfoid = '" + valIt.next() + "'"); } } } Database.commit(); } catch (SQLException e) { try { Database.rollback(); } catch (SQLException expr) { Log.d("NETBOXINFO", "PUT_NOERROR", "SQLException when rolling back: " + expr); } throw e; } return false; } /** * Remove the values for the given variable. The key is assumed to * be null; var must not be null. A default netboxid must be set * before this method is called. * * @param var The variable to remove values for * @return the number of removed values */ public static int remove(String var) { return removeError(null, null, var); } /** * Remove the values for the given key and variable. The key is * allowed to be null; var and val are not. A default netboxid must * be set before this method is called. * * @param key The key to remove values for * @param var The variable to remove values for * @return the number of removed values */ public static int remove(String key, String var) { return removeError(null, key, var); } /** * Remove the values for the given netboxid, key and variable. The key * is allowed to be null; var and val are not. If a default netboxid * is set netboxid is also allowed to be null. * * @param netboxid The netboxid to remove values for * @param key The key to remove values for * @param var The variable to remove values for * @return the number of removed values */ public static int remove(String netboxid, String key, String var) { return removeError(netboxid, key, var); } // Check for errors private static int removeError(String netboxid, String key, String var) { if (netboxid == null) netboxid = (String)netboxidMap.get(Thread.currentThread()); if (netboxid == null || var == null) throw new NullPointerException("Netboxid and var are not allowed to be null"); try { return removeNoError(netboxid, key, var); } catch (SQLException se) { throw new RuntimeException("Got SQLException, aborting remove: " + se.getMessage()); } } // Does not check for errors private static int removeNoError(String netboxid, String key, String var) throws SQLException { String k = (key == null || key.length() == 0) ? "key IS NULL" : "key = '"+Database.addSlashes(key)+"'"; String q = "DELETE FROM netboxinfo WHERE netboxid = '"+netboxid+"' AND " + k + " AND var = '"+Database.addSlashes(var)+"'"; return Database.update(q); } private static ResultSet getVals(String netboxid, String key, String var) throws SQLException { String k = (key == null || key.length() == 0) ? "key IS NULL" : "key = '"+Database.addSlashes(key)+"'"; String v = (var == null) ? "" : " AND var = '" + Database.addSlashes(var) + "'"; String v2 = (var == null) ? ", var" : ""; String q = "SELECT netboxinfoid, val" + v2 + " FROM netboxinfo WHERE netboxid = '"+netboxid+"' AND " + k + v + " ORDER BY val"; return Database.query(q); } // Insert new values into netboxinfo private static void insertVals(String netboxid, String key, String var, Iterator valIt) throws SQLException { while (valIt.hasNext()) { String val = (String)valIt.next(); String[] ins = { "netboxid", netboxid, "key", key, "var", var, "val", val }; Database.insert("netboxinfo", ins); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -