From 0708119f2944f53fc058188ff4643bf41728958d Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 2 Mar 2001 07:36:00 +0000 Subject: [PATCH] Added loadUserNames() and loadRealNames() for loading the user and real names of a list of users who's userids are known. git-svn-id: https://samskivert.googlecode.com/svn/trunk@78 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../servlet/user/UserRepository.java | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/servlet/user/UserRepository.java b/projects/samskivert/src/java/com/samskivert/servlet/user/UserRepository.java index fb5b9ab3..be4f96a6 100644 --- a/projects/samskivert/src/java/com/samskivert/servlet/user/UserRepository.java +++ b/projects/samskivert/src/java/com/samskivert/servlet/user/UserRepository.java @@ -1,5 +1,5 @@ // -// $Id: UserRepository.java,v 1.3 2001/03/02 02:38:31 mdb Exp $ +// $Id: UserRepository.java,v 1.4 2001/03/02 07:36:00 mdb Exp $ package com.samskivert.servlet.user; @@ -12,6 +12,7 @@ import org.apache.regexp.*; import com.samskivert.Log; import com.samskivert.jdbc.MySQLRepository; import com.samskivert.jdbc.jora.*; +import com.samskivert.util.IntMap; public class UserRepository extends MySQLRepository { @@ -243,6 +244,61 @@ public class UserRepository extends MySQLRepository }); } + /** + * Loads the usernames of the users identified by the supplied user + * ids and returns them in an array. If any users do not exist, their + * slot in the array will contain a null. + */ + public String[] loadUserNames (int[] userids) + throws SQLException + { + return loadNames(userids, "username"); + } + + /** + * Loads the real names of the users identified by the supplied user + * ids and returns them in an array. If any users do not exist, their + * slot in the array will contain a null. + */ + public String[] loadRealNames (int[] userids) + throws SQLException + { + return loadNames(userids, "realname"); + } + + protected String[] loadNames (int[] userids, String column) + throws SQLException + { + // 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]); + } + + // do the query + IntMap map = new IntMap(); + Statement stmt = _session.connection.createStatement(); + ResultSet rs = stmt.executeQuery("select userid, " + column + + "from users " + + "where userid in (" + ids + ")"); + while (rs.next()) { + int userid = rs.getInt(1); + String name = rs.getString(2); + map.put(userid, name); + } + + // 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; + } + public static void main (String[] args) { Properties props = new Properties();