Search in sources :

Example 11 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getSchemas.

public String[] getSchemas() throws HopDatabaseException {
    ArrayList<String> catalogList = new ArrayList<>();
    ResultSet catalogResultSet = null;
    try {
        catalogResultSet = getDatabaseMetaData().getSchemas();
        // Grab all the catalog names and put them in an array list
        while (catalogResultSet != null && catalogResultSet.next()) {
            catalogList.add(catalogResultSet.getString(1));
        }
    } catch (SQLException e) {
        throw new HopDatabaseException("Error getting schemas!", e);
    } finally {
        try {
            if (catalogResultSet != null) {
                catalogResultSet.close();
            }
        } catch (SQLException e) {
            throw new HopDatabaseException("Error closing resultset after getting schemas!", e);
        }
    }
    if (log.isDetailed()) {
        log.logDetailed("read :" + catalogList.size() + " schemas from db meta-data.");
    }
    return catalogList.toArray(new String[catalogList.size()]);
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException)

Example 12 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getOneRow.

public RowMetaAndData getOneRow(String sql, IRowMeta param, Object[] data) throws HopDatabaseException {
    ResultSet rs = openQuery(sql, param, data);
    if (rs != null) {
        // One value: a number
        Object[] row = getRow(rs);
        rowMeta = null;
        RowMeta tmpMeta = null;
        try {
            ResultSetMetaData md = rs.getMetaData();
            tmpMeta = getMetaFromRow(row, md);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (Exception e) {
                log.logError("Unable to close resultset", e);
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (Exception e) {
                    log.logError("Unable to close prepared statement pstmt", e);
                }
                pstmt = null;
            }
            if (selStmt != null) {
                try {
                    selStmt.close();
                } catch (Exception e) {
                    log.logError("Unable to close prepared statement sel_stmt", e);
                }
                selStmt = null;
            }
        }
        return new RowMetaAndData(tmpMeta, row);
    } else {
        return null;
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) FileObject(org.apache.commons.vfs2.FileObject) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 13 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getSynonymMap.

public Map<String, Collection<String>> getSynonymMap(String schemanamein) throws HopDatabaseException {
    if (!databaseMeta.supportsSynonyms()) {
        return Collections.emptyMap();
    }
    String schemaname = schemanamein;
    if (schemaname == null && databaseMeta.useSchemaNameForTableList()) {
        schemaname = resolve(databaseMeta.getUsername()).toUpperCase();
    }
    Map<String, Collection<String>> synonymMap = new HashMap<>();
    ResultSet alltables = null;
    try {
        alltables = getDatabaseMetaData().getTables(null, schemaname, null, databaseMeta.getSynonymTypes());
        while (alltables.next()) {
            String cat = "";
            try {
                cat = alltables.getString("TABLE_CAT");
            } catch (Exception e) {
                // ignore
                if (log.isDebug()) {
                    log.logDebug("Error getting synonyms for field TABLE_CAT (ignored): " + e.toString());
                }
            }
            String schema = "";
            try {
                schema = alltables.getString("TABLE_SCHEM");
            } catch (Exception e) {
                // ignore
                if (log.isDebug()) {
                    log.logDebug("Error getting synonyms for field TABLE_SCHEM (ignored): " + e.toString());
                }
            }
            if (Utils.isEmpty(schema)) {
                schema = cat;
            }
            String table = alltables.getString(TABLES_META_DATA_TABLE_NAME);
            if (log.isRowLevel()) {
                log.logRowlevel(toString(), "got synonym from meta-data: " + databaseMeta.getQuotedSchemaTableCombination(this, schema, table));
            }
            multimapPut(schema, table, synonymMap);
        }
    } catch (SQLException e) {
        throw new HopDatabaseException("Error getting synonyms from schema [" + schemaname + "]", e);
    } finally {
        try {
            if (alltables != null) {
                alltables.close();
            }
        } catch (SQLException e) {
            throw new HopDatabaseException("Error closing resultset after getting synonyms from schema [" + schemaname + "]", e);
        }
    }
    if (log.isDetailed()) {
        log.logDetailed("read :" + multimapSize(synonymMap) + " synonyms from db meta-data.");
    }
    return synonymMap;
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 14 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getQueryFields.

public IRowMeta getQueryFields(String sql, boolean param, IRowMeta inform, Object[] data) throws HopDatabaseException {
    IRowMeta fields;
    DbCache dbcache = DbCache.getInstance();
    DbCacheEntry entry = null;
    // 
    if (dbcache != null) {
        entry = new DbCacheEntry(databaseMeta.getName(), sql);
        fields = dbcache.get(entry);
        if (fields != null) {
            return fields;
        }
    }
    if (connection == null) {
        // Cache test without connect.
        return null;
    }
    // 
    try {
        if (databaseMeta.supportsPreparedStatementMetadataRetrieval()) {
            // On with the regular program.
            // 
            fields = getQueryFieldsFromPreparedStatement(sql);
        } else {
            if (isDataServiceConnection()) {
                fields = getQueryFieldsFromDatabaseMetaData(sql);
            } else {
                fields = getQueryFieldsFromDatabaseMetaData();
            }
        }
    } catch (Exception e) {
        fields = getQueryFieldsFallback(sql, param, inform, data);
    }
    // Store in cache!!
    if (dbcache != null && fields != null) {
        dbcache.put(entry, fields);
    }
    return fields;
}
Also used : IRowMeta(org.apache.hop.core.row.IRowMeta) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Example 15 with HopDatabaseException

use of org.apache.hop.core.exception.HopDatabaseException in project hop by apache.

the class Database method getTableMap.

public Map<String, Collection<String>> getTableMap(String schemanamein, Map<String, String> props) throws HopDatabaseException {
    String schemaname = schemanamein;
    if (schemaname == null && databaseMeta.useSchemaNameForTableList()) {
        schemaname = resolve(databaseMeta.getUsername()).toUpperCase();
    }
    Map<String, Collection<String>> tableMap = new HashMap<>();
    ResultSet alltables = null;
    try {
        alltables = getDatabaseMetaData().getTables(null, schemaname, null, databaseMeta.getTableTypes());
        while (alltables.next()) {
            String cat = "";
            try {
                cat = alltables.getString("TABLE_CAT");
            } catch (Exception e) {
                // ignore
                if (log.isDebug()) {
                    log.logDebug("Error getting tables for field TABLE_CAT (ignored): " + e.toString());
                }
            }
            String schema = "";
            try {
                schema = alltables.getString("TABLE_SCHEM");
            } catch (Exception e) {
                // ignore
                if (log.isDebug()) {
                    log.logDebug("Error getting tables for field TABLE_SCHEM (ignored): " + e.toString());
                }
            }
            if (Utils.isEmpty(schema)) {
                schema = cat;
            }
            String table = alltables.getString(TABLES_META_DATA_TABLE_NAME);
            if (log.isRowLevel()) {
                log.logRowlevel(toString(), "got table from meta-data: " + databaseMeta.getQuotedSchemaTableCombination(this, schema, table));
            }
            // Check for any extra properties that might require validation
            if (props != null && !props.isEmpty()) {
                for (Map.Entry<String, String> prop : props.entrySet()) {
                    String propName = prop.getKey();
                    String tableProperty = alltables.getString(propName);
                    if (tableProperty != null) {
                        String propValue = prop.getValue();
                        if (tableProperty.equals(propValue)) {
                            multimapPut(schema, table, tableMap);
                        }
                    }
                }
            } else {
                multimapPut(schema, table, tableMap);
            }
        }
    } catch (SQLException e) {
        log.logError("Error getting tablenames from schema [" + schemaname + "]");
    } finally {
        try {
            if (alltables != null) {
                alltables.close();
            }
        } catch (SQLException e) {
            throw new HopDatabaseException("Error closing resultset after getting views from schema [" + schemaname + "]", e);
        }
    }
    if (log.isDetailed()) {
        log.logDetailed("read :" + multimapSize(tableMap) + " table names from db meta-data.");
    }
    return tableMap;
}
Also used : HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) DatabaseConnectionMap(org.apache.hop.core.database.map.DatabaseConnectionMap) HopDatabaseBatchException(org.apache.hop.core.exception.HopDatabaseBatchException) HopException(org.apache.hop.core.exception.HopException) HopDatabaseException(org.apache.hop.core.exception.HopDatabaseException) HopValueException(org.apache.hop.core.exception.HopValueException)

Aggregations

HopDatabaseException (org.apache.hop.core.exception.HopDatabaseException)106 HopException (org.apache.hop.core.exception.HopException)42 IRowMeta (org.apache.hop.core.row.IRowMeta)30 HopDatabaseBatchException (org.apache.hop.core.exception.HopDatabaseBatchException)29 HopValueException (org.apache.hop.core.exception.HopValueException)29 Database (org.apache.hop.core.database.Database)25 DatabaseMeta (org.apache.hop.core.database.DatabaseMeta)23 IValueMeta (org.apache.hop.core.row.IValueMeta)20 SQLException (java.sql.SQLException)16 FileObject (org.apache.commons.vfs2.FileObject)15 HopExtensionPoint (org.apache.hop.core.extension.HopExtensionPoint)15 RowMeta (org.apache.hop.core.row.RowMeta)14 Result (org.apache.hop.core.Result)9 HopTransformException (org.apache.hop.core.exception.HopTransformException)9 ICheckResult (org.apache.hop.core.ICheckResult)8 HopXmlException (org.apache.hop.core.exception.HopXmlException)6 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)6 PreparedStatement (java.sql.PreparedStatement)5 EnterSelectionDialog (org.apache.hop.ui.core.dialog.EnterSelectionDialog)5 ResultSet (java.sql.ResultSet)4