<commit_before>package io.quarkus.hibernate.orm.runtime.devconsole;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

import org.hibernate.LockOptions;
import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.NamedQueryDefinition;
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
import org.hibernate.tool.schema.spi.TargetDescriptor;

public class HibernateOrmDevConsoleInfoSupplier implements Supplier<HibernateOrmDevConsoleInfoSupplier.PersistenceUnitsInfo> {

    private static final String DEFAULT = "<default>";

    private static final PersistenceUnitsInfo INSTANCE = new PersistenceUnitsInfo();

    public static void pushPersistenceUnit(String persistenceUnitName,
            Metadata metadata, ServiceRegistry serviceRegistry, String importFile) {
        List<EntityInfo> managedEntities = new ArrayList<>();
        for (PersistentClass entityBinding : metadata.getEntityBindings()) {
            managedEntities.add(new EntityInfo(entityBinding.getClassName(), entityBinding.getTable().getName()));
        }

        List<QueryInfo> namedQueries = new ArrayList<>();
        for (NamedQueryDefinition queryDefinition : metadata.getNamedQueryDefinitions()) {
            namedQueries.add(new QueryInfo(queryDefinition));
        }

        List<QueryInfo> namedNativeQueries = new ArrayList<>();
        for (NamedSQLQueryDefinition staticQueryDefinition : metadata.getNamedNativeQueryDefinitions()) {
            namedNativeQueries.add(new QueryInfo(staticQueryDefinition));
        }

        String createDDL = generateDDL(SchemaExport.Action.CREATE, metadata, serviceRegistry, importFile);
        String dropDDL = generateDDL(SchemaExport.Action.DROP, metadata, serviceRegistry, importFile);

        INSTANCE.persistenceUnits.put(persistenceUnitName, new PersistenceUnitInfo(persistenceUnitName, managedEntities,
                namedQueries, namedNativeQueries, createDDL, dropDDL));
    }

    public static void clearData() {
        INSTANCE.persistenceUnits.clear();
    }

    private static String generateDDL(SchemaExport.Action action, Metadata metadata, ServiceRegistry serviceRegistry,
            String importFiles) {
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.setFormat(true);
        schemaExport.setDelimiter(";");
        schemaExport.setImportFiles(importFiles);
        StringWriter writer = new StringWriter();
        schemaExport.doExecution(action, false, metadata, serviceRegistry,
                new TargetDescriptor() {
                    @Override
                    public EnumSet<TargetType> getTargetTypes() {
                        return EnumSet.of(TargetType.SCRIPT);
                    }

                    @Override
                    public ScriptTargetOutput getScriptTargetOutput() {
                        return new ScriptTargetOutputToWriter(writer) {
                            @Override
                            public void accept(String command) {
                                super.accept(command);
                            }
                        };
                    }
                });
        return writer.toString();
    }

    @Override
    public PersistenceUnitsInfo get() {
        return INSTANCE;
    }

    public static class PersistenceUnitsInfo {

        private final Map<String, PersistenceUnitInfo> persistenceUnits = Collections
                .synchronizedMap(new TreeMap<>(new PersistenceUnitNameComparator()));

        public Collection<PersistenceUnitInfo> getPersistenceUnits() {
            return persistenceUnits.values();
        }

        public int getNumberOfNamedQueries() {
            return persistenceUnits.values().stream().map(PersistenceUnitInfo::getAllNamedQueries)
                    .mapToInt(List::size).reduce(Integer::sum).orElse(0);
        }

        public int getNumberOfEntities() {
            return persistenceUnits.values().stream().map(PersistenceUnitInfo::getManagedEntities)
                    .mapToInt(List::size).reduce(Integer::sum).orElse(0);
        }

    }
<commit_msg>Allow the application to start in dev mode even if we can't generate DDL scripts for the Dev UI<commit_after>package io.quarkus.hibernate.orm.runtime.devconsole;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;

import org.hibernate.LockOptions;
import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.NamedQueryDefinition;
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
import org.hibernate.tool.schema.spi.TargetDescriptor;

public class HibernateOrmDevConsoleInfoSupplier implements Supplier<HibernateOrmDevConsoleInfoSupplier.PersistenceUnitsInfo> {

