From 2e3ff3f65bbc9bd762d56cdf47d079e2c05286ba Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 5 Aug 2007 20:40:21 +0000 Subject: Simplified process for generating table aliases. --- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 36 ++++++++++----------- .../carbonado/repo/jdbc/TableAliasGenerator.java | 37 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/repo/jdbc/TableAliasGenerator.java (limited to 'src/main/java/com/amazon') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index 9d60741..384f1bc 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -76,7 +76,6 @@ import com.amazon.carbonado.util.QuickConstructorGenerator; class JDBCStorage extends StandardQueryFactory implements Storage, JDBCSupport { - private static final String TABLE_ALIAS_PREFIX = "T"; private static final int FIRST_RESULT_INDEX = 1; final JDBCRepository mRepository; @@ -298,9 +297,11 @@ class JDBCStorage extends StandardQueryFactory public QueryExecutor executor(Filter filter, OrderingList ordering) throws RepositoryException { + TableAliasGenerator aliasGenerator = new TableAliasGenerator(); + JoinNode jn; try { - JoinNodeBuilder jnb = new JoinNodeBuilder(); + JoinNodeBuilder jnb = new JoinNodeBuilder(aliasGenerator); if (filter == null) { jn = new JoinNode(getStorableInfo(), null); } else { @@ -834,21 +835,20 @@ class JDBCStorage extends StandardQueryFactory } } - /** - * @return new value for aliasCounter - */ - public int addJoin(ChainedProperty chained, int aliasCounter) + public void addJoin(ChainedProperty chained, TableAliasGenerator aliasGenerator) throws RepositoryException { - return addJoin(chained, aliasCounter, 0); + addJoin(chained, aliasGenerator, 0); } - private int addJoin(ChainedProperty chained, int aliasCounter, int offset) + private void addJoin(ChainedProperty chained, + TableAliasGenerator aliasGenerator, + int offset) throws RepositoryException { if ((chained.getChainCount() - offset) <= 0) { // At this point in the chain, there are no more joins. - return aliasCounter; + return; } StorableProperty property; if (offset == 0) { @@ -861,10 +861,10 @@ class JDBCStorage extends StandardQueryFactory if (subNode == null) { JDBCStorableInfo info = mRepository.examineStorable(property.getJoinedType()); JDBCStorableProperty jProperty = mRepository.getJDBCStorableProperty(property); - subNode = new JoinNode(jProperty, info, TABLE_ALIAS_PREFIX + (++aliasCounter)); + subNode = new JoinNode(jProperty, info, aliasGenerator.nextAlias()); mSubNodes.put(name, subNode); } - return subNode.addJoin(chained, aliasCounter, offset + 1); + subNode.addJoin(chained, aliasGenerator, offset + 1); } public String toString() { @@ -886,12 +886,12 @@ class JDBCStorage extends StandardQueryFactory * Filter visitor that constructs a JoinNode tree. */ private class JoinNodeBuilder extends Visitor { - private JoinNode mRootJoinNode; - private int mAliasCounter; + private final TableAliasGenerator mAliasGenerator; + private final JoinNode mRootJoinNode; - JoinNodeBuilder() { - mAliasCounter = 1; - mRootJoinNode = new JoinNode(getStorableInfo(), TABLE_ALIAS_PREFIX + mAliasCounter); + JoinNodeBuilder(TableAliasGenerator aliasGenerator) { + mAliasGenerator = aliasGenerator; + mRootJoinNode = new JoinNode(getStorableInfo(), aliasGenerator.nextAlias()); } public JoinNode getRootJoinNode() { @@ -909,7 +909,7 @@ class JDBCStorage extends StandardQueryFactory if (ordering != null) { for (OrderedProperty orderedProperty : ordering) { ChainedProperty chained = orderedProperty.getChainedProperty(); - mAliasCounter = mRootJoinNode.addJoin(chained, mAliasCounter); + mRootJoinNode.addJoin(chained, mAliasGenerator); } } } catch (RepositoryException e) { @@ -932,7 +932,7 @@ class JDBCStorage extends StandardQueryFactory private void visit(PropertyFilter filter) throws RepositoryException { ChainedProperty chained = filter.getChainedProperty(); - mAliasCounter = mRootJoinNode.addJoin(chained, mAliasCounter); + mRootJoinNode.addJoin(chained, mAliasGenerator); } } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/TableAliasGenerator.java b/src/main/java/com/amazon/carbonado/repo/jdbc/TableAliasGenerator.java new file mode 100644 index 0000000..5c73a2b --- /dev/null +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/TableAliasGenerator.java @@ -0,0 +1,37 @@ +/* + * Copyright 2007 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.repo.jdbc; + +/** + * Simple non-thread-safe class for generating SQL statement table aliases. + * + * @author Brian S O'Neill + */ +class TableAliasGenerator { + private static final String TABLE_ALIAS_PREFIX = "T"; + + private int mAliasCounter; + + TableAliasGenerator() { + } + + String nextAlias() { + return TABLE_ALIAS_PREFIX + (++mAliasCounter); + } +} -- cgit v1.2.3