📄 abstractcolumnspec.java
字号:
*
* @param aPO a value of type 'PersistentObject'
* @param helper a value of type 'JDBCHelper'
* @param pkColumnSpec a value of type 'ColumnSpec'
* @exception DuplicateRowException if a row with this value already exists.
*/
public static void validateUnique(PersistentObject aPO,
JDBCHelper helper,
ColumnSpec pkColumnSpec,
ColumnSpec attrColumnSpec,
DatabasePolicy dbPolicy,
String tableName)
throws DuplicateRowException
{
if (!attrColumnSpec.isUnique())
{
return;
}
if (attrColumnSpec.isNaturalPrimaryKey() &&
!aPO.hasNewPersistentState())
{
// No need to check natural primary keys that are already
// inserted even though they are automatically marked UNIQUE.
return;
}
// Make sure this value won't create a duplicate row
StringBuffer buffer = new StringBuffer();
buffer
.append("SELECT count(*) FROM ")
.append(tableName).append(" ")
.append("WHERE ")
.append(attrColumnSpec.buildWhereClause(aPO,
EQUALS,
tableName,
dbPolicy));
// If this PersistentObject is already in the database, make sure we
// don't get a match on itself.
if (!aPO.hasNewPersistentState())
{
buffer
.append(" AND ")
.append(
pkColumnSpec.buildWhereClause(
aPO,
NOT_EQUALS,
tableName,
dbPolicy));
}
int count = 0;
try
{
helper.executeQuery(buffer.toString());
helper.next();
count = helper.getint(1);
helper.close();
}
catch (Exception e)
{
throw new DatabaseException(e);
}
if (count > 0)
{
throw new DuplicateRowException(
"Attempt to insert a duplicate value for attribute - "
+ attrColumnSpec.getColumnName());
}
} // validateUnique(aPO, aJDBCHelper, pkColumnSpec, dbPolicy)
/* =============== Public Instance Methods =============== */
/**
* "Encode" the given object into a string representation that can be
* decoded later. This implementation is pretty basic. This method will
* *not* be used for generating SQL.
*
* @param obj a value of type 'Object'
* @return a value of type 'String'
*/
public String encode(Object obj)
{
return (obj==null ? "null" : obj.toString());
}
/**
* This method is similar in concept to the
* copyColumnValueToPersistentObject(...) method, but gets it's value
* from a String instead of a JDBCHelper.
*
* The String parameter must have been encoded with the
* encodeFromPersistentObject() method.
*
* @see AbstractColumnSpec#encodeFromPersistentObject
* @param aString a value of type 'String'
* @param aPO a value of type 'PersistentObject'
*/
public void decodeToPersistentObject(String aString,
PersistentObject aPO)
{
Object obj = this.decode(aString);
AbstractColumnSpec.setValueTo(obj,
aPO,
i_setter,
this.getColumnClass());
}
/**
* Convert the attribute for this object to a String that can be
* unconverted later by the decodeToPersistentObject(...) method. The
* results of this method are not to be used in SQL.
*
* @see AbstractColumnSpec#decodeToPersistentObject
* @param aPO a value of type 'PersistentObject'
* @return a value of type 'String'
*/
public String encodeFromPersistentObject(PersistentObject aPO)
{
Object obj = AbstractColumnSpec.getValueFrom(aPO, i_getter, null);
return this.encode(obj);
}
/**
* Copy the value of my column to the appropriate attribute for this
* persistent object.
*
* @param helper a value of type 'JDBCHelper'
* @param aPO a value of type 'PersistentObject'
* @exception SQLException if an error occurs
*/
public void copyColumnValueToPersistentObject(JDBCHelper helper,
PersistentObject aPO)
throws SQLException
{
if (i_setter == null || i_setter.length() == 0)
{
// Don't set the value since there is no setter.
return;
}
Object columnValue = this.getColumnValueFrom(helper);
if (columnValue == null)
{
columnValue = this.getDefault();
}
this.setValueTo(columnValue, aPO);
}
/**
* Copy the attribute I represent from one persistent object to another.
*
* @param aPO1 a value of type 'PersistentObject'
* @param aPO2 a value of type 'PersistentObject'
*/
public void copyAttribute(PersistentObject aPO1,
PersistentObject aPO2)
{
if (i_setter == null ||
i_setter.length() == 0)
{
// Don't set the value since there is no setter.
return;
}
Object value = this.getValueFrom(aPO1);
this.setValueTo(value, aPO2);
}
/**
* Note: This method is overridden by CompoundPrimaryKeyColumnSpec to
* include " AND " between each name-value pair.
*
* @param pkOrPersistentObject a value of type 'Object'
* @param dbPolicy a value of type 'DatabasePolicy'
* @return a value of type 'String'
*/
public String buildWhereClause(Object pkOrPersistentObject,
String separator,
String tableAlias,
DatabasePolicy dbPolicy)
{
return this.buildNameValuePair(pkOrPersistentObject,
separator,
tableAlias,
dbPolicy);
}
/**
* This method is used to build an equals (or not-equals) String for WHERE
* clauses and UPDATE statements.
*
* @param pkOrPersistentObject a value of type 'Object'
* @param separator a value of type 'String' - use EQUALS or NOT_EQUALS
* static final variables.
* @param tableName a value of type 'String'
* @param dbPolicy a value of type 'DatabasePolicy'
* @return a value of type 'String'
*/
public String buildNameValuePair(Object pkOrPersistentObject,
String separator,
String tableAlias,
DatabasePolicy dbPolicy)
{
StringBuffer buffer =
new StringBuffer();
String value = null;
buffer.append(this.getFullyQualifiedColumnName(tableAlias));
if (pkOrPersistentObject instanceof PersistentObject)
{
value =
this.getSqlValueFrom(
(PersistentObject)pkOrPersistentObject,
dbPolicy);
}
else
{
value = this.formatForSql(pkOrPersistentObject,dbPolicy);
}
if (value.equalsIgnoreCase("null"))
{
buffer.append(separator.equals(EQUALS) ? " IS ":" IS NOT ");
}
else
{
buffer.append(separator);
}
buffer.append(value);
return buffer.toString();
} // buildNameValuePair(...)
/**
* Make sure the PersistentObject object has this attribute (if required).
*
* @param aPO a value of type 'PersistentObject'
* @return a value of type 'Object' - This is the value of the attribute.
* This is so subclasses can call super.validateRequired().
* @exception MissingAttributeException if attribute should be populated
* and it is not.
*/
public Object validateRequired(PersistentObject aPO)
throws MissingAttributeException
{
if (!this.isRequired())
{
return null; // since this attribute is not required.
}
Object attr = this.getValueFrom(aPO);
// Ensure aColumnSpec is not null
if (attr == null)
{
throw new MissingAttributeException(
"Required attribute - "
+ this.getColumnName()
+ "- is a null value.");
}
return attr;
} // validateRequired(aPO)
/**
* If this is a unique column (or columns), make sure the value doesn't
* already exist.
*
* @param aPO a value of type 'PersistentObject'
* @param helper a value of type 'JDBCHelper'
* @param pkColumnSpec a value of type 'ColumnSpec'
* @param dbPolicy a value of type 'DatabasePolicy'
* @param tableName a value of type 'String'
* @exception DuplicateRowException if an error occurs
*/
public void validateUnique(PersistentObject aPO,
JDBCHelper helper,
ColumnSpec pkColumnSpec,
DatabasePolicy dbPolicy,
String tableName)
throws DuplicateRowException
{
AbstractColumnSpec.validateUnique(aPO,
helper,
pkColumnSpec,
this,
dbPolicy,
tableName);
}
/**
* Get a string representation of the value of this attribute from
* aPO. This string can be inserted into a SQL statement.
*
* @param aPO a value of type 'PersistentObject'
* @return a value of type 'String'
*/
public String getSqlValueFrom(PersistentObject aPO,
DatabasePolicy dbPolicy)
{
Object temp = this.getValueFrom(aPO);
return this.formatForSql(temp, dbPolicy);
} // getSqlValueFrom(...)
/**
* Get the value of this attribute from aPO. If the value is
* null, the default value will be returned.
*
* @param aPO a value of type 'PersistentObject'
* @return a value of type 'Object'
*/
public Object getValueFrom(PersistentObject aPO)
{
return AbstractColumnSpec.getValueFrom(aPO,
i_getter,
i_default);
} // getValueFrom(...)
/**
* This is a pass-through method that passes along my setter and column
* class.
*
* @param aValue a value of type 'Object'
* @param aPO a value of type 'PersistentObject'
*/
public void setValueTo(Object aValue,
PersistentObject aPO)
{
AbstractColumnSpec.setValueTo(aValue,
aPO,
i_setter,
this.getColumnClass());
}
/**
* Return something like: "Age INTEGER NOT NULL"
*
* @return a value of type 'String'
*/
public String columnDefinitionString(DatabasePolicy dbPolicy)
{
StringBuffer buffer = new StringBuffer();
buffer.append(i_columnName);
buffer.append(" ");
buffer.append(getSQLColumnType(dbPolicy));
if (this.isRequired())
{
buffer.append(" NOT NULL");
}
else if (!this.isPrimaryKey())
{
buffer.append(" NULL");
}
if (this.isSequencedPrimaryKey() &&
dbPolicy.autoIncrementIdentifier() != null)
{
buffer.append(" ");
buffer.append(dbPolicy.autoIncrementIdentifier());
}
return buffer.toString();
}
/* =============== Getters =============== */
public String getFullyQualifiedColumnName(String tableAlias)
{
if (tableAlias == null ||
tableAlias.trim().length()==0)
{
return i_columnName;
}
else
{
return tableAlias + "." + i_columnName;
}
}
public String getColumnName()
{
return i_columnName;
}
public String getGetter()
{
return i_getter;
}
public String getSetter()
{
return i_setter;
}
public boolean isRequired()
{
return i_required;
}
public boolean isSequencedPrimaryKey()
{
return i_sequencedPrimaryKey;
}
public boolean isNaturalPrimaryKey()
{
return i_naturalPrimaryKey;
}
public boolean isPrimaryKey()
{
return i_naturalPrimaryKey || i_sequencedPrimaryKey;
}
public boolean isUnique()
{
return i_unique;
}
public boolean isSubtypeIdentifier()
{
return i_subtypeIdentifier;
}
public Object getDefault()
{
return i_default;
}
public boolean isOptimisticLock()
{
return i_optimisticLock;
}
/**
* CompoundPrimaryKeyColumnSpec uses this to force it's children to
* "REQUIRED".
*/
public void setRequired(boolean b)
{
i_required = b;
}
/**
* Unless this is overridden, an exception will be thrown.
*
* @return a value of type 'Object'
*/
public Object optimisticLockDefaultValue()
{
throw new ConfigurationException(
this.getClass().toString() + " cannot be an optimistic lock.");
}
/** @return a JoinColumn subclass instance with data from myself. */
public abstract JoinColumn buildJoinColumn();
} // AbstractColumnSpec
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -