Search in sources :

Example 1 with Session

use of com.teradata.jaqy.Session in project jaqy by Teradata.

the class InfoCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws SQLException {
    if (args.length == 0) {
        interpreter.error("need to provide the information type.");
    }
    SessionUtils.checkOpen(interpreter);
    String type = args[0].toLowerCase();
    Session session = interpreter.getSession();
    JaqyConnection conn = session.getConnection();
    JaqyHelper helper = conn.getHelper();
    DatabaseMetaData metaData = conn.getMetaData();
    if ("behavior".equals(type) || "behaviors".equals(type))
        listBehaviors(interpreter, metaData, helper);
    else if ("catalog".equals(type) || "catalogs".equals(type))
        listCatalogs(interpreter, metaData, helper);
    else if ("client".equals(type))
        listClient(interpreter, metaData, helper);
    else if ("feature".equals(type) || "features".equals(type))
        listFeatures(interpreter, metaData, helper);
    else if ("function".equals(type) || "functions".equals(type))
        listFunctions(interpreter, metaData, helper);
    else if ("keyword".equals(type) || "keywords".equals(type))
        listKeywords(interpreter, metaData, helper);
    else if ("limit".equals(type) || "limits".equals(type))
        listLimits(interpreter, metaData, helper);
    else if ("schema".equals(type) || "schemas".equals(type))
        listSchemas(interpreter, metaData, helper);
    else if ("server".equals(type))
        listServer(interpreter, metaData, helper);
    else if ("table".equals(type))
        listTableTypes(interpreter, metaData, helper);
    else if ("type".equals(type) || "types".equals(type))
        listTypes(interpreter, metaData, helper);
    else if ("user".equals(type))
        listUser(interpreter, metaData, helper);
    else {
        interpreter.error("invalid information type: " + args[0]);
    }
}
Also used : JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) JaqyConnection(com.teradata.jaqy.connection.JaqyConnection) DatabaseMetaData(java.sql.DatabaseMetaData) Session(com.teradata.jaqy.Session)

Example 2 with Session

use of com.teradata.jaqy.Session in project jaqy by Teradata.

the class ListCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws SQLException {
    SessionUtils.checkOpen(interpreter);
    // 0 = catalog, 1 = schema, 2 = table
    int listType = 2;
    String catalogPattern = ".";
    String schemaPattern = ".";
    String tablePattern = "%";
    if (args.length == 0) {
    // no change to the pattern.
    } else if (args.length == 1) {
        catalogPattern = args[0];
        // we are selecting the catalogs
        if ("%".equals(args[0])) {
            listType = 0;
        } else {
            listType = 1;
        }
    } else if (args.length == 2) {
        catalogPattern = args[0];
        schemaPattern = args[1];
        listType = 1;
    } else if (args.length >= 3) {
        catalogPattern = args[0];
        schemaPattern = args[1];
        tablePattern = args[2];
        listType = 2;
    }
    Session session = interpreter.getSession();
    JaqyConnection conn = session.getConnection();
    DatabaseMetaData meta = conn.getMetaData();
    if (".".equals(catalogPattern)) {
        catalogPattern = conn.getCatalog(interpreter);
    }
    if (".".equals(schemaPattern)) {
        schemaPattern = conn.getSchema(interpreter);
    }
    JaqyHelper helper = conn.getHelper();
    ResultSet rs = null;
    String sep = meta.getCatalogSeparator();
    if (sep == null)
        sep = "/";
    if (listType == 0) {
        interpreter.println("-- Listing catalogs");
        rs = meta.getCatalogs();
    } else if (listType == 1) {
        interpreter.println("-- Listing schema: " + catalogPattern + sep + schemaPattern);
        rs = meta.getSchemas(catalogPattern, schemaPattern);
    } else if (listType == 2) {
        interpreter.println("-- Listing tables: " + catalogPattern + sep + schemaPattern + sep + tablePattern);
        rs = meta.getTables(catalogPattern, schemaPattern, tablePattern, null);
    }
    if (rs == null)
        return;
    interpreter.print(helper.getResultSet(rs, interpreter));
    rs.close();
}
Also used : JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) JaqyConnection(com.teradata.jaqy.connection.JaqyConnection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Session(com.teradata.jaqy.Session)

Example 3 with Session

use of com.teradata.jaqy.Session in project jaqy by Teradata.

the class OpenCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    Session session = interpreter.getSession();
    Display display = interpreter.getDisplay();
    if (session == null) {
        interpreter.error("No active sessions.");
    }
    if (!session.isClosed()) {
        interpreter.error("The current session already has a connection open.");
    }
    CommandLine cmdLine = getCommandLine(args);
    args = cmdLine.getArgs();
    if (args.length == 0) {
        interpreter.error("invalid command arguments.");
    }
    Properties prop = new Properties();
    cmdLine.getOptions();
    boolean hasUser = false;
    boolean hasPassword = false;
    for (Option option : cmdLine.getOptions()) {
        if ("p".equals(option.getOpt())) {
            prop.setProperty("password", option.getValue());
            hasPassword = true;
        } else if ("f".equals(option.getOpt())) {
            String password = display.getPassword(interpreter, "Password: ");
            prop.setProperty("password", password);
            hasPassword = true;
        } else if ("u".equals(option.getOpt())) {
            prop.setProperty("user", option.getValue());
            hasUser = true;
        } else if ("D".equals(option.getOpt())) {
            String key = option.getValue(0);
            String value = option.getValue(1);
            prop.setProperty(key, value);
        }
    }
    if (hasUser && !hasPassword) {
        // use a blank password if the user is specified and password is not.
        prop.setProperty("password", "");
    }
    session.open(args[0], prop, interpreter, display);
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) Properties(java.util.Properties) Session(com.teradata.jaqy.Session) Display(com.teradata.jaqy.interfaces.Display)

