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,14 +108,15 @@ 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
|
// look up the user
|
||||||
Cursor ec = _utable.select("where username = '" + username + "'");
|
String query = "where username = '" + username + "'";
|
||||||
|
Cursor ec = _utable.select(query);
|
||||||
|
|
||||||
// fetch the user from the cursor
|
// fetch the user from the cursor
|
||||||
User user = (User)ec.next();
|
User user = (User)ec.next();
|
||||||
@@ -124,6 +127,8 @@ public class UserRepository extends MySQLRepository
|
|||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Looks up a user by userid.
|
* Looks up a user by userid.
|
||||||
@@ -131,12 +136,12 @@ 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
|
// look up the user
|
||||||
Cursor ec = _utable.select("where userid = " + userid);
|
Cursor ec = _utable.select("where userid = " + userid);
|
||||||
|
|
||||||
@@ -149,6 +154,8 @@ public class UserRepository extends MySQLRepository
|
|||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Looks up a user by a session identifier.
|
* Looks up a user by a session identifier.
|
||||||
@@ -156,16 +163,16 @@ 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
|
// look up the user
|
||||||
Cursor ec = _utable.select("sessions",
|
Cursor ec = _utable.select("sessions", query);
|
||||||
"where authcode = '" + sessionKey +
|
|
||||||
"' AND sessions.userid = users.userid");
|
|
||||||
|
|
||||||
// fetch the user from the cursor
|
// fetch the user from the cursor
|
||||||
User user = (User)ec.next();
|
User user = (User)ec.next();
|
||||||
@@ -176,6 +183,8 @@ public class UserRepository extends MySQLRepository
|
|||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a user that was previously fetched from the repository.
|
* Updates a user that was previously fetched from the repository.
|
||||||
@@ -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,8 +310,12 @@ public class UserRepository extends MySQLRepository
|
|||||||
ids.append(userids[i]);
|
ids.append(userids[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final IntMap map = new IntMap();
|
||||||
|
|
||||||
// do the query
|
// do the query
|
||||||
IntMap map = new IntMap();
|
execute(new Operation () {
|
||||||
|
public Object invoke () throws SQLException
|
||||||
|
{
|
||||||
Statement stmt = _session.connection.createStatement();
|
Statement stmt = _session.connection.createStatement();
|
||||||
try {
|
try {
|
||||||
String query = "select userid, " + column +
|
String query = "select userid, " + column +
|
||||||
@@ -310,9 +328,14 @@ public class UserRepository extends MySQLRepository
|
|||||||
map.put(userid, name);
|
map.put(userid, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nothing to return
|
||||||
|
return null;
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
stmt.close();
|
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