Search in sources :

Example 16 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class AbstractQueryInterceptor method instantiateEntity.

/**
 * Instantiate the given entity for the given parameter values.
 *
 * @param rootEntity      The entity
 * @param parameterValues The parameter values
 * @return The entity
 * @throws IllegalArgumentException if the entity cannot be instantiated due to an illegal argument
 */
@NonNull
protected Object instantiateEntity(@NonNull Class<?> rootEntity, @NonNull Map<String, Object> parameterValues) {
    PersistentEntity entity = operations.getEntity(rootEntity);
    BeanIntrospection<?> introspection = BeanIntrospection.getIntrospection(rootEntity);
    Argument<?>[] constructorArguments = introspection.getConstructorArguments();
    Object instance;
    if (ArrayUtils.isNotEmpty(constructorArguments)) {
        Object[] arguments = new Object[constructorArguments.length];
        for (int i = 0; i < constructorArguments.length; i++) {
            Argument<?> argument = constructorArguments[i];
            String argumentName = argument.getName();
            Object v = parameterValues.get(argumentName);
            AnnotationMetadata argMetadata = argument.getAnnotationMetadata();
            if (v == null && !PersistentProperty.isNullableMetadata(argMetadata)) {
                PersistentProperty prop = entity.getPropertyByName(argumentName);
                if (prop == null || prop.isRequired()) {
                    throw new IllegalArgumentException("Argument [" + argumentName + "] cannot be null");
                }
            }
            arguments[i] = v;
        }
        instance = introspection.instantiate(arguments);
    } else {
        instance = introspection.instantiate();
    }
    BeanWrapper<Object> wrapper = BeanWrapper.getWrapper(instance);
    Collection<? extends PersistentProperty> persistentProperties = entity.getPersistentProperties();
    for (PersistentProperty prop : persistentProperties) {
        if (!prop.isReadOnly() && !prop.isGenerated()) {
            String propName = prop.getName();
            if (parameterValues.containsKey(propName)) {
                Object v = parameterValues.get(propName);
                if (v == null && !prop.isOptional()) {
                    throw new IllegalArgumentException("Argument [" + propName + "] cannot be null");
                }
                wrapper.setProperty(propName, v);
            } else if (prop.isRequired()) {
                final Optional<Object> p = wrapper.getProperty(propName, Object.class);
                if (!p.isPresent()) {
                    throw new IllegalArgumentException("Argument [" + propName + "] cannot be null");
                }
            }
        }
    }
    return instance;
}
Also used : Argument(io.micronaut.core.type.Argument) Optional(java.util.Optional) PersistentEntity(io.micronaut.data.model.PersistentEntity) PersistentProperty(io.micronaut.data.model.PersistentProperty) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull)

Example 17 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SchemaGenerator method createSchema.

/**
 * Initialize the schema for the configuration.
 * @param beanLocator The bean locator
 */
