2019-10-26 23:15:49 +02:00
package fr.pandacube.util.orm ;
2017-09-13 01:05:10 +02:00
2019-10-26 23:15:49 +02:00
import fr.pandacube.util.Log ;
2017-09-13 01:05:10 +02:00
2019-01-22 15:28:48 +01:00
/ * *
*
* @author Marc
*
* @param < F > the table class of this current foreign key field
* @param < T > the Java type of this field
* @param < P > the table class of the targeted primary key
* /
public class SQLFKField < F extends SQLElement < F > , T , P extends SQLElement < P > > extends SQLField < F , T > {
2017-09-13 01:05:10 +02:00
2019-01-22 15:28:48 +01:00
private SQLField < P , T > sqlPrimaryKeyField ;
private Class < P > sqlForeignKeyElemClass ;
2017-09-13 01:05:10 +02:00
2019-01-22 15:28:48 +01:00
protected SQLFKField ( SQLType < T > t , boolean nul , T deflt , Class < P > fkEl , SQLField < P , T > fkF ) {
2017-09-13 01:05:10 +02:00
super ( t , nul , deflt ) ;
construct ( fkEl , fkF ) ;
}
public static < E extends SQLElement < E > , F extends SQLElement < F > > SQLFKField < E , Integer , F > idFK ( boolean nul , Class < F > fkEl ) {
return idFK ( nul , null , fkEl ) ;
}
public static < E extends SQLElement < E > , F extends SQLElement < F > > SQLFKField < E , Integer , F > idFK ( boolean nul , Integer deflt , Class < F > fkEl ) {
if ( fkEl = = null ) throw new IllegalArgumentException ( " foreignKeyElement can't be null " ) ;
try {
SQLField < F , Integer > f = ORM . getSQLIdField ( fkEl ) ;
return new SQLFKField < > ( f . type , nul , deflt , fkEl , f ) ;
} catch ( ORMInitTableException e ) {
Log . severe ( " Can't create Foreign key Field targetting id field of ' " + fkEl + " ' " , e ) ;
return null ;
}
}
public static < E extends SQLElement < E > , T , F extends SQLElement < F > > SQLFKField < E , T , F > customFK ( boolean nul , Class < F > fkEl , SQLField < F , T > fkF ) {
return customFK ( nul , null , fkEl , fkF ) ;
}
public static < E extends SQLElement < E > , T , F extends SQLElement < F > > SQLFKField < E , T , F > customFK ( boolean nul , T deflt , Class < F > fkEl , SQLField < F , T > fkF ) {
if ( fkEl = = null ) throw new IllegalArgumentException ( " foreignKeyElement can't be null " ) ;
return new SQLFKField < > ( fkF . type , nul , deflt , fkEl , fkF ) ;
}
2019-01-22 15:28:48 +01:00
private void construct ( Class < P > fkEl , SQLField < P , T > fkF ) {
2017-09-13 01:05:10 +02:00
if ( fkF = = null ) throw new IllegalArgumentException ( " foreignKeyField can't be null " ) ;
try {
ORM . initTable ( fkEl ) ;
} catch ( ORMInitTableException e ) {
throw new RuntimeException ( e ) ;
}
2019-04-11 15:47:19 +02:00
if ( fkF . getSQLElementType ( ) = = null )
throw new RuntimeException ( " Can't initialize foreign key. The primary key in the table " + fkEl . getName ( ) + " is not properly initialized and can't be targetted by a forein key " ) ;
2019-01-16 11:15:42 +01:00
sqlPrimaryKeyField = fkF ;
2017-09-13 01:05:10 +02:00
sqlForeignKeyElemClass = fkEl ;
}
2019-01-22 15:28:48 +01:00
public SQLField < P , T > getPrimaryField ( ) {
2019-01-16 11:15:42 +01:00
return sqlPrimaryKeyField ;
2017-09-13 01:05:10 +02:00
}
2019-01-22 15:28:48 +01:00
public Class < P > getForeignElementClass ( ) {
2017-09-13 01:05:10 +02:00
return sqlForeignKeyElemClass ;
}
}