View Javadoc

1   package org.apache.torque.sql.objectbuilder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.torque.Column;
23  import org.apache.torque.TorqueException;
24  import org.apache.torque.adapter.Adapter;
25  import org.apache.torque.criteria.Criteria;
26  import org.apache.torque.criteria.PreparedStatementPart;
27  import org.apache.torque.criteria.SqlEnum;
28  import org.apache.torque.om.ObjectKey;
29  import org.apache.torque.sql.Query;
30  import org.apache.torque.sql.SqlBuilder;
31  
32  /**
33   * Builds a PreparedStatementPart from a column or single value.
34   *
35   * @version $Id: ObjectOrColumnPsPartBuilder.java 1355228 2012-06-29 03:38:08Z tfischer $
36   */
37  public class ObjectOrColumnPsPartBuilder implements ObjectPsPartBuilder
38  {
39      /**
40       * Builds a PreparedStatementPart from a column or single value.
41       *
42       * @param toBuildFrom the object to build the psPart from.
43       * @param ignoreCase If true and columns represent Strings, the appropriate
44       *        function defined for the database will be used to ignore
45       *        differences in case.
46       * @param adapter The adapter for the database for which the SQL
47       *        should be created, not null.
48       *
49       * @return the PreparedStatementPart for the object.
50       *
51       * @throws TorqueException when rendering fails.
52       */
53      public PreparedStatementPart buildPs(
54              Object toBuildFrom,
55              boolean ignoreCase,
56              Adapter adapter)
57          throws TorqueException
58      {
59          PreparedStatementPart result = new PreparedStatementPart();
60          if (toBuildFrom instanceof Column)
61          {
62              Column column = (Column) toBuildFrom;
63              if (ignoreCase)
64              {
65                  result.getSql().append(
66                          adapter.ignoreCase(column.getSqlExpression()));
67              }
68              else
69              {
70                  result.getSql().append(column.getSqlExpression());
71              }
72              return result;
73          }
74  
75          // plain object
76  
77          if (toBuildFrom instanceof Criteria)
78          {
79              Query subquery = SqlBuilder.buildQuery(
80                      (Criteria) toBuildFrom);
81              result.getPreparedStatementReplacements().addAll(
82                      subquery.getPreparedStatementReplacements());
83              result.getSql().append("(").append(subquery.toString()).append(")");
84              return result;
85          }
86  
87          if (toBuildFrom instanceof org.apache.torque.util.Criteria)
88          {
89              Query subquery = SqlBuilder.buildQuery(
90                      (org.apache.torque.util.Criteria) toBuildFrom);
91              result.getPreparedStatementReplacements().addAll(
92                      subquery.getPreparedStatementReplacements());
93              result.getSql().append("(").append(subquery.toString()).append(")");
94              return result;
95          }
96  
97          if (toBuildFrom.equals(
98                  SqlEnum.CURRENT_DATE)
99                  || toBuildFrom.equals(
100                         SqlEnum.CURRENT_TIME)
101                 || toBuildFrom.equals(
102                         SqlEnum.CURRENT_TIMESTAMP))
103         {
104             result.getSql().append(toBuildFrom.toString());
105             return result;
106         }
107         // If rValue is an ObjectKey, take the value of that ObjectKey.
108         if (toBuildFrom instanceof ObjectKey)
109         {
110             toBuildFrom = ((ObjectKey) toBuildFrom).getValue();
111         }
112 
113         // handle ignoreCase
114         if (ignoreCase && toBuildFrom instanceof String)
115         {
116             result.getSql().append(adapter.ignoreCase("?"));
117         }
118         else
119         {
120             result.getSql().append("?");
121         }
122         result.getPreparedStatementReplacements().add(toBuildFrom);
123         return result;
124     }
125 }