use of org.apache.hop.core.row.IValueMeta in project hop by apache.
the class Database method getSqlOutput.
/**
* Return SQL statement (INSERT INTO TableName ...
*
* @param schemaName tableName The schema
* @param tableName
* @param fields
* @param dateFormat date format of field
* @throws HopDatabaseException
*/
public String getSqlOutput(String schemaName, String tableName, IRowMeta fields, Object[] r, String dateFormat) throws HopDatabaseException {
StringBuilder ins = new StringBuilder(128);
try {
String schemaTable = databaseMeta.getQuotedSchemaTableCombination(this, schemaName, tableName);
ins.append("INSERT INTO ").append(schemaTable).append('(');
// now add the names in the row:
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
ins.append(", ");
}
String name = fields.getValueMeta(i).getName();
ins.append(databaseMeta.quoteField(name));
}
ins.append(") VALUES (");
java.text.SimpleDateFormat[] fieldDateFormatters = new java.text.SimpleDateFormat[fields.size()];
// new add values ...
for (int i = 0; i < fields.size(); i++) {
IValueMeta valueMeta = fields.getValueMeta(i);
Object valueData = r[i];
if (i > 0) {
ins.append(",");
}
//
if (valueMeta.isNull(valueData)) {
ins.append("null");
} else {
//
switch(valueMeta.getType()) {
case IValueMeta.TYPE_BOOLEAN:
case IValueMeta.TYPE_STRING:
String string = valueMeta.getString(valueData);
// Have the database dialect do the quoting.
// This also adds the single quotes around the string (thanks to
// PostgreSQL)
//
string = databaseMeta.quoteSqlString(string);
ins.append(string);
break;
case IValueMeta.TYPE_DATE:
Date date = fields.getDate(r, i);
if (Utils.isEmpty(dateFormat)) {
if (databaseMeta.getIDatabase().isOracleVariant()) {
if (fieldDateFormatters[i] == null) {
fieldDateFormatters[i] = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
}
ins.append("TO_DATE('").append(fieldDateFormatters[i].format(date)).append("', 'YYYY/MM/DD HH24:MI:SS')");
} else {
ins.append("'" + fields.getString(r, i) + "'");
}
} else {
try {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(dateFormat);
ins.append("'" + formatter.format(fields.getDate(r, i)) + "'");
} catch (Exception e) {
throw new HopDatabaseException("Error : ", e);
}
}
break;
default:
ins.append(fields.getString(r, i));
break;
}
}
}
ins.append(')');
} catch (Exception e) {
throw new HopDatabaseException(e);
}
return ins.toString();
}
use of org.apache.hop.core.row.IValueMeta in project hop by apache.
the class Database method getCreateTableStatement.
/**
* Generates SQL
*
* @param tableName the table name or schema/table combination: this needs to be quoted properly
* in advance.
* @param fields the fields
* @param tk the name of the technical key field
* @param useAutoIncrement true if we need to use auto-increment fields for a primary key
* @param pk the name of the primary/technical key field
* @param semicolon append semicolon to the statement
* @return the SQL needed to create the specified table and fields.
*/
public String getCreateTableStatement(String tableName, IRowMeta fields, String tk, boolean useAutoIncrement, String pk, boolean semicolon) {
StringBuilder retval = new StringBuilder();
IDatabase iDatabase = databaseMeta.getIDatabase();
retval.append(iDatabase.getCreateTableStatement());
retval.append(tableName + Const.CR);
retval.append("(").append(Const.CR);
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
retval.append(", ");
} else {
retval.append(" ");
}
IValueMeta v = fields.getValueMeta(i);
retval.append(databaseMeta.getFieldDefinition(v, tk, pk, useAutoIncrement));
}
// Technical keys
if (tk != null && databaseMeta.requiresCreateTablePrimaryKeyAppend()) {
retval.append(", PRIMARY KEY (").append(tk).append(")").append(Const.CR);
}
// Primary keys
if (pk != null && databaseMeta.requiresCreateTablePrimaryKeyAppend()) {
retval.append(", PRIMARY KEY (").append(pk).append(")").append(Const.CR);
}
retval.append(")").append(Const.CR);
retval.append(databaseMeta.getIDatabase().getDataTablespaceDDL(variables, databaseMeta));
if (pk == null && tk == null && databaseMeta.getIDatabase().isNeoviewVariant()) {
// use this as a default when no pk/tk is
retval.append("NO PARTITION");
// there, otherwise you get an error
}
if (semicolon) {
retval.append(";");
}
return retval.toString();
}
use of org.apache.hop.core.row.IValueMeta 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.IValueMeta in project hop by apache.
the class Database method getRow.
/**
* Get a row from the resultset.
*
* @param rs The resultset to get the row from
* @return one row or null if no row was found on the resultset or if an error occurred.
*/
public Object[] getRow(ResultSet rs, ResultSetMetaData dummy, IRowMeta rowInfo) throws HopDatabaseException {
long startTime = System.currentTimeMillis();
try {
int nrcols = rowInfo.size();
Object[] data = RowDataUtil.allocateRowData(nrcols);
if (rs.next()) {
for (int i = 0; i < nrcols; i++) {
IValueMeta val = rowInfo.getValueMeta(i);
data[i] = databaseMeta.getValueFromResultSet(rs, val, i);
}
} else {
data = null;
}
return data;
} catch (Exception ex) {
throw new HopDatabaseException("Couldn't get row from result set", ex);
} finally {
if (log.isGatheringMetrics()) {
long time = System.currentTimeMillis() - startTime;
log.snap(Metrics.METRIC_DATABASE_GET_ROW_SUM_TIME, databaseMeta.getName(), time);
log.snap(Metrics.METRIC_DATABASE_GET_ROW_MIN_TIME, databaseMeta.getName(), time);
log.snap(Metrics.METRIC_DATABASE_GET_ROW_MAX_TIME, databaseMeta.getName(), time);
log.snap(Metrics.METRIC_DATABASE_GET_ROW_COUNT, databaseMeta.getName());
}
}
}
use of org.apache.hop.core.row.IValueMeta in project hop by apache.
the class DatabaseMeta method quoteReservedWords.
/**
* Changes the names of the fields to their quoted equivalent if this is needed
*
* @param fields The row of fields to change
*/
public void quoteReservedWords(IRowMeta fields) {
for (int i = 0; i < fields.size(); i++) {
IValueMeta v = fields.getValueMeta(i);
v.setName(quoteField(v.getName()));
}
}
Aggregations