KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > EJBQLMultiColumnOperand


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.access.jdbc;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.cayenne.ejbql.EJBQLException;
25
26 /**
27  * A holder of multi-column match condition operand.
28  *
29  * @author Andrus Adamchik
30  * @since 3.0
31  */

32 abstract class EJBQLMultiColumnOperand {
33
34     static EJBQLMultiColumnOperand getPathOperand(
35             EJBQLTranslationContext context,
36             Map JavaDoc pathMap) {
37         return new PathMultiColumnOperand(context, pathMap);
38     }
39
40     static EJBQLMultiColumnOperand getObjectOperand(
41             EJBQLTranslationContext context,
42             Map JavaDoc object) {
43         return new ObjectMultiColumnOperand(context, object);
44     }
45
46     EJBQLTranslationContext context;
47     Map JavaDoc map;
48
49     Collection JavaDoc getKeys() {
50         return map.keySet();
51     }
52
53     abstract void appendValue(Object JavaDoc key);
54
55     private static class PathMultiColumnOperand extends EJBQLMultiColumnOperand {
56
57         PathMultiColumnOperand(EJBQLTranslationContext context, Map JavaDoc pathMap) {
58             this.context = context;
59             this.map = pathMap;
60         }
61
62         void appendValue(Object JavaDoc key) {
63
64             Object JavaDoc column = map.get(key);
65             if (column == null) {
66                 throw new EJBQLException("Invalid match path, no match for ID column "
67                         + key);
68             }
69
70             context.append(' ').append(column.toString());
71         }
72     }
73
74     private static class ObjectMultiColumnOperand extends EJBQLMultiColumnOperand {
75
76         ObjectMultiColumnOperand(EJBQLTranslationContext context, Map JavaDoc pathMap) {
77             this.context = context;
78             this.map = pathMap;
79         }
80
81         void appendValue(Object JavaDoc key) {
82             Object JavaDoc value = map.get(key);
83             if (value == null) {
84                 throw new EJBQLException("Invalid object, no match for ID column " + key);
85             }
86
87             String JavaDoc var = context.bindParameter(value);
88             context.append(" #bind($").append(var).append(')');
89         }
90     }
91 }
92
Popular Tags