Search in sources :

Example 1 with SQLTemplateDescriptor

use of org.apache.cayenne.map.SQLTemplateDescriptor in project cayenne by apache.

the class SQLTemplateScriptsTab method setSQL.

void setSQL(DocumentEvent e, String key) {
    if (key == null) {
        return;
    }
    SQLTemplateDescriptor query = getQuery();
    if (query == null) {
        return;
    }
    Document doc = e.getDocument();
    String text = null;
    try {
        text = doc.getText(0, doc.getLength());
    } catch (BadLocationException ex) {
        logger.warn("Error reading document", ex);
    }
    if (text != null) {
        text = text.trim();
        if (text.length() == 0) {
            text = null;
        }
    }
    // will call "verify" even if no changes have occured....
    if (key.equals(DEFAULT_LABEL)) {
        if (!Util.nullSafeEquals(text, query.getSql())) {
            query.setSql(text);
            mediator.fireQueryEvent(new QueryEvent(this, query));
        }
    } else {
        if (!Util.nullSafeEquals(text, query.getAdapterSql().get(key))) {
            query.getAdapterSql().put(key, text);
            mediator.fireQueryEvent(new QueryEvent(this, query));
        }
    }
}
Also used : SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) Document(javax.swing.text.Document) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with SQLTemplateDescriptor

use of org.apache.cayenne.map.SQLTemplateDescriptor in project cayenne by apache.

the class XMLDataMapLoaderTest method loadFullDataMap.

@Test
public void loadFullDataMap() {
    URL url = getClass().getResource("testConfigMap4.map.xml");
    DataMap map = loader.load(new URLResource(url));
    assertNotNull(map);
    assertEquals("testConfigMap4", map.getName());
    // check general state
    assertEquals(12, map.getDbEntities().size());
    assertEquals(17, map.getObjEntities().size());
    assertEquals(4, map.getProcedures().size());
    assertEquals(14, map.getQueryDescriptors().size());
    assertEquals(1, map.getEmbeddables().size());
    assertEquals("TEST_CATALOG", map.getDefaultCatalog());
    assertNull(map.getDefaultSchema());
    assertEquals("org.apache.cayenne.testdo.testmap", map.getDefaultPackage());
    assertTrue(map.isClientSupported());
    // check some loaded content
    assertEquals("org.apache.cayenne.testdo.testmap.Artist", map.getObjEntity("Artist").getClassName());
    assertEquals(5, map.getObjEntity("CompoundPainting").getAttributes().size());
    assertEquals(3, map.getObjEntity("Artist").getRelationships().size());
    assertEquals(7, map.getObjEntity("ArtistCallback").getCallbackMethods().size());
    assertEquals("name = \"test\"", map.getDbEntity("ARTGROUP").getQualifier().toString());
    assertEquals(4, map.getDbEntity("EXHIBIT").getAttributes().size());
    assertEquals(3, map.getDbEntity("PAINTING").getRelationships().size());
    assertEquals("gallery_seq", map.getDbEntity("GALLERY").getPrimaryKeyGenerator().getGeneratorName());
    DbAttribute pk1 = map.getDbEntity("EXHIBIT").getAttribute("EXHIBIT_ID");
    assertFalse(pk1.isGenerated());
    assertTrue(pk1.isPrimaryKey());
    DbAttribute pk2 = map.getDbEntity("GENERATED_COLUMN").getAttribute("GENERATED_COLUMN");
    assertTrue(pk2.isGenerated());
    assertTrue(pk2.isPrimaryKey());
    assertEquals(true, map.getProcedure("cayenne_tst_out_proc").isReturningValue());
    assertEquals(1, map.getProcedure("cayenne_tst_out_proc").getCallOutParameters().size());
    assertEquals(2, map.getProcedure("cayenne_tst_out_proc").getCallParameters().size());
    assertEquals("true", map.getQueryDescriptor("EjbqlQueryTest").getProperty("cayenne.GenericSelectQuery.fetchingDataRows"));
    SQLTemplateDescriptor descriptor = (SQLTemplateDescriptor) map.getQueryDescriptor("NonSelectingQuery");
    assertEquals("INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) " + "VALUES (512, 'No Painting Like This', 12.5)", descriptor.getAdapterSql().get("org.apache.cayenne.dba.db2.DB2Adapter"));
    assertEquals("TEST", map.getEmbeddable("org.apache.cayenne.testdo.Embeddable").getAttribute("test").getDbAttributeName());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 3 with SQLTemplateDescriptor

use of org.apache.cayenne.map.SQLTemplateDescriptor in project cayenne by apache.

the class EOModelProcessor method makeEOSQLQueryDescriptor.

protected QueryDescriptor makeEOSQLQueryDescriptor(ObjEntity root, Map plistMap) {
    SQLTemplateDescriptor descriptor = QueryDescriptor.sqlTemplateDescriptor();
    descriptor.setRoot(root);
    Object fetchLimit = plistMap.get("fetchLimit");
    if (fetchLimit != null) {
        try {
            if (fetchLimit instanceof Number) {
                descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, String.valueOf(((Number) fetchLimit).intValue()));
            } else if (isNumeric(fetchLimit.toString())) {
                descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit.toString());
            }
        } catch (NumberFormatException nfex) {
        // ignoring...
        }
    }
    // query
    // TODO: doesn't work with Stored Procedures.
    Map hints = (Map) plistMap.get("hints");
    if (hints != null && !hints.isEmpty()) {
        String sqlExpression = (String) hints.get("EOCustomQueryExpressionHintKey");
        if (sqlExpression != null) {
            descriptor.setSql(sqlExpression);
        }
    }
    return descriptor;
}
Also used : SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with SQLTemplateDescriptor

