Search in sources :

Example 6 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class OracleSchemaReader method readIndices.

@Override
public Map<String, List<IndexInfo>> readIndices(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<IndexInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT " + "i.table_name,i.index_name,ic.column_name,i.uniqueness " + "FROM user_indexes i " + "JOIN user_ind_columns ic ON i.index_name=ic.index_name " + "JOIN entities e ON (UPPER(e.name)=i.table_name) " + "ORDER BY i.table_name,i.index_name,ic.column_position");
    try {
        IndexInfo curIndex = null;
        String lastTable = null;
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            String indexName = rs.getString(2);
            if (!tableName.equals(lastTable) || curIndex == null || !curIndex.getName().equals(indexName)) {
                List<IndexInfo> list = result.get(tableName);
                if (list == null) {
                    list = new ArrayList<>();
                    result.put(tableName, list);
                }
                curIndex = new IndexInfo();
                lastTable = tableName;
                list.add(curIndex);
                curIndex.setName(indexName);
                curIndex.setUnique("UNIQUE".equals(rs.getString(4)));
            }
            String column = rs.getString(3);
            curIndex.addColumn(column);
        }
    } finally {
        connector.close(rs);
    }
    // Read functional indices separately
    // as this query contains streaming column which is read slowly
    rs = connector.executeQuery("SELECT " + "i.table_name,i.index_name,ic.column_position,c.data_default " + "FROM user_indexes i " + "JOIN user_ind_columns ic ON i.index_name=ic.index_name " + "JOIN entities e ON (UPPER(e.name)=i.table_name) " + "JOIN user_tab_cols c ON (c.column_name=ic.column_name AND c.table_name=ic.table_name) " + "WHERE c.virtual_column='YES' " + "ORDER BY i.table_name,i.index_name,ic.column_position");
    try {
        while (rs.next()) {
            // Read streaming column at first
            String defaultValue = rs.getString(4);
            String tableName = rs.getString(1).toLowerCase();
            String indexName = rs.getString(2);
            int pos = rs.getInt(3) - 1;
            List<IndexInfo> list = result.get(tableName);
            if (list == null)
                continue;
            for (IndexInfo indexInfo : list) {
                if (indexInfo.getName().equals(indexName)) {
                    defaultValue = GENERIC_REF_INDEX_PATTERN.matcher(defaultValue).replaceFirst("generic($2)");
                    defaultValue = UPPER_INDEX_PATTERN.matcher(defaultValue).replaceFirst("upper($1)");
                    defaultValue = LOWER_INDEX_PATTERN.matcher(defaultValue).replaceFirst("lower($1)");
                    indexInfo.getColumns().set(pos, defaultValue);
                }
            }
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)

Example 7 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class YamlDeserializer method readQuery.

public static Query readQuery(final LoadContext loadContext, final String queryName, final Map<String, Object> content, final Entity entity) {
    YamlDeserializer yamlDeserializer = new YamlDeserializer(loadContext);
    yamlDeserializer.setProject(entity.getProject());
    EntityDeserializer entityDeserializer = yamlDeserializer.new EntityDeserializer();
    try {
        return entityDeserializer.readQuery(queryName, content, entity);
    } catch (ReadException e) {
        Query query = new Query(queryName, entity);
        loadContext.addWarning(e.attachElement(query));
        return query;
    }
}
Also used : ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) Query(com.developmentontheedge.be5.metadata.model.Query)

Example 8 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class YamlDeserializer method readCustomizations.

/**
 * Used with customizations (module), entity, query, operation and static page.
 */
