Skip to content

Commit

Permalink
#6745 FDW output page. Script generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Sep 15, 2019
1 parent 6526a0d commit b4771b6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
Expand Up @@ -76,7 +76,6 @@ String getDescription() {
setNeedsProgressMonitor(true);

this.fdwPropertySource = new PropertySourceCustom();
this.fdwPropertySource.setDefValueResolver(database.getDataSource().getContainer().getVariablesResolver());
}

public PostgreDatabase getDatabase() {
Expand Down
Expand Up @@ -135,8 +135,9 @@ private void refreshFDWProperties() {
DBPDataSourceContainer targetDataSource = getWizard().getSelectedDataSource();
PostgreFDWConfigWizard.FDWInfo selectedFDW = getWizard().getSelectedFDW();

PropertySourceCustom propertySource = new PropertySourceCustom();
PropertySourceCustom propertySource = getWizard().getFdwPropertySource();
propertySource.setDefValueResolver(targetDataSource.getVariablesResolver());
propertySource.removeAll();
if (selectedFDW != null && selectedFDW.fdwDescriptor != null) {
propertySource.addProperties(selectedFDW.fdwDescriptor.getProperties());
} else if (selectedFDW != null) {
Expand Down Expand Up @@ -214,12 +215,6 @@ private void loadSettings() {
setErrorMessage(null);

// Detect FDW from target container
if (CommonUtils.isEmpty(fdwServerText.getText())) {
String fdwServerId = targetDataSource.getDriver().getId() + "_srv";
getWizard().setFdwServerId(fdwServerId);
fdwServerText.setText(fdwServerId);
}

PostgreFDWConfigWizard.FDWInfo fdwInfo = getWizard().getSelectedFDW();
if (fdwInfo == null) {
FDWConfigDescriptor fdwConfig = FDWConfigRegistry.getInstance().findFirstMatch(targetDataSource);
Expand All @@ -239,6 +234,12 @@ private void loadSettings() {

refreshFDWProperties();

if (CommonUtils.isEmpty(fdwServerText.getText())) {
String fdwServerId = (fdwInfo == null ? targetDataSource.getDriver().getId() : fdwInfo.getId()) + "_srv";
getWizard().setFdwServerId(fdwServerId);
fdwServerText.setText(fdwServerId);
}

// Fill entities
targetDataSourceText.setText(targetDataSource.getName());
targetDriverText.setText(targetDataSource.getDriver().getName());
Expand Down
Expand Up @@ -22,13 +22,16 @@
import org.eclipse.swt.widgets.Group;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.properties.PropertySourceCustom;
import org.jkiss.dbeaver.runtime.ui.UIServiceSQL;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage;
import org.jkiss.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;


class PostgreFDWConfigWizardPageFinal extends ActiveWizardPage<PostgreFDWConfigWizard> {
Expand Down Expand Up @@ -90,11 +93,11 @@ public void activatePage() {
}

private void generateScript() {
// Fill FDW list
StringBuilder script = new StringBuilder();
try {
getWizard().getRunnableContext().run(false, true, monitor -> {
try {
throw new DBCException("Not implemented yet");
generateScript(monitor, script);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
Expand All @@ -108,12 +111,41 @@ private void generateScript() {
}
setErrorMessage(null);

String sql = "CREATE FOREIGN SERVER";
String sql = script.toString();

UIServiceSQL service = DBWorkbench.getService(UIServiceSQL.class);
if (service != null) {
service.setSQLPanelText(sqlPanel, sql);
}
}

private void generateScript(DBRProgressMonitor monitor, StringBuilder script) throws DBException {
PostgreFDWConfigWizard.FDWInfo selectedFDW = getWizard().getSelectedFDW();
PropertySourceCustom propertySource = getWizard().getFdwPropertySource();
Map<Object, Object> propValues = propertySource.getPropertiesWithDefaults();

script.append("-- CREATE EXTENSION ").append(selectedFDW.getId()).append(";\n\n");
String serverId = getWizard().getFdwServerId();
script.append("CREATE SERVER ").append(serverId)
.append("\n\tFOREIGN DATA WRAPPER ").append(selectedFDW.getId())
.append("\n\tOPTIONS(");
boolean firstProp = true;
for (Map.Entry<Object, Object> pe : propValues.entrySet()) {
String propName = CommonUtils.toString(pe.getKey());
String propValue = CommonUtils.toString(pe.getValue());
if (CommonUtils.isEmpty(propName) || CommonUtils.isEmpty(propValue)) {
continue;
}
if (!firstProp) script.append(", ");
script.append(propName).append(" '").append(propValue).append("'");
firstProp = false;
}
script
.append(");\n\n");

script.append("CREATE USER MAPPING FOR CURRENT_USER SERVER ").append(serverId).append(";\n\n");

//CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhousedb_fdw OPTIONS(dbname 'default', driver '/usr/local/lib/odbc/libclickhouseodbc.so', host '46.101.202.143');
}

}
Expand Up @@ -91,6 +91,11 @@ public Map<Object, Object> getPropertiesWithDefaults() {
Map<Object, Object> allValues = new HashMap<>(defaultValues);
allValues.putAll(originalValues);
allValues.putAll(propValues);
if (defValueResolver != null) {
for (Map.Entry<Object, Object> prop : allValues.entrySet()) {
prop.setValue(getDefaultValue(prop.getValue()));
}
}
return allValues;
}

Expand Down Expand Up @@ -141,7 +146,10 @@ public Object getPropertyValue(@Nullable DBRProgressMonitor monitor, Object id)
if (value == null) {
value = originalValues.get(id);
}
return value != null ? value : getDefaultValue(defaultValues.get(id));
if (value == null) {
value = defaultValues.get(id);
}
return value != null ? getDefaultValue(value) : null;
}

@Override
Expand All @@ -158,7 +166,7 @@ public boolean isPropertySet(Object id)
return false;
}
final Object defaultValue = getDefaultValue(defaultValues.get(id));
return !CommonUtils.equalObjects(value, defaultValue);
return !CommonUtils.equalObjects(getDefaultValue(value), defaultValue);
}

@Override
Expand Down Expand Up @@ -199,4 +207,11 @@ public void resetPropertyValueToDefault(Object id)
originalValues.remove(id);
}

public void removeAll() {
props.clear();

originalValues.clear();
propValues.clear();
defaultValues.clear();
}
}

0 comments on commit b4771b6

Please sign in to comment.