| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.type.impl; |
| 20 | |
| 21 | import java.util.*; |
| 22 | import hu.netmind.beankeeper.parser.*; |
| 23 | import hu.netmind.beankeeper.common.StoreException; |
| 24 | import hu.netmind.beankeeper.model.*; |
| 25 | import hu.netmind.beankeeper.transaction.Transaction; |
| 26 | import hu.netmind.beankeeper.transaction.TransactionTracker; |
| 27 | import hu.netmind.beankeeper.db.Database; |
| 28 | import hu.netmind.beankeeper.schema.SchemaManager; |
| 29 | |
| 30 | /** |
| 31 | * Map implementation with LazyList backing. |
| 32 | * @author Brautigam Robert |
| 33 | * @version Revision: $Revision$ |
| 34 | */ |
| 35 | public class MapHandler extends ContainerHandler |
| 36 | { |
| 37 | private TransactionTracker transactionTracker = null; // Injected |
| 38 | private Database database = null; // Injected |
| 39 | private ClassTracker classTracker = null; // Injected |
| 40 | private SchemaManager schemaManager = null; // Injected |
| 41 | |
| 42 | public Class getContainerClass() |
| 43 | { |
| 44 | return MapImpl.class; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Create the subtable. |
| 49 | */ |
| 50 | public void ensureTableExists(ClassInfo parentInfo, String attributeName, boolean create) |
| 51 | { |
| 52 | Transaction tx = transactionTracker.getTransaction(TransactionTracker.TX_REQUIRED); |
| 53 | tx.begin(); |
| 54 | try |
| 55 | { |
| 56 | // Ensure map helper table |
| 57 | HashMap mapAttributeTypes = new HashMap(); |
| 58 | mapAttributeTypes.put("persistence_id",Long.class); |
| 59 | mapAttributeTypes.put("persistence_start",Long.class); |
| 60 | mapAttributeTypes.put("persistence_end",Long.class); |
| 61 | mapAttributeTypes.put("persistence_txstartid",Long.class); |
| 62 | mapAttributeTypes.put("persistence_txstart",Long.class); |
| 63 | mapAttributeTypes.put("persistence_txendid",Long.class); |
| 64 | mapAttributeTypes.put("persistence_txend",Long.class); |
| 65 | mapAttributeTypes.put("container_key",String.class); |
| 66 | mapAttributeTypes.put("value",Long.class); |
| 67 | ArrayList mapKeys = new ArrayList(); |
| 68 | mapKeys.add("persistence_id"); |
| 69 | mapKeys.add("persistence_txstart"); |
| 70 | mapKeys.add("container_key"); |
| 71 | database.ensureTable( |
| 72 | tx,schemaManager.getTableName( |
| 73 | parentInfo.getAttributeClassEntry(attributeName),attributeName), |
| 74 | mapAttributeTypes,mapKeys,create); |
| 75 | } catch ( StoreException e ) { |
| 76 | tx.markRollbackOnly(); |
| 77 | throw e; |
| 78 | } catch ( Throwable e ) { |
| 79 | tx.markRollbackOnly(); |
| 80 | throw new StoreException("Unknown exception",e); |
| 81 | } finally { |
| 82 | tx.commit(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create the approriate symbol entry when parsing a query. |
| 88 | */ |
| 89 | public WhereResolver.SymbolTableEntry getSymbolEntry(AttributeSpecifier spec, |
| 90 | WhereResolver.SymbolTableEntry previousEntry, ClassInfo previousInfo, |
| 91 | ReferenceTerm previousTerm) |
| 92 | { |
| 93 | String attributeName = spec.getIdentifier(); |
| 94 | // Create entry |
| 95 | WhereResolver.SymbolTableEntry entry = new WhereResolver.SymbolTableEntry(); |
| 96 | String alias = null; |
| 97 | if ( spec.getKeyname() != null ) |
| 98 | { |
| 99 | // If a specific map object was specified, then this is to |
| 100 | // be remembered. |
| 101 | alias=spec.getIdentifier()+"["+spec.getKeyname()+"]"; |
| 102 | } |
| 103 | entry.specifiedTerm = new SpecifiedTableTerm( |
| 104 | schemaManager.getTableName( |
| 105 | previousInfo.getAttributeClassEntry(attributeName),attributeName),alias); |
| 106 | entry.automatic = true; |
| 107 | entry.type = WhereResolver.SymbolTableEntry.TYPE_HANDLED; |
| 108 | entry.referenceColumn="value"; |
| 109 | // Create expression. |
| 110 | Expression connectorExpression = new Expression(); |
| 111 | ReferenceTerm leftTerm1 = previousTerm; |
| 112 | if ( previousEntry.automatic ) |
| 113 | previousEntry.termList.add(leftTerm1); |
| 114 | ReferenceTerm rightTerm1 = new ReferenceTerm(entry.specifiedTerm,"persistence_id"); |
| 115 | entry.termList.add(rightTerm1); |
| 116 | connectorExpression.add(leftTerm1); |
| 117 | connectorExpression.add("="); |
| 118 | connectorExpression.add(rightTerm1); |
| 119 | // This subexpression is only important, if |
| 120 | // there are more attributes to follow |
| 121 | if ( (spec.getKeyname()!=null) && (!"".equals(spec.getKeyname())) ) |
| 122 | { |
| 123 | connectorExpression.add("and"); |
| 124 | ReferenceTerm leftTerm2 = new ReferenceTerm(entry.specifiedTerm,"container_key"); |
| 125 | entry.termList.add(leftTerm2); |
| 126 | ConstantTerm rightTerm2 = new ConstantTerm(spec.getKeyname()); |
| 127 | connectorExpression.add(leftTerm2); |
| 128 | connectorExpression.add("="); |
| 129 | connectorExpression.add(rightTerm2); |
| 130 | } |
| 131 | // Expression |
| 132 | entry.expression=connectorExpression; |
| 133 | // Return entry |
| 134 | return entry; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Determine the next class info after the given specifier. |
| 139 | */ |
| 140 | public ClassInfo getSymbolInfo(WhereResolver.SymbolTableEntry entry, |
| 141 | AttributeSpecifier spec) |
| 142 | throws ParserException |
| 143 | { |
| 144 | if ( spec.getClassName() == null ) |
| 145 | throw new ParserException(ParserException.SYMBOL_ERROR,"class was not given after map reference"); |
| 146 | ClassInfo previousInfo = classTracker.getClassInfo( |
| 147 | classTracker.getMatchingClassEntry(spec.getClassName())); |
| 148 | if ( previousInfo == null ) |
| 149 | throw new ParserException(ParserException.SYMBOL_ERROR,"could not find class to map item classname: "+spec.getClassName()); |
| 150 | return previousInfo; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |