Skip to content

Commit

Permalink
Merge pull request apache#3876 from nadment/8535
Browse files Browse the repository at this point in the history
  • Loading branch information
hansva authored Apr 29, 2024
2 parents bd9b953 + a3570e1 commit 2f5964d
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import org.apache.hop.core.Const;
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;
Expand Down Expand Up @@ -377,59 +378,67 @@ private void logInfo(Object[] outputRow) throws HopValueException {
@Override
public boolean init() {

if (!super.init()) {
return false;
}
if (Utils.isEmpty(meta.getTableNameFieldName())) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.TableNameFieldNameMissing"));
return false;
}
String realSchemaName = resolve(meta.getSchemaName());
if (!Utils.isEmpty(realSchemaName)) {
data.realSchemaName = realSchemaName;
}
data.realTableNameFieldName = resolve(meta.getTableNameFieldName());
data.realObjectTypeFieldName = resolve(meta.getObjectTypeFieldName());
data.realIsSystemObjectFieldName = resolve(meta.isSystemObjectFieldName());
data.realSqlCreationFieldName = resolve(meta.getSqlCreationFieldName());
if (!meta.isIncludeCatalog()
&& !meta.isIncludeSchema()
&& !meta.isIncludeTable()
&& !meta.isIncludeView()
&& !meta.isIncludeProcedure()
&& !meta.isIncludeSynonym()) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.IncludeAtLeastOneType"));
return false;
}
if (super.init()) {

try {
// Create the output row meta-data
data.outputRowMeta = new RowMeta();
meta.getFields(
data.outputRowMeta, getTransformName(), null, null, this, metadataProvider); // get the
// metadata
// populated
} catch (Exception e) {
logError("Error initializing transform: " + e.toString());
logError(Const.getStackTracker(e));
return false;
}
DatabaseMeta databaseMeta = getPipelineMeta().findDatabase(meta.getConnection(), variables);
if (databaseMeta == null) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.InvalidConnection"));
return false;
}

data.db = new Database(this, this, meta.getDatabase());
try {
data.db.connect();
if (Utils.isEmpty(meta.getTableNameFieldName())) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.TableNameFieldNameMissing"));
return false;
}

if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "GetTableNames.Log.ConnectedToDB"));
String realSchemaName = resolve(meta.getSchemaName());
if (!Utils.isEmpty(realSchemaName)) {
data.realSchemaName = realSchemaName;
}
data.realTableNameFieldName = resolve(meta.getTableNameFieldName());
data.realObjectTypeFieldName = resolve(meta.getObjectTypeFieldName());
data.realIsSystemObjectFieldName = resolve(meta.isSystemObjectFieldName());
data.realSqlCreationFieldName = resolve(meta.getSqlCreationFieldName());
if (!meta.isIncludeCatalog()
&& !meta.isIncludeSchema()
&& !meta.isIncludeTable()
&& !meta.isIncludeView()
&& !meta.isIncludeProcedure()
&& !meta.isIncludeSynonym()) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.IncludeAtLeastOneType"));
return false;
}

return true;
} catch (HopException e) {
logError(BaseMessages.getString(PKG, "GetTableNames.Log.DBException") + e.getMessage());
if (data.db != null) {
data.db.disconnect();
try {
// Create the output row meta-data
data.outputRowMeta = new RowMeta();
meta.getFields(
data.outputRowMeta, getTransformName(), null, null, this, metadataProvider); // get
// the
// metadata
// populated
} catch (Exception e) {
logError("Error initializing transform: " + e.toString());
logError(Const.getStackTracker(e));
return false;
}

data.db = new Database(this, variables, databaseMeta);
try {
data.db.connect();
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "GetTableNames.Log.ConnectedToDB"));
}