@PostConstruct
public void createSchema(BeanLocator beanLocator) {
    RuntimeEntityRegistry runtimeEntityRegistry = beanLocator.getBean(RuntimeEntityRegistry.class);
    for (DataJdbcConfiguration configuration : configurations) {
        Dialect dialect = configuration.getDialect();
        SchemaGenerate schemaGenerate = configuration.getSchemaGenerate();
        if (schemaGenerate != null && schemaGenerate != SchemaGenerate.NONE) {
            String name = configuration.getName();
            List<String> packages = configuration.getPackages();
            Collection<BeanIntrospection<Object>> introspections;
            if (CollectionUtils.isNotEmpty(packages)) {
                introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class, packages.toArray(new String[0]));
            } else {
                introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class);
            }
            PersistentEntity[] entities = introspections.stream().filter(i -> !i.getBeanType().getName().contains("$")).filter(i -> !java.lang.reflect.Modifier.isAbstract(i.getBeanType().getModifiers())).map(beanIntrospection -> runtimeEntityRegistry.getEntity(beanIntrospection.getBeanType())).toArray(PersistentEntity[]::new);
            if (ArrayUtils.isNotEmpty(entities)) {
                DataSource dataSource = DelegatingDataSource.unwrapDataSource(beanLocator.getBean(DataSource.class, Qualifiers.byName(name)));
                try {
                    try (Connection connection = dataSource.getConnection()) {
                        SqlQueryBuilder builder = new SqlQueryBuilder(dialect);
                        if (dialect.allowBatch() && configuration.isBatchGenerate()) {
                            switch(schemaGenerate) {
                                case CREATE_DROP:
                                    try {
                                        String sql = builder.buildBatchDropTableStatement(entities);
                                        if (DataSettings.QUERY_LOG.isDebugEnabled()) {
                                            DataSettings.QUERY_LOG.debug("Dropping Tables: \n{}", sql);
                                        }
                                        try (PreparedStatement ps = connection.prepareStatement(sql)) {
                                            ps.executeUpdate();
                                        }
                                    } catch (SQLException e) {
                                        if (DataSettings.QUERY_LOG.isTraceEnabled()) {
                                            DataSettings.QUERY_LOG.trace("Drop Unsuccessful: " + e.getMessage());
                                        }
                                    }
                                case CREATE:
                                    String sql = builder.buildBatchCreateTableStatement(entities);
                                    if (DataSettings.QUERY_LOG.isDebugEnabled()) {
                                        DataSettings.QUERY_LOG.debug("Creating Tables: \n{}", sql);
                                    }
                                    try (PreparedStatement ps = connection.prepareStatement(sql)) {
                                        ps.executeUpdate();
                                    }
                                    break;
                                default:
                            }
                        } else {
                            switch(schemaGenerate) {
                                case CREATE_DROP:
                                    for (PersistentEntity entity : entities) {
                                        try {
                                            String[] statements = builder.buildDropTableStatements(entity);
                                            for (String sql : statements) {
                                                if (DataSettings.QUERY_LOG.isDebugEnabled()) {
                                                    DataSettings.QUERY_LOG.debug("Dropping Table: \n{}", sql);
                                                }
                                                try (PreparedStatement ps = connection.prepareStatement(sql)) {
                                                    ps.executeUpdate();
                                                }
                                            }
                                        } catch (SQLException e) {
                                            if (DataSettings.QUERY_LOG.isTraceEnabled()) {
                                                DataSettings.QUERY_LOG.trace("Drop Unsuccessful: " + e.getMessage());
                                            }
                                        }
                                    }
                                case CREATE:
                                    for (PersistentEntity entity : entities) {
                                        String[] sql = builder.buildCreateTableStatements(entity);
                                        for (String stmt : sql) {
                                            if (DataSettings.QUERY_LOG.isDebugEnabled()) {
                                                DataSettings.QUERY_LOG.debug("Executing CREATE statement: \n{}", stmt);
                                            }
                                            try {
                                                try (PreparedStatement ps = connection.prepareStatement(stmt)) {
                                                    ps.executeUpdate();
                                                }
                                            } catch (SQLException e) {
                                                if (DataSettings.QUERY_LOG.isWarnEnabled()) {
                                                    DataSettings.QUERY_LOG.warn("CREATE Statement Unsuccessful: " + e.getMessage());
                                                }
                                            }
                                        }
                                    }
                                    break;
                                default:
                            }
                        }
                    } catch (SQLException e) {
                        throw new DataAccessException("Unable to create database schema: " + e.getMessage(), e);
                    }
                } catch (NoSuchBeanException e) {
                    throw new ConfigurationException("No DataSource configured for setting [" + DataJdbcConfiguration.PREFIX + name + "]. Ensure the DataSource is configured correctly and try again.", e);
                }
            }
        }
    }
}
Also used : Connection(java.sql.Connection) ArrayUtils(io.micronaut.core.util.ArrayUtils) MappedEntity(io.micronaut.data.annotation.MappedEntity) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) Internal(io.micronaut.core.annotation.Internal) DelegatingDataSource(io.micronaut.transaction.jdbc.DelegatingDataSource) SQLException(java.sql.SQLException) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) DataSource(javax.sql.DataSource) DataAccessException(io.micronaut.data.exceptions.DataAccessException) BeanLocator(io.micronaut.context.BeanLocator) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Context(io.micronaut.context.annotation.Context) Collection(java.util.Collection) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) PreparedStatement(java.sql.PreparedStatement) SchemaGenerate(io.micronaut.data.runtime.config.SchemaGenerate) List(java.util.List) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) PersistentEntity(io.micronaut.data.model.PersistentEntity) CollectionUtils(io.micronaut.core.util.CollectionUtils) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) PostConstruct(javax.annotation.PostConstruct) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) DataSettings(io.micronaut.data.runtime.config.DataSettings) Collections(java.util.Collections) SQLException(java.sql.SQLException) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) MappedEntity(io.micronaut.data.annotation.MappedEntity) PersistentEntity(io.micronaut.data.model.PersistentEntity) Connection(java.sql.Connection) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) PreparedStatement(java.sql.PreparedStatement) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) DelegatingDataSource(io.micronaut.transaction.jdbc.DelegatingDataSource) DataSource(javax.sql.DataSource) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Dialect(io.micronaut.data.model.query.builder.sql.Dialect) SchemaGenerate(io.micronaut.data.runtime.config.SchemaGenerate) DataAccessException(io.micronaut.data.exceptions.DataAccessException) PostConstruct(javax.annotation.PostConstruct)

