Added code to load users from an array of userIds, return as a HashIntMap
keyed on userid. Pulled some code out of loadNames into a function so I could use it as well. Moved loadAllRealNames up higher in the file since it is a public function. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1218 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: UserRepository.java,v 1.26 2003/09/04 02:40:00 eric Exp $
|
||||
// $Id: UserRepository.java,v 1.27 2003/09/16 18:37:31 eric Exp $
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001 Michael Bayne
|
||||
@@ -235,6 +235,40 @@ public class UserRepository extends JORARepository
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up users by userid
|
||||
*
|
||||
* @return the users whom have a user id in the userIds array.
|
||||
*/
|
||||
public HashIntMap loadUsersFromId (int[] userIds)
|
||||
throws PersistenceException
|
||||
{
|
||||
// make sure we actually have something to do
|
||||
if (userIds.length == 0) {
|
||||
return new HashIntMap();
|
||||
}
|
||||
|
||||
final String ids = genIdString(userIds);
|
||||
|
||||
return (HashIntMap)execute(new Operation () {
|
||||
public Object invoke (Connection conn, DatabaseLiaison liaison)
|
||||
throws PersistenceException, SQLException
|
||||
{
|
||||
// look up the users
|
||||
Cursor ec = _utable.select("where userid in (" + ids + ")");
|
||||
|
||||
User user;
|
||||
HashIntMap data = new HashIntMap();
|
||||
while ((user = (User)ec.next()) != null) {
|
||||
user.setDirtyMask(_utable.getFieldMask());
|
||||
data.put(user.userId, user);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads up a user record that matches the specified where clause.
|
||||
* Returns null if no record matches.
|
||||
@@ -417,60 +451,6 @@ public class UserRepository extends JORARepository
|
||||
return loadNames(userIds, "realname");
|
||||
}
|
||||
|
||||
protected String[] loadNames (int[] userIds, final String column)
|
||||
throws PersistenceException
|
||||
{
|
||||
// if userids is zero length, we've got no work to do
|
||||
if (userIds.length == 0) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
// build up the string we need for the query
|
||||
final StringBuffer ids = new StringBuffer();
|
||||
for (int i = 0; i < userIds.length; i++) {
|
||||
if (ids.length() > 0) {
|
||||
ids.append(", ");
|
||||
}
|
||||
ids.append(userIds[i]);
|
||||
}
|
||||
|
||||
final HashIntMap map = new HashIntMap();
|
||||
|
||||
// do the query
|
||||
execute(new Operation () {
|
||||
public Object invoke (Connection conn, DatabaseLiaison liaison)
|
||||
throws PersistenceException, 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 {
|
||||
JDBCUtil.close(stmt);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// finally construct our result
|
||||
String[] result = new String[userIds.length];
|
||||
for (int i = 0; i < userIds.length; i++) {
|
||||
result[i] = (String)map.get(userIds[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the real names of every user in the system.
|
||||
* This is for Paul who whined about not knowing who was using Who,
|
||||
@@ -510,6 +490,73 @@ public class UserRepository extends JORARepository
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String[] loadNames (int[] userIds, final String column)
|
||||
throws PersistenceException
|
||||
{
|
||||
// if userids is zero length, we've got no work to do
|
||||
if (userIds.length == 0) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
final String ids = genIdString(userIds);
|
||||
|
||||
final HashIntMap map = new HashIntMap();
|
||||
|
||||
// do the query
|
||||
execute(new Operation () {
|
||||
public Object invoke (Connection conn, DatabaseLiaison liaison)
|
||||
throws PersistenceException, 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 {
|
||||
JDBCUtil.close(stmt);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// finally construct our result
|
||||
String[] result = new String[userIds.length];
|
||||
for (int i = 0; i < userIds.length; i++) {
|
||||
result[i] = (String)map.get(userIds[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Take the passed in int array and create the a string suitable for
|
||||
* using in a SQL set query (I.e., "select foo, from bar where userid
|
||||
* in (genIdString(userIds))"; )
|
||||
*/
|
||||
protected String genIdString (int[] userIds)
|
||||
{
|
||||
// build up the string we need for the query
|
||||
StringBuffer ids = new StringBuffer();
|
||||
for (int i = 0; i < userIds.length; i++) {
|
||||
if (ids.length() > 0) {
|
||||
ids.append(",");
|
||||
}
|
||||
ids.append(userIds[i]);
|
||||
}
|
||||
|
||||
return ids.toString();
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
Properties props = new Properties();
|
||||
|
||||
Reference in New Issue
Block a user