Java Code Examples for org.apache.commons.beanutils.DynaBean#set()

The following examples show how to use org.apache.commons.beanutils.DynaBean#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestPlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test the toColumnValues method.
 */
public void testToColumnValues()
{
    final String schema =
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='id' autoIncrement='true' type='INTEGER' primaryKey='true'/>\n"+
        "    <column name='name' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>";

    Database         database = parseDatabaseFromString(schema);
    PlatformImplBase platform = new TestPlatform();
    Table            table    = database.getTable(0);
    SqlDynaClass     clz      = SqlDynaClass.newInstance(table);
    DynaBean         db       = new SqlDynaBean(SqlDynaClass.newInstance(table));

    db.set("name", "name");

    Map map = platform.toColumnValues(clz.getSqlDynaProperties(), db);

    assertEquals("name",
                 map.get("name"));
    assertTrue(map.containsKey("id"));
}
 
Example 2
Source File: TestPlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Test the toColumnValues method.
 */
public void testToColumnValues()
{
    final String schema =
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='id' autoIncrement='true' type='INTEGER' primaryKey='true'/>\n"+
        "    <column name='name' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>";

    Database         database = parseDatabaseFromString(schema);
    PlatformImplBase platform = new TestPlatform();
    Table            table    = database.getTable(0);
    SqlDynaClass     clz      = SqlDynaClass.newInstance(table);
    DynaBean         db       = new SqlDynaBean(SqlDynaClass.newInstance(table));

    db.set("name", "name");

    Map map = platform.toColumnValues(clz.getSqlDynaProperties(), db);

    assertEquals("name",
                 map.get("name"));
    assertTrue(map.containsKey("id"));
}
 
Example 3
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the values of the columns constituting the indicated foreign key with the values
 * of the given identity.
 * 
 * @param bean     The bean whose columns shall be updated
 * @param fkName   The name of the foreign key
 * @param identity The target identity
 */
private void updateFKColumns(DynaBean bean, String fkName, Identity identity)
{
    Table      sourceTable = ((SqlDynaClass)bean.getDynaClass()).getTable();
    Table      targetTable = identity.getTable();
    ForeignKey fk          = null;

    for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++)
    {
        ForeignKey curFk = sourceTable.getForeignKey(idx);

        if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getQualifiedName()))
        {
            if (fkName.equals(getFKName(sourceTable, curFk)))
            {
                fk = curFk;
                break;
            }
        }
    }
    if (fk != null)
    {
        for (int idx = 0; idx < fk.getReferenceCount(); idx++)
        {
            Reference curRef       = fk.getReference(idx);
            Column    sourceColumn = curRef.getLocalColumn();
            Column    targetColumn = curRef.getForeignColumn();

            bean.set(sourceColumn.getName(), identity.getColumnValue(targetColumn.getName()));
        }
    }
}
 
Example 4
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified row from the table.
 * 
 * @param tableName      The name of the table (case insensitive)
 * @param pkColumnValues The values for the pk columns in order of definition
 */
protected void deleteRow(String tableName, Object[] pkColumnValues)
{
    Table    table     = getModel().findTable(tableName);
    DynaBean bean      = getModel().createDynaBeanFor(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; (idx < pkColumns.length) && (idx < pkColumnValues.length); idx++)
    {
        bean.set(pkColumns[idx].getName(), pkColumnValues[idx]);
    }
    getPlatform().delete(getModel(), bean);
}
 
Example 5
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the insert method.
 */
public void testInsertSingle() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1"); 

    getPlatform().insert(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 1",
                 getPropertyValue(bean, "TheText"));
}
 
Example 6
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the alteration of a pk column datatype change from integer to varchar.
 */
public void testPKColumnTypeIntegerToVarchar()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='VARCHAR' size='20' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean.get("pk") instanceof String)
    {
        bean.set("pk", ((String)bean.get("pk")).trim());
    }
    assertEquals((Object)"1", beans.get(0), "pk");
}
 
Example 7
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the alteration of a column datatype change from smallint to varchar.
 */
