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));
}
}
}
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());
}
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;
}
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"));
}
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);
}
Aggregations