use of org.datanucleus.store.rdbms.schema.RDBMSTableInfo in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method printInformation.
/**
* Method to output particular information owned by this datastore.
* Supports "DATASTORE" and "SCHEMA" categories.
* @param category Category of information
* @param ps PrintStream
* @throws Exception Thrown if an error occurs in the output process
*/
public void printInformation(String category, PrintStream ps) throws Exception {
DatastoreAdapter dba = getDatastoreAdapter();
super.printInformation(category, ps);
if (category.equalsIgnoreCase("DATASTORE")) {
ps.println(dba.toString());
ps.println();
ps.println("Database TypeInfo");
RDBMSTypesInfo typesInfo = (RDBMSTypesInfo) schemaHandler.getSchemaData(null, "types", null);
if (typesInfo != null) {
Iterator iter = typesInfo.getChildren().keySet().iterator();
while (iter.hasNext()) {
String jdbcTypeStr = (String) iter.next();
short jdbcTypeNumber = 0;
try {
jdbcTypeNumber = Short.parseShort(jdbcTypeStr);
} catch (NumberFormatException nfe) {
}
JDBCTypeInfo jdbcType = (JDBCTypeInfo) typesInfo.getChild(jdbcTypeStr);
Collection<String> sqlTypeNames = jdbcType.getChildren().keySet();
StringBuilder sqlTypesName = new StringBuilder();
String defaultSqlTypeName = null;
for (String sqlTypeName : sqlTypeNames) {
if (!sqlTypeName.equals("DEFAULT")) {
if (sqlTypesName.length() > 0) {
sqlTypesName.append(',');
}
sqlTypesName.append(sqlTypeName);
} else {
defaultSqlTypeName = ((SQLTypeInfo) jdbcType.getChild(sqlTypeName)).getTypeName();
}
}
// SQL type names for JDBC type
String typeStr = "JDBC Type=" + dba.getNameForJDBCType(jdbcTypeNumber) + " sqlTypes=" + sqlTypesName + (defaultSqlTypeName != null ? (" (default=" + defaultSqlTypeName + ")") : "");
ps.println(typeStr);
for (String sqlTypeName : sqlTypeNames) {
// SQL type details
if (!sqlTypeName.equals("DEFAULT")) {
SQLTypeInfo sqlType = (SQLTypeInfo) jdbcType.getChild(sqlTypeName);
ps.println(sqlType.toString(" "));
}
}
}
}
ps.println("");
// Print out the keywords info
ps.println("Database Keywords");
Iterator reservedWordsIter = dba.iteratorReservedWords();
while (reservedWordsIter.hasNext()) {
Object words = reservedWordsIter.next();
ps.println(words);
}
ps.println("");
} else if (category.equalsIgnoreCase("SCHEMA")) {
ps.println(dba.toString());
ps.println();
ps.println("TABLES");
ManagedConnection mc = connectionMgr.getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
RDBMSSchemaInfo schemaInfo = (RDBMSSchemaInfo) schemaHandler.getSchemaData(conn, "tables", new Object[] { this.catalogName, this.schemaName });
if (schemaInfo != null) {
Iterator tableIter = schemaInfo.getChildren().values().iterator();
while (tableIter.hasNext()) {
// Print out the table information
RDBMSTableInfo tableInfo = (RDBMSTableInfo) tableIter.next();
ps.println(tableInfo);
Iterator<StoreSchemaData> columnIter = tableInfo.getChildren().iterator();
while (columnIter.hasNext()) {
// Print out the column information
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) columnIter.next();
ps.println(colInfo);
}
}
}
} finally {
if (mc != null) {
mc.release();
}
}
ps.println("");
}
}
use of org.datanucleus.store.rdbms.schema.RDBMSTableInfo in project tests by datanucleus.
the class SchemaHandlerTest method testColumnRetrieval.
/**
* Test of the retrieval of columns.
*/
public void testColumnRetrieval() {
addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
PersistenceManager pm = pmf.getPersistenceManager();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
// Retrieve and check the table for SchemaClass1
DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
RDBMSTableInfo tableInfo1 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table1 });
assertNotNull("TableInfo from getColumns is NULL!", tableInfo1);
assertEquals("Number of columns for table " + table1 + " is wrong", 4, tableInfo1.getNumberOfChildren());
Iterator colsIter = tableInfo1.getChildren().iterator();
Collection colNamesPresent = new HashSet();
colNamesPresent.add("TABLE1_ID1");
colNamesPresent.add("TABLE1_ID2");
colNamesPresent.add("NAME");
colNamesPresent.add("OTHER_ID");
while (colsIter.hasNext()) {
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
String colInfoName = colInfo.getColumnName().toUpperCase();
if (colInfoName.equals("TABLE1_ID1") || colInfoName.equals("TABLE1_ID2") || colInfoName.equals("NAME") || colInfoName.equals("OTHER_ID")) {
colNamesPresent.remove(colInfoName);
}
}
assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
// Retrieve and check the table for SchemaClass2
DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
RDBMSTableInfo tableInfo2 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table2 });
assertEquals("Number of columns for table " + table2 + " is wrong", 3, tableInfo2.getNumberOfChildren());
colsIter = tableInfo2.getChildren().iterator();
colNamesPresent.clear();
colNamesPresent.add("TABLE2_ID");
colNamesPresent.add("NAME");
colNamesPresent.add("VALUE");
while (colsIter.hasNext()) {
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
String colInfoName = colInfo.getColumnName().toUpperCase();
if (colInfoName.equals("TABLE2_ID")) {
colNamesPresent.remove(colInfoName);
}
if (colInfoName.equals("NAME")) {
colNamesPresent.remove(colInfoName);
assertEquals("Length of column " + colInfo.getColumnName() + " has incorrect length", 20, colInfo.getColumnSize());
}
if (colInfoName.equals("VALUE")) {
colNamesPresent.remove(colInfoName);
}
}
assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
// Now check retrieval of a column for a table
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "VALUE" });
if (colInfo == null) {
colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "value" });
}
assertNotNull("Column VALUE for table " + table2 + " was not found", colInfo);
assertEquals("Column name is wrong", "VALUE", colInfo.getColumnName().toUpperCase());
}
use of org.datanucleus.store.rdbms.schema.RDBMSTableInfo in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method getColumnInfoForTable.
/**
* Returns the column info for a database table. This should be used instead
* of making direct calls to DatabaseMetaData.getColumns().
* <p>
* Where possible, this method loads and caches column info for more than
* just the table being requested, improving performance by reducing the
* overall number of calls made to DatabaseMetaData.getColumns() (each of
* which usually results in one or more database queries).
* </p>
* @param table The table/view
* @param conn JDBC connection to the database.
* @return A list of ColumnInfo objects describing the columns of the table.
* The list is in the same order as was supplied by getColumns(). If no
* column info is found for the given table, an empty list is returned.
* @throws SQLException Thrown if an error occurs
*/
public List<StoreSchemaData> getColumnInfoForTable(Table table, Connection conn) throws SQLException {
RDBMSTableInfo tableInfo = (RDBMSTableInfo) schemaHandler.getSchemaData(conn, "columns", new Object[] { table });
if (tableInfo == null) {
return Collections.EMPTY_LIST;
}
List<StoreSchemaData> cols = new ArrayList(tableInfo.getNumberOfChildren());
cols.addAll(tableInfo.getChildren());
return cols;
}
Aggregations