Abstract class of table fields in hibernate
Package org. hibernate. Mapping;
Import java. Io. serializable;
Import org. hibernate. hibernateexception;
Import org. hibernate. mappingexception;
Import org. hibernate. dialect. dialect;
Import org. hibernate. Engine. Mapping;
Import org. hibernate. util. stringhelper;
/**
* A column of a relational database table
* @ Author Gavin king
*/
Public class column implements selectable, serializable, cloneable {
Public static final int default_length = 255;
Public static final int default_precision = 19;
Public static final int default_scale = 2;
Private int length = default_length;
Private int precision = default_precision;
Private int scale = default_scale;
Private value;
Private int typeindex = 0;
Private string name;
Private Boolean nullable = true;
Private Boolean unique = false;
Private string sqltype;
Private integer sqltypecode;
Private Boolean quoted = false;
Int uniqueinteger;
Private string checkconstraint;
Private string comment;
Private string defaultvalue;
Public column (){};
Public column (string columnname ){
Setname (columnname );
}
Public int getlength (){
Return length;
}
Public void setlength (INT length ){
This. Length = length;
}
Public Value getvalue (){
Return value;
}
Public void setvalue (value ){
This. value = value;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
If (
Name. charat (0) = ''' |
Dialect. Quote. indexof (name. charat (0)>-1 // todo: deprecated, remove eventually
){
Quoted = true;
This. Name = Name. substring (1, name. Length ()-1 );
}
Else {
This. Name = Name;
}
}
/** Returns quoted name as it wocould be in the Mapping File .*/
Public String getquotedname (){
Return quoted?
"'" + Name + "'":
Name;
}
Public String getquotedname (dialect d ){
Return quoted?
D. openquote () + name + D. closequote ():
Name;
}
/**
* For any column name, generate an alias that is unique
* To that column name, and also 10 characters or less
* In length.
*/
Public String getalias (dialect ){
String alias = Name;
String unique = integer. tostring (uniqueinteger) + '_';
Int lastletter = stringhelper. lastindexofletter (name );
If (lastletter =-1 ){
Alias = "column ";
}
Else if (lastletter <name. Length ()-1 ){
Alias = Name. substring (0, lastletter + 1 );
}
If (alias. Length ()> dialect. getmaxaliaslength ()){
Alias = alias. substring (0, dialect. getmaxaliaslength ()-Unique. Length ());
}
Boolean userawname = Name. Equals (alias )&&
! Quoted &&
! Name. tolowercase (). Equals ("rowid ");
If (userawname ){
Return alias;
}
Else {
Return alias + unique;
}
}
/**
* Generate a column alias that is unique sans SS multiple tables
*/
Public String getalias (dialect, Table ){
Return getalias (dialect) + Table. getuniqueinteger () + '_';
}
Public Boolean isnullable (){
Return nullable;
}
Public void setnullable (Boolean nullable ){
This. nullable = nullable;
}
Public int gettypeindex (){
Return typeindex;
}
Public void settypeindex (INT typeindex ){
This. typeindex = typeindex;
}
Public int getsqltypecode (mapping) throws mappingexception {
Org. hibernate. type. Type type = getvalue (). GetType ();
Try {
Int sqltypecode = type. sqltypes (mapping) [gettypeindex ()];
If (getsqltypecode ()! = NULL & getsqltypecode (). intvalue ()! = Sqltypecode ){
Throw new mappingexception ("sqltype code's does not match. mapped as" + sqltypecode + "but is" + getsqltypecode ());
}
Return sqltypecode;
}
Catch (exception e ){
Throw new mappingexception (
"Cocould not determine type for column" +
Name +
"Of type" +
Type. getclass (). getname () +
":" +
E. getclass (). getname (),
E
);
}
}
/**
* Returns the underlying columns sqltypecode.
* If null, it is because the sqltype code is unknown.
*
* Use # getsqltypecode (mapping) to retreive the sqltypecode used
* For the columns associated value/type.
*
* @ Return sqltypecode if it is set, otherwise null.
*/
Public integer getsqltypecode (){
Return sqltypecode;
}
Public void setsqltypecode (integer typecode ){
Sqltypecode = typecode;
}
Public Boolean isunique (){
Return unique;
}
Public String getsqltype (dialect, mapping) throws hibernateexception {
Return sqltype = NULL?
Dialect. gettypename (getsqltypecode (mapping), getlength (), getprecision (), getscale ()):
Sqltype;
}
Public Boolean equals (Object object ){
Return object instanceof column & equals (column) object );
}
Public Boolean equals (column ){
If (null = column) return false;
If (this = column) return true;
Return isquoted ()?
Name. Equals (column. Name ):
Name. inclusignorecase (column. Name );
}
// Used also for generation of FK names!
Public int hashcode (){
Return isquoted ()?
Name. hashcode ():
Name. tolowercase (). hashcode ();
}
Public String getsqltype (){
Return sqltype;
}
Public void setsqltype (string sqltype ){
This. sqltype = sqltype;
}
Public void setunique (Boolean unique ){
This. Unique = unique;
}
Public Boolean isquoted (){
Return quoted;
}
Public String tostring (){
Return getclass (). getname () + '(' + getname () + ')';
}
Public String getcheckconstraint (){
Return checkconstraint;
}
Public void setcheckconstraint (string checkconstraint ){
This. checkconstraint = checkconstraint;
}
Public Boolean hascheckconstraint (){
Return checkconstraint! = NULL;
}
Public String gettemplate (dialect ){
Return getquotedname (dialect );
}
Public Boolean isformula (){
Return false;
}
Public String gettext (dialect d ){
Return getquotedname (d );
}
Public String gettext (){
Return getname ();
}
Public int getprecision (){
Return precision;
}
Public void setprecision (INT scale ){
This. Precision = scale;
}
Public int getscale (){
Return scale;
}
Public void setscale (INT scale ){
This. Scale = scale;
}
Public String getcomment (){
Return comment;
}
Public void setcomment (string comment ){
This. Comment = comment;
}
Public String getdefaultvalue (){
Return defaultvalue;
}
Public void setdefaultvalue (string defaultvalue ){
This. defaultvalue = defaultvalue;
}
Public String getcanonicalname (){
Return quoted? Name: Name. tolowercase ();
}
/**
* Shallow copy, the value is not copied
*/
Protected object clone (){
Column copy = new column ();
Copy. setlength (length );
Copy. setscale (scale );
Copy. setvalue (value );
Copy. settypeindex (typeindex );
Copy. setname (getquotedname ());
Copy. setnullable (nullable );
Copy. setprecision (precision );
Copy. setunique (unique );
Copy. setsqltype (sqltype );
Copy. setsqltypecode (sqltypecode );
Copy. uniqueinteger = uniqueinteger; // usually useless
Copy. setcheckconstraint (checkconstraint );
Copy. setcomment (comment );
Copy. setdefaultvalue (defaultvalue );
Return copy;
}
}