Search in sources :

Example 36 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class SQLMap method getDBResultSet.

/**
 * NOTE: DO NOT USE THIS METHOD ON LARGE RESULTSETS WITHOUT SETTING
 * ENDINDEX.
 */
@Override
public final ResultSet getDBResultSet(boolean updateOnly) throws SQLException, UserException {
    createConnection();
    if (con == null) {
        AuditLog.log("SQLMap", "Could not connect to database: " + datasource + ", check your connection", Level.SEVERE, (myAccess != null ? myAccess.accessID : "unknown access"));
        throw new UserException(-1, "in SQLMap. Could not open database connection [driver = " + driver + ", url = " + url + ", username = '" + username + "', password = '" + password + "']");
    }
    if (debug) {
        Access.writeToConsole(myAccess, "SQLMAP, GOT CONNECTION, STARTING QUERY\n");
    }
    // batch mode?
    this.batchMode = updateOnly && ((this.query == null) || (this.query.length() == 0)) && (this.update != null) && (this.update.indexOf(SQLBatchUpdateHelper.DELIMITER) > 0);
    if (this.batchMode) {
        if (this.debug) {
            Access.writeToConsole(myAccess, this.getClass() + ": detected batch mode, trying a batch update\n");
        }
        this.helper = new SQLBatchUpdateHelper(this.update, this.con, this.parameters, this.myAccess, this.getDbIdentifier(), this, this.isLegacyMode, this.debug, updateOnly);
        this.updateCount = this.helper.getUpdateCount();
        // this.batchMode = false;
        return (this.helper.getResultSet());
    }
    if (debug) {
        Access.writeToConsole(myAccess, "BEFORE PREPARESTATEMENT()\n");
    }
    // Check for open statement.
    if (this.statement != null) {
        try {
            this.statement.close();
        } catch (Exception e) {
        }
        this.statement = null;
    }
    if (query != null) {
        this.statement = con.prepareStatement(query);
    } else {
        this.statement = con.prepareStatement(update);
    }
    openResultSets++;
    if (debug) {
        Access.writeToConsole(myAccess, "AFTER PREPARESTATEMENT(), SETTING MAXROWS...\n");
    }
    this.statement.setMaxRows(this.endIndex);
    if (debug) {
        Access.writeToConsole(myAccess, "SET MAXROWS DONE..SETTING STATEMENT PARAMETERS\n");
    }
    setStatementParameters(statement);
    ResultSet rs = null;
    if (updateOnly) {
        this.statement.executeUpdate();
    } else {
        try {
            if (debug) {
                Access.writeToConsole(myAccess, "CALLING EXECUTEQUERY()\n");
            }
            rs = this.statement.executeQuery();
            if (debug) {
                Access.writeToConsole(myAccess, "GOT RESULTSET!!!!!\n");
            }
        } catch (SQLException e) {
            // using executeQuery() if query does not return a resultset.
            if (e.getMessage().indexOf("JZ0R2") == -1) {
                throw e;
            }
        }
    }
    this.updateCount = this.statement.getUpdateCount();
    // dump any SQL warnings
    if (debug) {
        SQLWarning warning = this.statement.getWarnings();
        while (warning != null) {
            Access.writeToConsole(myAccess, "SQL warning: " + warning.getMessage() + "\n");
            warning = warning.getNextWarning();
        }
    }
    return rs;
}
Also used : SQLWarning(java.sql.SQLWarning) SQLBatchUpdateHelper(com.dexels.navajo.adapter.sqlmap.SQLBatchUpdateHelper) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) UserException(com.dexels.navajo.script.api.UserException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SQLException(java.sql.SQLException)

Example 37 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class SQLMap method getRecords.

/**
 * Get all records from resultset as Binary object (x-separated file)
 *
 * @return
 */
@Override
public Binary getRecords() throws UserException {
    java.io.File tempFile = null;
    ResultSet rs = null;
    try {
        Binary b = null;
        rs = getDBResultSet(false);
        tempFile = File.createTempFile("sqlmap_records", "navajo");
        FileOutputStream fos = new FileOutputStream(tempFile);
        OutputStreamWriter fw = new OutputStreamWriter(fos, "UTF-8");
        int columns = 0;
        ResultSetMetaData meta = null;
        try {
            meta = rs.getMetaData();
            columns = meta.getColumnCount();
            if (this.showHeader) {
                for (int j = 0; j < columns; j++) {
                    String column = meta.getColumnLabel(j + 1);
                    if (j == 0) {
                        fw.write(column);
                    } else {
                        fw.write(this.separator + column);
                    }
                }
                fw.write((!dosMode) ? "\n" : "\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace(Access.getConsoleWriter(myAccess));
        }
        while (rs.next()) {
            for (int j = 1; j <= columns; j++) {
                String value = (rs.getObject(j) != null ? rs.getString(j) + "" : "");
                if (j == 1) {
                    fw.write(value);
                } else {
                    fw.write(this.separator + value);
                }
            }
            fw.write((!dosMode) ? "\n" : "\r\n");
        }
        fw.flush();
        fw.close();
        b = new Binary(tempFile, false);
        fos.close();
        return b;
    } catch (Exception ioe) {
        throw new UserException(-1, ioe.getMessage(), ioe);
    } finally {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
                resetAll();
            } catch (SQLException e) {
                e.printStackTrace(Access.getConsoleWriter(myAccess));
            }
        }
        if (tempFile != null) {
            try {
                tempFile.delete();
            } catch (Exception ioe2) {
                ioe2.printStackTrace(Access.getConsoleWriter(myAccess));
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SQLException(java.sql.SQLException)

Example 38 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class SQLMap method store.

@Override
public void store() throws MappableException, UserException {
    if (con == null && gc == null) {
        logger.debug("SQLMap closed without executing any queries!");
        return;
    }
    if (myResultSetIterator != null) {
        myResultSetIterator.close();
        resetAll();
    }
    cleanupBinaryStreams();
    if (transactionContext == -1) {
        String resetSession = null;
        if (myConnectionBroker != null && this.alternativeUsername != null) {
            if (SQLMapConstants.POSTGRESDB.equals(this.getDbIdentifier()) || SQLMapConstants.ENTERPRISEDB.equals(this.getDbIdentifier())) {
                resetSession = "SET SEARCH_PATH TO " + myConnectionBroker.getUsername() + ",public";
            } else {
                resetSession = "ALTER SESSION SET CURRENT_SCHEMA = " + myConnectionBroker.getUsername();
            }
        }
        try {
            if (con != null && !con.isClosed()) {
                if (resetSession != null) {
                    PreparedStatement stmt = con.prepareStatement(resetSession);
                    stmt.executeUpdate();
                    stmt.close();
                }
                // Determine autocommit value
                if (myConnectionBroker == null || myConnectionBroker.hasAutoCommit()) {
                    if (!autoCommit && !kill) {
                        // Only commit if kill (rollback)
                        // was not called.
                        con.commit();
                    }
                    con.setAutoCommit(true);
                }
                if (multiTenantGrusConnection != null) {
                    GrusProviderFactory.getInstance().release(multiTenantGrusConnection);
                    multiTenantGrusConnection = null;
                    // multiTenantGrusConnection == gc
                    gc = null;
                }
            }
        } catch (SQLException sqle) {
            logger.warn("COULD NOT RESET SCHEMA. session: " + resetSession);
            AuditLog.log("SQLMap", sqle.getMessage(), sqle, Level.SEVERE, (myAccess != null ? myAccess.accessID : "unknown access"));
            throw new UserException(-1, sqle.getMessage(), sqle);
        } finally {
            if (fixedBroker != null && myConnectionBroker != null) {
                // Free connection.
                if (gc != null) {
                    myConnectionBroker.freeConnection(gc);
                } else {
                    myConnectionBroker.freeConnection(con);
                }
                // Make sure to set some these to null to prevent double freeing.
                myConnectionBroker = null;
                con = null;
                gc = null;
            }
        }
    }
    if (ownConnection && GrusProviderFactory.getInstance() != null && gc != null) {
        GrusProviderFactory.getInstance().release(gc);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) UserException(com.dexels.navajo.script.api.UserException)

Example 39 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class SQLMap method resetAll.

protected void resetAll() throws UserException {
    this.query = this.update = null;
    try {
        if (this.statement != null) {
            this.statement.close();
            this.statement = null;
            openResultSets--;
        }
        if (this.helper != null) {
            this.helper.closeLast();
            this.helper = null;
        }
        if (this.batchMode) {
            this.batchMode = false;
        }
    } catch (Exception e) {
        AuditLog.log("SQLMap", e.getMessage(), e, Level.SEVERE, (myAccess != null ? myAccess.accessID : "unknown access"));
        throw new UserException(-1, e.getMessage(), e);
    }
}
Also used : UserException(com.dexels.navajo.script.api.UserException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SQLException(java.sql.SQLException)

Example 40 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class SQLMap method setQuery.

/**
 * Use this method to define a new query. All parameters used by a previous
 * query are removed. replace " characters with ' characters.
 */
@Override
public void setQuery(final String newQuery) throws UserException {
    if (newQuery.indexOf(";") != -1) {
        throw new UserException(-1, "Use of semicolon in query fields is not allowed, maybe you meant to use an update field?");
    }
    String quotesQuery = newQuery.replace('"', (this.replaceQueryDoubleQuotes) ? '\'' : '\"');
    query = ora2pgQuery(quotesQuery);
    if (debug) {
        Access.writeToConsole(myAccess, "SQLMap(): query = " + query + "\n");
    }
    this.savedQuery = query;
    this.resultSet = null;
    this.update = null;
    parameters = new ArrayList();
}
Also used : ArrayList(java.util.ArrayList) UserException(com.dexels.navajo.script.api.UserException)

Aggregations

UserException (com.dexels.navajo.script.api.UserException)113 MappableException (com.dexels.navajo.script.api.MappableException)54 IOException (java.io.IOException)33 NavajoException (com.dexels.navajo.document.NavajoException)25 Message (com.dexels.navajo.document.Message)22 SQLException (java.sql.SQLException)19 SystemException (com.dexels.navajo.script.api.SystemException)18 Binary (com.dexels.navajo.document.types.Binary)14 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)13 Property (com.dexels.navajo.document.Property)12 ArrayList (java.util.ArrayList)12 Navajo (com.dexels.navajo.document.Navajo)11 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)11 ResultSet (java.sql.ResultSet)11 MappingException (com.dexels.navajo.script.api.MappingException)10 ResultSetMetaData (java.sql.ResultSetMetaData)9 Element (org.w3c.dom.Element)8 CompilationException (com.dexels.navajo.script.api.CompilationException)7 File (java.io.File)7 NodeList (org.w3c.dom.NodeList)7