Modified Repository.Operation.invoke() to return a value so that it can be
used easily for database lookups as well as database stores. Modified UserRepository to conform to the new signature as well as to use execute() for all database operations. git-svn-id: https://samskivert.googlecode.com/svn/trunk@137 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: Repository.java,v 1.6 2001/05/26 04:49:31 mdb Exp $
|
// $Id: Repository.java,v 1.7 2001/06/01 07:27:59 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.jdbc;
|
package com.samskivert.jdbc;
|
||||||
|
|
||||||
@@ -168,18 +168,20 @@ public abstract class Repository
|
|||||||
*/
|
*/
|
||||||
protected interface Operation
|
protected interface Operation
|
||||||
{
|
{
|
||||||
public void invoke () throws SQLException;
|
public Object invoke () throws SQLException;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the supplied operation. In the event of a transient
|
* Executes the supplied operation. In the event of a transient
|
||||||
* failure, the repository will attempt to reestablish the database
|
* failure, the repository will attempt to reestablish the database
|
||||||
* connection and try the operation again.
|
* connection and try the operation again.
|
||||||
|
*
|
||||||
|
* @return whatever value is returned by the invoked operation.
|
||||||
*/
|
*/
|
||||||
protected void execute (Operation op)
|
protected Object execute (Operation op)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
execute(op, true);
|
return execute(op, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,22 +193,29 @@ public abstract class Repository
|
|||||||
* to a transient failure (like losing the connection to the database
|
* to a transient failure (like losing the connection to the database
|
||||||
* or deadlock detection), the connection to the database will be
|
* or deadlock detection), the connection to the database will be
|
||||||
* reestablished and the operation attempted once more.
|
* reestablished and the operation attempted once more.
|
||||||
|
*
|
||||||
|
* @return whatever value is returned by the invoked operation.
|
||||||
*/
|
*/
|
||||||
protected void execute (Operation op, boolean retryOnTransientFailure)
|
protected Object execute (Operation op, boolean retryOnTransientFailure)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
|
Object rv = null;
|
||||||
|
|
||||||
// make sure our connection is established
|
// make sure our connection is established
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// invoke the operation
|
// invoke the operation
|
||||||
op.invoke();
|
rv = op.invoke();
|
||||||
|
|
||||||
// commit the session
|
// commit the session
|
||||||
if (supportsTransactions()) {
|
if (supportsTransactions()) {
|
||||||
_session.commit();
|
_session.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return the operation result
|
||||||
|
return rv;
|
||||||
|
|
||||||
} catch (SQLException sqe) {
|
} catch (SQLException sqe) {
|
||||||
// back out our changes if something got hosed
|
// back out our changes if something got hosed
|
||||||
if (supportsTransactions()) {
|
if (supportsTransactions()) {
|
||||||
@@ -227,7 +236,7 @@ public abstract class Repository
|
|||||||
// to reset the connection, we simply close it. it will be
|
// to reset the connection, we simply close it. it will be
|
||||||
// reopened on our subsequent call to execute()
|
// reopened on our subsequent call to execute()
|
||||||
shutdown();
|
shutdown();
|
||||||
execute(op, false);
|
return execute(op, false);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw sqe;
|
throw sqe;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: UserRepository.java,v 1.9 2001/05/26 07:09:38 mdb Exp $
|
// $Id: UserRepository.java,v 1.10 2001/06/01 07:27:59 mdb Exp $
|
||||||
|
|
||||||
package com.samskivert.servlet.user;
|
package com.samskivert.servlet.user;
|
||||||
|
|
||||||
@@ -81,11 +81,13 @@ public class UserRepository extends MySQLRepository
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
execute(new Operation () {
|
execute(new Operation () {
|
||||||
public void invoke () throws SQLException
|
public Object invoke () throws SQLException
|
||||||
{
|
{
|
||||||
_utable.insert(user);
|
_utable.insert(user);
|
||||||
// update the userid now that it's known
|
// update the userid now that it's known
|
||||||
user.userid = lastInsertedId();
|
user.userid = lastInsertedId();
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -106,23 +108,26 @@ public class UserRepository extends MySQLRepository
|
|||||||
* @return the user with the specified user id or null if no user with
|
* @return the user with the specified user id or null if no user with
|
||||||
* that id exists.
|
* that id exists.
|
||||||
*/
|
*/
|
||||||
public User loadUser (String username)
|
public User loadUser (final String username)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// make sure our session is established
|
return (User)execute(new Operation () {
|
||||||
ensureConnection();
|
public Object invoke () throws SQLException
|
||||||
|
{
|
||||||
|
// look up the user
|
||||||
|
String query = "where username = '" + username + "'";
|
||||||
|
Cursor ec = _utable.select(query);
|
||||||
|
|
||||||
// look up the user
|
// fetch the user from the cursor
|
||||||
Cursor ec = _utable.select("where username = '" + username + "'");
|
User user = (User)ec.next();
|
||||||
|
if (user != null) {
|
||||||
|
// call next() again to cause the cursor to close itself
|
||||||
|
ec.next();
|
||||||
|
}
|
||||||
|
|
||||||
// fetch the user from the cursor
|
return user;
|
||||||
User user = (User)ec.next();
|
}
|
||||||
if (user != null) {
|
});
|
||||||
// call next() again to cause the cursor to close itself
|
|
||||||
ec.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,23 +136,25 @@ public class UserRepository extends MySQLRepository
|
|||||||
* @return the user with the specified user id or null if no user with
|
* @return the user with the specified user id or null if no user with
|
||||||
* that id exists.
|
* that id exists.
|
||||||
*/
|
*/
|
||||||
public User loadUser (int userid)
|
public User loadUser (final int userid)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// make sure our session is established
|
return (User)execute(new Operation () {
|
||||||
ensureConnection();
|
public Object invoke () throws SQLException
|
||||||
|
{
|
||||||
|
// look up the user
|
||||||
|
Cursor ec = _utable.select("where userid = " + userid);
|
||||||
|
|
||||||
// look up the user
|
// fetch the user from the cursor
|
||||||
Cursor ec = _utable.select("where userid = " + userid);
|
User user = (User)ec.next();
|
||||||
|
if (user != null) {
|
||||||
|
// call next() again to cause the cursor to close itself
|
||||||
|
ec.next();
|
||||||
|
}
|
||||||
|
|
||||||
// fetch the user from the cursor
|
return user;
|
||||||
User user = (User)ec.next();
|
}
|
||||||
if (user != null) {
|
});
|
||||||
// call next() again to cause the cursor to close itself
|
|
||||||
ec.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,25 +163,27 @@ public class UserRepository extends MySQLRepository
|
|||||||
* @return the user associated with the specified session or null of
|
* @return the user associated with the specified session or null of
|
||||||
* no session exists with the supplied identifier.
|
* no session exists with the supplied identifier.
|
||||||
*/
|
*/
|
||||||
public User loadUserBySession (String sessionKey)
|
public User loadUserBySession (final String sessionKey)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// make sure our session is established
|
return (User)execute(new Operation () {
|
||||||
ensureConnection();
|
public Object invoke () throws SQLException
|
||||||
|
{
|
||||||
|
String query = "where authcode = '" + sessionKey +
|
||||||
|
"' AND sessions.userid = users.userid";
|
||||||
|
// look up the user
|
||||||
|
Cursor ec = _utable.select("sessions", query);
|
||||||
|
|
||||||
// look up the user
|
// fetch the user from the cursor
|
||||||
Cursor ec = _utable.select("sessions",
|
User user = (User)ec.next();
|
||||||
"where authcode = '" + sessionKey +
|
if (user != null) {
|
||||||
"' AND sessions.userid = users.userid");
|
// call next() again to cause the cursor to close itself
|
||||||
|
ec.next();
|
||||||
|
}
|
||||||
|
|
||||||
// fetch the user from the cursor
|
return user;
|
||||||
User user = (User)ec.next();
|
}
|
||||||
if (user != null) {
|
});
|
||||||
// call next() again to cause the cursor to close itself
|
|
||||||
ec.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,9 +193,11 @@ public class UserRepository extends MySQLRepository
|
|||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
execute(new Operation () {
|
execute(new Operation () {
|
||||||
public void invoke () throws SQLException
|
public Object invoke () throws SQLException
|
||||||
{
|
{
|
||||||
_utable.update(user);
|
_utable.update(user);
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -198,9 +209,11 @@ public class UserRepository extends MySQLRepository
|
|||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
execute(new Operation () {
|
execute(new Operation () {
|
||||||
public void invoke () throws SQLException
|
public Object invoke () throws SQLException
|
||||||
{
|
{
|
||||||
_utable.delete(user);
|
_utable.delete(user);
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -226,12 +239,14 @@ public class UserRepository extends MySQLRepository
|
|||||||
|
|
||||||
// insert the session into the database
|
// insert the session into the database
|
||||||
execute(new Operation () {
|
execute(new Operation () {
|
||||||
public void invoke () throws SQLException
|
public Object invoke () throws SQLException
|
||||||
{
|
{
|
||||||
_session.execute("insert into sessions " +
|
_session.execute("insert into sessions " +
|
||||||
"(authcode, userid, expires) values('" +
|
"(authcode, userid, expires) values('" +
|
||||||
authcode + "', " + user.userid + ", '" +
|
authcode + "', " + user.userid + ", '" +
|
||||||
expires + "')");
|
expires + "')");
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -246,10 +261,12 @@ public class UserRepository extends MySQLRepository
|
|||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
execute(new Operation () {
|
execute(new Operation () {
|
||||||
public void invoke () throws SQLException
|
public Object invoke () throws SQLException
|
||||||
{
|
{
|
||||||
_session.execute("delete from sessions where " +
|
_session.execute("delete from sessions where " +
|
||||||
"expires <= CURRENT_DATE()");
|
"expires <= CURRENT_DATE()");
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -276,19 +293,16 @@ public class UserRepository extends MySQLRepository
|
|||||||
return loadNames(userids, "realname");
|
return loadNames(userids, "realname");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String[] loadNames (int[] userids, String column)
|
protected String[] loadNames (int[] userids, final String column)
|
||||||
throws SQLException
|
throws SQLException
|
||||||
{
|
{
|
||||||
// make sure our session is established
|
|
||||||
ensureConnection();
|
|
||||||
|
|
||||||
// if userids is zero length, we've got no work to do
|
// if userids is zero length, we've got no work to do
|
||||||
if (userids.length == 0) {
|
if (userids.length == 0) {
|
||||||
return new String[0];
|
return new String[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// build up the string we need for the query
|
// build up the string we need for the query
|
||||||
StringBuffer ids = new StringBuffer();
|
final StringBuffer ids = new StringBuffer();
|
||||||
for (int i = 0; i < userids.length; i++) {
|
for (int i = 0; i < userids.length; i++) {
|
||||||
if (ids.length() > 0) {
|
if (ids.length() > 0) {
|
||||||
ids.append(", ");
|
ids.append(", ");
|
||||||
@@ -296,23 +310,32 @@ public class UserRepository extends MySQLRepository
|
|||||||
ids.append(userids[i]);
|
ids.append(userids[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// do the query
|
final IntMap map = new IntMap();
|
||||||
IntMap map = new IntMap();
|
|
||||||
Statement stmt = _session.connection.createStatement();
|
|
||||||
try {
|
|
||||||
String query = "select userid, " + column +
|
|
||||||
" from users " +
|
|
||||||
"where userid in (" + ids + ")";
|
|
||||||
ResultSet rs = stmt.executeQuery(query);
|
|
||||||
while (rs.next()) {
|
|
||||||
int userid = rs.getInt(1);
|
|
||||||
String name = rs.getString(2);
|
|
||||||
map.put(userid, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
} finally {
|
// do the query
|
||||||
stmt.close();
|
execute(new Operation () {
|
||||||
}
|
public Object invoke () throws SQLException
|
||||||
|
{
|
||||||
|
Statement stmt = _session.connection.createStatement();
|
||||||
|
try {
|
||||||
|
String query = "select userid, " + column +
|
||||||
|
" from users " +
|
||||||
|
"where userid in (" + ids + ")";
|
||||||
|
ResultSet rs = stmt.executeQuery(query);
|
||||||
|
while (rs.next()) {
|
||||||
|
int userid = rs.getInt(1);
|
||||||
|
String name = rs.getString(2);
|
||||||
|
map.put(userid, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// finally construct our result
|
// finally construct our result
|
||||||
String[] result = new String[userids.length];
|
String[] result = new String[userids.length];
|
||||||
|
|||||||
Reference in New Issue
Block a user