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()]);
}
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;
}
}
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;
}
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;
}
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;
}
Aggregations