public void testColumnTypeSmallintToVarchar()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='SMALLINT'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue' type='VARCHAR' size='20'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1), new Short((short)2) });

	alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0); 

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean.get("avalue") instanceof String)
    {
        bean.set("avalue", ((String)bean.get("avalue")).trim());
    }
    assertEquals(new Integer(1), beans.get(0), "pk");
    assertEquals((Object)"2",    beans.get(0), "avalue");
}
 
Example 8
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specified row from the table.
 * 
 * @param tableName      The name of the table (case insensitive)
 * @param pkColumnValues The values for the pk columns in order of definition
 */
protected void deleteRow(String tableName, Object[] pkColumnValues)
{
    Table    table     = getModel().findTable(tableName);
    DynaBean bean      = getModel().createDynaBeanFor(table);
    Column[] pkColumns = table.getPrimaryKeyColumns();

    for (int idx = 0; (idx < pkColumns.length) && (idx < pkColumnValues.length); idx++)
    {
        bean.set(pkColumns[idx].getName(), pkColumnValues[idx]);
    }
    getPlatform().delete(getModel(), bean);
}
 
Example 9
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the store method.
 */
public void testStoreNew() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1"); 

    getPlatform().store(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 1",
                 getPropertyValue(bean, "TheText"));
}
 
Example 10
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the store method.
 */
public void testStoreExisting() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    insertData(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<data>\n"+
        "  <TestTable TheId='1' TheText='Text 1'/>\n"+
        "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 10"); 

    getPlatform().store(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 10",
                 getPropertyValue(bean, "TheText"));
}
 
Example 11
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the store method.
 */
public void testStoreNew() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1"); 

    getPlatform().store(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 1",
                 getPropertyValue(bean, "TheText"));
}
 
Example 12
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the update method.
 */
public void testUpdate() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    insertData(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<data>\n"+
        "  <TestTable TheId='1' TheText='Text 1'/>\n"+
        "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 10"); 

    getPlatform().update(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 10",
                 getPropertyValue(bean, "TheText"));
}
 
Example 13
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the update method.
 */
public void testUpdate() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    insertData(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<data>\n"+
        "  <TestTable TheId='1' TheText='Text 1'/>\n"+
        "</data>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 10"); 

    getPlatform().update(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 10",
                 getPropertyValue(bean, "TheText"));
}
 
Example 14
Source File: TestDynaSqlQueries.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the insert method.
 */
public void testInsertSingle() throws Exception
{
    createDatabase(
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='ddlutils'>\n"+
        "  <table name='TestTable'>\n"+
        "    <column name='TheId' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='TheText' type='VARCHAR' size='15'/>\n"+
        "  </table>\n"+
        "</database>");

    SqlDynaClass dynaClass = SqlDynaClass.newInstance(getModel().getTable(0));
    DynaBean     dynaBean  = new SqlDynaBean(dynaClass);

    dynaBean.set("TheId", new Integer(1));
    dynaBean.set("TheText", "Text 1"); 

    getPlatform().insert(getModel(), dynaBean);

    List beans = getPlatform().fetch(getModel(),
                                     "SELECT * FROM " + asIdentifier("TestTable"),
                                     new Table[] { getModel().getTable(0) });

    assertEquals(1,
                 beans.size());

    DynaBean bean = (DynaBean)beans.get(0);

    assertEquals(new Integer(1),
                 getPropertyValue(bean, "TheId"));
    assertEquals("Text 1",
                 getPropertyValue(bean, "TheText"));
}
 
Example 15
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the alteration of a pk column datatype change from integer to varchar.
 */
public void testPKColumnTypeIntegerToVarchar()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='VARCHAR' size='20' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0);

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean.get("pk") instanceof String)
    {
        bean.set("pk", ((String)bean.get("pk")).trim());
    }
    assertEquals((Object)"1", beans.get(0), "pk");
}
 
Example 16
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the change of the datatypes of PK and FK columns from integer to varchar.
 */