use of org.apache.cayenne.map.SQLTemplateDescriptor in project cayenne by apache.

the class PrefetchTypeForSqlTemplateHandlerTest method testLoad.

@Test
public void testLoad() throws Exception {
    final DataMap map = new DataMap();
    parse("query", new HandlerFactory() {

        @Override
        public NamespaceAwareNestedTagHandler createHandler(NamespaceAwareNestedTagHandler parent) {
            return new QueryDescriptorHandler(parent, map);
        }
    });
    SQLTemplateDescriptor sqlTemplateDescriptor = (SQLTemplateDescriptor) map.getQueryDescriptor("query");
    assertEquals(3, sqlTemplateDescriptor.getPrefetchesMap().size());
    assertEquals(1, (int) sqlTemplateDescriptor.getPrefetchesMap().get("paintings"));
    assertEquals(2, (int) sqlTemplateDescriptor.getPrefetchesMap().get("paintings.artist"));
    assertEquals(3, (int) sqlTemplateDescriptor.getPrefetchesMap().get("paintings.gallery"));
}
Also used : SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 5 with SQLTemplateDescriptor

use of org.apache.cayenne.map.SQLTemplateDescriptor in project cayenne by apache.

the class BaseTemplatesGenerationTest method testSQLTemplate.

@Test
public void testSQLTemplate() throws Exception {
    dataMap.setName("SQLTemplate");
    DbEntity dbEntity = new DbEntity();
    objEntity.setDbEntity(dbEntity);
    objEntity.setClassName("Test");
    SQLTemplateDescriptor sqlTemplateDescriptor = new SQLTemplateDescriptor();
    sqlTemplateDescriptor.setSql("SELECT * FROM table");
    sqlTemplateDescriptor.setRoot(objEntity);
    sqlTemplateDescriptor.setName("select");
    sqlTemplateDescriptor.setRoot(objEntity);
    Collection<QueryDescriptor> descriptors = new ArrayList<>();
    descriptors.add(sqlTemplateDescriptor);
    DataMapArtifact dataMapArtifact = new DataMapArtifact(dataMap, descriptors);
    execute(dataMapArtifact);
}
Also used : SelectQueryDescriptor(org.apache.cayenne.map.SelectQueryDescriptor) QueryDescriptor(org.apache.cayenne.map.QueryDescriptor) SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SQLTemplateDescriptor (org.apache.cayenne.map.SQLTemplateDescriptor)6 DataMap (org.apache.cayenne.map.DataMap)3 Test (org.junit.Test)3 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BadLocationException (javax.swing.text.BadLocationException)1 Document (javax.swing.text.Document)1 QueryEvent (org.apache.cayenne.configuration.event.QueryEvent)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 QueryDescriptor (org.apache.cayenne.map.QueryDescriptor)1 SelectQueryDescriptor (org.apache.cayenne.map.SelectQueryDescriptor)1 URLResource (org.apache.cayenne.resource.URLResource)1 JCayenneTextPane (org.apache.cayenne.swing.components.textpane.JCayenneTextPane)1