Return-Path: Delivered-To: apmail-db-ojb-dev-archive@www.apache.org Received: (qmail 28172 invoked from network); 23 Jul 2004 07:32:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 23 Jul 2004 07:32:00 -0000 Received: (qmail 2938 invoked by uid 500); 23 Jul 2004 07:31:58 -0000 Delivered-To: apmail-db-ojb-dev-archive@db.apache.org Received: (qmail 2909 invoked by uid 500); 23 Jul 2004 07:31:58 -0000 Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "OJB Developers List" Reply-To: "OJB Developers List" Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 2893 invoked by uid 500); 23 Jul 2004 07:31:58 -0000 Received: (qmail 2880 invoked by uid 99); 23 Jul 2004 07:31:58 -0000 X-ASF-Spam-Status: No, hits=0.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Fri, 23 Jul 2004 00:31:57 -0700 Received: (qmail 28119 invoked by uid 1797); 23 Jul 2004 07:31:56 -0000 Date: 23 Jul 2004 07:31:56 -0000 Message-ID: <20040723073156.28118.qmail@minotaur.apache.org> From: tomdz@apache.org To: db-ojb-cvs@apache.org Subject: cvs commit: db-ojb/src/schema ojbtest-data-new.xml X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N tomdz 2004/07/23 00:31:56 Modified: src/java/org/apache/ojb/broker/util/dbhandling PreparedModel.java . build.xml Added: src/schema ojbtest-data-new.xml Log: Fixed indirection table handling in the common-sql db-handling. First version of unit test data adapted to the common-sql db-handling. Revision Changes Path 1.2 +77 -70 db-ojb/src/java/org/apache/ojb/broker/util/dbhandling/PreparedModel.java Index: PreparedModel.java =================================================================== RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/dbhandling/PreparedModel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- PreparedModel.java 22 Jul 2004 07:48:52 -0000 1.1 +++ PreparedModel.java 23 Jul 2004 07:31:56 -0000 1.2 @@ -15,9 +15,7 @@ * limitations under the License. */ -import java.util.HashMap; -import java.util.Iterator; -import java.util.TreeMap; +import java.util.*; import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Database; @@ -38,8 +36,6 @@ private TreeMap _elementToTable = new TreeMap(); private HashMap _elementToColumnMap = new HashMap(); private HashMap _elementToRequiredColumnsMap = new HashMap(); - private HashMap _indirectionTableToElementName = new HashMap(); - public PreparedModel(DescriptorRepository model, Database schema) { prepareModel(model, schema); @@ -61,7 +57,16 @@ { HashMap requiredColumns = (HashMap)_elementToRequiredColumnsMap.get(elementName); - return requiredColumns == null ? false : requiredColumns.containsKey(attributeName); + if (requiredColumns == null) + { + return false; + } + else + { + Boolean status = (Boolean)requiredColumns.get(attributeName); + + return status == null ? false : status.booleanValue(); + } } public Table getTableFor(String elementName) @@ -109,8 +114,8 @@ _elementToRequiredColumnsMap.put(elementName, requiredColumnsMap); } extractAttributes(classDesc, mappedTable, columnsMap, requiredColumnsMap); - extractIndirectionTables(model, schema, classDesc); } + extractIndirectionTables(model, schema); } private void extractAttributes(ClassDescriptor classDesc, Table mappedTable, TreeMap columnsMap, HashMap requiredColumnsMap) @@ -137,96 +142,98 @@ /** * Extracts indirection tables from the given class descriptor, and adds elements - * for them. + * for them. In contrast to normal elements, for indirection tables the element name + * matches the table name, and the attribute names match the column names. * - * @param classDesc The class descriptor - * @param elements The elements + * @param model The model + * @param elements The elements */ - private void extractIndirectionTables(DescriptorRepository model, Database schema, ClassDescriptor classDesc) + private void extractIndirectionTables(DescriptorRepository model, Database schema) { - String elementName = getElementName(classDesc.getClassNameOfObject()); + HashMap indirectionTables = new HashMap(); - for (Iterator it = classDesc.getCollectionDescriptors().iterator(); it.hasNext();) + // first we gather all participants for each m:n relationship + for (Iterator classDescIt = model.getDescriptorTable().values().iterator(); classDescIt.hasNext();) { - CollectionDescriptor collDesc = (CollectionDescriptor)it.next(); - String tableName = collDesc.getIndirectionTable(); + ClassDescriptor classDesc = (ClassDescriptor)classDescIt.next(); - if ((tableName != null) && - (tableName.length() > 0)) + for (Iterator collDescIt = classDesc.getCollectionDescriptors().iterator(); collDescIt.hasNext();) { - // TODO: determine either top-most type combination or shortest name combination for - // this indirection table - String indirectionElementName = (String)_indirectionTableToElementName.get(tableName); - ClassDescriptor otherClassDesc = model.getDescriptorFor(collDesc.getItemClassName()); - String otherElementName = getElementName(otherClassDesc.getClassNameOfObject()); + CollectionDescriptor collDesc = (CollectionDescriptor)collDescIt.next(); + String indirTable = collDesc.getIndirectionTable(); - if (indirectionElementName == null) + if ((indirTable != null) && (indirTable.length() > 0)) { - if (elementName.compareTo(otherElementName) > 0) - { - // searching for collection at other end - for (Iterator otherCollIt = otherClassDesc.getCollectionDescriptors().iterator(); otherCollIt.hasNext();) - { - CollectionDescriptor otherCollDesc = (CollectionDescriptor)otherCollIt.next(); + Set columns = (Set)indirectionTables.get(indirTable); - if (tableName.equals(otherCollDesc.getIndirectionTable())) - { - indirectionElementName = otherElementName + "-" + otherCollDesc.getAttributeName() + "-" + elementName; - break; - } - } - } - if (indirectionElementName == null) + if (columns == null) { - indirectionElementName = elementName + "-" + collDesc.getAttributeName() + "-" + otherElementName; + columns = new HashSet(); + indirectionTables.put(indirTable, columns); } - _indirectionTableToElementName.put(collDesc.getIndirectionTable(), indirectionElementName); + columns.addAll(Arrays.asList(collDesc.getFksToThisClass())); + columns.addAll(Arrays.asList(collDesc.getFksToItemClass())); } + } + } + if (indirectionTables.isEmpty()) + { + // nothing to do + return; + } - Table mappedTable = (Table)_elementToTable.get(indirectionElementName); - TreeMap columnsMap = (TreeMap)_elementToColumnMap.get(indirectionElementName); - HashMap requiredColumnsMap = (HashMap)_elementToRequiredColumnsMap.get(indirectionElementName); + for (Iterator it = indirectionTables.keySet().iterator(); it.hasNext();) + { + String tableName = (String)it.next(); + Set columns = (Set)indirectionTables.get(tableName); + String elementName = tableName; - if (mappedTable == null) + for (Iterator classDescIt = model.getDescriptorTable().values().iterator(); classDescIt.hasNext();) + { + ClassDescriptor classDesc = (ClassDescriptor)classDescIt.next(); + + if (tableName.equals(classDesc.getFullTableName())) { - mappedTable = schema.findTable(collDesc.getIndirectionTable()); - if (mappedTable == null) + elementName = getElementName(classDesc.getClassNameOfObject()); + + FieldDescriptor[] fieldDescs = classDesc.getFieldDescriptions(); + + if (fieldDescs != null) { - continue; + for (int idx = 0; idx < fieldDescs.length; idx++) + { + columns.remove(fieldDescs[idx].getColumnName()); + } } - columnsMap = new TreeMap(); - requiredColumnsMap = new HashMap(); - _elementToTable.put(indirectionElementName, mappedTable); - _elementToColumnMap.put(indirectionElementName, columnsMap); - _elementToRequiredColumnsMap.put(indirectionElementName, requiredColumnsMap); } - extractIndirectionKeys(classDesc, elementName, mappedTable, columnsMap, requiredColumnsMap); - extractIndirectionKeys(otherClassDesc, otherElementName, mappedTable, columnsMap, requiredColumnsMap); } - } - } - - private void extractIndirectionKeys(ClassDescriptor classDesc, String elementName, Table mappedTable, TreeMap columnsMap, HashMap requiredColumnsMap) - { - FieldDescriptor[] fieldDescs = classDesc.getFieldDescriptions(); - if (fieldDescs != null) - { - for (int idx = 0; idx < fieldDescs.length; idx++) + Table mappedTable = (Table)_elementToTable.get(elementName); + TreeMap columnsMap = (TreeMap)_elementToColumnMap.get(elementName); + HashMap requiredColumnsMap = (HashMap)_elementToRequiredColumnsMap.get(elementName); + + if (mappedTable == null) { - if (!fieldDescs[idx].isPrimaryKey()) + mappedTable = schema.findTable(elementName); + if (mappedTable == null) { continue; } - - Column column = mappedTable.findColumn(fieldDescs[idx].getColumnName()); + columnsMap = new TreeMap(); + requiredColumnsMap = new HashMap(); + _elementToTable.put(elementName, mappedTable); + _elementToColumnMap.put(elementName, columnsMap); + _elementToRequiredColumnsMap.put(elementName, requiredColumnsMap); + } + for (Iterator columnIt = columns.iterator(); columnIt.hasNext();) + { + String columnName = (String)columnIt.next(); + Column column = mappedTable.findColumn(columnName); if (column != null) { - String shortAttrName = elementName + "-" + getShortAttributeName(fieldDescs[idx].getAttributeName()); - - columnsMap.put(shortAttrName, column); - requiredColumnsMap.put(shortAttrName, Boolean.TRUE); + columnsMap.put(columnName, column); + requiredColumnsMap.put(columnName, Boolean.TRUE); } } } @@ -240,7 +247,7 @@ */ private String getElementName(String className) { - String elementName = className.replace('$', '.'); + String elementName = className.replace('$', '_'); elementName = elementName.substring(elementName.lastIndexOf('.') + 1); return elementName; 1.147 +2 -2 db-ojb/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/db-ojb/build.xml,v retrieving revision 1.146 retrieving revision 1.147 diff -u -r1.146 -r1.147 --- build.xml 22 Jul 2004 07:48:53 -0000 1.146 +++ build.xml 23 Jul 2004 07:31:56 -0000 1.147 @@ -440,7 +440,7 @@ - + 1.1 db-ojb/src/schema/ojbtest-data-new.xml Index: ojbtest-data-new.xml ===================================================================
--------------------------------------------------------------------- To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org For additional commands, e-mail: ojb-dev-help@db.apache.org