Example 4 with Session

use of com.teradata.jaqy.Session in project jaqy by Teradata.

the class PwdCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    SessionUtils.checkOpen(interpreter);
    Session session = interpreter.getSession();
    JaqyConnection conn = session.getConnection();
    JaqyHelper helper = conn.getHelper();
    String catalog = null;
    String schema = null;
    DatabaseMetaData meta = conn.getMetaData();
    String catalogName = "CATALOG";
    String schemaName = "SCHEMA";
    StringBuilder builder = new StringBuilder();
    try {
        catalog = conn.getCatalog(interpreter);
    } catch (SQLException ex) {
    }
    if (catalog != null && catalog.length() != 0) {
        try {
            catalogName = meta.getCatalogTerm();
        } catch (SQLException ex) {
        }
        builder.append(catalogName).append(" : ").append(catalog);
    }
    try {
        schema = helper.getSchema(interpreter);
    } catch (SQLException ex) {
    }
    if (schema != null && schema.length() != 0) {
        try {
            schemaName = meta.getSchemaTerm();
        } catch (SQLException ex) {
        }
        if (builder.length() > 0)
            builder.append(", ");
        builder.append(schemaName).append(" : ").append(schema);
    }
    interpreter.println(builder.toString());
}
Also used : JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) SQLException(java.sql.SQLException) JaqyConnection(com.teradata.jaqy.connection.JaqyConnection) DatabaseMetaData(java.sql.DatabaseMetaData) Session(com.teradata.jaqy.Session)

Example 5 with Session

use of com.teradata.jaqy.Session in project jaqy by Teradata.

the class ImportTableCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    JaqyImporter<?> importer = interpreter.getImporter();
    if (importer == null) {
        interpreter.error("There is no current import.");
    }
    SchemaInfo schemaInfo = importer.getSchema();
    if (schemaInfo == null) {
        interpreter.error("Current import schema is not available.");
    }
    if (args.length == 0) {
        interpreter.error("Staging table name is not specified.");
    }
    StringBuilder buffer = new StringBuilder();
    for (String arg : args) buffer.append(arg);
    String tableName = buffer.toString();
    SessionUtils.checkOpen(interpreter);
    Session session = interpreter.getSession();
    JaqyConnection conn = session.getConnection();
    JaqyHelper helper = conn.getHelper();
    String sql = SchemaUtils.getTableSchema(helper, schemaInfo, tableName, false);
    boolean prevCommit = conn.getAutoCommit();
    if (!prevCommit)
        conn.setAutoCommit(true);
    interpreter.println("-- Table Schema --");
    interpreter.println(sql);
    session.executeQuery(sql, interpreter, 1);
    sql = null;
    if (!prevCommit)
        conn.setAutoCommit(false);
    buffer.setLength(0);
    buffer.append("INSERT INTO ").append(tableName).append(" VALUES (");
    int columnCount = schemaInfo.columns.length;
    for (int i = 0; i < columnCount; ++i) {
        if (i > 0)
            buffer.append(',');
        buffer.append('?');
    }
    buffer.append(')');
    sql = buffer.toString();
    interpreter.println("-- INSERTION --");
    interpreter.println(sql);
    try {
        session.importQuery(sql, interpreter);
    } finally {
        interpreter.setQueryMode(QueryMode.Regular);
    }
}
Also used : JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) JaqyConnection(com.teradata.jaqy.connection.JaqyConnection) SchemaInfo(com.teradata.jaqy.schema.SchemaInfo) Session(com.teradata.jaqy.Session)

Aggregations

Session (com.teradata.jaqy.Session)9 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)5 JaqyConnection (com.teradata.jaqy.connection.JaqyConnection)4 DatabaseMetaData (java.sql.DatabaseMetaData)3 CommandLine (org.apache.commons.cli.CommandLine)3 Option (org.apache.commons.cli.Option)3 Display (com.teradata.jaqy.interfaces.Display)2 SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)2 JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)1 Path (com.teradata.jaqy.interfaces.Path)1 Reader (java.io.Reader)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1