[Cherbot-commit] SF.net SVN: cherbot: [117] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-07-07 12:38:56
|
Revision: 117
http://svn.sourceforge.net/cherbot/?rev=117&view=rev
Author: christianhujer
Date: 2007-07-07 05:38:54 -0700 (Sat, 07 Jul 2007)
Log Message:
-----------
Added automatic setting of id in case of new records.
Modified Paths:
--------------
trunk/src/net/sf/cherbot/Server.java
trunk/src/net/sf/cherbot/ServerModule.java
Modified: trunk/src/net/sf/cherbot/Server.java
===================================================================
--- trunk/src/net/sf/cherbot/Server.java 2007-07-07 12:07:45 UTC (rev 116)
+++ trunk/src/net/sf/cherbot/Server.java 2007-07-07 12:38:54 UTC (rev 117)
@@ -41,6 +41,13 @@
this.protocol = protocol;
}
+ /** Sets the id of this server.
+ * @param id The id of this server.
+ */
+ public void setId(final int id) {
+ this.id = id;
+ }
+
/** Returns the id of this server.
* @return The id of this server.
*/
@@ -92,7 +99,7 @@
/** {@inheritDoc} */
@NotNull @Override public String toString() {
- return protocol + "://" + hostname + ":" + port + "/";
+ return protocol + "://" + hostname + ":" + port + "/ " + id;
}
} // class Server
Modified: trunk/src/net/sf/cherbot/ServerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/ServerModule.java 2007-07-07 12:07:45 UTC (rev 116)
+++ trunk/src/net/sf/cherbot/ServerModule.java 2007-07-07 12:38:54 UTC (rev 117)
@@ -45,19 +45,28 @@
final PreparedStatement lookup = con.prepareStatement("SELECT id FROM Server WHERE id = ?");
final PreparedStatement update = con.prepareStatement("UPDATE Server SET host = ?, port = ?, protocol = ? WHERE id = ?");
final PreparedStatement insert = con.prepareStatement("INSERT INTO Server (host, port, protocol) VALUES (?, ?, ?)");
+ final PreparedStatement getId = con.prepareStatement("VALUES IDENTITY_VAL_LOCAL()");
for (final Server server : servers) {
+ final boolean newRecord;
lookup.setInt(1, server.getId());
final PreparedStatement stmt;
- if (lookup.executeQuery().next()) {
+ newRecord = !lookup.executeQuery().next();
+ if (newRecord) {
+ stmt = insert;
+ } else {
stmt = update;
update.setInt(4, server.getId());
- } else {
- stmt = insert;
}
stmt.setString(1, server.getHostname());
stmt.setInt(2, server.getPort());
stmt.setString(3, server.getProtocol());
stmt.executeUpdate();
+ if (newRecord) {
+ final ResultSet rs = getId.executeQuery();
+ rs.next();
+ server.setId(rs.getInt(1));
+ rs.close();
+ }
}
lookup.close();
update.close();
@@ -85,7 +94,7 @@
/** Returns a list of servers.
* @return a list of servers
*/
- public List<Server> getServers() {
+ @NotNull public List<Server> getServers() {
return Collections.unmodifiableList(servers);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|