use of org.talend.sqlbuilder.erdiagram.ui.nodes.Relation in project tdi-studio-se by Talend.
the class ErDiagramComposite method getSqlStatement.
//$NON-NLS-1$
@SuppressWarnings("unchecked")
private String getSqlStatement() {
//$NON-NLS-1$
String sql = "";
List<String> tables = new ArrayList<String>();
List<String> columns = new ArrayList<String>();
List<String> wheres = new ArrayList<String>();
if (editor != null) {
if (editor.getViewer().getContents() instanceof ErDiagramPart) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String schemaPrefix = "".equals(getSchema()) ? "" : getSchema() + ".";
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String schemaPrefixWithDoubleQuotes = "".equals(getSchema()) ? "" : "\"" + getSchema() + "\".";
ErDiagramPart er = (ErDiagramPart) editor.getViewer().getContents();
for (Object object : er.getChildren()) {
if (object instanceof TablePart) {
TablePart tablePart = (TablePart) object;
Table table = (Table) tablePart.getModel();
if (TextUtil.isDoubleQuotesNeededDbType(getCurrentDbType())) {
//$NON-NLS-1$
tables.add(schemaPrefixWithDoubleQuotes + "\"" + table.getElementName() + "\"");
} else {
tables.add(schemaPrefix + TalendTextUtils.addQuotesWithSpaceField(table.getElementName(), getCurrentDbType()));
}
boolean oracleDbType = TextUtil.isOracleDbType(getCurrentDbType());
for (Object obj : tablePart.getChildren()) {
if (obj instanceof ColumnPart) {
ColumnPart columnPart = (ColumnPart) obj;
Column column = (Column) columnPart.getModel();
CheckBox isSelected = columnPart.getPrimativeFigure().getFigureCustomColumnIsSelectedFigure();
if (isSelected != null && isSelected.isSelected() && !column.getElementName().equals("*")) {
//$NON-NLS-1$
if (TextUtil.isDoubleQuotesNeededDbType(getCurrentDbType())) {
//$NON-NLS-1$
columns.add(schemaPrefixWithDoubleQuotes + "\"" + table.getElementName() + "\".\"" + column.getElementName() + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"\"");
} else {
// added by hyWang
String leftQuote = TalendTextUtils.getQuoteByDBType(getCurrentDbType(), true);
String rightQuote = TalendTextUtils.getQuoteByDBType(getCurrentDbType(), false);
String columnContent = column.getElementName();
//$NON-NLS-1$
Pattern pattern = Pattern.compile("\\w+");
Matcher matcher = pattern.matcher(columnContent);
EDatabaseTypeName dbType = EDatabaseTypeName.getTypeFromDbType(getCurrentDbType());
// modify for bug 12092
boolean sqlKeyword = KeywordsValidator.isSqlKeyword(column.getElementName(), dbType.getProduct());
if (!matcher.matches() || (sqlKeyword && oracleDbType)) {
columns.add(schemaPrefix + TalendTextUtils.addQuotesWithSpaceField(table.getElementName(), getCurrentDbType()) + //$NON-NLS-1$
"." + TalendTextUtils.addQuotesWithSpaceField(leftQuote + column.getElementName() + rightQuote, getCurrentDbType()));
} else {
columns.add(schemaPrefix + TalendTextUtils.addQuotesWithSpaceField(table.getElementName(), getCurrentDbType()) + //$NON-NLS-1$
"." + TalendTextUtils.addQuotesWithSpaceField(column.getElementName(), getCurrentDbType()));
}
}
}
for (Relation rel : (List<Relation>) column.getOutputs()) {
Column source = rel.getSource();
Column target = rel.getTarget();
if (TextUtil.isDoubleQuotesNeededDbType(getCurrentDbType())) {
String where1 = schemaPrefixWithDoubleQuotes + "\"" + source.getTable().getElementName() + //$NON-NLS-1$ //$NON-NLS-2$
"\".\"" + source.getElementName() + "\"= " + schemaPrefixWithDoubleQuotes + "\"" + //$NON-NLS-1$
target.getTable().getElementName() + "\".\"" + target.getElementName() + //$NON-NLS-1$ //$NON-NLS-2$
"\"";
if (!wheres.contains(where1)) {
wheres.add(where1);
}
} else {
String where1 = schemaPrefix + TalendTextUtils.addQuotesWithSpaceField(source.getTable().getElementName(), getCurrentDbType()) + //$NON-NLS-1$
"." + TalendTextUtils.addQuotesWithSpaceField(source.getElementName(), getCurrentDbType()) + //$NON-NLS-1$
"=" + schemaPrefix + TalendTextUtils.addQuotesWithSpaceField(target.getTable().getElementName(), getCurrentDbType()) + //$NON-NLS-1$
"." + TalendTextUtils.addQuotesWithSpaceField(target.getElementName(), getCurrentDbType());
if (!wheres.contains(where1)) {
wheres.add(where1);
}
}
}
}
}
}
}
}
}
// Mssql query need add catalog and schema before the table, like this "catolog.schema.table"
Connection conn = null;
if (rootNode != null) {
Item connectionItem = rootNode.getObject().getProperty().getItem();
if (connectionItem instanceof ConnectionItem) {
conn = ((ConnectionItem) connectionItem).getConnection();
}
}
if (getCurrentDbType() != null && (getCurrentDbType().equals(EDatabaseTypeName.MSSQL.getDisplayName()) || getCurrentDbType().equals(EDatabaseTypeName.MSSQL.name())) && conn != null) {
List<String> newTables = new ArrayList<String>();
for (String str : tables) {
newTables.add(getMssqlCatalog(str, conn));
}
tables = newTables;
}
sql = getSelectStatement(tables, columns, wheres);
if (sql.endsWith(",")) {
//$NON-NLS-1$
return sql.substring(0, sql.length() - 1);
} else if (sql.endsWith(" and ")) {
//$NON-NLS-1$
return sql.substring(0, sql.length() - 5);
}
//$NON-NLS-1$
return "";
}
use of org.talend.sqlbuilder.erdiagram.ui.nodes.Relation in project tdi-studio-se by Talend.
the class RelationPart method createEditPolicies.
/*
* (non-Javadoc)
*
* @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
*/
@Override
protected void createEditPolicies() {
// Selection handle edit policy.
// Makes the connection show a feedback, when selected by the user.
installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE, new ConnectionEndpointEditPolicy());
// Allows the removal of the connection model element
installEditPolicy(EditPolicy.CONNECTION_ROLE, new ConnectionEditPolicy() {
protected Command getDeleteCommand(GroupRequest request) {
List<Relation> connectionList = new ArrayList<Relation>();
for (int i = 0; i < request.getEditParts().size(); i++) {
if (request.getEditParts().get(i) instanceof RelationPart) {
connectionList.add(((Relation) ((RelationPart) request.getEditParts().get(i)).getModel()));
}
}
return new RelationDeleteCommand(connectionList);
}
});
}
use of org.talend.sqlbuilder.erdiagram.ui.nodes.Relation in project tdi-studio-se by Talend.
the class ColumnGraphicalEditPolicy method getReconnectTargetCommand.
@Override
protected Command getReconnectTargetCommand(ReconnectRequest request) {
Relation relation = (Relation) request.getConnectionEditPart().getModel();
Column newTarget = (Column) getHost().getModel();
RelationReconnectionCommand command = new RelationReconnectionCommand(relation);
command.setNewTarget(newTarget);
return command;
}
use of org.talend.sqlbuilder.erdiagram.ui.nodes.Relation in project tdi-studio-se by Talend.
the class ColumnGraphicalEditPolicy method getReconnectSourceCommand.
@Override
protected Command getReconnectSourceCommand(ReconnectRequest request) {
Relation relation = (Relation) request.getConnectionEditPart().getModel();
Column newSource = (Column) getHost().getModel();
RelationReconnectionCommand command = new RelationReconnectionCommand(relation);
command.setNewSource(newSource);
return command;
}
use of org.talend.sqlbuilder.erdiagram.ui.nodes.Relation in project tdi-studio-se by Talend.
the class DeleteTableCommand method execute.
/*
* (non-Javadoc)
*
* @see org.eclipse.gef.commands.Command#execute()
*/
@Override
public void execute() {
for (Table table : this.tables) {
erDiagram.removeTable(table);
for (Object obj : table.getColumns()) {
if (obj instanceof Column) {
List<Relation> inputs = ((Column) obj).getInputs();
List<Relation> outputs = ((Column) obj).getOutputs();
for (Relation relation : inputs) {
Column preColumn = relation.getSource();
if (!columns.contains(preColumn)) {
preColumn.removeOutputRelation(relation);
}
}
for (Relation relation : outputs) {
Column preColumn = relation.getTarget();
if (!columns.contains(preColumn)) {
preColumn.removeInputRelation(relation);
}
}
}
}
}
}
Aggregations