Skip to content

Commit

Permalink
Merge pull request apache#64 from yanpingyou/youyou
Browse files Browse the repository at this point in the history
Add getCommandsTableItem() in DBAdapter
  • Loading branch information
littlezhou authored May 5, 2017
2 parents bddab7f + f1eb9ce commit ad03708
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import org.apache.hadoop.ssm.actions.ActionType;

public class CommandInfo {
private int cid;
private int rid;
private long cid;
private long rid;
private ActionType actionId;
private CommandState state;
private String parameters;
private long generateTime;
private long stateChangedTime;

public CommandInfo(int cid, int rid, ActionType actionId, CommandState state
, String parameters, long generateTime, long stateChangedTime) {
public CommandInfo(long cid, long rid, ActionType actionId, CommandState state,
String parameters, long generateTime, long stateChangedTime) {
this.cid = cid;
this.rid = rid;
this.actionId = actionId;
Expand All @@ -40,13 +40,13 @@ public CommandInfo(int cid, int rid, ActionType actionId, CommandState state
this.stateChangedTime = stateChangedTime;
}

public int getCid() {
public long getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getRid() {
public long getRid() {
return rid;
}
public void setRid(int rid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,22 @@ public synchronized void insertCommandsTable(CommandInfo[] commands) {
}
}

public CommandInfo getCommands(String sql) {
public List<CommandInfo> getCommandsTableItem(String cidCondition, String ridCondition,
CommandState state) {
String sqlPrefix = "SELECT * FROM commands WHERE ";
String sqlFromCid = (cidCondition == null) ? "" : "AND cid " + cidCondition;
String sqlFromRid = (ridCondition == null) ? "" : "AND rid " + ridCondition;
String sqlFromState = (state == null) ? "" : "AND state = " + state;
String sqlFinal = "";
if (cidCondition != null || ridCondition != null || state != null) {
sqlFinal = sqlPrefix + sqlFromCid + sqlFromRid + sqlFromState;
sqlFinal = sqlFinal.replaceFirst("AND ", "");
return getCommands(sqlFinal);
}
return null;
}

private List<CommandInfo> getCommands(String sql) {
ResultSet result;
try {
result = executeQuery(sql);
Expand All @@ -602,7 +617,7 @@ public CommandInfo getCommands(String sql) {
e.printStackTrace();
}
}
return ret.size() > 0 ? ret.get(0) : null;
return ret;
}

private List<CommandInfo> convertCommandsTableItem(ResultSet resultSet) {
Expand All @@ -613,8 +628,8 @@ private List<CommandInfo> convertCommandsTableItem(ResultSet resultSet) {
try {
while (resultSet.next()) {
CommandInfo commands = new CommandInfo(
resultSet.getInt("cid"),
resultSet.getInt("rid"),
resultSet.getLong("cid"),
resultSet.getLong("rid"),
ActionType.fromValue((int)resultSet.getByte("action_id")),
CommandState.fromValue((int)resultSet.getByte("state")),
resultSet.getString("parameters"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ public void testInsertCommandsTable() throws Exception {
CommandState.EXECUTING, "test", 123123333l, 232444444l);
CommandInfo command2 = new CommandInfo(0, 78, ActionType.ConvertToEC,
CommandState.PAUSED, "tt", 123178333l, 232444994l);
CommandInfo[] commands = {command1, command2 };
CommandInfo[] commands = {command1, command2};
dbAdapter.insertCommandsTable(commands);
int cid = 1;
String sql = "SELECT * FROM commands WHERE cid = '" + cid + "'";
CommandInfo com = dbAdapter.getCommands(sql);
Assert.assertTrue(com.getActionId() == ActionType.None);
String cidCondition = ">= 2 ";
String ridCondition = "= 78 ";
CommandState state = null;
List<CommandInfo> com = dbAdapter.getCommandsTableItem(cidCondition, ridCondition, state);
Assert.assertTrue(com.get(0).getActionId() == ActionType.ConvertToEC);
if (conn != null) {
conn.close();
}
Expand Down

0 comments on commit ad03708

Please sign in to comment.