@SuppressWarnings("unchecked")
private void readCustomizations(final Map<String, Object> serialized, final BeVectorCollection<?> target, boolean replace) {
    if (project == null)
        throw new IllegalStateException();
    final Map<String, Object> serializedCustomizations = (Map<String, Object>) serialized.get("customizations");
    if (serializedCustomizations == null || serializedCustomizations.isEmpty())
        return;
    final BeVectorCollection<PageCustomization> customizations = replace ? new PageCustomizations(target) : target.getOrCreateCollection(PageCustomization.CUSTOMIZATIONS_COLLECTION, PageCustomization.class);
    try {
        for (final String name : serializedCustomizations.keySet()) {
            final Map<String, Object> content = (Map<String, Object>) serializedCustomizations.get(name);
            final List<String> splitted = StreamEx.split(name, "\\.").toList();
            final String type;
            final String domain;
            if (splitted.size() == 1) {
                type = "";
                domain = splitted.get(0);
            } else {
                type = splitted.get(splitted.size() - 1);
                splitted.remove(splitted.size() - 1);
                domain = String.join(".", splitted);
            }
            final PageCustomization customization = new PageCustomization(type, domain, customizations);
            customization.setCode((String) content.get(TAG_CODE));
            customization.setOriginModuleName(project.getProjectOrigin());
            DataElementUtils.saveQuiet(customization);
        }
    } catch (Exception e) {
        loadContext.addWarning(new ReadException(e, target, project.getLocation()));
    }
    if (replace)
        DataElementUtils.save(customizations);
}
Also used : ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) PageCustomizations(com.developmentontheedge.be5.metadata.model.PageCustomizations) PageCustomization(com.developmentontheedge.be5.metadata.model.PageCustomization) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException)

Example 9 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class MassChangeTest method testMassChange.

@Test
public void testMassChange() {
    Project project = new Project("test");
    Entity entity = new Entity("entity", project.getApplication(), EntityType.TABLE);
    DataElementUtils.save(entity);
    Query queryToChange = new Query("queryToChange", entity);
    DataElementUtils.save(queryToChange);
    Query query = new Query("query", entity);
    DataElementUtils.save(query);
    MassChange mc = new MassChange("Query[name*=\"Change\"]", project.getApplication().getMassChangeCollection(), Collections.singletonMap("type", QueryType.D2));
    assertEquals("type", mc.getPropertiesString());
    assertEquals("Query[name*=Change]", mc.getSelectorString());
    DataElementUtils.save(mc);
    assertEquals(QueryType.D1, queryToChange.getType());
    assertEquals(QueryType.D1, query.getType());
    LoadContext context = new LoadContext();
    project.applyMassChanges(context);
    context.check();
    assertEquals(QueryType.D2, entity.getQueries().get("queryToChange").getType());
    assertEquals(QueryType.D1, entity.getQueries().get("query").getType());
}
Also used : LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Test(org.junit.Test)

Example 10 with Query

use of com.developmentontheedge.be5.metadata.model.Query in project be5 by DevelopmentOnTheEdge.

the class ReadModelFromXmlTest method testWriteReadQueryOperation.

@Test
public void testWriteReadQueryOperation() throws Exception {
    final Project project = new Project("TestProject");
    final Module module = project.getApplication();
    DataElementUtils.saveQuiet(module);
    final Entity table = new Entity("table", module, EntityType.TABLE);
    DataElementUtils.saveQuiet(table);
    final Operation op = Operation.createOperation("op", Operation.OPERATION_TYPE_JAVA, table);
    DataElementUtils.saveQuiet(op);
    final Query query = new Query("q", table);
    DataElementUtils.saveQuiet(query);
    query.getOperationNames().add("op");
    final Path tempFolder = Files.createTempDirectory("be4-temp");
    Serialization.save(project, tempFolder);
    final Project readProject = Serialization.load(tempFolder);
    final Entity readEntity = readProject.getApplication().getEntity("table");
    assertEquals("op", readEntity.getQueries().get("q").getOperationNames().getValuesArray()[0]);
    FileUtils.deleteRecursively(tempFolder);
}
Also used : Path(java.nio.file.Path) Project(com.developmentontheedge.be5.metadata.model.Project) Entity(com.developmentontheedge.be5.metadata.model.Entity) Query(com.developmentontheedge.be5.metadata.model.Query) Operation(com.developmentontheedge.be5.metadata.model.Operation) Module(com.developmentontheedge.be5.metadata.model.Module) Test(org.junit.Test)

Aggregations

Query (com.developmentontheedge.be5.metadata.model.Query)29 Entity (com.developmentontheedge.be5.metadata.model.Entity)16 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)15 Project (com.developmentontheedge.be5.metadata.model.Project)12 Module (com.developmentontheedge.be5.metadata.model.Module)8 Path (java.nio.file.Path)8 Operation (com.developmentontheedge.be5.metadata.model.Operation)6 HashMap (java.util.HashMap)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)4 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)4 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)4 Map (java.util.Map)4 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)3 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)3 LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)3 LinkedHashMap (java.util.LinkedHashMap)3 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)2 Action (com.developmentontheedge.be5.model.Action)2