
org.sonar.server.user.index.UserResultSetIterator Maven / Gradle / Ivy
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.user.index;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
import org.sonar.db.DbClient;
import org.sonar.db.ResultSetIterator;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Scrolls over table USERS and reads documents to populate the user index
*/
class UserResultSetIterator extends ResultSetIterator {
private static final String[] FIELDS = {
// column 1
"u.login",
"u.name",
"u.email",
"u.active",
"u.scm_accounts",
"u.created_at",
"u.updated_at",
};
private static final String SQL_ALL = "select " + StringUtils.join(FIELDS, ",") + " from users u ";
private static final String SQL_AFTER_DATE = SQL_ALL + " where u.updated_at>?";
static UserResultSetIterator create(DbClient dbClient, DbSession session, long afterDate) {
try {
String sql = afterDate > 0L ? SQL_AFTER_DATE : SQL_ALL;
PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(session, sql);
if (afterDate > 0L) {
stmt.setLong(1, afterDate);
}
return new UserResultSetIterator(stmt);
} catch (SQLException e) {
throw new IllegalStateException("Fail to prepare SQL request to select all users", e);
}
}
private UserResultSetIterator(PreparedStatement stmt) throws SQLException {
super(stmt);
}
@Override
protected UserDoc read(ResultSet rs) throws SQLException {
UserDoc doc = new UserDoc(Maps.newHashMapWithExpectedSize(7));
String login = rs.getString(1);
// all the keys must be present, even if value is null
doc.setLogin(login);
doc.setName(rs.getString(2));
doc.setEmail(rs.getString(3));
doc.setActive(rs.getBoolean(4));
doc.setScmAccounts(UserDto.decodeScmAccounts(rs.getString(5)));
doc.setCreatedAt(rs.getLong(6));
doc.setUpdatedAt(rs.getLong(7));
return doc;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy