Skip to content

Commit

Permalink
Merge pull request apache#4118 from hansva/main
Browse files Browse the repository at this point in the history
Revert "fix apache#3833 - CombinationLookup - specify connection through va…
  • Loading branch information
hansva authored Jul 8, 2024
2 parents 6632a6b + f948f0a commit a93256f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ private int getTechKeyCreation() {
}

private void determineTechKeyCreation() {
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);
String keyCreation = meta.getFields().getReturnFields().getTechKeyCreation();
if (databaseMeta.supportsAutoinc()
if (meta.getDatabaseMeta().supportsAutoinc()
&& CombinationLookupMeta.CREATION_METHOD_AUTOINC.equals(keyCreation)) {
setTechKeyCreation(CREATION_METHOD_AUTOINC);
} else if (databaseMeta.supportsSequences()
} else if (meta.getDatabaseMeta().supportsSequences()
&& CombinationLookupMeta.CREATION_METHOD_SEQUENCE.equals(keyCreation)) {
setTechKeyCreation(CREATION_METHOD_SEQUENCE);
} else {
Expand Down Expand Up @@ -219,8 +218,6 @@ private Object[] lookupValues(IRowMeta rowMeta, Object[] row) throws HopExceptio
List<KeyField> keyFields = fields.getKeyFields();
ReturnFields returnFields = fields.getReturnFields();

DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);

Object[] lookupRow = new Object[data.lookupRowMeta.size()];
int lookupIndex = 0;

Expand All @@ -243,7 +240,7 @@ private Object[] lookupValues(IRowMeta rowMeta, Object[] row) throws HopExceptio
lookupRow[lookupIndex] = row[rowIndex]; // KEYi = ?
lookupIndex++;

if (databaseMeta.requiresCastToVariousForIsNull()
if (meta.getDatabaseMeta().requiresCastToVariousForIsNull()
&& rowMeta.getValueMeta(rowIndex).getType() == IValueMeta.TYPE_STRING) {
lookupRow[lookupIndex] =
rowMeta.getValueMeta(rowIndex).isNull(row[rowIndex]) ? null : "NotNull"; // KEYi IS
Expand Down Expand Up @@ -350,10 +347,10 @@ public boolean processRow() throws HopException {

data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);

data.schemaTable =
databaseMeta.getQuotedSchemaTableCombination(
this, data.realSchemaName, data.realTableName);
meta.getDatabaseMeta()
.getQuotedSchemaTableCombination(this, data.realSchemaName, data.realTableName);

determineTechKeyCreation();

Expand Down Expand Up @@ -428,7 +425,7 @@ public boolean processRow() throws HopException {
* retval: name of the key to return
*/
public void setCombiLookup(IRowMeta inputRowMeta) throws HopDatabaseException {
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);
DatabaseMeta databaseMeta = meta.getDatabaseMeta();
CFields fields = meta.getFields();
List<KeyField> keyFields = fields.getKeyFields();
ReturnFields returnFields = fields.getReturnFields();
Expand Down Expand Up @@ -505,7 +502,7 @@ public void setCombiLookup(IRowMeta inputRowMeta) throws HopDatabaseException {
public Long combiInsert(IRowMeta rowMeta, Object[] row, Long valKey, Long valCrc)
throws HopDatabaseException {
String debug = "Combination insert";
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);
DatabaseMeta databaseMeta = meta.getDatabaseMeta();
CFields fields = meta.getFields();
List<KeyField> keyFields = fields.getKeyFields();
ReturnFields returnFields = fields.getReturnFields();
Expand Down Expand Up @@ -713,23 +710,13 @@ public boolean init() {
} else {
data.cache = new HashMap<>();
}

if (Utils.isEmpty(meta.getConnection())) {
logError(
BaseMessages.getString(
PKG, "CombinationLookup.Init.ConnectionMissing", getTransformName()));
return false;
}

DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);
if (databaseMeta == null) {
if (meta.getDatabaseMeta() == null) {
logError(
BaseMessages.getString(
PKG, "CombinationLookup.Init.ConnectionMissing", getTransformName()));
return false;
}

data.db = new Database(this, this, databaseMeta);
data.db = new Database(this, this, meta.getDatabaseMeta());
try {
data.db.connect();

Expand Down Expand Up @@ -788,8 +775,7 @@ private void preloadCache(IRowMeta hashRowMeta)
throw new HopConfigException(
BaseMessages.getString(PKG, "CombinationLookup.Log.UnexpectedError"));
}
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);

DatabaseMeta databaseMeta = meta.getDatabaseMeta();
if (databaseMeta == null) {
throw new HopConfigException(
BaseMessages.getString(PKG, "CombinationLookup.Log.UnexpectedError"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void widgetSelected(SelectionEvent e) {
}
};
backupChanged = input.hasChanged();
databaseMeta = input.getDatabaseMeta();

// TransformName line
wlTransformName = new Label(shell, SWT.RIGHT);
Expand All @@ -178,9 +179,8 @@ public void widgetSelected(SelectionEvent e) {
wTransformName.setLayoutData(fdTransformName);

// Connection line
wConnection = addConnectionLine(shell, wTransformName, input.getConnection(), lsMod);
wConnection = addConnectionLine(shell, wTransformName, input.getDatabaseMeta(), lsMod);
wConnection.addSelectionListener(lsSelection);

wConnection.addModifyListener(
e -> {
// We have new content: change connection:
Expand Down Expand Up @@ -708,26 +708,24 @@ public void getData() {
// Determine the creation of the technical key for
// backwards compatibility. Can probably be removed at
// version 3.x or so (Sven Boden).

if (Utils.isEmpty(input.getConnection())) {
DatabaseMeta dbMeta = pipelineMeta.findDatabase(input.getConnection(), variables);
if (dbMeta == null || !dbMeta.supportsAutoinc()) {
returnFields.setUseAutoIncrement(false);
}
if (dbMeta != null
&& dbMeta.supportsSequences()
&& StringUtils.isNotEmpty(fields.getSequenceFrom())) {
wSeq.setText(fields.getSequenceFrom());
returnFields.setUseAutoIncrement(false);
wTableMax.setSelection(false);
}
DatabaseMeta dbMeta = input.getDatabaseMeta();
if (dbMeta == null || !dbMeta.supportsAutoinc()) {
returnFields.setUseAutoIncrement(false);
}
wAutoinc.setSelection(returnFields.isUseAutoIncrement());

wSeqButton.setSelection(StringUtils.isNotEmpty(fields.getSequenceFrom()));
if (!returnFields.isUseAutoIncrement() && StringUtils.isEmpty(fields.getSequenceFrom())) {
wTableMax.setSelection(true);
}

if (dbMeta != null
&& dbMeta.supportsSequences()
&& StringUtils.isNotEmpty(fields.getSequenceFrom())) {
wSeq.setText(fields.getSequenceFrom());
returnFields.setUseAutoIncrement(false);
wTableMax.setSelection(false);
}
} else {
// The "creation" field now determines the behaviour of the
// key creation.
Expand All @@ -748,8 +746,8 @@ public void getData() {
wTable.setText(Const.NVL(input.getTableName(), ""));
wTk.setText(Const.NVL(returnFields.getTechnicalKeyField(), ""));

if (input.getConnection() != null) {
wConnection.setText(input.getConnection());
if (input.getDatabaseMeta() != null) {
wConnection.setText(input.getDatabaseMeta().getName());
}
wHashfield.setText(Const.NVL(input.getHashField(), ""));

Expand Down Expand Up @@ -826,7 +824,7 @@ private void getInfo(CombinationLookupMeta in) {
fields.setSequenceFrom(null);
}

in.setConnection(wConnection.getText());
in.setDatabaseMeta(findDatabase(wConnection.getText()));
in.setCommitSize(Const.toInt(wCommit.getText(), 0));
in.setCacheSize(Const.toInt(wCachesize.getText(), 0));

Expand Down Expand Up @@ -956,7 +954,7 @@ private void create() {
shell,
SWT.NONE,
variables,
pipelineMeta.findDatabase(wConnection.getText(), variables),
info.getDatabaseMeta(),
DbCache.getInstance(),
sql.getSql());
sqledit.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.hop.core.annotations.Transform;
import org.apache.hop.core.database.Database;
import org.apache.hop.core.database.DatabaseMeta;
import org.apache.hop.core.exception.HopDatabaseException;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.exception.HopTransformException;
import org.apache.hop.core.row.IRowMeta;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class CombinationLookupMeta
storeWithName = true,
injectionKey = "CONNECTIONNAME",
injectionKeyDescription = "CombinationLookup.Injection.CONNECTION_NAME")
private String connection;
private DatabaseMeta databaseMeta;

/** replace fields with technical key? */
@HopMetadataProperty(
Expand Down Expand Up @@ -146,7 +147,7 @@ public CombinationLookupMeta clone() {
public void setDefault() {
schemaName = "";
tableName = BaseMessages.getString(PKG, "CombinationLookupMeta.DimensionTableName.Label");
connection = null;
databaseMeta = null;
commitSize = 100;
cacheSize = DEFAULT_CACHE_SIZE;
replaceFields = false;
Expand Down Expand Up @@ -197,9 +198,6 @@ public void check(
CheckResult cr;
String errorMessage = "";

DatabaseMeta databaseMeta =
getParentTransformMeta().getParentPipelineMeta().findDatabase(connection, variables);

if (databaseMeta != null) {
Database db = new Database(loggingObject, variables, databaseMeta);
try {
Expand Down Expand Up @@ -401,9 +399,6 @@ public SqlStatement getSqlStatements(
TransformMeta transformMeta,
IRowMeta prev,
IHopMetadataProvider metadataProvider) {

DatabaseMeta databaseMeta =
getParentTransformMeta().getParentPipelineMeta().findDatabase(connection, variables);
SqlStatement retval =
new SqlStatement(transformMeta.getName(), databaseMeta, null); // default: nothing to do!

Expand Down Expand Up @@ -652,9 +647,6 @@ public void analyseImpact(
String[] output,
IRowMeta info,
IHopMetadataProvider metadataProvider) {

DatabaseMeta databaseMeta =
getParentTransformMeta().getParentPipelineMeta().findDatabase(connection, variables);
// The keys are read-only...
for (int i = 0; i < fields.getKeyFields().size(); i++) {
KeyField keyField = fields.getKeyFields().get(i);
Expand Down Expand Up @@ -699,6 +691,22 @@ public boolean supportsErrorHandling() {
return true;
}

protected IRowMeta getDatabaseTableFields(Database db, String schemaName, String tableName)
throws HopDatabaseException {
// First try without connecting to the database... (can be S L O W)
String schemaTable = databaseMeta.getQuotedSchemaTableCombination(db, schemaName, tableName);
IRowMeta extraFields = db.getTableFields(schemaTable);
if (extraFields == null) { // now we need to connect
db.connect();
extraFields = db.getTableFields(schemaTable);
}
return extraFields;
}

Database createDatabaseObject(IVariables variables) {
return new Database(loggingObject, variables, databaseMeta);
}

/**
* Gets schemaName
*
Expand Down Expand Up @@ -735,12 +743,22 @@ public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getConnection() {
return connection;
/**
* Gets databaseMeta
*
* @return value of databaseMeta
*/
public DatabaseMeta getDatabaseMeta() {
return databaseMeta;
}

public void setConnection(String connection) {
this.connection = connection;
/**
* Sets databaseMeta
*
* @param databaseMeta value of databaseMeta
*/
public void setDatabaseMeta(DatabaseMeta databaseMeta) {
this.databaseMeta = databaseMeta;
}

/**
Expand Down

0 comments on commit a93256f

Please sign in to comment.