mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Add static getTableName(Class) and instance.tableName() methods.
This commit is contained in:
@ -583,4 +583,31 @@ public abstract class QRecordEntity
|
|||||||
return (null);
|
return (null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***************************************************************************
|
||||||
|
**
|
||||||
|
***************************************************************************/
|
||||||
|
public static String getTableName(Class<? extends QRecordEntity> entityClass) throws QException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Field tableNameField = entityClass.getDeclaredField("TABLE_NAME");
|
||||||
|
String tableNameValue = (String) tableNameField.get(null);
|
||||||
|
return (tableNameValue);
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
throw (new QException("Could not get TABLE_NAME from entity class: " + entityClass.getSimpleName(), e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***************************************************************************
|
||||||
|
** named without the 'get' to avoid conflict w/ entity fields named that...
|
||||||
|
***************************************************************************/
|
||||||
|
public String tableName() throws QException
|
||||||
|
{
|
||||||
|
return (getTableName(this.getClass()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
@ -566,4 +567,22 @@ class QRecordEntityTest extends BaseTest
|
|||||||
assertEquals(0, order.getLineItems().size());
|
assertEquals(0, order.getLineItems().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testTableName() throws QException
|
||||||
|
{
|
||||||
|
assertEquals(Item.TABLE_NAME, QRecordEntity.getTableName(Item.class));
|
||||||
|
assertEquals(Item.TABLE_NAME, Item.getTableName(Item.class));
|
||||||
|
assertEquals(Item.TABLE_NAME, new Item().tableName());
|
||||||
|
|
||||||
|
//////////////////////////////////
|
||||||
|
// no TABLE_NAME in Order class //
|
||||||
|
//////////////////////////////////
|
||||||
|
assertThatThrownBy(() -> Order.getTableName(Order.class));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user