Package org.apache.tuscany.das.rdb.config.wrapper

Examples of org.apache.tuscany.das.rdb.config.wrapper.TableWrapper


        super();
    }

    public InsertCommandImpl getInsertCommand(MappingWrapper config, DataObject changedObject, Table t) {
        List parameters = new ArrayList();
        TableWrapper table = new TableWrapper(t);
        StringBuffer statement = new StringBuffer("insert into ");
        //JIRA-952
        if(config.getConfig().isDatabaseSchemaNameSupported()){
          statement.append(t.getSchemaName()+"."+t.getTableName());
        }
        else{
        statement.append(t.getTableName());
        }
        HashSet changedProperties = getAttributeProperties(changedObject, config, table);
        Iterator i;
        if ( changedProperties.isEmpty() ) {
            i = changedObject.getType().getProperties().iterator();
        } else {
            i = changedProperties.iterator();
        }

        List attributes = new ArrayList();
        List generatedKeys = new ArrayList();
        while (i.hasNext()) {
            Property attr = (Property) i.next();
            if ( attr.getType().isDataType()) {
                if (table.isGeneratedColumnProperty(attr.getName())) {
                 generatedKeys.add(attr.getName());
             } else {
                 attributes.add(attr.getName());
                    parameters.add(changedObject.getType().getProperty(attr.getName()));
             }
            }
        }

        statement.append("(");
        Iterator attrs = attributes.iterator();
        while (attrs.hasNext()) {
            String name = (String) attrs.next();
            statement.append("");
            Column c = config.getColumnByPropertyName(t, name);
            statement.append(c == null ? name : c.getColumnName());
            if (attrs.hasNext()) {
                statement.append(", ");
            } else {
                statement.append(")");
            }
        }

        statement.append(" values (");
        for (int idx = 1; idx <= attributes.size(); idx++) {
            statement.append('?');
            if (idx < attributes.size()) {
                statement.append(", ");
            } else {
                statement.append(")");
            }
        }

        InsertCommandImpl cmd = new InsertCommandImpl(statement.toString(),
                (String[]) generatedKeys.toArray(new String[0]));
        Iterator params = parameters.iterator();
        for (int idx = 1; params.hasNext(); idx++) {
            Property property = (Property) params.next();
            ParameterExtendedImpl p = new ParameterExtendedImpl();
            p.setName(property.getName());
            p.setType(property.getType());
            p.setColumnType(SDODataTypeHelper.columnTypeForSDOType(property.getType()));
            p.setConverter(getConverter(table.getConverter(property.getName())));
            p.setIndex(idx);
            cmd.addParameter(p);

        }
        if (this.logger.isDebugEnabled()) {
View Full Code Here


        super();
    }

    //JIRA-952
    private String getDeleteStatement(MappingWrapper mapping, Table t) {
        TableWrapper table = new TableWrapper(t);

        StringBuffer statement = new StringBuffer();
        statement.append("delete from ");
        if(mapping.getConfig().isDatabaseSchemaNameSupported()){
          statement.append(t.getSchemaName()+"."+t.getTableName());
        }
        else{
        statement.append(t.getTableName());
        }
        statement.append(" where ");

        Iterator names = table.getPrimaryKeyNames().iterator();
        Iterator properties = table.getPrimaryKeyProperties().iterator();
        while (names.hasNext() && properties.hasNext()) {
            String name = (String) names.next();
            statement.append(name);
            statement.append(" = ?");
            if (names.hasNext() && properties.hasNext()) {
View Full Code Here

        return statement.toString();
    }

    public DeleteCommandImpl getDeleteCommand(MappingWrapper mapping, Table t) {
        TableWrapper tw = new TableWrapper(t);
        DeleteCommandImpl deleteCommand = new DeleteCommandImpl(getDeleteStatement(mapping, t));

        Iterator i = tw.getPrimaryKeyProperties().iterator();
        for (int idx = 1; i.hasNext(); idx++) {
            String property = (String) i.next();
            ParameterExtendedImpl p = new ParameterExtendedImpl();
            p.setName(property);
            p.setColumnType(SDODataTypeHelper.columnTypeForSDOType(SDODataTypes.OBJECT));
            p.setConverter(getConverter(tw.getConverter(property)));
            p.setIndex(idx);
            deleteCommand.addParameter(p);
        }
        return deleteCommand;
    }
View Full Code Here

        // to the list of propertyNames
        Config model = configWrapper.getConfig();
        if (model != null) {
            Iterator tablesFromModel = model.getTable().iterator();
            while (tablesFromModel.hasNext()) {
                TableWrapper t = new TableWrapper((Table) tablesFromModel.next());
                if (tableToPropertyMap.get(t.getTypeName()) == null) {
                    tableToPropertyMap.put(t.getTypeName(), Collections.EMPTY_LIST);
                }
            }
        }
    }
View Full Code Here

        }
        Update update = table.getUpdate();
        if (update == null) {
            updateCommand = UpdateGenerator.INSTANCE.getUpdateCommand(mapping, changedObject, table);
        } else {
            TableWrapper t = new TableWrapper(table);
            if (t.getCollisionColumn() != null) {
                updateCommand = new OptimisticWriteCommandImpl(update);
            } else {
                updateCommand = new UpdateCommandImpl(update);
            }
        }
View Full Code Here

        }
        else{
          //other cases, its better to use typeName as r.getPrimaryKeyTable()
          //gives tableName and tableName and typeName can be different
          //and SDO looks for typeName and not tableName.
          parentRef = getParentReference((new TableWrapper(tbl)).getTypeName());
        }
       
        DataObject parent = dataObject.getDataObject(parentRef);
       
        if (parent == null) {
View Full Code Here

            if (rel.isKeyRestricted()) {
                throw new RuntimeException("Can not modify a one to one relationship that is key restricted");
            }
            // This is a one-one relationship
            Table t = mapping.getTableByTypeName(changedObject.getType().getName());
            TableWrapper tw = new TableWrapper(t);
            RelationshipWrapper rw = new RelationshipWrapper(rel);
            if ((rel.getForeignKeyTable().equals(t.getTableName()))
                    && (CollectionsUtil.disjoint(tw.getPrimaryKeyProperties(), rw.getForeignKeys()))) {
                return true;
            }

        }
