use of org.apache.hop.core.row.RowMeta in project hop by apache.
the class Database method getGeneratedKeys.
/**
* @param ps The prepared insert statement to use
* @return The generated keys in auto-increment fields
* @throws HopDatabaseException in case something goes wrong retrieving the keys.
*/
public RowMetaAndData getGeneratedKeys(PreparedStatement ps) throws HopDatabaseException {
ResultSet keys = null;
try {
// 1 row of keys
keys = ps.getGeneratedKeys();
ResultSetMetaData resultSetMetaData = keys.getMetaData();
if (resultSetMetaData == null) {
resultSetMetaData = ps.getMetaData();
}
IRowMeta rowMeta;
if (resultSetMetaData == null) {
rowMeta = new RowMeta();
rowMeta.addValueMeta(new ValueMetaInteger("ai-key"));
} else {
rowMeta = getRowInfo(resultSetMetaData, false, false);
}
return new RowMetaAndData(rowMeta, getRow(keys, resultSetMetaData, rowMeta));
} catch (Exception ex) {
throw new HopDatabaseException("Unable to retrieve key(s) from auto-increment field(s)", ex);
} finally {
if (keys != null) {
try {
keys.close();
} catch (SQLException e) {
log.logError("Unable to close resultset of auto-generated keys", e);
}
}
}
}
use of org.apache.hop.core.row.RowMeta in project hop by apache.
the class Database method getAlterTableStatement.
public String getAlterTableStatement(String tableName, IRowMeta fields, String tk, boolean useAutoIncrement, String pk, boolean semicolon) throws HopDatabaseException {
String retval = "";
// Get the fields that are in the table now:
IRowMeta tabFields = getTableFields(tableName);
// Don't forget to quote these as well...
databaseMeta.quoteReservedWords(tabFields);
// Find the missing fields
IRowMeta missing = new RowMeta();
for (int i = 0; i < fields.size(); i++) {
IValueMeta v = fields.getValueMeta(i);
// Not found?
if (tabFields.searchValueMeta(v.getName()) == null) {
// nope --> Missing!
missing.addValueMeta(v);
}
}
if (missing.size() != 0) {
for (int i = 0; i < missing.size(); i++) {
IValueMeta v = missing.getValueMeta(i);
retval += databaseMeta.getAddColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
}
}
// Find the surplus fields
IRowMeta surplus = new RowMeta();
for (int i = 0; i < tabFields.size(); i++) {
IValueMeta v = tabFields.getValueMeta(i);
// Found in table, not in input ?
if (fields.searchValueMeta(v.getName()) == null) {
// yes --> surplus!
surplus.addValueMeta(v);
}
}
if (surplus.size() != 0) {
for (int i = 0; i < surplus.size(); i++) {
IValueMeta v = surplus.getValueMeta(i);
retval += databaseMeta.getDropColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
}
}
//
// OK, see if there are fields for which we need to modify the type...
// (length, precision)
//
IRowMeta modify = new RowMeta();
for (int i = 0; i < fields.size(); i++) {
IValueMeta desiredField = fields.getValueMeta(i);
IValueMeta currentField = tabFields.searchValueMeta(desiredField.getName());
if (desiredField != null && currentField != null) {
String desiredDDL = databaseMeta.getFieldDefinition(desiredField, tk, pk, useAutoIncrement);
String currentDDL = databaseMeta.getFieldDefinition(currentField, tk, pk, useAutoIncrement);
boolean mod = !desiredDDL.equalsIgnoreCase(currentDDL);
if (mod) {
modify.addValueMeta(desiredField);
}
}
}
if (modify.size() > 0) {
for (int i = 0; i < modify.size(); i++) {
IValueMeta v = modify.getValueMeta(i);
retval += databaseMeta.getModifyColumnStatement(tableName, v, tk, useAutoIncrement, pk, true);
}
}
return retval;
}
use of org.apache.hop.core.row.RowMeta 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.row.RowMeta in project hop by apache.
the class Database method getMetaFromRow.
public RowMeta getMetaFromRow(Object[] row, ResultSetMetaData md) throws SQLException, HopDatabaseException {
RowMeta meta = new RowMeta();
for (int i = 0; i < md.getColumnCount(); i++) {
IValueMeta valueMeta = getValueFromSqlType(md, i + 1, true, false);
meta.addValueMeta(valueMeta);
}
return meta;
}
use of org.apache.hop.core.row.RowMeta in project hop by apache.
the class Database method getQueryFieldsFromDatabaseMetaData.
private IRowMeta getQueryFieldsFromDatabaseMetaData(String sql) throws Exception {
ResultSet columns = connection.getMetaData().getColumns("", "", StringUtils.isNotBlank(sql) ? sql : databaseMeta.getName(), "");
IRowMeta rowMeta = new RowMeta();
while (columns.next()) {
IValueMeta valueMeta = null;
String name = columns.getString("COLUMN_NAME");
String type = columns.getString("SOURCE_DATA_TYPE");
int size = columns.getInt("COLUMN_SIZE");
if (type.equals("Integer") || type.equals("Long")) {
valueMeta = new ValueMetaInteger();
} else if (type.equals("BigDecimal") || type.equals("BigNumber")) {
valueMeta = new ValueMetaBigNumber();
} else if (type.equals("Double") || type.equals("Number")) {
valueMeta = new ValueMetaNumber();
} else if (type.equals("String")) {
valueMeta = new ValueMetaString();
} else if (type.equals("Date")) {
valueMeta = new ValueMetaDate();
} else if (type.equals("Boolean")) {
valueMeta = new ValueMetaBoolean();
} else if (type.equals("Binary")) {
valueMeta = new ValueMetaBinary();
} else if (type.equals("Timestamp")) {
valueMeta = new ValueMetaTimestamp();
} else if (type.equals("Internet Address")) {
valueMeta = new ValueMetaInternetAddress();
}
if (valueMeta != null) {
valueMeta.setName(name);
valueMeta.setComments(name);
valueMeta.setLength(size);
valueMeta.setOriginalColumnTypeName(type);
valueMeta.setConversionMask(columns.getString("SOURCE_MASK"));
valueMeta.setDecimalSymbol(columns.getString("SOURCE_DECIMAL_SYMBOL"));
valueMeta.setGroupingSymbol(columns.getString("SOURCE_GROUPING_SYMBOL"));
valueMeta.setCurrencySymbol(columns.getString("SOURCE_CURRENCY_SYMBOL"));
rowMeta.addValueMeta(valueMeta);
} else {
log.logBasic("Database.getQueryFields() IValueMeta mapping not resolved for the column " + name);
rowMeta = null;
break;
}
}
if (rowMeta != null && !rowMeta.isEmpty()) {
return rowMeta;
} else {
throw new Exception("Error in Database.getQueryFields()");
}
}
Aggregations