return true;
} catch (HopException e) {
logError(BaseMessages.getString(PKG, "GetTableNames.Log.DBException") + e.getMessage());
if (data.db != null) {
data.db.disconnect();
}
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public String open() {
wTransformName.setLayoutData(fdTransformName);

// Connection line
wConnection = addConnectionLine(shell, wTransformName, input.getDatabase(), null);
wConnection =
addConnectionLine(shell, wTransformName, input.getConnection(), e -> input.setChanged());

// schemaname fieldname ...
wlSchemaName = new Label(shell, SWT.RIGHT);
Expand Down Expand Up @@ -228,7 +229,7 @@ public String open() {
// Include Catalogs
wlIncludeCatalog = new Label(wSettings, SWT.RIGHT);
wlIncludeCatalog.setText(
BaseMessages.getString(PKG, "GetCatalogNamesDialog.IncludeCatalog.Label"));
BaseMessages.getString(PKG, "GetTableNamesDialog.IncludeCatalog.Label"));
PropsUi.setLook(wlIncludeCatalog);
FormData fdlIncludeCatalog = new FormData();
fdlIncludeCatalog.left = new FormAttachment(0, -margin);
Expand All @@ -238,16 +239,15 @@ public String open() {
wIncludeCatalog = new Button(wSettings, SWT.CHECK);
PropsUi.setLook(wIncludeCatalog);
wIncludeCatalog.setToolTipText(
BaseMessages.getString(PKG, "GetCatalogNamesDialog.IncludeCatalog.Tooltip"));
BaseMessages.getString(PKG, "GetTableNamesDialog.IncludeCatalog.Tooltip"));
FormData fdIncludeCatalog = new FormData();
fdIncludeCatalog.left = new FormAttachment(middle, -margin);
fdIncludeCatalog.top = new FormAttachment(wlIncludeCatalog, 0, SWT.CENTER);
wIncludeCatalog.setLayoutData(fdIncludeCatalog);

// Include Schemas
Label wlIncludeSchema = new Label(wSettings, SWT.RIGHT);
wlIncludeSchema.setText(
BaseMessages.getString(PKG, "GetSchemaNamesDialog.IncludeSchema.Label"));
wlIncludeSchema.setText(BaseMessages.getString(PKG, "GetTableNamesDialog.IncludeSchema.Label"));
PropsUi.setLook(wlIncludeSchema);
FormData fdlincludeSchema = new FormData();
fdlincludeSchema.left = new FormAttachment(0, -margin);
Expand All @@ -257,7 +257,7 @@ public String open() {
wIncludeSchema = new Button(wSettings, SWT.CHECK);
PropsUi.setLook(wIncludeSchema);
wIncludeSchema.setToolTipText(
BaseMessages.getString(PKG, "GetSchemaNamesDialog.IncludeSchema.Tooltip"));
BaseMessages.getString(PKG, "GetTableNamesDialog.IncludeSchema.Tooltip"));
FormData fdincludeSchema = new FormData();
fdincludeSchema.left = new FormAttachment(middle, -margin);
fdincludeSchema.top = new FormAttachment(wlIncludeSchema, 0, SWT.CENTER);
Expand Down Expand Up @@ -558,8 +558,8 @@ public void getData() {
logDebug(toString(), BaseMessages.getString(PKG, "GetTableNamesDialog.Log.GettingKeyInfo"));
}

if (input.getDatabase() != null) {
wConnection.setText(input.getDatabase().getName());
if (input.getConnection() != null) {
wConnection.setText(input.getConnection());
}
if (input.getSchemaName() != null) {
wSchemaName.setText(input.getSchemaName());
Expand Down Expand Up @@ -629,8 +629,7 @@ private void ok() {
}
transformName = wTransformName.getText(); // return value

getInfo(input);
if (input.getDatabase() == null) {
if (Utils.isEmpty(wConnection.getText())) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(
BaseMessages.getString(PKG, "GetTableNamesDialog.InvalidConnection.DialogMessage"));
Expand All @@ -640,16 +639,12 @@ private void ok() {
}

input.setChanged();
getInfo(input);
dispose();
}

private void getInfo(GetTableNamesMeta info) {
try {
info.setDatabase(DatabaseMeta.loadDatabase(getMetadataProvider(), wConnection.getText()));
} catch (Exception e) {
info.setDatabase(null);
new ErrorDialog(shell, "Error", "Error loading database connection", e);
}
info.setConnection(wConnection.getText());
info.setSchemaName(wSchemaName.getText());
info.setTableNameFieldName(wTableNameField.getText());
info.setSqlCreationFieldName(wSqlCreationField.getText());
Expand All @@ -662,13 +657,21 @@ private void getInfo(GetTableNamesMeta info) {
info.setIncludeProcedure(wIncludeProcedure.getSelection());
info.setIncludeSynonym(wIncludeSynonym.getSelection());
info.setAddSchemaInOutput(wAddSchemaInOutput.getSelection());

info.setDynamicSchema(wDynamicSchema.getSelection());
info.setSchemaNameField(wSchemaField.getText());
}

private boolean checkUserInput(GetTableNamesMeta meta) {

if (Utils.isEmpty(meta.getConnection())) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(
BaseMessages.getString(PKG, "GetTableNamesDialog.InvalidConnection.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "GetTableNamesDialog.InvalidConnection.DialogTitle"));
mb.open();
return false;
}

if (Utils.isEmpty(meta.getTableNameFieldName())) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(
Expand All @@ -688,14 +691,7 @@ private void preview() {
GetTableNamesMeta oneMeta = new GetTableNamesMeta();

getInfo(oneMeta);
if (oneMeta.getDatabase() == null) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(
BaseMessages.getString(PKG, "GetTableNamesDialog.InvalidConnection.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "GetTableNamesDialog.InvalidConnection.DialogTitle"));
mb.open();
return;
}

if (!checkUserInput(oneMeta)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@
public class GetTableNamesMeta extends BaseTransformMeta<GetTableNames, GetTableNamesData> {
private static final Class<?> PKG = GetTableNamesMeta.class; // For Translator

/** database connection */
/** The database connection */
@HopMetadataProperty(
storeWithName = true,
key = "connection",
injectionKey = "CONNECTIONNAME",
injectionKeyDescription = "GetTableNames.Injection.CONNECTION_NAME")
private DatabaseMeta database;
private String connection;

@HopMetadataProperty(
key = "schemaname",
Expand Down Expand Up @@ -150,7 +149,7 @@ public GetTableNamesMeta() {

public GetTableNamesMeta(GetTableNamesMeta m) {
this();
this.database = m.database == null ? null : new DatabaseMeta(m.database);
this.connection = m.connection;
this.schemaName = m.schemaName;
this.tableNameFieldName = m.tableNameFieldName;
this.sqlCreationFieldName = m.sqlCreationFieldName;
Expand All @@ -174,7 +173,7 @@ public GetTableNamesMeta clone() {

@Override
public void setDefault() {
database = null;
connection = null;
schemaName = null;
includeCatalog = false;
includeSchema = false;
Expand Down Expand Up @@ -247,9 +246,16 @@ public void check(
CheckResult cr;
String errorMessage = "";

if (database == null) {
errorMessage = BaseMessages.getString(PKG, "GetTableNamesMeta.CheckResult.InvalidConnection");
cr = new CheckResult(ICheckResult.TYPE_RESULT_ERROR, errorMessage, transformMeta);
DatabaseMeta databaseMeta = pipelineMeta.findDatabase(connection, variables);
if (databaseMeta == null) {
cr =
new CheckResult(
ICheckResult.TYPE_RESULT_ERROR,
BaseMessages.getString(
PKG,
"GetTableNamesMeta.CheckResult.InvalidConnection",
variables.resolve(connection)),
transformMeta);
remarks.add(cr);
}
if (Utils.isEmpty(tableNameFieldName)) {
Expand All @@ -264,6 +270,17 @@ public void check(
remarks.add(cr);
}

if (!isIncludeCatalog()
&& !isIncludeSchema()
&& !isIncludeTable()
&& !isIncludeView()
&& !isIncludeProcedure()
&& !isIncludeSynonym()) {
errorMessage =
BaseMessages.getString(PKG, "GetTableNamesMeta.CheckResult.IncludeAtLeastOneType");
remarks.add(new CheckResult(ICheckResult.TYPE_RESULT_ERROR, errorMessage, transformMeta));
}

// See if we have input streams leading to this transform!
if (input.length > 0 && !isDynamicSchema()) {
cr =
Expand All @@ -287,18 +304,12 @@ public boolean supportsErrorHandling() {
return true;
}

/**
* @return Returns the database.
*/
public DatabaseMeta getDatabase() {
return database;
public String getConnection() {
return connection;
}

/**
* @param database The database to set.
*/
public void setDatabase(DatabaseMeta database) {
this.database = database;
public void setConnection(String connection) {
this.connection = connection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
# limitations under the License.
#

GetCatalogNamesDialog.IncludeCatalog.Label=Include catalogs
GetCatalogNamesDialog.IncludeCatalog.Tooltip=Include catalogs in output
GetSchemaNamesDialog.IncludeSchema.Label=Include schemas
GetSchemaNamesDialog.IncludeSchema.Tooltip=Include schemas list
GetTableNames.Description=Get table names from database connection and send them to the next transform
GetTableNames.Error.IncludeAtLeastOneType=You must specify at least one type\!
GetTableNames.Error.TableNameFieldNameMissing=TablenameField fieldname is missing\!
GetTableNames.Exception.CouldnotFindField=We can not find field [{0}] in input stream\!
GetTableNames.Injection.ADD_SCHEMA_IN_OUTPUT=Include schema name in the output.
GetTableNames.Injection.CONNECTION_NAME=The name of the database connection to get table names from.
GetTableNames.Injection.DYNAMIC_SCHEMA=Set this option to get the schema name from a field in a previous transform.
GetTableNames.Injection.FIELDS=Fields
GetTableNames.Injection.INCLUDE_CATALOG=Include catalogs in the output.
GetTableNames.Injection.INCLUDE_PROCEDURE=Include stored procedures in the output.
GetTableNames.Injection.INCLUDE_SCHEMA=Include schema list in the output.
Expand All @@ -35,10 +30,8 @@ GetTableNames.Injection.INCLUDE_TABLE=Include tables in the output.
GetTableNames.Injection.INCLUDE_VIEW=Include views in the output.
GetTableNames.Injection.IS_SYSTEM_OBJECT_FIELD_NAME=The name of the field to indicate if the result is a system object.
GetTableNames.Injection.OBJECT_TYPE_FIELD_NAME=The name of the field for the object type.
GetTableNames.Injection.OUTPUT=Output Options
GetTableNames.Injection.SCHEMA_NAME=The name of the database schema to use.
GetTableNames.Injection.SCHEMA_NAME_FIELD=The name of the field to get the schema name from.
GetTableNames.Injection.SETTINGS=Settings
GetTableNames.Injection.SQL_CREATION_FIELD_NAME=The name of the field containing the SQL "create" statement.
GetTableNames.Injection.TABLE_NAME_FIELD_NAME=The name of the field for the table names.
GetTableNames.LineNumber=Line# {0}
Expand All @@ -60,8 +53,12 @@ GetTableNamesDialog.Error.TableNameFieldNameMissingTitle=Missing field name
GetTableNamesDialog.ErrorInPreview.DialogMessage=Error while previewing data\!
GetTableNamesDialog.FailedToGetFields.DialogMessage=Error getting fields from previous transforms\!
GetTableNamesDialog.FailedToGetFields.DialogTitle=Error
GetTableNamesDialog.IncludeCatalog.Label=Include catalogs
GetTableNamesDialog.IncludeCatalog.Tooltip=Include catalogs in output
GetTableNamesDialog.IncludeProcedure.Label=Include procedures
GetTableNamesDialog.IncludeProcedure.Tooltip=Include procedures
GetTableNamesDialog.IncludeSchema.Label=Include schemas
GetTableNamesDialog.IncludeSchema.Tooltip=Include schemas list
GetTableNamesDialog.includeSynonym.Label=Include synonyms
GetTableNamesDialog.IncludeSynonym.Tooltip=Include synonyms
GetTableNamesDialog.IncludeTable.Label=Include tables
Expand Down Expand Up @@ -92,6 +89,7 @@ GetTableNamesDialog.TableNameFieldName.Tooltip=Specify here the name of the Tabl
GetTableNamesDialog.TransformName.Label=Transform name
GetTableNamesDialog.wOutputFields.Label=Output fields
GetTableNamesDialog.wSettings.Label=Settings
GetTableNamesMeta.CheckResult.IncludeAtLeastOneType=You must specify at least one type to include
GetTableNamesMeta.CheckResult.InvalidConnection=Database connection is missing or invalid\!
GetTableNamesMeta.CheckResult.NoInputReceived=This transform is not expected to receive input stream\!
GetTableNamesMeta.CheckResult.ReceivingInfoFromOtherTransforms=This transform will generate output stream.
Expand Down
Loading

0 comments on commit 2f5964d

Please sign in to comment.