Search in sources :

Example 6 with ParsedNodeException

use of liquibase.parser.core.ParsedNodeException in project liquibase by liquibase.

the class DeleteDataChange method customLoadLogic.

@Override
protected void customLoadLogic(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    ParsedNode whereParams = parsedNode.getChild(null, "whereParams");
    if (whereParams != null) {
        for (ParsedNode param : whereParams.getChildren(null, "param")) {
            ColumnConfig columnConfig = new ColumnConfig();
            try {
                columnConfig.load(param, resourceAccessor);
            } catch (ParsedNodeException e) {
                e.printStackTrace();
            }
            addWhereParam(columnConfig);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 7 with ParsedNodeException

use of liquibase.parser.core.ParsedNodeException in project liquibase by liquibase.

the class AbstractDatabaseObject method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    for (ParsedNode child : parsedNode.getChildren()) {
        String name = child.getName();
        if (name.equals("snapshotId")) {
            this.snapshotId = child.getValue(String.class);
            continue;
        }
        Class propertyType = ObjectUtil.getPropertyType(this, name);
        if (propertyType != null && Collection.class.isAssignableFrom(propertyType) && !(child.getValue() instanceof Collection)) {
            if (this.attributes.get(name) == null) {
                this.setAttribute(name, new ArrayList<Column>());
            }
            this.getAttribute(name, List.class).add(child.getValue());
        } else {
            Object childValue = child.getValue();
            if (childValue != null && childValue instanceof String) {
                Matcher matcher = Pattern.compile("(.*)!\\{(.*)\\}").matcher((String) childValue);
                if (matcher.matches()) {
                    String stringValue = matcher.group(1);
                    try {
                        Class<?> aClass = Class.forName(matcher.group(2));
                        if (Date.class.isAssignableFrom(aClass)) {
                            Date date = new ISODateFormat().parse(stringValue);
                            childValue = aClass.getConstructor(long.class).newInstance(date.getTime());
                        } else if (Enum.class.isAssignableFrom(aClass)) {
                            childValue = Enum.valueOf((Class<? extends Enum>) aClass, stringValue);
                        } else {
                            childValue = aClass.getConstructor(String.class).newInstance(stringValue);
                        }
                    } catch (Exception e) {
                        throw new UnexpectedLiquibaseException(e);
                    }
                }
            }
            this.attributes.put(name, childValue);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) Matcher(java.util.regex.Matcher) ParsedNodeException(liquibase.parser.core.ParsedNodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParseException(java.text.ParseException) ISODateFormat(liquibase.util.ISODateFormat) Column(liquibase.structure.core.Column) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 8 with ParsedNodeException

use of liquibase.parser.core.ParsedNodeException in project liquibase by liquibase.

the class ChangeSet method handleChildNode.

protected void handleChildNode(ParsedNode child, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    if (child.getName().equals("rollback")) {
        handleRollbackNode(child, resourceAccessor);
    } else if (child.getName().equals("validCheckSum") || child.getName().equals("validCheckSums")) {
        if (child.getValue() == null) {
            return;
        }
        if (child.getValue() instanceof Collection) {
            for (Object checksum : (Collection) child.getValue()) {
                addValidCheckSum((String) checksum);
            }
        } else {
            addValidCheckSum(child.getValue(String.class));
        }
    } else if (child.getName().equals("modifySql")) {
        String dbmsString = StringUtils.trimToNull(child.getChildValue(null, "dbms", String.class));
        String contextString = StringUtils.trimToNull(child.getChildValue(null, "context", String.class));
        String labelsString = StringUtils.trimToNull(child.getChildValue(null, "labels", String.class));
        boolean applyToRollback = child.getChildValue(null, "applyToRollback", false);
        Set<String> dbms = new HashSet<String>();
        if (dbmsString != null) {
            dbms.addAll(StringUtils.splitAndTrim(dbmsString, ","));
        }
        ContextExpression context = null;
        if (contextString != null) {
            context = new ContextExpression(contextString);
        }
        Labels labels = null;
        if (labelsString != null) {
            labels = new Labels(labelsString);
        }
        List<ParsedNode> potentialVisitors = child.getChildren();
        for (ParsedNode node : potentialVisitors) {
            SqlVisitor sqlVisitor = SqlVisitorFactory.getInstance().create(node.getName());
            if (sqlVisitor != null) {
                sqlVisitor.setApplyToRollback(applyToRollback);
                if (dbms.size() > 0) {
                    sqlVisitor.setApplicableDbms(dbms);
                }
                sqlVisitor.setContexts(context);
                sqlVisitor.setLabels(labels);
                sqlVisitor.load(node, resourceAccessor);
                addSqlVisitor(sqlVisitor);
            }
        }
    } else if (child.getName().equals("preConditions")) {
        this.preconditions = new PreconditionContainer();
        try {
            this.preconditions.load(child, resourceAccessor);
        } catch (ParsedNodeException e) {
            e.printStackTrace();
        }
    } else if (child.getName().equals("changes")) {
        for (ParsedNode changeNode : child.getChildren()) {
            handleChildNode(changeNode, resourceAccessor);
        }
    } else {
        Change change = toChange(child, resourceAccessor);
        if (change == null && child.getValue() instanceof String) {
            this.setAttribute(child.getName(), child.getValue());
        } else {
            addChange(change);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) PreconditionContainer(liquibase.precondition.core.PreconditionContainer) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 9 with ParsedNodeException

use of liquibase.parser.core.ParsedNodeException in project liquibase by liquibase.

the class AbstractChange method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    ChangeMetaData metaData = ChangeFactory.getInstance().getChangeMetaData(this);
    this.setResourceAccessor(resourceAccessor);
    try {
        Collection<ChangeParameterMetaData> changeParameters = metaData.getParameters().values();
        for (ChangeParameterMetaData param : changeParameters) {
            if (Collection.class.isAssignableFrom(param.getDataTypeClass())) {
                if (param.getDataTypeClassParameters().length == 1) {
                    Class collectionType = (Class) param.getDataTypeClassParameters()[0];
                    if (ColumnConfig.class.isAssignableFrom(collectionType)) {
                        List<ParsedNode> columnNodes = new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
                        columnNodes.addAll(parsedNode.getChildren(null, "column"));
                        Object nodeValue = parsedNode.getValue();
                        if (nodeValue instanceof ParsedNode) {
                            columnNodes.add((ParsedNode) nodeValue);
                        } else if (nodeValue instanceof Collection) {
                            for (Object nodeValueChild : ((Collection) nodeValue)) {
                                if (nodeValueChild instanceof ParsedNode) {
                                    columnNodes.add((ParsedNode) nodeValueChild);
                                }
                            }
                        }
                        for (ParsedNode child : columnNodes) {
                            if (child.getName().equals("column") || child.getName().equals("columns")) {
                                List<ParsedNode> columnChildren = child.getChildren(null, "column");
                                if (columnChildren != null && columnChildren.size() > 0) {
                                    for (ParsedNode columnChild : columnChildren) {
                                        ColumnConfig columnConfig = createEmptyColumnConfig(collectionType);
                                        columnConfig.load(columnChild, resourceAccessor);
                                        ((ChangeWithColumns) this).addColumn(columnConfig);
                                    }
                                } else {
                                    ColumnConfig columnConfig = createEmptyColumnConfig(collectionType);
                                    columnConfig.load(child, resourceAccessor);
                                    ((ChangeWithColumns) this).addColumn(columnConfig);
                                }
                            }
                        }
                    } else if (LiquibaseSerializable.class.isAssignableFrom(collectionType)) {
                        if (!collectionType.isInterface() && !Modifier.isAbstract(collectionType.getModifiers())) {
                            String elementName = ((LiquibaseSerializable) collectionType.newInstance()).getSerializedObjectName();
                            List<ParsedNode> nodes = new ArrayList<ParsedNode>(parsedNode.getChildren(null, param.getParameterName()));
                            if (!elementName.equals(param.getParameterName())) {
                                nodes.addAll(parsedNode.getChildren(null, elementName));
                            }
                            Object nodeValue = parsedNode.getValue();
                            if (nodeValue instanceof ParsedNode) {
                                nodes.add((ParsedNode) nodeValue);
                            } else if (nodeValue instanceof Collection) {
                                for (Object nodeValueChild : ((Collection) nodeValue)) {
                                    if (nodeValueChild instanceof ParsedNode) {
                                        nodes.add((ParsedNode) nodeValueChild);
                                    }
                                }
                            }
                            for (ParsedNode node : nodes) {
                                if (node.getName().equals(elementName) || node.getName().equals(param.getParameterName())) {
                                    List<ParsedNode> childNodes = node.getChildren(null, elementName);
                                    if (childNodes != null && childNodes.size() > 0) {
                                        for (ParsedNode childNode : childNodes) {
                                            LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                            childObject.load(childNode, resourceAccessor);
                                            ((Collection) param.getCurrentValue(this)).add(childObject);
                                        }
                                    } else {
                                        LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                        childObject.load(node, resourceAccessor);
                                        ((Collection) param.getCurrentValue(this)).add(childObject);
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (LiquibaseSerializable.class.isAssignableFrom(param.getDataTypeClass())) {
                if (!param.getDataTypeClass().isInterface() && !Modifier.isAbstract(param.getDataTypeClass().getModifiers())) {
                    try {
                        ParsedNode child = parsedNode.getChild(null, param.getParameterName());
                        if (child != null) {
                            LiquibaseSerializable serializableChild = (LiquibaseSerializable) param.getDataTypeClass().newInstance();
                            serializableChild.load(child, resourceAccessor);
                            param.setValue(this, serializableChild);
                        }
                    } catch (InstantiationException e) {
                        throw new UnexpectedLiquibaseException(e);
                    } catch (IllegalAccessException e) {
                        throw new UnexpectedLiquibaseException(e);
                    }
                }
            } else {
                Object childValue = parsedNode.getChildValue(null, param.getParameterName(), param.getDataTypeClass());
                if (childValue == null && param.getSerializationType() == SerializationType.DIRECT_VALUE) {
                    childValue = parsedNode.getValue();
                }
                param.setValue(this, childValue);
            }
        }
    } catch (InstantiationException e) {
        throw new UnexpectedLiquibaseException(e);
    } catch (IllegalAccessException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    customLoadLogic(parsedNode, resourceAccessor);
    try {
        this.finishInitialization();
    } catch (SetupException e) {
        throw new ParsedNodeException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseObject(liquibase.structure.DatabaseObject) LiquibaseSerializable(liquibase.serializer.LiquibaseSerializable)

Example 10 with ParsedNodeException

use of liquibase.parser.core.ParsedNodeException in project liquibase by liquibase.

the class UpdateDataChange method customLoadLogic.

@Override
protected void customLoadLogic(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    ParsedNode whereParams = parsedNode.getChild(null, "whereParams");
    if (whereParams != null) {
        for (ParsedNode param : whereParams.getChildren(null, "param")) {
            ColumnConfig columnConfig = new ColumnConfig();
            try {
                columnConfig.load(param, resourceAccessor);
            } catch (ParsedNodeException e) {
                e.printStackTrace();
            }
            addWhereParam(columnConfig);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Aggregations

ParsedNodeException (liquibase.parser.core.ParsedNodeException)14 ParsedNode (liquibase.parser.core.ParsedNode)12 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 ContextExpression (liquibase.ContextExpression)2 Change (liquibase.change.Change)2 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)2 DatabaseObject (liquibase.structure.DatabaseObject)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BigInteger (java.math.BigInteger)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Matcher (java.util.regex.Matcher)1 Labels (liquibase.Labels)1