summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2007-08-05 20:40:21 +0000
committerBrian S. O'Neill <bronee@gmail.com>2007-08-05 20:40:21 +0000
commit2e3ff3f65bbc9bd762d56cdf47d079e2c05286ba (patch)
tree1c933fb989e5ac474113d491f109bbba0183a79b /src/main/java
parentd1f058804bd5dd1c18e95d94e3f90aff112216d0 (diff)
Simplified process for generating table aliases.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java36
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/TableAliasGenerator.java37
2 files changed, 55 insertions, 18 deletions
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<S extends Storable> extends StandardQueryFactory<S>
implements Storage<S>, JDBCSupport<S>
{
- 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<S extends Storable> extends StandardQueryFactory<S>
public QueryExecutor<S> executor(Filter<S> filter, OrderingList<S> 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<S extends Storable> extends StandardQueryFactory<S>
}
}
- /**
- * @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<S extends Storable> extends StandardQueryFactory<S>
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<S extends Storable> extends StandardQueryFactory<S>
* Filter visitor that constructs a JoinNode tree.
*/
private class JoinNodeBuilder extends Visitor<S, Object, Object> {
- 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<S extends Storable> extends StandardQueryFactory<S>
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<S extends Storable> extends StandardQueryFactory<S>
private void visit(PropertyFilter<S> filter) throws RepositoryException {
ChainedProperty<S> 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);
+ }
+}