Search in sources :

Example 11 with ParsedNodeException

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

the class CustomChangeWrapper method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    setClassLoader(resourceAccessor.toClassLoader());
    try {
        setClass(parsedNode.getChildValue(null, "class", String.class));
    } catch (CustomChangeException e) {
        throw new ParsedNodeException(e);
    }
    super.load(parsedNode, resourceAccessor);
}
Also used : ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 12 with ParsedNodeException

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

the class ColumnConfig method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    for (ParsedNode child : parsedNode.getChildren()) {
        if (!ObjectUtil.hasProperty(this, child.getName())) {
            throw new ParsedNodeException("Unexpected node: " + child.getName());
        }
    }
    name = parsedNode.getChildValue(null, "name", String.class);
    computed = parsedNode.getChildValue(null, "computed", Boolean.class);
    type = parsedNode.getChildValue(null, "type", String.class);
    encoding = parsedNode.getChildValue(null, "encoding", String.class);
    autoIncrement = parsedNode.getChildValue(null, "autoIncrement", Boolean.class);
    startWith = parsedNode.getChildValue(null, "startWith", BigInteger.class);
    incrementBy = parsedNode.getChildValue(null, "incrementBy", BigInteger.class);
    remarks = parsedNode.getChildValue(null, "remarks", String.class);
    descending = parsedNode.getChildValue(null, "descending", Boolean.class);
    value = parsedNode.getChildValue(null, "value", String.class);
    if (value == null) {
        value = StringUtils.trimToNull((String) parsedNode.getValue());
    }
    setValueNumeric(parsedNode.getChildValue(null, "valueNumeric", String.class));
    try {
        valueDate = parsedNode.getChildValue(null, "valueDate", Date.class);
    } catch (ParsedNodeException e) {
        valueComputed = new DatabaseFunction(parsedNode.getChildValue(null, "valueDate", String.class));
    }
    valueBoolean = parsedNode.getChildValue(null, "valueBoolean", Boolean.class);
    valueBlobFile = parsedNode.getChildValue(null, "valueBlobFile", String.class);
    valueClobFile = parsedNode.getChildValue(null, "valueClobFile", String.class);
    String valueComputedString = parsedNode.getChildValue(null, "valueComputed", String.class);
    if (valueComputedString != null) {
        valueComputed = new DatabaseFunction(valueComputedString);
    }
    String valueSequenceNextString = parsedNode.getChildValue(null, "valueSequenceNext", String.class);
    if (valueSequenceNextString != null) {
        valueSequenceNext = new SequenceNextValueFunction(valueSequenceNextString);
    }
    String valueSequenceCurrentString = parsedNode.getChildValue(null, "valueSequenceCurrent", String.class);
    if (valueSequenceCurrentString != null) {
        valueSequenceCurrent = new SequenceCurrentValueFunction(valueSequenceCurrentString);
    }
    defaultValueConstraintName = parsedNode.getChildValue(null, "defaultValueConstraintName", String.class);
    defaultValue = parsedNode.getChildValue(null, "defaultValue", String.class);
    setDefaultValueNumeric(parsedNode.getChildValue(null, "defaultValueNumeric", String.class));
    try {
        defaultValueDate = parsedNode.getChildValue(null, "defaultValueDate", Date.class);
    } catch (ParsedNodeException e) {
        defaultValueComputed = new DatabaseFunction(parsedNode.getChildValue(null, "defaultValueDate", String.class));
    }
    defaultValueBoolean = parsedNode.getChildValue(null, "defaultValueBoolean", Boolean.class);
    String defaultValueComputedString = parsedNode.getChildValue(null, "defaultValueComputed", String.class);
    if (defaultValueComputedString != null) {
        defaultValueComputed = new DatabaseFunction(defaultValueComputedString);
    }
    String defaultValueSequenceNextString = parsedNode.getChildValue(null, "defaultValueSequenceNext", String.class);
    if (defaultValueSequenceNextString != null) {
        defaultValueSequenceNext = new SequenceNextValueFunction(defaultValueSequenceNextString);
    }
    loadConstraints(parsedNode.getChild(null, "constraints"));
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) SequenceCurrentValueFunction(liquibase.statement.SequenceCurrentValueFunction) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseFunction(liquibase.statement.DatabaseFunction) BigInteger(java.math.BigInteger) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) Date(java.util.Date)