Example 18 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class MongoQueryBuilder method traversePersistentProperties.

private void traversePersistentProperties(List<Association> associations, PersistentProperty property, BiConsumer<List<Association>, PersistentProperty> consumerProperty) {
    if (property instanceof Embedded) {
        Embedded embedded = (Embedded) property;
        PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
        Collection<? extends PersistentProperty> embeddedProperties = embeddedEntity.getPersistentProperties();
        List<Association> newAssociations = new ArrayList<>(associations);
        newAssociations.add((Association) property);
        for (PersistentProperty embeddedProperty : embeddedProperties) {
            traversePersistentProperties(newAssociations, embeddedProperty, consumerProperty);
        }
    } else if (property instanceof Association) {
        Association association = (Association) property;
        if (association.isForeignKey()) {
            return;
        }
        List<Association> newAssociations = new ArrayList<>(associations);
        newAssociations.add((Association) property);
        PersistentEntity associatedEntity = association.getAssociatedEntity();
        PersistentProperty assocIdentity = associatedEntity.getIdentity();
        if (assocIdentity == null) {
            throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
        }
        if (assocIdentity instanceof Association) {
            traversePersistentProperties(newAssociations, assocIdentity, consumerProperty);
        } else {
            consumerProperty.accept(newAssociations, assocIdentity);
        }
    } else {
        consumerProperty.accept(associations, property);
    }
}
Also used : Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Embedded(io.micronaut.data.model.Embedded) PersistentProperty(io.micronaut.data.model.PersistentProperty)

Example 19 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method buildJoinTableInsert.

/**
 * Builds a join table insert statement for a given entity and association.
 *
 * @param entity      The entity
 * @param association The association
 * @return The join table insert statement
 */
@NonNull
public String buildJoinTableInsert(@NonNull PersistentEntity entity, @NonNull Association association) {
    if (!isForeignKeyWithJoinTable(association)) {
        throw new IllegalArgumentException("Join table inserts can only be built for foreign key associations that are mapped with a join table.");
    } else {
        Optional<Association> inverseSide = association.getInverseSide().map(Function.identity());
        Association owningAssociation = inverseSide.orElse(association);
        AnnotationMetadata annotationMetadata = owningAssociation.getAnnotationMetadata();
        NamingStrategy namingStrategy = entity.getNamingStrategy();
        String joinTableName = annotationMetadata.stringValue(ANN_JOIN_TABLE, "name").orElseGet(() -> namingStrategy.mappedName(association));
        List<String> leftJoinColumns = resolveJoinTableJoinColumns(annotationMetadata, true, entity, namingStrategy);
        List<String> rightJoinColumns = resolveJoinTableJoinColumns(annotationMetadata, false, association.getAssociatedEntity(), namingStrategy);
        boolean escape = shouldEscape(entity);
        String columns = Stream.concat(leftJoinColumns.stream(), rightJoinColumns.stream()).map(columnName -> escape ? quote(columnName) : columnName).collect(Collectors.joining(","));
        String placeholders = IntStream.range(0, leftJoinColumns.size() + rightJoinColumns.size()).mapToObj(i -> "?").collect(Collectors.joining(","));
        return INSERT_INTO + quote(joinTableName) + " (" + columns + ") VALUES (" + placeholders + ")";
    }
}
Also used : DataType(io.micronaut.data.model.DataType) SqlMembers(io.micronaut.data.annotation.sql.SqlMembers) Arrays(java.util.Arrays) IDENTITY(io.micronaut.data.annotation.GeneratedValue.Type.IDENTITY) ListIterator(java.util.ListIterator) ArrayUtils(io.micronaut.core.util.ArrayUtils) SEQUENCE(io.micronaut.data.annotation.GeneratedValue.Type.SEQUENCE) MappedProperty(io.micronaut.data.annotation.MappedProperty) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) Locale(java.util.Locale) Map(java.util.Map) QueryResult(io.micronaut.data.model.query.builder.QueryResult) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) PersistentProperty(io.micronaut.data.model.PersistentProperty) Index(io.micronaut.data.annotation.Index) MappingException(io.micronaut.data.exceptions.MappingException) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AUTO(io.micronaut.data.annotation.GeneratedValue.Type.AUTO) StringUtils(io.micronaut.core.util.StringUtils) AbstractSqlLikeQueryBuilder(io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder) List(java.util.List) Stream(java.util.stream.Stream) UUID(io.micronaut.data.annotation.GeneratedValue.Type.UUID) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Experimental(io.micronaut.core.annotation.Experimental) Pattern(java.util.regex.Pattern) Relation(io.micronaut.data.annotation.Relation) IntStream(java.util.stream.IntStream) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) Join(io.micronaut.data.annotation.Join) Creator(io.micronaut.core.annotation.Creator) QueryModel(io.micronaut.data.model.query.QueryModel) MappedEntity(io.micronaut.data.annotation.MappedEntity) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Embedded(io.micronaut.data.model.Embedded) Pageable(io.micronaut.data.model.Pageable) BiConsumer(java.util.function.BiConsumer) Clob(java.sql.Clob) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) Indexes(io.micronaut.data.annotation.Indexes) NonNull(io.micronaut.core.annotation.NonNull) JoinPath(io.micronaut.data.model.query.JoinPath) Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) CollectionUtils(io.micronaut.core.util.CollectionUtils) StringJoiner(java.util.StringJoiner) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) Repository(io.micronaut.data.annotation.Repository) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Blob(java.sql.Blob) Collections(java.util.Collections) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) Association(io.micronaut.data.model.Association) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull)

