Search in sources :

Example 16 with ParsedNode

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

the class ExecuteShellCommandChange method customLoadLogic.

@Override
protected void customLoadLogic(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    ParsedNode argsNode = parsedNode.getChild(null, "args");
    if (argsNode == null) {
        argsNode = parsedNode;
    }
    for (ParsedNode arg : argsNode.getChildren(null, "arg")) {
        addArg(arg.getChildValue(null, "value", String.class));
    }
    String passedValue = StringUtils.trimToNull(parsedNode.getChildValue(null, "os", String.class));
    if (passedValue == null) {
        this.os = new ArrayList<String>();
    } else {
        List<String> os = StringUtils.splitAndTrim(StringUtils.trimToEmpty(parsedNode.getChildValue(null, "os", String.class)), ",");
        if (os.size() == 1 && os.get(0).equals("")) {
            this.os = null;
        } else if (os.size() > 0) {
            this.os = os;
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode)

Example 17 with ParsedNode

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

the class Column method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    super.load(parsedNode, resourceAccessor);
    ParsedNode typeNode = parsedNode.getChild(null, "type");
    if (typeNode != null) {
        DataType type = new DataType();
        type.load(typeNode, resourceAccessor);
        setType(type);
    }
    ParsedNode autoIncrementInformation = parsedNode.getChild(null, "autoIncrementInformation");
    if (autoIncrementInformation != null) {
        AutoIncrementInformation info = new AutoIncrementInformation();
        info.load(autoIncrementInformation, resourceAccessor);
        setAutoIncrementInformation(info);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode)

Example 18 with ParsedNode

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

the class DatabaseSnapshot method loadObjects.

protected void loadObjects(Map<String, DatabaseObject> objectMap, Map<String, DatabaseObject> allObjects, ParsedNode node, ResourceAccessor resourceAccessor) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParsedNodeException {
    if (node == null) {
        return;
    }
    for (ParsedNode typeNode : node.getChildren()) {
        Class<? extends DatabaseObject> objectType = (Class<? extends DatabaseObject>) Class.forName(typeNode.getName());
        for (ParsedNode objectNode : typeNode.getChildren()) {
            DatabaseObject databaseObject = objectType.newInstance();
            databaseObject.load(objectNode, resourceAccessor);
            String key = objectType.getName() + "#" + databaseObject.getSnapshotId();
            objectMap.put(key, databaseObject);
            allObjects.put(key, databaseObject);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) DatabaseObject(liquibase.structure.DatabaseObject)

Example 19 with ParsedNode

use of liquibase.parser.core.ParsedNode 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 20 with ParsedNode

use of liquibase.parser.core.ParsedNode 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

ParsedNode (liquibase.parser.core.ParsedNode)23 ParsedNodeException (liquibase.parser.core.ParsedNodeException)12 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 ContextExpression (liquibase.ContextExpression)4 DatabaseObject (liquibase.structure.DatabaseObject)4 Labels (liquibase.Labels)3 InputStream (java.io.InputStream)2 ParseException (java.text.ParseException)2 Change (liquibase.change.Change)2 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 ChangeLogParseException (liquibase.exception.ChangeLogParseException)2 Yaml (org.yaml.snakeyaml.Yaml)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1