Search in sources :

Example 1 with ColumnPart

use of org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart 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 "";
}
Also used : Pattern(java.util.regex.Pattern) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) Table(org.talend.sqlbuilder.erdiagram.ui.nodes.Table) Matcher(java.util.regex.Matcher) ConnectionItem(org.talend.core.model.properties.ConnectionItem) ArrayList(java.util.ArrayList) TablePart(org.talend.sqlbuilder.erdiagram.ui.parts.TablePart) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) Item(org.talend.core.model.properties.Item) ConnectionItem(org.talend.core.model.properties.ConnectionItem) Relation(org.talend.sqlbuilder.erdiagram.ui.nodes.Relation) Column(org.talend.sqlbuilder.erdiagram.ui.nodes.Column) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) CheckBox(org.eclipse.draw2d.CheckBox) ColumnPart(org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart) List(java.util.List) ArrayList(java.util.ArrayList) EDatabaseTypeName(org.talend.core.database.EDatabaseTypeName) ErDiagramPart(org.talend.sqlbuilder.erdiagram.ui.parts.ErDiagramPart)

Example 2 with ColumnPart

use of org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart in project tdi-studio-se by Talend.

the class CreateRelationAction method run.

@Override
public void run() {
    if (getSelectedObjects().isEmpty()) {
        return;
    }
    List parts = getSelectedObjects();
    if (parts.size() == 1) {
        Object o = parts.get(0);
        if (!(o instanceof ColumnPart)) {
            return;
        }
        columnPart = (ColumnPart) o;
    } else {
        return;
    }
    /**
         * Create a mouse down event that thinks it is over the blob and dispatch it. This is a bit of a fudge to mimic
         * what the user ought to do.
         */
    Point point = null;
    point = columnPart.getFigure().getClientArea().getCenter();
    Point p = point;
    columnPart.getFigure().translateToAbsolute(p);
    Canvas canvas = (Canvas) columnPart.getViewer().getControl();
    Event event = new Event();
    event.button = 1;
    event.count = 0;
    event.detail = 0;
    event.end = 0;
    event.height = 0;
    event.keyCode = 0;
    event.start = 0;
    event.stateMask = 0;
    // any old time... doesn't matter
    event.time = 9516624;
    event.type = 3;
    event.widget = canvas;
    event.width = 0;
    event.x = p.x + 3;
    event.y = p.y + 3;
    WhereConnectionCreationTool tool = new WhereConnectionCreationTool(new CreationFactory() {

        public Object getNewObject() {
            return null;
        }

        public Object getObjectType() {
            return null;
        }
    });
    columnPart.getViewer().getEditDomain().setActiveTool(tool);
    canvas.notifyListeners(3, event);
}
Also used : Canvas(org.eclipse.swt.widgets.Canvas) CreationFactory(org.eclipse.gef.requests.CreationFactory) ColumnPart(org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart) Event(org.eclipse.swt.widgets.Event) List(java.util.List) Point(org.eclipse.draw2d.geometry.Point)

Example 3 with ColumnPart

use of org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart in project tdi-studio-se by Talend.

the class ErDiagramPartFactory method createEditPart.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart, java.lang.Object)
     */
public EditPart createEditPart(EditPart context, Object model) {
    EditPart part = null;
    if (model instanceof ErDiagram) {
        part = new ErDiagramPart();
    } else if (model instanceof Column) {
        part = new ColumnPart();
    } else if (model instanceof Table) {
        part = new TablePart();
    } else if (model instanceof Relation) {
        part = new RelationPart();
    } else {
        return null;
    }
    // tell the newly created part about the model object
    part.setModel(model);
    return part;
}
Also used : RelationPart(org.talend.sqlbuilder.erdiagram.ui.parts.RelationPart) Relation(org.talend.sqlbuilder.erdiagram.ui.nodes.Relation) Table(org.talend.sqlbuilder.erdiagram.ui.nodes.Table) ErDiagram(org.talend.sqlbuilder.erdiagram.ui.nodes.ErDiagram) Column(org.talend.sqlbuilder.erdiagram.ui.nodes.Column) EditPart(org.eclipse.gef.EditPart) TablePart(org.talend.sqlbuilder.erdiagram.ui.parts.TablePart) ColumnPart(org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart) ErDiagramPart(org.talend.sqlbuilder.erdiagram.ui.parts.ErDiagramPart)

Example 4 with ColumnPart

use of org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart in project tdi-studio-se by Talend.

the class CreateRelationAction method calculateEnabled.

@Override
protected boolean calculateEnabled() {
    if (getSelectedObjects().isEmpty()) {
        return false;
    }
    List parts = getSelectedObjects();
    if (parts.size() == 1) {
        Object o = parts.get(0);
        if (!(o instanceof ColumnPart)) {
            return false;
        }
        columnPart = (ColumnPart) o;
        if (!columnPart.isActive()) {
            return false;
        }
        Column col = (Column) columnPart.getModel();
        if (col.getElementName().equals("*")) {
            //$NON-NLS-1$
            return false;
        }
        return true;
    }
    return false;
}
Also used : Column(org.talend.sqlbuilder.erdiagram.ui.nodes.Column) ColumnPart(org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart) List(java.util.List)

Example 5 with ColumnPart

use of org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart in project tdi-studio-se by Talend.

the class WhereConnectionCreationTool method performConnectionStartWith.

public void performConnectionStartWith(EditPart sourcePart) {
    setConnectionSource(sourcePart);
    updateTargetRequest();
    Command cmd = ((ColumnPart) sourcePart).getCommand(getTargetRequest());
    setCurrentCommand(cmd);
    setState(STATE_CONNECTION_STARTED);
}
Also used : Command(org.eclipse.gef.commands.Command) ColumnPart(org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart)

Aggregations

ColumnPart (org.talend.sqlbuilder.erdiagram.ui.parts.ColumnPart)5 List (java.util.List)3 Column (org.talend.sqlbuilder.erdiagram.ui.nodes.Column)3 Relation (org.talend.sqlbuilder.erdiagram.ui.nodes.Relation)2 Table (org.talend.sqlbuilder.erdiagram.ui.nodes.Table)2 ErDiagramPart (org.talend.sqlbuilder.erdiagram.ui.parts.ErDiagramPart)2 TablePart (org.talend.sqlbuilder.erdiagram.ui.parts.TablePart)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CheckBox (org.eclipse.draw2d.CheckBox)1 Point (org.eclipse.draw2d.geometry.Point)1 EditPart (org.eclipse.gef.EditPart)1 Command (org.eclipse.gef.commands.Command)1 CreationFactory (org.eclipse.gef.requests.CreationFactory)1 Canvas (org.eclipse.swt.widgets.Canvas)1 Event (org.eclipse.swt.widgets.Event)1 EDatabaseTypeName (org.talend.core.database.EDatabaseTypeName)1 Connection (org.talend.core.model.metadata.builder.connection.Connection)1 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)1