use of henplus.SQLSession in project henplus by neurolabs.
the class SetCommand method registerLastCommandListener.
public void registerLastCommandListener(final CommandDispatcher dispatcher) {
_specialVariables.add(SPECIAL_LAST_COMMAND);
dispatcher.addExecutionListener(new ExecutionListener() {
@Override
public void beforeExecution(final SQLSession session, final String command) {
}
@Override
public void afterExecution(final SQLSession session, final String command, final int result) {
setVariable(SPECIAL_LAST_COMMAND, command.trim());
}
});
}
use of henplus.SQLSession in project henplus by neurolabs.
the class ConnectCommand method execute.
/**
* execute the command given.
*/
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
SQLSession session = null;
final StringTokenizer st = new StringTokenizer(param);
final int argc = st.countTokens();
if ("sessions".equals(cmd)) {
showSessions();
return SUCCESS;
} else if ("connect".equals(cmd)) {
if (argc < 1 || argc > 2) {
return SYNTAX_ERROR;
}
String url = (String) st.nextElement();
String alias = argc == 2 ? st.nextToken() : null;
if (alias == null) {
/*
* we only got one parameter. So the that single parameter might
* have been an alias. let's see..
*/
if (_knownUrls.containsKey(url)) {
final String possibleAlias = url;
url = _knownUrls.get(url);
if (!possibleAlias.equals(url)) {
alias = possibleAlias;
}
}
}
try {
session = new SQLSession(url, null, null);
_knownUrls.put(url, url);
if (alias != null) {
_knownUrls.put(alias, url);
}
_currentSessionName = createSessionName(session, alias);
_sessionManager.addSession(_currentSessionName, session);
_sessionManager.setCurrentSession(session);
} catch (final Exception e) {
HenPlus.msg().println(e.toString());
return EXEC_FAILED;
}
} else if ("switch".equals(cmd)) {
String sessionName = null;
if (argc != 1 && _sessionManager.getSessionCount() != 2) {
return SYNTAX_ERROR;
}
if (argc == 0 && _sessionManager.getSessionCount() == 2) {
final Iterator<String> it = _sessionManager.getSessionNames().iterator();
while (it.hasNext()) {
sessionName = it.next();
if (!sessionName.equals(_currentSessionName)) {
break;
}
}
} else {
sessionName = (String) st.nextElement();
}
session = _sessionManager.getSessionByName(sessionName);
if (session == null) {
HenPlus.msg().println("'" + sessionName + "': no such session");
return EXEC_FAILED;
}
_currentSessionName = sessionName;
} else if ("rename-session".equals(cmd)) {
String sessionName = null;
if (argc != 1) {
return SYNTAX_ERROR;
}
sessionName = (String) st.nextElement();
if (sessionName.length() < 1) {
return SYNTAX_ERROR;
}
/*
* // moved to sessionmanager.renameSession
*
* if (_sessionManager.sessionNameExists(sessionName)) {
* HenPlus.err().println("A session with that name already exists");
* return EXEC_FAILED; }
*
* session =
* _sessionManager.removeSessionWithName(currentSessionName); if
* (session == null) { return EXEC_FAILED; }
* _sessionManager.addSession(sessionName, session);
*/
final int renamed = _sessionManager.renameSession(_currentSessionName, sessionName);
if (renamed == EXEC_FAILED) {
return EXEC_FAILED;
}
_currentSessionName = sessionName;
session = _sessionManager.getCurrentSession();
} else if ("disconnect".equals(cmd)) {
_currentSessionName = null;
if (argc != 0) {
return SYNTAX_ERROR;
}
_sessionManager.closeCurrentSession();
HenPlus.msg().println("session closed.");
if (_sessionManager.hasSessions()) {
_currentSessionName = _sessionManager.getFirstSessionName();
session = _sessionManager.getSessionByName(_currentSessionName);
}
}
if (_currentSessionName != null) {
_henplus.setPrompt(_currentSessionName + "> ");
} else {
_henplus.setDefaultPrompt();
}
_henplus.setCurrentSession(session);
return SUCCESS;
}
use of henplus.SQLSession in project henplus by neurolabs.
the class ListUserObjectsCommand method completeAllColumns.
public Iterator<String> completeAllColumns(final String partialColumn) {
final SQLSession session = _henplus.getCurrentSession();
if (session == null) {
return null;
}
final NameCompleter completer = getAllColumnsCompleter(session);
return completer.getAlternatives(partialColumn);
}
use of henplus.SQLSession in project henplus by neurolabs.
the class ListUserObjectsCommand method columnsFor.
/**
* fixme: add this to the cached values determined by rehash.
*/
public Collection<String> columnsFor(String tabName) {
final SQLSession session = _henplus.getCurrentSession();
final Set<String> result = new HashSet<String>();
// use createStmt
final Connection conn = session.getConnection();
ResultSet rset = null;
String schema = null;
final int schemaDelim = tabName.indexOf('.');
if (schemaDelim > 0) {
schema = tabName.substring(0, schemaDelim);
tabName = tabName.substring(schemaDelim + 1);
}
try {
final DatabaseMetaData meta = conn.getMetaData();
rset = meta.getColumns(conn.getCatalog(), schema, tabName, null);
while (rset.next()) {
result.add(rset.getString(4));
}
} catch (final Exception e) {
// ignore.
} finally {
if (rset != null) {
try {
rset.close();
} catch (final Exception e) {
}
}
}
return result;
}
use of henplus.SQLSession in project henplus by neurolabs.
the class ConnectCommand method connect.
/**
* @param url
* @param username
* @param password
* @throws ClassNotFoundException
* @throws SQLException
* @throws IOException
*/
private void connect(final String url, final String username, final String password) throws ClassNotFoundException, SQLException, IOException {
SQLSession session;
session = new SQLSession(url, username, password);
_currentSessionName = createSessionName(session, null);
_sessionManager.addSession(_currentSessionName, session);
_knownUrls.put(url, url);
_henplus.setPrompt(_currentSessionName + "> ");
_sessionManager.setCurrentSession(session);
}
Aggregations