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

The following examples show how to use org.apache.commons.beanutils.DynaBean#get() . 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: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an identity object for the specified foreign key using the foreignkey column values
 * of the supplied bean.
 * 
 * @param owningTable The table owning the foreign key
 * @param fk          The foreign key
 * @param bean        The bean
 * @return The identity
 */
private Identity buildIdentityFromFK(Table owningTable, ForeignKey fk, DynaBean bean)
{
    Identity identity = new Identity(fk.getForeignTable(), getFKName(owningTable, fk));

    for (int idx = 0; idx < fk.getReferenceCount(); idx++)
    {
        Reference reference = (Reference)fk.getReference(idx);
        Object    value     = bean.get(reference.getLocalColumnName());

        if (value == null)
        {
            return null;
        }
        identity.setColumnValue(reference.getForeignColumnName(), value);
    }
    return identity;
}
 
Example 2
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the value of the bean's property that has the given name. Depending on the
 * case-setting of the current builder, the case of teh name is considered or not. 
 * 
 * @param bean     The bean
 * @param propName The name of the property
 * @return The value
 */
protected Object getPropertyValue(DynaBean bean, String propName)
{
    if (getPlatform().isDelimitedIdentifierModeOn())
    {
        return bean.get(propName);
    }
    else
    {
        DynaProperty[] props = bean.getDynaClass().getDynaProperties();

        for (int idx = 0; idx < props.length; idx++)
        {
            if (propName.equalsIgnoreCase(props[idx].getName()))
            {
                return bean.get(props[idx].getName());
            }
        }
        throw new IllegalArgumentException("The bean has no property with the name "+propName);
    }
}
 
Example 3
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the specified attribute value of the given bean with the expected object.
 * 
 * @param expected The expected object
 * @param bean     The bean
 * @param attrName The attribute name
 */
protected void assertEquals(Object expected, Object bean, String attrName)
{
    DynaBean dynaBean = (DynaBean)bean;
    Object   value    = dynaBean.get(attrName);

    if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean))
    {
        SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass();
        Column       column    = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn();

        if (TypeMap.isBinaryType(column.getTypeCode()))
        {
            value = new BinaryObjectsHelper().deserialize((byte[])value);
        }
    }
    if (expected == null)
    {
        assertNull(value);
    }
    else
    {
        assertEquals(expected, value);
    }
}
 
Example 4
Source File: TestAgainstLiveDatabaseBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Compares the specified attribute value of the given bean with the expected object.
 * 
 * @param expected The expected object
 * @param bean     The bean
 * @param attrName The attribute name
 */
protected void assertEquals(Object expected, Object bean, String attrName)
{
    DynaBean dynaBean = (DynaBean)bean;
    Object   value    = dynaBean.get(attrName);

    if ((value instanceof byte[]) && !(expected instanceof byte[]) && (dynaBean instanceof SqlDynaBean))
    {
        SqlDynaClass dynaClass = (SqlDynaClass)((SqlDynaBean)dynaBean).getDynaClass();
        Column       column    = ((SqlDynaProperty)dynaClass.getDynaProperty(attrName)).getColumn();

        if (TypeMap.isBinaryType(column.getTypeCode()))
        {
            value = new BinaryObjectsHelper().deserialize((byte[])value);
        }
    }
    if (expected == null)
    {
        assertNull(value);
    }
    else
    {
        assertEquals(expected, value);
    }
}
 
Example 5
Source File: DataToDatabaseSink.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an identity object for the specified foreign key using the foreignkey column values
 * of the supplied bean.
 * 
 * @param owningTable The table owning the foreign key
 * @param fk          The foreign key
 * @param bean        The bean
 * @return The identity
 */
private Identity buildIdentityFromFK(Table owningTable, ForeignKey fk, DynaBean bean)
{
    Identity identity = new Identity(fk.getForeignTable(), getFKName(owningTable, fk));

    for (int idx = 0; idx < fk.getReferenceCount(); idx++)
    {
        Reference reference = (Reference)fk.getReference(idx);
        Object    value     = bean.get(reference.getLocalColumnName());

        if (value == null)
        {
            return null;
        }
        identity.setColumnValue(reference.getForeignColumnName(), value);
    }
    return identity;
}
 