Example 20 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method buildJoin.

@Override
protected String[] buildJoin(final String alias, JoinPath joinPath, String joinType, StringBuilder target, Map<String, String> appliedJoinPaths, QueryState queryState) {
    Association[] associationPath = joinPath.getAssociationPath();
    String[] joinAliases;
    if (ArrayUtils.isEmpty(associationPath)) {
        throw new IllegalArgumentException("Invalid association path [" + joinPath.getPath() + "]");
    }
    List<Association> joinAssociationsPath = new ArrayList<>(associationPath.length);
    joinAliases = new String[associationPath.length];
    StringJoiner pathSoFar = new StringJoiner(".");
    String joinAlias = alias;
    for (int i = 0; i < associationPath.length; i++) {
        Association association = associationPath[i];
        pathSoFar.add(association.getName());
        if (association instanceof Embedded) {
            joinAssociationsPath.add(association);
            continue;
        }
        String currentPath = pathSoFar.toString();
        String existingAlias = appliedJoinPaths.get(currentPath);
        if (existingAlias != null) {
            joinAliases[i] = existingAlias;
            joinAlias = existingAlias;
        } else {
            PersistentEntity associatedEntity = association.getAssociatedEntity();
            int finalI = i;
            JoinPath joinPathToUse = queryState.getQueryModel().getJoinPath(currentPath).orElseGet(() -> new JoinPath(currentPath, Arrays.copyOfRange(associationPath, 0, finalI + 1), joinPath.getJoinType(), joinPath.getAlias().orElse(null)));
            joinAliases[i] = getAliasName(joinPathToUse);
            String currentJoinAlias = joinAliases[i];
            buildJoin(joinType, target, queryState, joinAssociationsPath, joinAlias, association, associatedEntity, findOwner(joinAssociationsPath, association).orElseGet(queryState::getEntity), currentJoinAlias);
            joinAlias = currentJoinAlias;
        }
        joinAssociationsPath.clear();
    }
    return joinAliases;
}
Also used : Association(io.micronaut.data.model.Association) JoinPath(io.micronaut.data.model.query.JoinPath) PersistentEntity(io.micronaut.data.model.PersistentEntity) ArrayList(java.util.ArrayList) Embedded(io.micronaut.data.model.Embedded) StringJoiner(java.util.StringJoiner)

Aggregations

PersistentEntity (io.micronaut.data.model.PersistentEntity)27 PersistentProperty (io.micronaut.data.model.PersistentProperty)15 Association (io.micronaut.data.model.Association)13 Embedded (io.micronaut.data.model.Embedded)11 List (java.util.List)11 ArrayList (java.util.ArrayList)10 Collection (java.util.Collection)9 CollectionUtils (io.micronaut.core.util.CollectionUtils)8 MappedEntity (io.micronaut.data.annotation.MappedEntity)8 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)7 NonNull (io.micronaut.core.annotation.NonNull)7 ArrayUtils (io.micronaut.core.util.ArrayUtils)7 Relation (io.micronaut.data.annotation.Relation)7 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)7 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)7 JoinPath (io.micronaut.data.model.query.JoinPath)7 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)6 StringUtils (io.micronaut.core.util.StringUtils)6 MappingException (io.micronaut.data.exceptions.MappingException)6 Pageable (io.micronaut.data.model.Pageable)6