Example 13 with ParsedNodeException

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

the class DatabaseSnapshot method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    try {
        Map<String, DatabaseObject> referencedObjects = new HashMap<String, DatabaseObject>();
        Map<String, DatabaseObject> objects = new HashMap<String, DatabaseObject>();
        Map<String, DatabaseObject> allObjects = new HashMap<String, DatabaseObject>();
        ParsedNode databaseNode = parsedNode.getChild(null, "database");
        DatabaseConnection connection = getDatabase().getConnection();
        if (databaseNode != null && connection instanceof OfflineConnection) {
            ((OfflineConnection) connection).setDatabaseMajorVersion(databaseNode.getChildValue(null, "majorVersion", Integer.class));
            ((OfflineConnection) connection).setDatabaseMinorVersion(databaseNode.getChildValue(null, "minorVersion", Integer.class));
            ((OfflineConnection) connection).setProductVersion(databaseNode.getChildValue(null, "productVersion", String.class));
            ((OfflineConnection) connection).setConnectionUserName(databaseNode.getChildValue(null, "user", String.class));
        }
        loadObjects(referencedObjects, allObjects, parsedNode.getChild(null, "referencedObjects"), resourceAccessor);
        loadObjects(objects, allObjects, parsedNode.getChild(null, "objects"), resourceAccessor);
        for (DatabaseObject object : allObjects.values()) {
            for (String attr : new ArrayList<String>(object.getAttributes())) {
                Object value = object.getAttribute(attr, Object.class);
                if (value instanceof String && allObjects.containsKey(value)) {
                    if (ObjectUtil.hasProperty(object, attr)) {
                        ObjectUtil.setProperty(object, attr, allObjects.get(value));
                    } else {
                        object.setAttribute(attr, allObjects.get(value));
                    }
                } else if (value instanceof Collection && ((Collection) value).size() > 0 && allObjects.containsKey(((Collection) value).iterator().next())) {
                    List newList = new ArrayList();
                    for (String element : (Collection<String>) value) {
                        newList.add(allObjects.get(element));
                    }
                    if (ObjectUtil.hasProperty(object, attr)) {
                        ObjectUtil.setProperty(object, attr, newList);
                    } else {
                        object.setAttribute(attr, newList);
                    }
                } else {
                    if (value != null && ObjectUtil.hasProperty(object, attr)) {
                        if (value instanceof byte[] && ObjectUtil.getPropertyType(object, attr).equals(String.class)) {
                            value = new String((byte[]) value, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
                        }
                        object.setAttribute(attr, null);
                        ObjectUtil.setProperty(object, attr, value);
                    }
                }
            }
        }
        for (DatabaseObject object : objects.values()) {
            this.allFound.add(object);
        }
        for (DatabaseObject object : referencedObjects.values()) {
            this.referencedObjects.add(object);
        }
    } catch (Exception e) {
        throw new ParsedNodeException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OfflineConnection(liquibase.database.OfflineConnection) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseObject(liquibase.structure.DatabaseObject) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 14 with ParsedNodeException

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

the class AbstractLiquibaseSerializable method load.

public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    for (ParsedNode childNode : parsedNode.getChildren()) {
        if (!shouldAutoLoad(childNode)) {
            continue;
        }
        try {
            if (this.getSerializableFields().contains(childNode.getName())) {
                Class dataTypeClass = this.getSerializableFieldDataTypeClass(childNode.getName());
                if (Collection.class.isAssignableFrom(dataTypeClass)) {
                    Type[] dataTypeClassParameters = getSerializableFieldDataTypeClassParameters(childNode.getName());
                    if (dataTypeClassParameters.length == 1) {
                        Class collectionType = null;
                        if (dataTypeClassParameters[0] instanceof Class) {
                            collectionType = (Class) dataTypeClassParameters[0];
                        } else if (dataTypeClassParameters[0] instanceof ParameterizedType) {
                            collectionType = (Class) ((ParameterizedType) dataTypeClassParameters[0]).getRawType();
                        }
                        if (collectionType != null && LiquibaseSerializable.class.isAssignableFrom(collectionType) && !collectionType.isInterface() && !Modifier.isAbstract(collectionType.getModifiers())) {
                            String elementName = ((LiquibaseSerializable) collectionType.newInstance()).getSerializedObjectName();
                            List<ParsedNode> elementNodes = Collections.emptyList();
                            if (childNode.getName().equals(elementName)) {
                                elementNodes = Collections.singletonList(childNode);
                            } else {
                                elementNodes = childNode.getChildren(null, elementName);
                            }
                            if (!elementNodes.isEmpty()) {
                                Collection collection = ((Collection) getSerializableFieldValue(childNode.getName()));
                                for (ParsedNode node : elementNodes) {
                                    LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                    childObject.load(node, resourceAccessor);
                                    collection.add(childObject);
                                }
                            }
                        }
                    }
                }
                if (LiquibaseSerializable.class.isAssignableFrom(dataTypeClass)) {
                    if (!dataTypeClass.isInterface() && !Modifier.isAbstract(dataTypeClass.getModifiers())) {
                        LiquibaseSerializable childObject = (LiquibaseSerializable) dataTypeClass.newInstance();
                        childObject.load(childNode, resourceAccessor);
                        setSerializableFieldValue(childNode.getName(), childObject);
                    }
                } else if (childNode.getValue() != null) {
                    ObjectUtil.setProperty(this, childNode.getName(), convertEscaped(childNode.getValue().toString()));
                }
            } else {
                for (String field : this.getSerializableFields()) {
                    Class dataTypeClass = this.getSerializableFieldDataTypeClass(field);
                    if (Collection.class.isAssignableFrom(dataTypeClass)) {
                        Type[] dataTypeClassParameters = getSerializableFieldDataTypeClassParameters(field);
                        if (dataTypeClassParameters.length == 1) {
                            Class collectionType = null;
                            if (dataTypeClassParameters[0] instanceof Class) {
                                collectionType = (Class) dataTypeClassParameters[0];
                            } else if (dataTypeClassParameters[0] instanceof ParameterizedType) {
                                collectionType = (Class) ((ParameterizedType) dataTypeClassParameters[0]).getRawType();
                            }
                            if (collectionType != null && LiquibaseSerializable.class.isAssignableFrom(collectionType) && !collectionType.isInterface() && !Modifier.isAbstract(collectionType.getModifiers())) {
                                String elementName = ((LiquibaseSerializable) collectionType.newInstance()).getSerializedObjectName();
                                List<ParsedNode> elementNodes = Collections.emptyList();
                                if (childNode.getName().equals(elementName)) {
                                    elementNodes = Collections.singletonList(childNode);
                                } else if (childNode.getName().equals(field)) {
                                    elementNodes = childNode.getChildren(null, elementName);
                                }
                                if (!elementNodes.isEmpty()) {
                                    Collection collection = ((Collection) getSerializableFieldValue(field));
                                    for (ParsedNode node : elementNodes) {
                                        LiquibaseSerializable childObject = (LiquibaseSerializable) collectionType.newInstance();
                                        childObject.load(node, resourceAccessor);
                                        collection.add(childObject);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new ParsedNodeException("Error setting property", e);
        }
    }
    if (parsedNode.getValue() != null) {
        for (String field : this.getSerializableFields()) {
            SerializationType type = this.getSerializableFieldType(field);
            if (type == SerializationType.DIRECT_VALUE) {
                Object value = parsedNode.getValue(String.class);
                value = convertEscaped(value);
                ObjectUtil.setProperty(this, field, value);
            } else if (type == SerializationType.NAMED_FIELD) {
                Object value = parsedNode.getChildValue(null, field, Object.class);
                value = convertEscaped(value);
                ObjectUtil.setProperty(this, field, value);
            }
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ParsedNodeException(liquibase.parser.core.ParsedNodeException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) 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