Ejemplo n.º 1
0
  @Override
  public DataNode createDataNode(DataNodeDescriptor nodeDescriptor) throws Exception {

    DataNode dataNode = new DataNode(nodeDescriptor.getName());

    dataNode.setJdbcEventLogger(jdbcEventLogger);
    dataNode.setRowReaderFactory(rowReaderFactory);
    dataNode.setBatchTranslatorFactory(batchTranslatorFactory);

    dataNode.setDataSourceLocation(nodeDescriptor.getParameters());

    DataSource dataSource = dataSourceFactory.getDataSource(nodeDescriptor);

    dataNode.setDataSourceFactory(nodeDescriptor.getDataSourceFactoryType());
    dataNode.setDataSource(dataSource);

    // schema update strategy
    String schemaUpdateStrategyType = nodeDescriptor.getSchemaUpdateStrategyType();

    if (schemaUpdateStrategyType == null) {
      dataNode.setSchemaUpdateStrategy(defaultSchemaUpdateStrategy);
      dataNode.setSchemaUpdateStrategyName(defaultSchemaUpdateStrategy.getClass().getName());
    } else {
      SchemaUpdateStrategy strategy =
          objectFactory.newInstance(SchemaUpdateStrategy.class, schemaUpdateStrategyType);
      dataNode.setSchemaUpdateStrategyName(schemaUpdateStrategyType);
      dataNode.setSchemaUpdateStrategy(strategy);
    }

    // DbAdapter
    dataNode.setAdapter(adapterFactory.createAdapter(nodeDescriptor, dataSource));

    return dataNode;
  }
  public void testDBGeneratorStrategy() throws Exception {

    String template = "SELECT #result('id' 'int') FROM SUS1";
    SQLTemplate query = new SQLTemplate(Object.class, template);

    DataMap map = node.getEntityResolver().getDataMap("sus-map");
    DataNode dataNode = createDataNode(map);
    int sizeDB = getNameTablesInDB(dataNode).size();
    MockOperationObserver observer = new MockOperationObserver();
    try {

      generateDBWithDBGeneratorStrategy(dataNode, query, observer);
      int sizeDB2 = getNameTablesInDB(dataNode).size();
      assertEquals(2, sizeDB2 - sizeDB);
      dataNode.performQueries(Collections.singletonList((Query) query), observer);
      int sizeDB3 = getNameTablesInDB(dataNode).size();
      assertEquals(sizeDB2, sizeDB3);
    } finally {
      DataNode dataNode2 = createDataNode(map);
      dataNode2.setSchemaUpdateStrategy(
          (SchemaUpdateStrategy)
              Class.forName(dataNode2.getSchemaUpdateStrategyName()).newInstance());
      dropTables(map, dataNode2, observer);
    }
    assertEquals(getNameTablesInDB(dataNode).size(), sizeDB);
  }
  public void testMixedStrategyTableNoExist() throws Exception {

    String template = "SELECT #result('id' 'int') FROM SUS1";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    DataMap map = node.getEntityResolver().getDataMap("sus-map");
    DataNode dataNode = createDataNode(map);
    int sizeDB = getNameTablesInDB(dataNode).size();
    MockOperationObserver observer = new MockOperationObserver();

    setStrategy(ThrowOnPartialOrCreateSchemaStrategy.class.getName(), dataNode);

    try {
      dataNode.performQueries(Collections.singletonList((Query) query), observer);
      Map<String, Boolean> nameTables = getNameTablesInDB(dataNode);
      assertTrue(nameTables.get("sus1") != null || nameTables.get("SUS1") != null);
      int sizeDB2 = getNameTablesInDB(dataNode).size();
      assertEquals(2, sizeDB2 - sizeDB);
      dataNode.performQueries(Collections.singletonList((Query) query), observer);
      int sizeDB3 = getNameTablesInDB(dataNode).size();
      assertEquals(sizeDB2, sizeDB3);
    } finally {
      DataNode dataNode2 = createDataNode(map);
      dataNode2.setSchemaUpdateStrategy(
          (SchemaUpdateStrategy)
              Class.forName(dataNode2.getSchemaUpdateStrategyName()).newInstance());
      dropTables(map, dataNode2, observer);
    }
    assertEquals(getNameTablesInDB(dataNode).size(), sizeDB);
  }
 private void setStrategy(String name, DataNode dataNode) {
   dataNode.setSchemaUpdateStrategyName(name);
   try {
     dataNode.setSchemaUpdateStrategy(
         (SchemaUpdateStrategy)
             Class.forName(dataNode.getSchemaUpdateStrategyName()).newInstance());
   } catch (Exception e) {
     throw new CayenneRuntimeException(e);
   }
 }