Skip to content

Commit

Permalink
TEIIDDES-3124, TEIIDDES-2888
Browse files Browse the repository at this point in the history
 * Added export logic to escape embedded single quotes
 * Added import logic to remove the escape character
 * Added NOTES group in import Teiid DDL page to warn users that
non-escaped single quotes may result in import errors
  • Loading branch information
blafond committed Oct 23, 2017
1 parent 36228b3 commit 96f6784
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
Expand Up @@ -50,6 +50,8 @@ class DdlImporterUiI18n {
static final String IMPORT_COMPLETED_WITH_WARNINGS_MESSAGE = i18n("importCompletedWithWarningsMessage"); //$NON-NLS-1$
static final String MODEL_GROUP_LABEL = i18n("modelGroupLabel"); //$NON-NLS-1$
static final String OPTIONS_GROUP_LABEL = i18n("optionsGroupLabel"); //$NON-NLS-1$
static final String NOTES_GROUP_LABEL= i18n("invalidDdlNotesLabel"); //$NON-NLS-1$
static final String NOTES_GROUP_TEXT= i18n("invalidDdlNotesMessage"); //$NON-NLS-1$

static final String DIFFERENCE_PAGE_TITLE = i18n("differencePage.title"); //$NON-NLS-1$
static final String DIFFERENCE_PAGE_DESCRIPTION = i18n("differencePage.description"); //$NON-NLS-1$
Expand Down
Expand Up @@ -50,7 +50,9 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
Expand Down Expand Up @@ -345,6 +347,20 @@ public void widgetSelected( final SelectionEvent event ) {
createNameOptionsTab(tabFolder);
createDdlTab(tabFolder);

Group helpGroup = WidgetFactory.createGroup(hostPanel, DdlImporterUiI18n.NOTES_GROUP_LABEL, SWT.NONE | SWT.BORDER_DASH,1); //$NON-NLS-1$
helpGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

{
Text helpText = new Text(helpGroup, SWT.WRAP | SWT.READ_ONLY);
helpText.setText(DdlImporterUiI18n.NOTES_GROUP_TEXT);
helpText.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
helpText.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.heightHint = 50;
gd.horizontalSpan=3;
helpText.setLayoutData(gd);
}

scrolledComposite.sizeScrolledPanel();

setControl(hostPanel);
Expand Down
Expand Up @@ -37,6 +37,10 @@ importCompletedWithWarningsTitle = DDL Import Warnings
importCompletedWithWarningsMessage = The relational model was created, but with importer warnings. Consult the list below to determine if re-import is required.
modelGroupLabel=Model Definition
optionsGroupLabel=Options
invalidDdlNotesLabel=Notes
invalidDdlNotesMessage= Import errors may occur if literal strings containing embedded single quotes are not properly escaped.\n\n\
\t\tSample VALID string:\t\t\'(''Stock'')\'\n\
\t\tSample INVALID string:\t\t\'('Stock')\'
differencePage.title = Select the Differences to Apply
differencePage.description = Select the items to apply, then click 'Finish'.
Expand Down
Expand Up @@ -535,8 +535,10 @@ protected void setDataType(AstNode node, RelationalColumn column) throws Excepti
column.setNullable(getRelRefNullable(prop.toString()));

prop = node.getProperty(StandardDdlLexicon.DEFAULT_VALUE);
if (prop != null)
column.setDefaultValue(prop.toString());
if (prop != null) {
String newValue = prop.toString().replaceAll("''", "'");
column.setDefaultValue(newValue);
}
}

@Override
Expand Down
Expand Up @@ -680,10 +680,8 @@ private String getColumnProperties(Column col) {
//
String defaultValue = col.getDefaultValue();
if (!StringUtilities.isEmpty(defaultValue)) {
// defaultValue = convertDefaultValue(defaultValue);
if( !StringUtilities.isSingleQuoted(defaultValue ) ) {
defaultValue = StringUtilities.getQuotedValue(defaultValue, QUOTE_MARK);
}
defaultValue = convertDefaultValue(defaultValue);
defaultValue = ensureSingleQuotedString(defaultValue);
sb.append(TeiidSQLConstants.Reserved.DEFAULT).append(SPACE).append(defaultValue).append(SPACE);
}

Expand Down Expand Up @@ -731,6 +729,18 @@ private String getColumnProperties(Column col) {
return sb.toString().trim();
}

public String ensureSingleQuotedString(String input) {
if( StringUtilities.isSingleQuoted(input) ) {
return input;
} else {
String copyOfInput = input;
copyOfInput.replace("\'", "\\\\'");
StringBuilder result = new StringBuilder();
result.append(SQUOTE + copyOfInput + SQUOTE);
return result.toString();
}
}

private static String convertDefaultValue(String originalValue) {
String removedSQuotes = null;
if( !StringUtilities.isSingleQuoted(originalValue )) {
Expand All @@ -742,8 +752,10 @@ private static String convertDefaultValue(String originalValue) {
if( removedSQuotes.contains("\'")) {
StringBuilder sb = new StringBuilder();
for( char ch : removedSQuotes.toCharArray() ) {
boolean addedQuote = false;
if( ch == '\'') {
sb.append('\\');
sb.append('\'');
addedQuote = true;
}
sb.append(ch);
}
Expand Down

0 comments on commit 96f6784

Please sign in to comment.