Search in sources :

Example 1 with BufferedMCLWriter

use of nl.cwi.monetdb.mcl.io.BufferedMCLWriter in project pentaho-kettle by pentaho.

the class MonetDBBulkLoader method executeSql.

/*
   * executeSQL Uses the MonetDB API to create a new server connection and the associated buffered Reader and Writer to
   * execute a single query.
   *
   * @param Query string
   *
   * @param Host URI
   *
   * @param Numerical port
   *
   * @param Username for establishing the connection
   *
   * @param Password for establishing the connection
   *
   * @param database to connect to
   */
protected static void executeSql(String query, String host, int port, String user, String password, String db) throws Exception {
    MapiSocket mserver = null;
    try {
        mserver = getMonetDBConnection(host, port, user, password, db);
        BufferedMCLReader in = mserver.getReader();
        BufferedMCLWriter out = mserver.getWriter();
        String error = in.waitForPrompt();
        if (error != null) {
            throw new Exception("ERROR waiting for input reader: " + error);
        }
        // the leading 's' is essential, since it is a protocol
        // marker that should not be omitted, likewise the
        // trailing semicolon
        out.write('s');
        System.out.println(query);
        out.write(query);
        out.write(';');
        out.newLine();
        out.writeLine("");
        String line = null;
        while ((line = in.readLine()) != null) {
            int type = in.getLineType();
            // read till we get back to the prompt
            if (type == BufferedMCLReader.PROMPT) {
                break;
            }
            switch(type) {
                case BufferedMCLReader.ERROR:
                    System.err.println(line);
                    break;
                case BufferedMCLReader.RESULT:
                    System.out.println(line);
                    break;
                default:
                    // unknown, header, ...
                    break;
            }
        }
    } finally {
        if (mserver != null) {
            mserver.close();
        }
    }
}
Also used : MapiSocket(nl.cwi.monetdb.mcl.net.MapiSocket) BufferedMCLWriter(nl.cwi.monetdb.mcl.io.BufferedMCLWriter) BufferedMCLReader(nl.cwi.monetdb.mcl.io.BufferedMCLReader) KettleException(org.pentaho.di.core.exception.KettleException) MonetDBRowLimitException(org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)

Aggregations

BufferedMCLReader (nl.cwi.monetdb.mcl.io.BufferedMCLReader)1 BufferedMCLWriter (nl.cwi.monetdb.mcl.io.BufferedMCLWriter)1 MapiSocket (nl.cwi.monetdb.mcl.net.MapiSocket)1 KettleException (org.pentaho.di.core.exception.KettleException)1 MonetDBRowLimitException (org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)1