2022-07-20 13:18:57 +02:00
package fr.pandacube.lib.db ;
2021-03-21 12:41:43 +01:00
2022-07-20 13:18:57 +02:00
import fr.pandacube.lib.util.Log ;
2021-03-21 12:41:43 +01:00
/ * *
2022-08-01 22:21:04 +02:00
* A foreign key field in a SQL table .
2021-03-21 12:41:43 +01:00
* @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 > {
2022-08-01 22:21:04 +02:00
private SQLField < P , T > sqlPrimaryKeyField ;
private Class < P > sqlForeignKeyElemClass ;
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/* package */ SQLFKField ( SQLType < T > t , boolean nul , T deflt , Class < P > fkEl , SQLField < P , T > fkF ) {
super ( t , nul , deflt ) ;
construct ( fkEl , fkF ) ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/* package */ static < E extends SQLElement < E > , F extends SQLElement < F > > SQLFKField < E , Integer , F > idFK ( boolean nul , Class < F > fkEl ) {
return idFK ( nul , null , fkEl ) ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/* package */ 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 = DB . getSQLIdField ( fkEl ) ;
return new SQLFKField < > ( f . type , nul , deflt , fkEl , f ) ;
} catch ( DBInitTableException e ) {
2023-06-20 00:15:46 +02:00
Log . severe ( " Can't create Foreign key Field targeting id field of ' " + fkEl + " ' " , e ) ;
2022-08-01 22:21:04 +02:00
return null ;
}
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/* package */ 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 ) ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/* package */ 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 ) ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
private void construct ( Class < P > fkEl , SQLField < P , T > fkF ) {
if ( fkF = = null ) throw new IllegalArgumentException ( " foreignKeyField can't be null " ) ;
try {
DB . initTable ( fkEl ) ;
} catch ( DBInitTableException e ) {
throw new RuntimeException ( e ) ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
if ( fkF . getSQLElementType ( ) = = null )
2023-06-20 00:15:46 +02:00
throw new RuntimeException ( " Can't initialize foreign key. The primary key in the table " + fkEl . getName ( ) + " is not properly initialized and can't be targeted by a foreign key " ) ;
2022-08-01 22:21:04 +02:00
sqlPrimaryKeyField = fkF ;
sqlForeignKeyElemClass = fkEl ;
}
2021-03-21 12:41:43 +01:00
2022-08-01 22:21:04 +02:00
/ * *
* Gets the targeted field of this foreign key .
* @return the targeted field of this foreign key .
* /
public SQLField < P , T > getPrimaryField ( ) {
return sqlPrimaryKeyField ;
}
/ * *
* Gets the type of the table containing the targeted field of this foreign key .
* @return the type of the table containing the targeted field of this foreign key .
* /
public Class < P > getForeignElementClass ( ) {
return sqlForeignKeyElemClass ;
}
2021-03-21 12:41:43 +01:00
}