public void testPKAndFKColumnTypesIntegerToVarchar()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip1'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='INTEGER' required='false'/>\n"+
        "    <foreign-key foreignTable='roundtrip1'>\n"+
        "      <reference local='fk' foreign='pk'/>\n"+
        "    </foreign-key>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip1'>\n"+
        "    <column name='pk' type='VARCHAR' size='128' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='VARCHAR' size='128' required='false'/>\n"+
        "    <foreign-key foreignTable='roundtrip1'>\n"+
        "      <reference local='fk' foreign='pk'/>\n"+
        "    </foreign-key>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip1", new Object[] { new Integer(1) });
    insertRow("roundtrip2", new Object[] { new Integer(1), new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans1 = getRows("roundtrip1");
    List     beans2 = getRows("roundtrip2");
    DynaBean bean1  = (DynaBean)beans1.get(0);
    DynaBean bean2  = (DynaBean)beans2.get(0);

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean1.get("pk") instanceof String)
    {
        bean1.set("pk", ((String)bean1.get("pk")).trim());
    }
    if (bean2.get("fk") instanceof String)
    {
        bean2.set("fk", ((String)bean2.get("fk")).trim());
    }
    assertEquals((Object)"1",    bean1, "pk");
    assertEquals(new Integer(1), bean2, "pk");
    assertEquals((Object)"1",    bean2, "fk");
}
 
Example 17
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the change of the datatypes of PK and FK columns from integer to varchar.
 */
public void testPKAndFKColumnTypesIntegerToVarchar()
{
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip1'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='INTEGER' required='false'/>\n"+
        "    <foreign-key foreignTable='roundtrip1'>\n"+
        "      <reference local='fk' foreign='pk'/>\n"+
        "    </foreign-key>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip1'>\n"+
        "    <column name='pk' type='VARCHAR' size='128' primaryKey='true' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='VARCHAR' size='128' required='false'/>\n"+
        "    <foreign-key foreignTable='roundtrip1'>\n"+
        "      <reference local='fk' foreign='pk'/>\n"+
        "    </foreign-key>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip1", new Object[] { new Integer(1) });
    insertRow("roundtrip2", new Object[] { new Integer(1), new Integer(1) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans1 = getRows("roundtrip1");
    List     beans2 = getRows("roundtrip2");
    DynaBean bean1  = (DynaBean)beans1.get(0);
    DynaBean bean2  = (DynaBean)beans2.get(0);

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean1.get("pk") instanceof String)
    {
        bean1.set("pk", ((String)bean1.get("pk")).trim());
    }
    if (bean2.get("fk") instanceof String)
    {
        bean2.set("fk", ((String)bean2.get("fk")).trim());
    }
    assertEquals((Object)"1",    bean1, "pk");
    assertEquals(new Integer(1), bean2, "pk");
    assertEquals((Object)"1",    bean2, "fk");
}
 
Example 18
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the alteration of an indexed column datatype change from smallint to varchar.
 */
public void testUniqueIndexColumnTypeSmallintToVarchar()
{
    if (!getPlatformInfo().isIndicesSupported())
    {
        return;
    }

    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue1' type='SMALLINT'/>\n"+
        "    <column name='avalue2' type='DOUBLE'/>\n"+
        "    <unique name='testindex'>\n"+
        "      <unique-column name='avalue1'/>\n"+
        "      <unique-column name='avalue2'/>\n"+
        "    </unique>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='avalue1' type='VARCHAR' size='20'/>\n"+
        "    <column name='avalue2' type='DOUBLE'/>\n"+
        "    <unique name='testindex'>\n"+
        "      <unique-column name='avalue1'/>\n"+
        "      <unique-column name='avalue2'/>\n"+
        "    </unique>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    insertRow("roundtrip", new Object[] { new Integer(1), new Short((short)2), new Double(3.0) });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List     beans = getRows("roundtrip");
    DynaBean bean  = (DynaBean)beans.get(0); 

    // Some databases (e.g. DB2) pad the string for some reason, so we manually trim it
    if (bean.get("avalue1") instanceof String)
    {
        bean.set("avalue1", ((String)bean.get("avalue1")).trim());
    }
    assertEquals(new Integer(1),  beans.get(0), "pk");
    assertEquals((Object)"2",     beans.get(0), "avalue1");
    assertEquals(new Double(3.0), beans.get(0), "avalue2");
}
 
