Skip to content

Commit

Permalink
Fix params on DBAgent update multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
andreldsr committed Mar 8, 2024
1 parent 6926a51 commit 0314f08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
*/
package com.pinterest.deployservice.db;

import com.pinterest.deployservice.bean.*;

import com.pinterest.deployservice.bean.AgentBean;
import com.pinterest.deployservice.bean.AgentState;
import com.pinterest.deployservice.bean.DeployStage;
import com.pinterest.deployservice.bean.SetClause;
import com.pinterest.deployservice.dao.AgentDAO;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.*;
import java.util.Collection;
import java.util.List;


public class DBAgentDAOImpl implements AgentDAO {
private static final String UPDATE_AGENT_TEMPLATE =
"UPDATE agents SET %s WHERE host_id=? AND env_id=?";
private static final String UPDATE_AGENTS_BY_HOSTIDS =
"UPDATE agents SET %s WHERE host_id IN (?) AND env_id=?";
"UPDATE agents SET %s WHERE host_id IN (%s) AND env_id=?";
private static final String UPDATE_AGENT_BY_ID_TEMPLATE =
"UPDATE agents SET %s WHERE host_id=?";
private static final String RESET_FAILED_AGENTS =
Expand Down Expand Up @@ -107,8 +113,12 @@ public void update(String hostId, String envId, AgentBean agentBean) throws Exce
@Override
public void updateMultiple(Collection<String> hostIds, String envId, AgentBean agentBean) throws Exception {
SetClause setClause = agentBean.genSetClause();
String clause = String.format(UPDATE_AGENTS_BY_HOSTIDS, setClause.getClause());
new QueryRunner(dataSource).update(clause, setClause.getValueArray(), hostIds, envId);
String clause = String.format(UPDATE_AGENTS_BY_HOSTIDS, setClause.getClause(), QueryUtils.genStringListParamClause(hostIds));
for(String hostId : hostIds) {
setClause.addValue(hostId);
}
setClause.addValue(envId);
new QueryRunner(dataSource).update(clause, setClause.getValueArray());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public static String genStringGroupClause(Collection<String> names) {
return sb.toString();
}

public static String genStringListParamClause(Collection<String> names) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < names.size(); i++) {
sb.append("?,");
}
if(sb.length()>0)
sb.setLength(sb.length() - 1);
return sb.toString();
}

public static <E extends Enum<E>>String genEnumGroupClause(Collection<E> names) {
StringBuilder sb = new StringBuilder();
for (E name : names) {
Expand Down

0 comments on commit 0314f08

Please sign in to comment.