use of henplus.SessionManager in project henplus by neurolabs.
the class TableDiffCommand method complete.
/*
* (non-Javadoc)
*
* @see henplus.Command#complete(henplus.CommandDispatcher,
* java.lang.String, java.lang.String)
*/
@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
final StringTokenizer st = new StringTokenizer(partialCommand);
// skip cmd.
st.nextToken();
int argIndex = st.countTokens();
/*
* the following input is given: "command token1 [TAB_PRESSED]" in this
* case the partialCommand is "command token1", the last word has a
* length 0!
*
* another input: "command toke[TAB_PRESSED]" then the partialCommand is
* "command toke", the last word is "toke".
*/
if (lastWord.length() > 0) {
argIndex--;
}
// check completion for --singledb
if (argIndex == 0 && lastWord.startsWith("-")) {
return new Iterator<String>() {
private boolean _next = true;
@Override
public boolean hasNext() {
return _next;
}
@Override
public String next() {
_next = false;
return OPTION_SINGLE_DB;
}
@Override
public void remove() {
/* do nothing */
}
};
} else if (partialCommand.indexOf(OPTION_SINGLE_DB) != -1 && argIndex > 0) {
final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
final SQLSession session = sessionManager.getCurrentSession();
final HashSet<String> alreadyGiven = new HashSet<String>();
while (st.hasMoreElements()) {
alreadyGiven.add(st.nextToken());
}
final ListUserObjectsCommand objectList = HenPlus.getInstance().getObjectLister();
final Iterator<String> iter = objectList.completeTableName(session, lastWord);
return new Iterator<String>() {
String table = null;
@Override
public boolean hasNext() {
while (iter.hasNext()) {
table = iter.next();
if (alreadyGiven.contains(table) && !lastWord.equals(table)) {
continue;
}
return true;
}
return false;
}
@Override
public String next() {
return table;
}
@Override
public void remove() {
throw new UnsupportedOperationException("no!");
}
};
} else if (partialCommand.indexOf(OPTION_SINGLE_DB) == -1 && argIndex == 0) {
// !singledb && process the first session
return HenPlus.getInstance().getSessionManager().completeSessionName(lastWord);
} else if (partialCommand.indexOf(OPTION_SINGLE_DB) == -1 && argIndex == 1) {
// !singledb && process the second session
final String firstSession = st.nextToken();
return getSecondSessionCompleter(lastWord, firstSession);
} else if (argIndex > 1) {
// process tables
final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
final SQLSession first = sessionManager.getSessionByName(st.nextToken());
final SQLSession second = sessionManager.getSessionByName(st.nextToken());
final HashSet<String> alreadyGiven = new HashSet<String>();
while (st.hasMoreElements()) {
alreadyGiven.add(st.nextToken());
}
final ListUserObjectsCommand objectList = HenPlus.getInstance().getObjectLister();
final Iterator<String> firstIter = objectList.completeTableName(first, lastWord);
final Iterator<String> secondIter = objectList.completeTableName(second, lastWord);
final Iterator<String> intersectionIter = getIntersection(firstIter, secondIter);
return new Iterator<String>() {
String table = null;
@Override
public boolean hasNext() {
while (intersectionIter.hasNext()) {
table = intersectionIter.next();
if (alreadyGiven.contains(table) && !lastWord.equals(table)) {
continue;
}
return true;
}
return false;
}
@Override
public String next() {
return table;
}
@Override
public void remove() {
throw new UnsupportedOperationException("no!");
}
};
}
return null;
}
use of henplus.SessionManager in project henplus by neurolabs.
the class TableDiffCommand method executeDoubleDb.
private int executeDoubleDb(final StringTokenizer st, final boolean colNameIgnoreCase) {
if (st.countTokens() < 3) {
return SYNTAX_ERROR;
}
final SessionManager sessionManager = HenPlus.getInstance().getSessionManager();
if (sessionManager.getSessionCount() < 2) {
Logger.error("You need two valid sessions for this command.");
return SYNTAX_ERROR;
}
final SQLSession first = sessionManager.getSessionByName(st.nextToken());
final SQLSession second = sessionManager.getSessionByName(st.nextToken());
if (first == null || second == null) {
Logger.error("You need two valid sessions for this command.");
return EXEC_FAILED;
} else if (first == second) {
Logger.error("You should specify two different sessions for this command.");
return EXEC_FAILED;
} else if (!st.hasMoreTokens()) {
Logger.error("You should specify at least one table.");
return EXEC_FAILED;
}
try {
final long start = System.currentTimeMillis();
int count = 0;
final ListUserObjectsCommand objectLister = HenPlus.getInstance().getObjectLister();
final SortedSet<String> tablesOne = objectLister.getTableNamesForSession(first);
final SortedSet<String> tablesTwo = objectLister.getTableNamesForSession(second);
// which tables got already
final Set<String> alreadyDiffed = new HashSet<String>();
// diffed?
/*
* which tables are found in the first session via wildcards but are
* not contained in the second session?
*/
final ArrayList<String> missedFromWildcards = new ArrayList<String>();
while (st.hasMoreTokens()) {
final String nextToken = st.nextToken();
if ("*".equals(nextToken) || nextToken.indexOf('*') > -1) {
Iterator<String> iter = null;
if ("*".equals(nextToken)) {
iter = objectLister.getTableNamesIteratorForSession(first);
} else if (nextToken.indexOf('*') > -1) {
final String tablePrefix = nextToken.substring(0, nextToken.length() - 1);
final NameCompleter compl = new NameCompleter(tablesOne);
iter = compl.getAlternatives(tablePrefix);
}
while (iter.hasNext()) {
final String objTableName = iter.next();
count = diffConditionally(objTableName, colNameIgnoreCase, first, second, tablesTwo, alreadyDiffed, missedFromWildcards, count);
}
} else if (!alreadyDiffed.contains(nextToken)) {
diffTable(first, second, nextToken, colNameIgnoreCase);
alreadyDiffed.add(nextToken);
count++;
}
}
final StringBuilder msg = new StringBuilder();
msg.append("Diffing ").append(count).append(count == 1 ? " table took " : " tables took ").append(System.currentTimeMillis() - start).append(" ms.");
// the user know this.
if (missedFromWildcards.size() > 0) {
msg.append("\nTables which matched a given wildcard in your first\n" + "session but were not found in your second session:\n");
for (String missed : missedFromWildcards) {
msg.append(missed).append(", ");
}
// remove the last two chars
msg.delete(msg.length() - 2, msg.length());
}
Logger.info(msg.toString());
} catch (final Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
Aggregations