View Full Code Here

    }

    public UpdateCommandImpl getUpdateCommand(MappingWrapper mapping, DataObject changedObject, Table table) {     
        List parameters = new ArrayList();
        Type type = changedObject.getType();
        TableWrapper tableWrapper = new TableWrapper(table);
        StringBuffer statement = new StringBuffer("update ");
        //JIRA-952
        if(mapping.getConfig().isDatabaseSchemaNameSupported()){
          statement.append(table.getSchemaName()+"."+table.getTableName());
        }
        else{
        statement.append(table.getTableName());
        }
        statement.append(" set ");

        ChangeSummary summary = changedObject.getDataGraph().getChangeSummary();
        HashSet changedFields = getChangedFields(mapping, summary, changedObject, tableWrapper);
        Iterator i = changedFields.iterator();
     
        int idx = 1;
        while (i.hasNext()) {
            Property property = (Property) i.next();
            Column c = tableWrapper.getColumnByPropertyName(property.getName());         
           
            if ((c == null) || !c.isCollision() || !c.isPrimaryKey()) {
                String columnName = c == null ? property.getName() : c.getColumnName();
                appendFieldSet(statement, idx > 1, columnName);
                parameters.add(createParameter(tableWrapper, property, idx++));
            }
        }
       
        Column c = tableWrapper.getManagedColumn();
        if (c != null) {
            appendFieldSet(statement, idx > 1, c.getColumnName());
            String propertyName = c.getPropertyName() == null ? c.getColumnName() : c.getPropertyName();
            parameters.add(createManagedParameter(tableWrapper,
                    changedObject.getProperty(propertyName), idx++));
        }
       
        statement.append(" where ");

        Iterator pkColumnNames = tableWrapper.getPrimaryKeyNames().iterator();
        Iterator pkPropertyNames = tableWrapper.getPrimaryKeyProperties().iterator();
        while (pkColumnNames.hasNext() && pkPropertyNames.hasNext()) {
            String columnName = (String) pkColumnNames.next();
            String propertyName = (String) pkPropertyNames.next();
            statement.append(columnName);
            statement.append(" = ?");
            if (pkColumnNames.hasNext() && pkPropertyNames.hasNext()) {
                statement.append(" and ");
            }
            parameters.add(createParameter(tableWrapper, type.getProperty(propertyName), idx++));
        }

        if (tableWrapper.getCollisionColumn() == null) {
            Iterator iter = changedFields.iterator();
            while (iter.hasNext()) {
                statement.append(" and ");
                Property changedProperty = (Property) iter.next();
                Column column = tableWrapper.getColumnByPropertyName(changedProperty.getName());
                statement.append(column == null ? changedProperty.getName() : column.getColumnName());
                                
                Object value;
                Setting setting = summary.getOldValue(changedObject, changedProperty);
                // Setting is null if this is a relationship change
                if (setting == null) {
                    value = changedObject.get(changedProperty);
                } else {
                    value = setting.getValue();
                }
               
                if (value == null) {                  
                    statement.append(" is null");                   
                } else {
                    ParameterImpl param = createCollisionParameter(tableWrapper, changedProperty, idx++);
                    statement.append(" = ?");
                    param.setValue(value);
                    parameters.add(param);
                }
               
              
            }
          
        } else {
            statement.append(" and ");
            statement.append(tableWrapper.getCollisionColumn().getColumnName());
            statement.append(" = ?");
            parameters.add(createParameter(tableWrapper,
                    type.getProperty(tableWrapper.getCollisionColumnPropertyName()), idx++));                      
        }                 

        UpdateCommandImpl updateCommand = new OptimisticWriteCommandImpl(statement.toString());
       
        Iterator params = parameters.iterator();
View Full Code Here

        // to the list of propertyNames
        Config model = configWrapper.getConfig();
        if (model != null) {
            Iterator tablesFromModel = model.getTable().iterator();
            while (tablesFromModel.hasNext()) {
                TableWrapper t = new TableWrapper((Table) tablesFromModel.next());
                if (tableToPropertyMap.get(t.getTypeName()) == null) {
                    tableToPropertyMap.put(t.getTypeName(), Collections.EMPTY_LIST);
                }
            }
        }

    }
