/** * Method to create a column info for the current row. Overrides the * dataType/columnSize/decimalDigits to cater for MSSQL particularities. * * @param rs ResultSet from DatabaseMetaData.getColumns() * @return column info */ public RDBMSColumnInfo newRDBMSColumnInfo(ResultSet rs) { RDBMSColumnInfo info = new RDBMSColumnInfo(rs); short dataType = info.getDataType(); switch (dataType) { case Types.DATE: case Types.TIME: case Types.TIMESTAMP: // Values > 0 inexplicably get returned here. info.setDecimalDigits(0); break; default: break; } return info; }
/** 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 .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}); 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(); if (colInfo.getColumnName().equals("TABLE1_ID1")) { colNamesPresent.remove(colInfo.getColumnName()); } if (colInfo.getColumnName().equals("TABLE1_ID2")) { colNamesPresent.remove(colInfo.getColumnName()); } if (colInfo.getColumnName().equals("NAME")) { colNamesPresent.remove(colInfo.getColumnName()); } if (colInfo.getColumnName().equals("OTHER_ID")) { colNamesPresent.remove(colInfo.getColumnName()); } } 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(); if (colInfo.getColumnName().equals("TABLE2_ID")) { colNamesPresent.remove(colInfo.getColumnName()); } if (colInfo.getColumnName().equals("NAME")) { colNamesPresent.remove(colInfo.getColumnName()); assertEquals( "Length of column " + colInfo.getColumnName() + " has incorrect length", 20, colInfo.getColumnSize()); } if (colInfo.getColumnName().equals("VALUE")) { colNamesPresent.remove(colInfo.getColumnName()); } } 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"}); assertNotNull("Column VALUE for table " + table2 + " was not found", colInfo); assertEquals("Column name is wrong", "VALUE", colInfo.getColumnName()); }