Search in sources :

Example 1 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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 JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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 JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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 4 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper in project jaqy by Teradata.

the class AvroExporter method export.

@Override
public long export(JaqyResultSet rs, JaqyInterpreter interpreter) throws Exception {
    JaqyHelper helper = rs.getHelper();
    SchemaInfo schemaInfo = ResultSetMetaDataUtils.getColumnInfo(rs.getMetaData().getMetaData(), helper);
    Schema schema = AvroUtils.getSchema(schemaInfo, helper);
    interpreter.getGlobals().log(Level.INFO, "schema is " + schema.toString(true));
    DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
    DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(writer);
    if (m_codecFactory != null)
        dataFileWriter.setCodec(m_codecFactory);
    dataFileWriter.create(schema, m_os);
    long count = AvroUtils.print(dataFileWriter, schema, rs, schemaInfo);
    dataFileWriter.close();
    return count;
}
Also used : JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) Schema(org.apache.avro.Schema) DataFileWriter(org.apache.avro.file.DataFileWriter) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) SchemaInfo(com.teradata.jaqy.schema.SchemaInfo)

Example 5 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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

JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)10 Session (com.teradata.jaqy.Session)5 JaqyConnection (com.teradata.jaqy.connection.JaqyConnection)4 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)4 SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)3 TypeHandler (com.teradata.jaqy.typehandler.TypeHandler)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)2 InMemoryResultSetMetaData (com.teradata.jaqy.resultset.InMemoryResultSetMetaData)1 FullColumnInfo (com.teradata.jaqy.schema.FullColumnInfo)1 CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)1 ColumnNode (com.teradata.jaqy.utils.exp.ColumnNode)1 ExpNode (com.teradata.jaqy.utils.exp.ExpNode)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Schema (org.apache.avro.Schema)1 DataFileWriter (org.apache.avro.file.DataFileWriter)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 GenericRecord (org.apache.avro.generic.GenericRecord)1