sjednoceni sql dotazu podle nazvu tabulky

verze3
Michal 2026-08-31 22:04:47 +02:00
parent 19a5934081
commit 123776549f
9 changed files with 146 additions and 118 deletions

View File

@ -21,6 +21,7 @@ public final class Database {
private static final SnmpDao snmpDao = new SnmpDao();
private static final ObjectTypeDao objectTypeDao = new ObjectTypeDao();
private static final ConnectionDao connectionDao = new ConnectionDao();
private static final ProbeDao probeDao = new ProbeDao();
@ -177,15 +178,15 @@ public final class Database {
}
public static int addNodeProbe(NodeProbe probe) {
return snmpDao.addNodeProbe(probe);
return probeDao.addNodeProbe(probe);
}
public static void removeNodeProbe(int id) {
nodeDao.removeNodeProbe(id);
probeDao.removeNodeProbe(id);
}
public static void editNodeProbe(NodeProbe probe) {
nodeDao.editNodeProbe(probe);
probeDao.editNodeProbe(probe);
}
public static int addSnmpTemplate(SnmpTemplate template) {

View File

@ -10,13 +10,22 @@ import jnet.lib.LogFile;
import jnet.lib.object.Node;
public class ConnectionDao extends AbstractDao {
public static final String TABLE = "connection";
/**
* Pro daný seznam uzlů vrátí všechna jejich spojení (bez duplicit).
*/
public List<NodeConnection> getConnectionsForNodes(List<Node> nodes) {
return getUniqueConnectionsForNodes(nodes);
}
/**
* Vrátí všechna spojení, do kterých daný uzel vstupuje nebo z nich
* vystupuje.
*/
public List<NodeConnection> getConnectionsForNode(int nodeId) {
String sql = "SELECT DISTINCT * FROM connection WHERE nodeFrom = ? OR nodeTo = ?";
String sql = "SELECT DISTINCT * FROM " + TABLE + " WHERE nodeFrom = ? OR nodeTo = ?";
return queryList(sql, rs -> new NodeConnection(
rs.getInt("id"),
rs.getInt("siteId"),
@ -38,7 +47,7 @@ public class ConnectionDao extends AbstractDao {
return new ArrayList<>();
}
// Sestavíme dynamický IN dotaz
StringBuilder sql = new StringBuilder("SELECT DISTINCT * FROM connection WHERE nodeFrom IN (");
StringBuilder sql = new StringBuilder("SELECT DISTINCT * FROM " + TABLE + " WHERE nodeFrom IN (");
for (int i = 0; i < nodeIds.size(); i++) {
sql.append("?");
if (i < nodeIds.size() - 1) {
@ -92,7 +101,7 @@ public class ConnectionDao extends AbstractDao {
}
public int addConnection(NodeConnection nc) {
String sql = "INSERT INTO connection (siteId, nodeFrom, nodeTo, typeConnection, readTraffic, trafficNode, trafficIface) "
String sql = "INSERT INTO " + TABLE + " (siteId, nodeFrom, nodeTo, typeConnection, readTraffic, trafficNode, trafficIface) "
+ "VALUES (?,?,?,?,?,?,?)";
return insertAndGetId(sql,
nc.getSite(),
@ -110,7 +119,7 @@ public class ConnectionDao extends AbstractDao {
* Vrátí unikátní spojení pro danou site
*/
public List<NodeConnection> getConnectionsForSite(int siteId) {
String sql = "SELECT * FROM connection WHERE siteId = ?";
String sql = "SELECT * FROM " + TABLE + " WHERE siteId = ?";
return queryList(sql, rs -> new NodeConnection(
rs.getInt("id"),
rs.getInt("siteId"),
@ -124,7 +133,7 @@ public class ConnectionDao extends AbstractDao {
}
public void removeNodeConnection(int id) {
if (deleteById("connection", id)) {
if (deleteById(TABLE, id)) {
LogFile.printInfo("Node connection ID " + id + " removed from database");
} else {
LogFile.printErr("Node connection ID " + id + " not removed from database");
@ -134,7 +143,7 @@ public class ConnectionDao extends AbstractDao {
public void editNodeconnection(NodeConnection connection) {
try {
executeUpdate(
"UPDATE connection SET typeConnection=?, readTraffic=?, trafficNode=?, trafficIface=? WHERE id=?",
"UPDATE " + TABLE + " SET typeConnection=?, readTraffic=?, trafficNode=?, trafficIface=? WHERE id=?",
connection.getType(),
(connection.isReadTraffic() ? 1:0),
connection.getTrafficNode(),

View File

@ -6,9 +6,11 @@ import java.sql.Timestamp;
import java.util.List;
public class EventDao extends AbstractDao {
public static final String TABLE = "events";
public long insertEvent(int nodeId, int status, long time) {
String sql = "INSERT INTO events (node, status, start) VALUES (?, ?, ?)";
String sql = "INSERT INTO " + TABLE + " (node, status, start) VALUES (?, ?, ?)";
return insertAndGetId(sql, nodeId, status, new Timestamp(time));
}
@ -21,7 +23,7 @@ public class EventDao extends AbstractDao {
}
public List<Event> getAllEvents() {
String sql = "SELECT * FROM events";
String sql = "SELECT * FROM " + TABLE;
return queryList(sql, rs -> {
Timestamp start = rs.getTimestamp("start");
Timestamp end = rs.getTimestamp("end");

View File

@ -4,29 +4,23 @@ import jnet.lib.LogFile;
import jnet.lib.Status;
import jnet.lib.object.Node;
import jnet.lib.object.NodeProbe;
import jnet.lib.object.NodeConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class NodeDao extends AbstractDao {
public static final String TABLE = "node";
private final ConnectionDao connectionDao = new ConnectionDao();
public List<Node> getNodesForSite(int siteId) {
String sql = "SELECT * FROM node WHERE siteId = ?";
String sql = "SELECT * FROM " + TABLE + " WHERE siteId = ?";
return queryList(sql, this::mapNode, siteId);
}
/**
* Pro daný seznam uzlů vrátí všechna jejich spojení (bez duplicit).
*/
public List<NodeConnection> getConnectionsForNodes(List<Node> nodes) {
return connectionDao.getUniqueConnectionsForNodes(nodes);
}
public int addNode(Node node) {
String sql = "INSERT INTO node (name, ip, x, y, type, siteId, icon, ping, user, password, "
String sql = "INSERT INTO " + TABLE + " (name, ip, x, y, type, siteId, icon, ping, user, password, "
+ "description, location, snmpProfile, winbox, winboxPort, ssh, sshPort, web, "
+ "webPort, webVerze, telnet, telnetPort, sms, smsPort, smsVerze) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
@ -44,7 +38,7 @@ public class NodeDao extends AbstractDao {
}
public void removeNode(int id) {
if (deleteById("node", id)) {
if (deleteById(TABLE, id)) {
LogFile.printInfo("Node ID " + id + " removed from database");
} else {
LogFile.printErr("Node ID " + id + " not removed from database");
@ -53,7 +47,7 @@ public class NodeDao extends AbstractDao {
public void updatePosition(int nodeId, int x, int y) {
try {
executeUpdate("UPDATE node SET x = ?, y = ? WHERE id = ?", x, y, nodeId);
executeUpdate("UPDATE " + TABLE + " SET x = ?, y = ? WHERE id = ?", x, y, nodeId);
} catch (SQLException e) {
LogFile.printErr("MySQL exception: " + e.getMessage());
}
@ -89,14 +83,14 @@ public class NodeDao extends AbstractDao {
rs.getString("smsPort"),
rs.getInt("smsVerze"),
rs.getInt("siteLink"),
getNodeProbe(rs.getInt("id"))
new ProbeDao().getNodeProbe(rs.getInt("id"))
);
}
public void updateNode(Node node) {
try {
executeUpdate(
"UPDATE node SET name=?, siteId=?, type=?, x=?, y=?, ip=?, ping=?, icon=?, user=?, password=?, description=?, location=?, snmpProfile=?, winbox=?, winboxPort=?, ssh=?, sshPort=?, web=?, webPort=?, webVerze=?, telnet=?, telnetPort=?, sms=?, smsPort=?, smsVerze=?, siteLink=? WHERE id=?",
"UPDATE " + TABLE + " SET name=?, siteId=?, type=?, x=?, y=?, ip=?, ping=?, icon=?, user=?, password=?, description=?, location=?, snmpProfile=?, winbox=?, winboxPort=?, ssh=?, sshPort=?, web=?, webPort=?, webVerze=?, telnet=?, telnetPort=?, sms=?, smsPort=?, smsVerze=?, siteLink=? WHERE id=?",
node.getName(),
node.getSiteId(),
node.getType(),
@ -130,52 +124,4 @@ public class NodeDao extends AbstractDao {
}
}
private List<NodeProbe> getNodeProbe(int nodeId) {
String sql = "SELECT * FROM nodeProbe WHERE node_id = ?";
return queryList(sql, this::mapNodeProbe, nodeId);
}
private NodeProbe mapNodeProbe(ResultSet rs) throws SQLException {
return new NodeProbe(rs.getInt("id"),
rs.getString("name"),
rs.getString("oid"),
rs.getInt("snmp_data_type"),
rs.getString("conversion_type"),
rs.getDouble("conversion_value"),
rs.getString("conversion_expression"),
rs.getString("unit"),
rs.getInt("value_map_id"),
rs.getInt("interval_time"),
(rs.getInt("view_on_map") == 1 ? true:false)
);
}
public void removeNodeProbe(int id) {
if (deleteById("nodeProbe", id)) {
LogFile.printInfo("Node probe ID " + id + " removed from database");
} else {
LogFile.printErr("Node probe ID " + id + " not removed from database");
}
}
public void editNodeProbe(NodeProbe probe) {
try {
executeUpdate(
"UPDATE nodeProbe SET name=?, oid=?, snmp_data_type=?, conversion_type=?, conversion_value=?, conversion_expression=?, value_map_id=?, unit=?, interval_time=?, view_on_map=? WHERE id=?",
probe.getName(),
probe.getOid(),
probe.getDataType(),
probe.getConversionType(),
probe.getConversionValue(),
probe.getConversionExpression(),
probe.getValueMap(),
probe.getUnit(),
probe.getInterval(),
(probe.isViewOnMap() ? 1:0),
probe.getId() // poslední parametr pro WHERE
);
} catch (SQLException e) {
LogFile.printErr("Chyba při aktualizaci uzlu: " + e.getMessage());
}
}
}

View File

@ -5,9 +5,11 @@ import java.util.List;
import jnet.lib.LogFile;
public class ObjectTypeDao extends AbstractDao {
public static final String TABLE = "objectType";
public List<ObjectType> getObjectTypes() {
String sql = "SELECT * FROM objectType";
String sql = "SELECT * FROM " + TABLE;
return queryList(sql, rs -> new ObjectType(
rs.getInt("id"),
rs.getString("name"),
@ -16,12 +18,12 @@ public class ObjectTypeDao extends AbstractDao {
}
public int addObjectType(ObjectType ot) {
String sql = "INSERT INTO objectType (name, image) VALUES (?, ?)";
String sql = "INSERT INTO " + TABLE + " (name, image) VALUES (?, ?)";
return insertAndGetId(sql, ot.getName(), ot.getImgStr());
}
public void deleteDeviceType(int id) {
if (deleteById("objectType", id)) {
if (deleteById(TABLE, id)) {
LogFile.printInfo("Device type ID " + id + " removed from database");
} else {
LogFile.printErr("Device type ID " + id + " not removed from database");

View File

@ -0,0 +1,80 @@
package jnet.server.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import jnet.lib.LogFile;
import jnet.lib.object.NodeProbe;
public class ProbeDao extends AbstractDao {
public static final String TABLE = "nodeProbe";
public List<NodeProbe> getNodeProbe(int nodeId) {
String sql = "SELECT * FROM " + TABLE + " WHERE node_id = ?";
return queryList(sql, this::mapNodeProbe, nodeId);
}
private NodeProbe mapNodeProbe(ResultSet rs) throws SQLException {
return new NodeProbe(rs.getInt("id"),
rs.getString("name"),
rs.getString("oid"),
rs.getInt("snmp_data_type"),
rs.getString("conversion_type"),
rs.getDouble("conversion_value"),
rs.getString("conversion_expression"),
rs.getString("unit"),
rs.getInt("value_map_id"),
rs.getInt("interval_time"),
(rs.getInt("view_on_map") == 1 ? true:false)
);
}
public void removeNodeProbe(int id) {
if (deleteById(TABLE, id)) {
LogFile.printInfo("Node probe ID " + id + " removed from database");
} else {
LogFile.printErr("Node probe ID " + id + " not removed from database");
}
}
public void editNodeProbe(NodeProbe probe) {
try {
executeUpdate(
"UPDATE " + TABLE + " SET name=?, oid=?, snmp_data_type=?, conversion_type=?, conversion_value=?, conversion_expression=?, value_map_id=?, unit=?, interval_time=?, view_on_map=? WHERE id=?",
probe.getName(),
probe.getOid(),
probe.getDataType(),
probe.getConversionType(),
probe.getConversionValue(),
probe.getConversionExpression(),
probe.getValueMap(),
probe.getUnit(),
probe.getInterval(),
(probe.isViewOnMap() ? 1:0),
probe.getId() // poslední parametr pro WHERE
);
} catch (SQLException e) {
LogFile.printErr("Chyba při aktualizaci uzlu: " + e.getMessage());
}
}
public int addNodeProbe(NodeProbe probe) {
String sql = "INSERT INTO " + TABLE + " (name, node_id, oid, snmp_data_type, conversion_type, conversion_value, conversion_expression, value_map_id, unit, interval_time, view_on_map) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
return insertAndGetId(
sql,
probe.getName(),
probe.getNodeId(),
probe.getOid(),
probe.getDataType(),
probe.getConversionType(),
probe.getConversionValue(),
probe.getConversionExpression(),
probe.getValueMap(),
probe.getUnit(),
probe.getInterval(),
(probe.isViewOnMap() ? 1 : 0)
);
}
}

View File

@ -9,15 +9,14 @@ import java.util.List;
public class SiteDao extends AbstractDao {
private final NodeDao nodeDao = new NodeDao();
private final ConnectionDao connectionDao = new ConnectionDao();
public static final String TABLE = "site";
public List<Site> getSiteList() {
String sql = "SELECT * FROM site";
String sql = "SELECT * FROM " + TABLE;
return queryList(sql, rs -> {
int siteId = rs.getInt("id");
List<Node> nodes = nodeDao.getNodesForSite(siteId);
List<NodeConnection> connections = connectionDao.getConnectionsForSite(siteId);
List<Node> nodes = new NodeDao().getNodesForSite(siteId);
List<NodeConnection> connections = new ConnectionDao().getConnectionsForSite(siteId);
return new Site(
siteId,
rs.getString("name"),
@ -29,7 +28,7 @@ public class SiteDao extends AbstractDao {
}
public int addSite(String name) {
return insertAndGetId("INSERT INTO site (name) VALUES (?)", name);
return insertAndGetId("INSERT INTO " + TABLE + " (name) VALUES (?)", name);
}
public void removeSite(int id) {
@ -42,7 +41,7 @@ public class SiteDao extends AbstractDao {
public void setSiteLock(boolean locked, int siteId) {
try {
executeUpdate("UPDATE site SET locked = ? WHERE id = ?", locked, siteId);
executeUpdate("UPDATE " + TABLE + " SET locked = ? WHERE id = ?", locked, siteId);
} catch (SQLException e) {
LogFile.printErr("MySQL exception: " + e.getMessage());
}

View File

@ -9,9 +9,14 @@ import jnet.lib.object.SnmpTemplate;
import jnet.lib.object.SnmpValueMap;
public class SnmpDao extends AbstractDao {
public static final String TABLE_PROFILE = "snmpProfile";
public static final String TABLE_TEMPLATE = "snmp_oid_template";
public static final String TABLE_VALUE_MAP = "snmp_value_map";
public static final String TABLE_VALUE_MAP_ITEM = "snmp_value_map_item";
public List<SnmpProfile> getAllProfile() {
String sql = "SELECT * FROM snmpProfile";
String sql = "SELECT * FROM " + TABLE_PROFILE;
return queryList(sql, rs -> {
return new SnmpProfile(
rs.getInt("id"),
@ -24,7 +29,7 @@ public class SnmpDao extends AbstractDao {
}
public List<SnmpTemplate> getSnmpTemplate() {
String sql = "SELECT * FROM snmp_oid_template";
String sql = "SELECT * FROM " + TABLE_TEMPLATE;
return queryList(sql, rs -> {
return new SnmpTemplate(
rs.getInt("id"),
@ -43,7 +48,7 @@ public class SnmpDao extends AbstractDao {
}
public List<SnmpValueMap> getSnmpValueMapList() {
String sql = "SELECT * FROM snmp_value_map";
String sql = "SELECT * FROM " + TABLE_VALUE_MAP;
return queryList(sql, rs -> new SnmpValueMap(
rs.getInt("id"),
rs.getString("name"),
@ -52,7 +57,7 @@ public class SnmpDao extends AbstractDao {
}
private List<SnmpValueMap.Item> getSnmpValueMapItem(int id) {
String sql = "SELECT * FROM snmp_value_map_item WHERE map_id = ?";
String sql = "SELECT * FROM " + TABLE_VALUE_MAP_ITEM + " WHERE map_id = ?";
return queryList(sql, rs -> new SnmpValueMap.Item(
rs.getInt("id"),
rs.getString("raw_value"),
@ -61,7 +66,7 @@ public class SnmpDao extends AbstractDao {
}
public int addSnmpProfile(SnmpProfile profile) {
String sql = "INSERT INTO snmpProfile (name, communityRead, port, version) VALUES (?, ?, ?, ?)";
String sql = "INSERT INTO " + TABLE_PROFILE + " (name, communityRead, port, version) VALUES (?, ?, ?, ?)";
return insertAndGetId(
sql,
profile.getName(),
@ -74,7 +79,7 @@ public class SnmpDao extends AbstractDao {
public void editSnmpProfile(SnmpProfile profile) {
try {
executeUpdate(
"UPDATE snmpProfile SET name=?, communityRead=?, port=?, version=? WHERE id=?",
"UPDATE " + TABLE_PROFILE + " SET name=?, communityRead=?, port=?, version=? WHERE id=?",
profile.getName(),
profile.getCommunityRead(),
profile.getPort(),
@ -87,33 +92,15 @@ public class SnmpDao extends AbstractDao {
}
public void removeSnmpProfile(int id) {
if (deleteById("snmpProfile", id)) {
if (deleteById(TABLE_PROFILE, id)) {
LogFile.printInfo("SNMP profile ID " + id + " removed from database");
} else {
LogFile.printErr("SNMP profile ID " + id + " not removed from database");
}
}
public int addNodeProbe(NodeProbe probe) {
String sql = "INSERT INTO nodeProbe (name, node_id, oid, snmp_data_type, conversion_type, conversion_value, conversion_expression, value_map_id, unit, interval_time, view_on_map) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
return insertAndGetId(
sql,
probe.getName(),
probe.getNodeId(),
probe.getOid(),
probe.getDataType(),
probe.getConversionType(),
probe.getConversionValue(),
probe.getConversionExpression(),
probe.getValueMap(),
probe.getUnit(),
probe.getInterval(),
(probe.isViewOnMap() ? 1 : 0)
);
}
public int addSnmpTemplate(SnmpTemplate template) {
String sql = "INSERT INTO snmp_oid_template (name, device_type, oid, snmp_data_type, conversion_type, conversion_value, conversion_expression, value_map_id, unit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
String sql = "INSERT INTO " + TABLE_TEMPLATE + " (name, device_type, oid, snmp_data_type, conversion_type, conversion_value, conversion_expression, value_map_id, unit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
return insertAndGetId(
sql,
template.getName(),
@ -131,7 +118,7 @@ public class SnmpDao extends AbstractDao {
public void editSnmpTemplate(SnmpTemplate template) {
try {
executeUpdate(
"UPDATE snmp_oid_template SET name=?, device_type=?, oid=?, snmp_data_type=?, conversion_type=?, conversion_value=?, conversion_expression=?, value_map_id=?, unit=? WHERE id=?",
"UPDATE " + TABLE_TEMPLATE + " SET name=?, device_type=?, oid=?, snmp_data_type=?, conversion_type=?, conversion_value=?, conversion_expression=?, value_map_id=?, unit=? WHERE id=?",
template.getName(),
template.getDataType(),
template.getOid(),
@ -149,7 +136,7 @@ public class SnmpDao extends AbstractDao {
}
public void deleteSnmpTemplate(int id) {
if (deleteById("snmp_oid_template", id)) {
if (deleteById(TABLE_TEMPLATE, id)) {
LogFile.printInfo("SNMP template ID " + id + " removed from database");
} else {
LogFile.printErr("SNMP template ID " + id + " not removed from database");

View File

@ -7,15 +7,17 @@ import java.util.List;
import jnet.lib.LogFile;
public class UserDao extends AbstractDao {
public static final String TABLE = "users";
public boolean verifyUser(String username, String plainPassword) {
String sql = "SELECT password FROM users WHERE username = ?";
String sql = "SELECT password FROM " + TABLE + " WHERE username = ?";
String storedHash = querySingle(sql, rs -> rs.getString("password"), username);
return storedHash != null && PasswordHashing.verify(plainPassword, storedHash);
}
public User getUserInfo(String username) {
String sql = "SELECT * FROM users WHERE username = ?";
String sql = "SELECT * FROM " + TABLE + " WHERE username = ?";
return querySingle(sql, rs -> new User(
rs.getInt("id"),
rs.getString("username"),
@ -31,7 +33,7 @@ public class UserDao extends AbstractDao {
}
public List<User> getUsers() {
String sql = "SELECT * FROM users";
String sql = "SELECT * FROM " + TABLE;
return queryList(sql, rs -> new User(
rs.getInt("id"),
rs.getString("username"),
@ -47,7 +49,7 @@ public class UserDao extends AbstractDao {
}
public int addUser(User user) {
String sql = "INSERT INTO users (username, password, add_map, remove_map, edit_map, "
String sql = "INSERT INTO " + TABLE + " (username, password, add_map, remove_map, edit_map, "
+ "add_object, remove_object, edit_object, edit_server) VALUES (?,?,?,?,?,?,?,?,?)";
return insertAndGetId(sql,
user.getUsername(), user.getPassword(),
@ -62,7 +64,7 @@ public class UserDao extends AbstractDao {
if (u.getPassword() == null) {
executeUpdate(
//edit bez zmeny hesla
"UPDATE users SET username=?, add_map=?, remove_map=?, edit_map=?, add_object=?, remove_object=?, edit_object=?, edit_server=? WHERE id=?",
"UPDATE " + TABLE + " SET username=?, add_map=?, remove_map=?, edit_map=?, add_object=?, remove_object=?, edit_object=?, edit_server=? WHERE id=?",
u.getUsername(),
(u.isAddMap() ? 1 : 0),
(u.isRemoveMap() ? 1 : 0),
@ -76,7 +78,7 @@ public class UserDao extends AbstractDao {
} else {
executeUpdate(
// edit se zmenou hesla
"UPDATE users SET username=?, password=?, add_map=?, remove_map=?, edit_map=?, add_object=?, remove_object=?, edit_object=?, edit_server=? WHERE id=?",
"UPDATE " + TABLE + " SET username=?, password=?, add_map=?, remove_map=?, edit_map=?, add_object=?, remove_object=?, edit_object=?, edit_server=? WHERE id=?",
u.getUsername(),
u.getPassword(),
(u.isAddMap() ? 1 : 0),
@ -96,7 +98,7 @@ public class UserDao extends AbstractDao {
}
public void deleteUser(int userId) {
if (deleteById("users", userId)) {
if (deleteById(TABLE, userId)) {
LogFile.printInfo("User ID " + userId + " removed from database");
} else {
LogFile.printErr("User ID " + userId + " not removed from database");