use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SingleFieldMultiMapping method addColumns.
/**
* Convenience method to add a datastore field for this mapping.
* If this mapping is a "full" mapping (for a field in a table) then a Column will be added,
* otherwise (mapping representing a parameter in a query) will just add a datastore mapping.
* The datastore mapping is added to the end of the datastoreMappings.
* @param typeName Java type of the field to add the column for.
*/
protected void addColumns(String typeName) {
MappingManager mgr = storeMgr.getMappingManager();
Column column = null;
if (table != null) {
// Full mapping, so add column to back the datastore mapping
column = mgr.createColumn(this, typeName, getNumberOfDatastoreMappings());
}
mgr.createDatastoreMapping(this, column, typeName);
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SequenceTable method initialize.
/**
* Method to initialise the table.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr) {
assertIsUninitialized();
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
// "SEQUENCE_NAME" column
sequenceNameMapping = storeMgr.getMappingManager().getMapping(String.class);
Column colSequenceName = addColumn(String.class.getName(), idFactory.newColumnIdentifier(sequenceNameColumnName), sequenceNameMapping, null);
colSequenceName.setPrimaryKey();
colSequenceName.getColumnMetaData().setLength(Integer.valueOf("255"));
colSequenceName.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
getStoreManager().getMappingManager().createDatastoreMapping(sequenceNameMapping, colSequenceName, String.class.getName());
// "NEXT_VAL" column
nextValMapping = storeMgr.getMappingManager().getMapping(Long.class);
Column colNextVal = addColumn(Long.class.getName(), idFactory.newColumnIdentifier(nextValColumnName), nextValMapping, null);
getStoreManager().getMappingManager().createDatastoreMapping(nextValMapping, colNextVal, Long.class.getName());
// Set up JDBC statements for supported operations
insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + colSequenceName.getIdentifier() + "," + colNextVal.getIdentifier() + ") VALUES (?,?)";
incrementByStmt = "UPDATE " + identifier.getFullyQualifiedName(false) + " SET " + colNextVal.getIdentifier() + "=(" + colNextVal.getIdentifier() + "+?) WHERE " + colSequenceName.getIdentifier() + "=?";
deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + colSequenceName.getIdentifier() + "=?";
deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
fetchStmt = "SELECT " + colNextVal.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + colSequenceName.getIdentifier() + "=?";
if (dba.supportsOption(DatastoreAdapter.LOCK_WITH_SELECT_FOR_UPDATE)) {
fetchStmt += " FOR UPDATE";
}
fetchAllStmt = "SELECT " + colNextVal.getIdentifier() + "," + colSequenceName.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + colSequenceName.getIdentifier();
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class BaseDatastoreAdapter method getCreateTableStatement.
/**
* Returns the appropriate SQL to create the given table having the given
* columns. No column constraints or key definitions should be included.
* It should return something like:
* <pre>
* CREATE TABLE FOO ( BAR VARCHAR(30), BAZ INTEGER )
* </pre>
*
* @param table The table to create.
* @param columns The columns of the table.
* @param props Properties for controlling the table creation
* @param factory Factory for identifiers
* @return The text of the SQL statement.
*/
public String getCreateTableStatement(TableImpl table, Column[] columns, Properties props, IdentifierFactory factory) {
StringBuilder createStmt = new StringBuilder();
String indent = " ";
if (getContinuationString().length() == 0) {
indent = "";
}
// CREATE TABLE with column specifiers
createStmt.append("CREATE TABLE ").append(table.toString()).append(getContinuationString()).append("(").append(getContinuationString());
for (int i = 0; i < columns.length; ++i) {
if (i > 0) {
createStmt.append(",").append(getContinuationString());
}
createStmt.append(indent).append(columns[i].getSQLDefinition());
}
// PRIMARY KEY(col[,col])
if (supportsOption(PRIMARYKEY_IN_CREATE_STATEMENTS)) {
PrimaryKey pk = table.getPrimaryKey();
if (pk != null && pk.size() > 0) {
boolean includePk = true;
if (supportsOption(AUTO_INCREMENT_PK_IN_CREATE_TABLE_COLUMN_DEF)) {
for (Column pkCol : pk.getColumns()) {
if (pkCol.isIdentity()) {
// This column is auto-increment and is specified in the column def so ignore here
includePk = false;
break;
}
}
}
if (includePk) {
createStmt.append(",").append(getContinuationString());
if (pk.getName() != null) {
String identifier = factory.getIdentifierInAdapterCase(pk.getName());
createStmt.append(indent).append("CONSTRAINT ").append(identifier).append(" ").append(pk.toString());
} else {
createStmt.append(indent).append(pk.toString());
}
}
}
}
// UNIQUE( col [,col] )
if (supportsOption(UNIQUE_IN_END_CREATE_STATEMENTS)) {
StringBuilder uniqueConstraintStmt = new StringBuilder();
for (int i = 0; i < columns.length; ++i) {
if (columns[i].isUnique()) {
if (uniqueConstraintStmt.length() < 1) {
uniqueConstraintStmt.append(",").append(getContinuationString());
uniqueConstraintStmt.append(indent).append(" UNIQUE (");
} else {
uniqueConstraintStmt.append(",");
}
uniqueConstraintStmt.append(columns[i].getIdentifier().toString());
}
}
if (uniqueConstraintStmt.length() > 1) {
uniqueConstraintStmt.append(")");
createStmt.append(uniqueConstraintStmt.toString());
}
}
// FOREIGN KEY(col [,col] ) REFERENCES {TBL} (col [,col])
if (supportsOption(FK_IN_END_CREATE_STATEMENTS)) {
StringBuilder fkConstraintStmt = new StringBuilder();
ClassLoaderResolver clr = table.getStoreManager().getNucleusContext().getClassLoaderResolver(null);
List<ForeignKey> fks = table.getExpectedForeignKeys(clr);
if (fks != null && !fks.isEmpty()) {
for (ForeignKey fk : fks) {
NucleusLogger.GENERAL.debug(">> TODO Add FK in CREATE TABLE as " + fk);
// TODO Add the FK. Make sure that the other table exists
}
}
if (fkConstraintStmt.length() > 1) {
createStmt.append(fkConstraintStmt.toString());
}
}
// CHECK (column_identifier IN (literal[,literal]))
if (supportsOption(CHECK_IN_END_CREATE_STATEMENTS)) {
StringBuilder checkConstraintStmt = new StringBuilder();
for (int i = 0; i < columns.length; ++i) {
if (columns[i].getCheckConstraints() != null) {
checkConstraintStmt.append(",").append(getContinuationString());
checkConstraintStmt.append(indent).append(columns[i].getCheckConstraints());
}
}
if (checkConstraintStmt.length() > 1) {
createStmt.append(checkConstraintStmt.toString());
}
}
createStmt.append(getContinuationString()).append(")");
return createStmt.toString();
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SQLAnywhereAdapter method getCreateTableStatement.
/**
* Returns the appropriate SQL to create the given table having the given columns. No column constraints
* or key definitions should be included. It should return something like:
*
* <pre>
* CREATE TABLE FOO ( BAR VARCHAR(30), BAZ INTEGER )
* </pre>
* @param table The table to create.
* @param columns The columns of the table.
* @param props Properties for controlling the table creation
* @param factory Factory for identifiers
* @return The text of the SQL statement.
*/
public String getCreateTableStatement(TableImpl table, Column[] columns, Properties props, IdentifierFactory factory) {
StringBuilder createStmt = new StringBuilder();
String indent = " ";
if (getContinuationString().length() == 0) {
indent = "";
}
// CREATE TABLE with column specifiers
createStmt.append("CREATE TABLE ").append(table.toString()).append(getContinuationString()).append("(").append(getContinuationString());
for (int i = 0; i < columns.length; ++i) {
if (i > 0) {
createStmt.append(",").append(getContinuationString());
}
createStmt.append(indent).append(columns[i].getSQLDefinition());
}
// PRIMARY KEY(col[,col])
if (supportsOption(PRIMARYKEY_IN_CREATE_STATEMENTS)) {
PrimaryKey pk = table.getPrimaryKey();
if (pk != null && pk.size() > 0) {
boolean includePk = true;
if (supportsOption(AUTO_INCREMENT_PK_IN_CREATE_TABLE_COLUMN_DEF)) {
for (Column pkCol : pk.getColumns()) {
if (pkCol.isIdentity()) {
// This column is auto-increment and is specified in the column def so ignore here
includePk = false;
break;
}
}
}
if (includePk) {
createStmt.append(",").append(getContinuationString());
if (pk.getName() != null) {
String identifier = factory.getIdentifierInAdapterCase(pk.getName());
createStmt.append(indent).append("CONSTRAINT ").append(identifier).append(" ").append(pk.toString());
} else {
createStmt.append(indent).append(pk.toString());
}
}
}
}
// UNIQUE( col [,col] )
if (supportsOption(UNIQUE_IN_END_CREATE_STATEMENTS)) {
StringBuilder uniqueConstraintStmt = new StringBuilder();
for (int i = 0; i < columns.length; ++i) {
if (columns[i].isUnique()) {
if (uniqueConstraintStmt.length() < 1) {
uniqueConstraintStmt.append(",").append(getContinuationString());
uniqueConstraintStmt.append(indent).append(" UNIQUE (");
} else {
uniqueConstraintStmt.append(",");
}
uniqueConstraintStmt.append(columns[i].getIdentifier().toString());
}
}
if (uniqueConstraintStmt.length() > 1) {
uniqueConstraintStmt.append(")");
createStmt.append(uniqueConstraintStmt.toString());
}
}
// FOREIGN KEY(col [,col] ) REFERENCES {TBL} (col [,col])
if (supportsOption(FK_IN_END_CREATE_STATEMENTS)) {
StringBuilder fkConstraintStmt = new StringBuilder();
ClassLoaderResolver clr = table.getStoreManager().getNucleusContext().getClassLoaderResolver(null);
List<ForeignKey> fks = table.getExpectedForeignKeys(clr);
if (fks != null && !fks.isEmpty()) {
for (ForeignKey fk : fks) {
// TODO Ensure that the other table exists, for now assume it does
createStmt.append(",").append(getContinuationString());
if (fk.getName() != null) {
String identifier = factory.getIdentifierInAdapterCase(fk.getName());
createStmt.append(indent).append("CONSTRAINT ").append(identifier).append(" ").append(fk.toString());
} else {
createStmt.append(indent).append(fk.toString());
}
}
}
if (fkConstraintStmt.length() > 1) {
createStmt.append(fkConstraintStmt.toString());
}
}
// CHECK (column_identifier IN (literal[,literal]))
if (supportsOption(CHECK_IN_END_CREATE_STATEMENTS)) {
StringBuilder checkConstraintStmt = new StringBuilder();
for (int i = 0; i < columns.length; ++i) {
if (columns[i].getCheckConstraints() != null) {
checkConstraintStmt.append(",").append(getContinuationString());
checkConstraintStmt.append(indent).append(columns[i].getCheckConstraints());
}
}
if (checkConstraintStmt.length() > 1) {
createStmt.append(checkConstraintStmt.toString());
}
}
createStmt.append(getContinuationString()).append(")");
return createStmt.toString();
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SchemaTable method initialize.
/**
* Method to initialise the table.
* @param clr The ClassLoaderResolver
*/
public void initialize(ClassLoaderResolver clr) {
assertIsUninitialized();
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
MappingManager mapMgr = getStoreManager().getMappingManager();
classMapping = mapMgr.getMapping(String.class);
Column class_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("CLASS_NAME"), classMapping, null);
mapMgr.createDatastoreMapping(classMapping, class_column, String.class.getName());
class_column.getColumnMetaData().setLength(128);
class_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
class_column.setPrimaryKey();
tableMapping = mapMgr.getMapping(String.class);
Column table_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TABLE_NAME"), tableMapping, null);
mapMgr.createDatastoreMapping(tableMapping, table_column, String.class.getName());
table_column.getColumnMetaData().setLength(128);
table_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
typeMapping = mapMgr.getMapping(String.class);
Column type_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("TYPE"), typeMapping, null);
mapMgr.createDatastoreMapping(typeMapping, type_column, String.class.getName());
type_column.getColumnMetaData().setLength(4);
type_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
// TODO Change type to SMALLINT/BIT
ownerMapping = mapMgr.getMapping(String.class);
Column owner_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("OWNER"), ownerMapping, null);
mapMgr.createDatastoreMapping(ownerMapping, owner_column, String.class.getName());
owner_column.getColumnMetaData().setLength(2);
owner_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
versionMapping = mapMgr.getMapping(String.class);
Column version_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("VERSION"), versionMapping, null);
mapMgr.createDatastoreMapping(versionMapping, version_column, String.class.getName());
version_column.getColumnMetaData().setLength(20);
version_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
interfaceNameMapping = mapMgr.getMapping(String.class);
Column interfaceName_column = addColumn(String.class.getName(), idFactory.newColumnIdentifier("INTERFACE_NAME"), interfaceNameMapping, null);
mapMgr.createDatastoreMapping(interfaceNameMapping, interfaceName_column, String.class.getName());
interfaceName_column.getColumnMetaData().setLength(255);
interfaceName_column.getColumnMetaData().setJdbcType(JdbcType.VARCHAR);
interfaceName_column.setNullable(true);
// Set up JDBC statements for supported operations
insertStmt = "INSERT INTO " + identifier.getFullyQualifiedName(false) + " (" + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + ") VALUES (?,?,?,?,?,?)";
deleteStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + "=?";
deleteAllStmt = "DELETE FROM " + identifier.getFullyQualifiedName(false);
fetchAllStmt = "SELECT " + class_column.getIdentifier() + "," + table_column.getIdentifier() + "," + type_column.getIdentifier() + "," + owner_column.getIdentifier() + "," + version_column.getIdentifier() + "," + interfaceName_column.getIdentifier() + " FROM " + identifier.getFullyQualifiedName(false) + " ORDER BY " + table_column.getIdentifier();
fetchStmt = "SELECT 1 FROM " + identifier.getFullyQualifiedName(false) + " WHERE " + idFactory.getIdentifierInAdapterCase("CLASS_NAME") + " = ? ";
state = TABLE_STATE_INITIALIZED;
}
Aggregations