Example 19
Source File: DynaBeansExample.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/website/test.html")
public String testDynaBeanController(Model model) {

    //创建动态 bean
    DynaBean dynaBean = new LazyDynaBean();

    //字符串属性,通过属性名 name 调用
    dynaBean.set("name", "Peter Collinson"); //simple

    //数字属性 ,通过属性名 gender 调用
    dynaBean.set("gender", new Integer(1));  //simple

    //bean 属性,通过属性名 bean 调用
    UserBean beanExample = new UserBean();
    beanExample.setName("name string."); //simple
    dynaBean.set("bean", beanExample);

    //map 属性,通过 customer 和 title 调用
    dynaBean.set("customer", "title", "Mr");      // mapped
    dynaBean.set("customer", "surname", "Smith"); // mapped

    //可以指定属性顺序,通过 address 和 顺序号调用
    /**
     * 此方式无法通过 jstl 调用,不使用
     * */
    dynaBean.set("address", 0, "addressLine1");     // indexed
    dynaBean.set("address", 1, "addressLine2");     // indexed
    dynaBean.set("address", 2, "addressLine3");     // indexed

    //其他用法
    /**
     * 可以转换成  map , 用法同 LazyDynaBean
     */
    LazyDynaMap dynaMapBean = new LazyDynaMap();
    dynaMapBean.set("name", "Peter Collinson"); //simple;
    Map myMap = dynaMapBean.getMap();   // retrieve the Map


    //传递到前端
    model.addAttribute("test", dynaBean);
    return "2760/jsp/test";

    /**
     * 前端调用
     */
    //页面 jstl 读取,不需要,用引入任何类,仅引入 jstl 标签即可使用
    //注意,使用 map 这个关键字
    //http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BasicDynaBean.html
    //官方 doc 中,getMap() 方法解释处有说明

    /**
       调用代码 : test 为 bean 名称,如果是字符串 $ {map.name} 即可
     $ {test.map.name} <br />
     $ {test.map.gender} <br />
     $ {test.map.bean.name} <br />
     $ {test.map.customer.title} <br />
     $ {test.map.customer.surname} <br />

     */

    //http://www.programgo.com/article/85353617047/
}
 
Example 20
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Directly inserts the given bean into the database.
 * 
 * @param table The table of the bean
 * @param bean  The bean
 */
private void insertSingleBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException
{
    try
    {
        boolean    needTwoStepInsert = false;
        ForeignKey selfRefFk         = null;

        if (!_platform.isIdentityOverrideOn() &&
            _tablesWithSelfIdentityReference.contains(table))
        {
            selfRefFk = table.getSelfReferencingForeignKey();

            // in case of a self-reference (fk points to the very row that we're inserting)
            // and (at least) one of the pk columns is an identity column, we first need
            // to insert the row with the fk columns set to null
            Identity pkIdentity = buildIdentityFromPKs(table, bean);
            Identity fkIdentity = buildIdentityFromFK(table, selfRefFk, bean);

            if (pkIdentity.equals(fkIdentity))
            {
                if (_tablesWithRequiredSelfReference.contains(table))
                {
                    throw new DataSinkException("Can only insert rows with fk pointing to themselves when all fk columns can be NULL (row pk is " + pkIdentity + ")");
                }
                else
                {
                    needTwoStepInsert = true;
                }
            }
        }

        if (needTwoStepInsert)
        {
            // we first insert the bean without the fk, then in the second step we update the bean
            // with the row with the identity pk values
            ArrayList fkValues = new ArrayList();

            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
            {
                String columnName = selfRefFk.getReference(idx).getLocalColumnName();

                fkValues.add(bean.get(columnName));
                bean.set(columnName, null);
            }
            _platform.insert(_connection, _model, bean);
            for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
            {
                bean.set(selfRefFk.getReference(idx).getLocalColumnName(), fkValues.get(idx));
            }
            _platform.update(_connection, _model, bean);
        }
        else
        {
            _platform.insert(_connection, _model, bean);
        }
        if (!_connection.getAutoCommit())
        {
            _connection.commit();
        }
    }
    catch (Exception ex)
    {
        if (_haltOnErrors)
        {
            _platform.returnConnection(_connection);
            throw new DataSinkException(ex);
        }
        else
        {
            _log.warn("Exception while inserting a row into the database", ex);
        }
    }
}