Example 6
Source File: PostgreSqlPlatform.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException
{
    int     typeCode = property.getColumn().getTypeCode();
    Object  value    = dynaBean.get(property.getName());

    // PostgreSQL doesn't like setNull for BYTEA columns
    if (value == null)
    {
        switch (typeCode)
        {
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.BLOB:
                statement.setBytes(sqlIndex, null);
                break;
            default:
                statement.setNull(sqlIndex, typeCode);
                break;
        }
    }
    else
    {
        super.setObject(statement, sqlIndex, dynaBean, property);
    }
}
 
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 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 8
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 9
Source File: PostgreSqlPlatform.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException
{
    int     typeCode = property.getColumn().getTypeCode();
    Object  value    = dynaBean.get(property.getName());

    // PostgreSQL doesn't like setNull for BYTEA columns
    if (value == null)
    {
        switch (typeCode)
        {
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.BLOB:
                statement.setBytes(sqlIndex, null);
                break;
            default:
                statement.setNull(sqlIndex, typeCode);
                break;
        }
    }
    else
    {
        super.setObject(statement, sqlIndex, dynaBean, property);
    }
}
 
Example 10
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 11
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the alteration of the datatypes of columns of a PK and FK that
 * will be dropped.
 */
public void testPKAndFKColumnTypesAndDropFK()
{
    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' primaryKey='false' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='VARCHAR' required='false'/>\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 12
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 testIndexColumnTypeSmallintToVarchar()
{
    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"+
        "    <index name='testindex'>\n"+
        "      <index-column name='avalue1'/>\n"+
        "      <index-column name='avalue2'/>\n"+
        "    </index>\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"+
        "    <index name='testindex'>\n"+
        "      <index-column name='avalue1'/>\n"+
        "      <index-column name='avalue2'/>\n"+
        "    </index>\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 13
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 14
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 15
Source File: DatabaseTestHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
     * Helper method for build a SELECT statement.
     * 
     * @param targetPlatform The platform for the queried database
     * @param table          The queried table
     * @param whereCols      The optional columns that make up the WHERE clause
     * @param whereValues    The optional column value that make up the WHERE clause
     * @return The query string
     */
    private String buildQueryString(Platform targetPlatform, Table table, Column[] whereCols, DynaBean whereValues)
    {
        StringBuilder result = new StringBuilder();

        result.append("SELECT * FROM ");
        if (targetPlatform.isDelimitedIdentifierModeOn())
        {
            result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
        }
        result.append(table.getName());
        if (targetPlatform.isDelimitedIdentifierModeOn())
        {
            result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
        }
        if ((whereCols != null) && (whereCols.length > 0))
        {
            result.append(" WHERE ");
            for (int idx = 0; idx < whereCols.length; idx++)
            {
                Object value = (whereValues == null ? null : whereValues.get(whereCols[idx].getName()));

                if (idx > 0)
                {
                    result.append(" AND ");
                }
                if (targetPlatform.isDelimitedIdentifierModeOn())
                {
// GemStone changes BEGIN
                    whereCols[idx].getDelimitedName(result,
                        targetPlatform.getPlatformInfo().getDelimiterToken());
                }
                else {
                    result.append(whereCols[idx].getName());
                    /* (original code)
                    result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
                }
                result.append(whereCols[idx].getName());
                if (targetPlatform.isDelimitedIdentifierModeOn())
                {
                    result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
                    */
// GemStone changes END
                }
                result.append(" = ");
                if (value == null)
                {
                    result.append("NULL");
                }
                else
                {
                    if (!whereCols[idx].isOfNumericType())
                    {
                        result.append(targetPlatform.getPlatformInfo().getValueQuoteToken());
                    }
                    result.append(value.toString());
                    if (!whereCols[idx].isOfNumericType())
                    {
                        result.append(targetPlatform.getPlatformInfo().getValueQuoteToken());
                    }
                }
            }
        }

        return result.toString();
    }
 
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 alteration of the datatypes of columns of a PK and FK that
 * will be dropped.
 */
public void testPKAndFKColumnTypesAndDropFK()
{
    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' primaryKey='false' required='true'/>\n"+
        "  </table>\n"+
        "  <table name='roundtrip2'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        "    <column name='fk' type='VARCHAR' required='false'/>\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: DatabaseTestHelper.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
     * Helper method for build a SELECT statement.
     * 
     * @param targetPlatform The platform for the queried database
     * @param table          The queried table
     * @param whereCols      The optional columns that make up the WHERE clause
     * @param whereValues    The optional column value that make up the WHERE clause
     * @return The query string
     */
    private String buildQueryString(Platform targetPlatform, Table table, Column[] whereCols, DynaBean whereValues)
    {
        StringBuilder result = new StringBuilder();

        result.append("SELECT * FROM ");
        if (targetPlatform.isDelimitedIdentifierModeOn())
        {
            result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
        }
        result.append(table.getName());
        if (targetPlatform.isDelimitedIdentifierModeOn())
        {
            result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
        }
        if ((whereCols != null) && (whereCols.length > 0))
        {
            result.append(" WHERE ");
            for (int idx = 0; idx < whereCols.length; idx++)
            {
                Object value = (whereValues == null ? null : whereValues.get(whereCols[idx].getName()));

                if (idx > 0)
                {
                    result.append(" AND ");
                }
                if (targetPlatform.isDelimitedIdentifierModeOn())
                {
// GemStone changes BEGIN
                    whereCols[idx].getDelimitedName(result,
                        targetPlatform.getPlatformInfo().getDelimiterToken());
                }
                else {
                    result.append(whereCols[idx].getName());
                    /* (original code)
                    result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
                }
                result.append(whereCols[idx].getName());
                if (targetPlatform.isDelimitedIdentifierModeOn())
                {
                    result.append(targetPlatform.getPlatformInfo().getDelimiterToken());
                    */
// GemStone changes END
                }
                result.append(" = ");
                if (value == null)
                {
                    result.append("NULL");
                }
                else
                {
                    if (!whereCols[idx].isOfNumericType())
                    {
                        result.append(targetPlatform.getPlatformInfo().getValueQuoteToken());
                    }
                    result.append(value.toString());
                    if (!whereCols[idx].isOfNumericType())
                    {
                        result.append(targetPlatform.getPlatformInfo().getValueQuoteToken());
                    }
                }
            }
        }

        return result.toString();
    }
 
Example 19
Source File: PlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a parameter of the prepared statement based on the type of the column of the property.
 * 
 * @param statement The statement
 * @param sqlIndex  The index of the parameter to set in the statement
 * @param dynaBean  The bean of which to take the value
 * @param property  The property of the bean, which also defines the corresponding column
 */
protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException
{
    int     typeCode = property.getColumn().getTypeCode();
    Object  value    = dynaBean.get(property.getName());

    setStatementParameterValue(statement, sqlIndex, typeCode, value);
}
 
Example 20
Source File: PlatformImplBase.java    From gemfirexd-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Sets a parameter of the prepared statement based on the type of the column of the property.
 * 
 * @param statement The statement
 * @param sqlIndex  The index of the parameter to set in the statement
 * @param dynaBean  The bean of which to take the value
 * @param property  The property of the bean, which also defines the corresponding column
 */
protected void setObject(PreparedStatement statement, int sqlIndex, DynaBean dynaBean, SqlDynaProperty property) throws SQLException
{
    int     typeCode = property.getColumn().getTypeCode();
    Object  value    = dynaBean.get(property.getName());

    setStatementParameterValue(statement, sqlIndex, typeCode, value);
}