Database Schema and Table Naming

Model Options

Database Schema and Table naming follows the logic described here.

Options:

// behavior of <4.x nsx-prime/expanders
// the value was ignored and component.name.toUpperCase() was set:
//* comp_hasSchema = componentOption("hasDataBaseSchema : <IgnoredValue>")
//  * if comp_hasSchema.defined ? comp_hasSchema.value <- component.name.toUpperCase()

// behavior of >=4.x nsx-prime/expanders
// the actual value of the option is used
* comp_hasSchema = componentOption("hasDataBaseSchema : <schemaName>")

* data_hasSchema = dataOption("hasDataBaseSchema")
* data_schemaName = dataOption("hasDataSchemaName : <schemaName>")

* data_tableName = dataOption("hasDataTableName : <tableName>")

Schema Name

1. data_schemaName.defined ? data_schemaName.value
2. comp_hasSchema.defined ? comp_hasSchema.value
3. data_hasSchema.defined ? component.name.toUpperCase()
4. null // ElementOption.none()

Table Name

1. data_tableName.defined ? data_table.value
2. data_hasSchema.defined ? dataElement.packageName.lastSegment + '_' + dataElement.name
3. data_schemaName.defined ? dataElement.packageName.lastSegment + '_' + dataElement.name
4. comp_hasSchema.defined ? dataElement.name
5. UNIQUE_PREFIX + '_' + dataElement.name

where UNIQUE_PREFIX = application.shortName.first(4).toUpperCase()
             + '_'
             + component.name.first(4).toUpperCase()

// E.g. for application("myApp") and component("myComp")
// UNIQUE_PREFIX = "MYAP_MYCO"

Possible Variations

Note that component created in PrimeRadiant will automatically contain componentOption("hasDataBaseSchema : <COMPONENTNAME>") – variation test_comp_hasSchema().

public class TableMappingTest {

  @Test
  public void test_noOptions() {
    // application("MyApp")
    ElementSpec spec = component("myComp",
        dataElement("City"));

    assertSchema(spec, hasProperty("defined", is(false)));
    assertTable(spec, is("MYAP_MYCO_City"));
  }

  @Test
  public void test_comp_hasSchema() {
    ElementSpec spec = component("myComp",
        componentOption("hasDataBaseSchema : COMP_SCHEMA"),
        dataElement("City"));

    assertSchema(spec, is("COMP_SCHEMA"));
    assertTable(spec, is("City"));
  }

  @Test
  public void test_data_schemaName() {
    ElementSpec spec = component("myComp",
        dataElement("City",
            set("packageName", "test.world"),
            dataOption("hasDataSchemaName : DE_SCHEMA")));

    assertSchema(spec, is("DE_SCHEMA"));
    assertTable(spec, is("world_City"));
  }

  @Test
  public void test_data_hasSchema() {
    ElementSpec spec = component("myComp",
        dataElement("City",
            set("packageName", "test.world"),
            dataOption("hasDataBaseSchema")));

    assertSchema(spec, is("MYCOMP"));
    assertTable(spec, is("world_City"));
  }

  @Test
  public void test_data_tableName() {
    ElementSpec spec = component("myComp",
        dataElement("City",
            set("packageName", "test.world"),
            dataOption("hasDataTableName : CityTable")));

    assertTable(spec, is("CityTable"));
  }
}

Internal Transformation For Expanders

During transfromation phase, direct lookup schema and table ElementOptions are added to every dataElement. Their value is based on the conditional logic mentioned above.

The dataOption("databaseTableName") is always defined. The dataOption("databaseSchemaName") is always defined except when the value would be empty string, in which case it is not defined (instead of being a defined, but empty string).