View Full Code Here

        super();
    }

    public InsertCommandImpl getInsertCommand(MappingWrapper config, DataObject changedObject, Table t) {
        List parameters = new ArrayList();
        TableWrapper table = new TableWrapper(t);
        StringBuffer statement = new StringBuffer("insert into ");
        //JIRA-952
        if(config.getConfig().isDatabaseSchemaNameSupported()){
          statement.append(t.getSchemaName()+"."+t.getTableName());
        }
        else{
        statement.append(t.getTableName());
        }
        HashSet changedProperties = getAttributeProperties(changedObject, config, table);
        Iterator i;
        if ( changedProperties.isEmpty() ) {
            i = changedObject.getType().getProperties().iterator();
        } else {
            i = changedProperties.iterator();
        }

        List attributes = new ArrayList();
        List generatedKeys = new ArrayList();
        while (i.hasNext()) {
            Property attr = (Property) i.next();
            if ( attr.getType().isDataType()) {
                if (table.isGeneratedColumnProperty(attr.getName())) {
                 generatedKeys.add(attr.getName());
             } else {
                 attributes.add(attr.getName());
                    parameters.add(changedObject.getType().getProperty(attr.getName()));
             }
            }
        }

        statement.append("(");
        Iterator attrs = attributes.iterator();
        while (attrs.hasNext()) {
            String name = (String) attrs.next();
            statement.append("");
            Column c = config.getColumnByPropertyName(t, name);
            statement.append(c == null ? name : c.getColumnName());
            if (attrs.hasNext()) {
                statement.append(", ");
            } else {
                statement.append(")");
            }
        }

        statement.append(" values (");
        for (int idx = 1; idx <= attributes.size(); idx++) {
            statement.append('?');
            if (idx < attributes.size()) {
                statement.append(", ");
            } else {
                statement.append(")");
            }
        }

        InsertCommandImpl cmd = new InsertCommandImpl(statement.toString(),
                (String[]) generatedKeys.toArray(new String[0]));
        Iterator params = parameters.iterator();
        for (int idx = 1; params.hasNext(); idx++) {
            Property property = (Property) params.next();
            ParameterImpl p = new ParameterImpl();
            p.setName(property.getName());
            p.setType(property.getType());
            p.setConverter(getConverter(table.getConverter(property.getName())));
            p.setIndex(idx);
            cmd.addParameter(p);

        }
        if (this.logger.isDebugEnabled()) {
View Full Code Here

TOP

Related Classes of org.apache.tuscany.das.rdb.config.wrapper.TableWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.