    private static final String DEFAULT = "<default>";

    private static final PersistenceUnitsInfo INSTANCE = new PersistenceUnitsInfo();

    public static void pushPersistenceUnit(String persistenceUnitName,
            Metadata metadata, ServiceRegistry serviceRegistry, String importFile) {
        List<EntityInfo> managedEntities = new ArrayList<>();
        for (PersistentClass entityBinding : metadata.getEntityBindings()) {
            managedEntities.add(new EntityInfo(entityBinding.getClassName(), entityBinding.getTable().getName()));
        }

        List<QueryInfo> namedQueries = new ArrayList<>();
        for (NamedQueryDefinition queryDefinition : metadata.getNamedQueryDefinitions()) {
            namedQueries.add(new QueryInfo(queryDefinition));
        }

        List<QueryInfo> namedNativeQueries = new ArrayList<>();
        for (NamedSQLQueryDefinition staticQueryDefinition : metadata.getNamedNativeQueryDefinitions()) {
            namedNativeQueries.add(new QueryInfo(staticQueryDefinition));
        }

        String createDDL = generateDDL(SchemaExport.Action.CREATE, metadata, serviceRegistry, importFile);
        String dropDDL = generateDDL(SchemaExport.Action.DROP, metadata, serviceRegistry, importFile);

        INSTANCE.persistenceUnits.put(persistenceUnitName, new PersistenceUnitInfo(persistenceUnitName, managedEntities,
                namedQueries, namedNativeQueries, createDDL, dropDDL));
    }

    public static void clearData() {
        INSTANCE.persistenceUnits.clear();
    }

    private static String generateDDL(SchemaExport.Action action, Metadata metadata, ServiceRegistry serviceRegistry,
            String importFiles) {
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.setFormat(true);
        schemaExport.setDelimiter(";");
        schemaExport.setImportFiles(importFiles);
        StringWriter writer = new StringWriter();
        try {
            schemaExport.doExecution(action, false, metadata, serviceRegistry,
                    new TargetDescriptor() {
                        @Override
                        public EnumSet<TargetType> getTargetTypes() {
                            return EnumSet.of(TargetType.SCRIPT);
                        }

                        @Override
                        public ScriptTargetOutput getScriptTargetOutput() {
                            return new ScriptTargetOutputToWriter(writer) {
                                @Override
                                public void accept(String command) {
                                    super.accept(command);
                                }
                            };
                        }
                    });
        } catch (RuntimeException e) {
            StringWriter stackTraceWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(stackTraceWriter));
            return "Could not generate DDL: \n" + stackTraceWriter.toString();
        }
        return writer.toString();
    }

    @Override
    public PersistenceUnitsInfo get() {
        return INSTANCE;
    }

    public static class PersistenceUnitsInfo {

        private final Map<String, PersistenceUnitInfo> persistenceUnits = Collections
                .synchronizedMap(new TreeMap<>(new PersistenceUnitNameComparator()));

        public Collection<PersistenceUnitInfo> getPersistenceUnits() {
            return persistenceUnits.values();
        }

        public int getNumberOfNamedQueries() {
            return persistenceUnits.values().stream().map(PersistenceUnitInfo::getAllNamedQueries)
                    .mapToInt(List::size).reduce(Integer::sum).orElse(0);
        }

        public int getNumberOfEntities() {
            return persistenceUnits.values().stream().map(PersistenceUnitInfo::getManagedEntities)
                    .mapToInt(List::size).reduce(Integer::sum).orElse(0);
        }

    }

{'fraction_non_alphanumeric': 0.08513485690755611, 'fraction_numerical': 0.0006176652254478073, 'mean_word_length': 2.8536295120983737, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1470803', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2311, 'n_words': 465}