/*-------------------------------------------------------------------------- * Copyright 2008 utgenome.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *--------------------------------------------------------------------------*/ //-------------------------------------- // utgb-shell Project // // Query.java // Since: Jan 16, 2008 // // $URL$ // $Author$ //-------------------------------------- package org.utgenome.shell; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.List; import org.utgenome.config.ConnectionInfo; import org.utgenome.config.DatabaseInfo; import org.utgenome.config.TrackConfiguration; import org.xerial.db.DBException; import org.xerial.db.sql.DatabaseAccess; import org.xerial.db.sql.ResultSetHandler; import org.xerial.util.CollectionUtil; import org.xerial.util.Predicate; import org.xerial.util.StringUtil; import org.xerial.util.cui.OptionParser; import org.xerial.util.cui.OptionParserException; import org.xerial.util.log.Logger; /** * An UTGBShell sub commands that performs query * * @author leo * */ public class Query extends UTGBShellSubCommandBase { private static Logger _logger = Logger.getLogger(Query.class); private static enum Opt { } private OptionParser optionParser = new OptionParser(); @SuppressWarnings("unchecked") @Override public void execute(String[] args) throws UTGBShellException { String sql = ""; try { optionParser.parse(args); if (optionParser.getArgumentLength() < 2) throw new UTGBShellException("An database ID and SQL query are requried in the command line arguments."); final String databaseID = optionParser.getArgument(0); sql = optionParser.getArgument(1); _logger.debug("query: [" + sql + "]"); TrackConfiguration config = loadTrackConfiguration(); List dbList = CollectionUtil.select(config.getDatabaseList(), new Predicate() { public boolean apply(DatabaseInfo input) { return input.getId().equals(databaseID); } }); if (dbList.isEmpty()) { _logger.warn("No databas found for the given ID: " + databaseID); return; } for (DatabaseInfo db : dbList) { assert (db.getId().equals(databaseID)); for (ConnectionInfo conn : db.getConnectionList()) { DatabaseAccess dbAccess = JDBCUtil.getDatabaseAccess(conn); dbAccess.query(StringUtil.unquote(sql), new ResultSetHandler() { @Override public Object handle(ResultSet rs) throws SQLException { ResultSetMetaData metadata = rs.getMetaData(); for (int i = 1; i <= metadata.getColumnCount(); i++) { Object obj = rs.getObject(i); if (obj != null) System.out.print(obj.toString()); System.out.print("|"); } System.out.println(); return null; } }); } } } catch (OptionParserException e) { throw new UTGBShellException(e); } catch (DBException e) { _logger.error("query error: " + sql); throw new UTGBShellException(e); } } @Override public String name() { return "query"; } public String getDetailedDescription() { return loadUsage("help-query.txt"); } public String getOneLinerDescription() { return "performs a query for a database described in the config/track-config.xml file"; } public String getOptionList() { return optionParser.helpMessage(); } }