1 package org.apache.torque.criteria;
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 java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.lang.builder.EqualsBuilder;
29 import org.apache.commons.lang.builder.HashCodeBuilder;
30 /**
31 * The rendered SQL for a part of a prepared statement.
32 *
33 * @version $Id: PreparedStatementPart.java 1351125 2012-06-17 16:51:03Z tv $
34 */
35 public class PreparedStatementPart implements Serializable
36 {
37 /** Version id for serializing. */
38 private static final long serialVersionUID = 1L;
39
40 /**
41 * The SQL for the part, not null.
42 */
43 private final StringBuilder sql = new StringBuilder();
44
45 /**
46 * The replacements for the prepared statement, not null.
47 */
48 private final List<Object> preparedStatementReplacements
49 = new ArrayList<Object>();
50
51 /**
52 * Default constructor, creates an empty PreparedStatementPart.
53 */
54 public PreparedStatementPart()
55 {
56 // empty
57 }
58
59 /**
60 * Constructor, creates a pre-filled PreparedStatementPart.
61 *
62 * @param sql The sql to fill into the sql buffer initially, or null.
63 * @param preparedStatementReplacements the prepared statement replacements
64 * to start with, or null.
65 */
66 public PreparedStatementPart(
67 String sql,
68 Object... preparedStatementReplacements)
69 {
70 if (!StringUtils.isEmpty(sql))
71 {
72 this.sql.append(sql);
73 }
74 if (preparedStatementReplacements != null)
75 {
76 this.preparedStatementReplacements.addAll(
77 Arrays.asList(preparedStatementReplacements));
78 }
79 }
80
81 /**
82 * Returns the SQL of the part.
83 *
84 * @return the SQL as mutable StringBuilder, not null.
85 */
86 public StringBuilder getSql()
87 {
88 return sql;
89 }
90
91 /**
92 * Returns the SQL of the part as String.
93 *
94 * @return the SQL, not null.
95 */
96 public String getSqlAsString()
97 {
98 return sql.toString();
99 }
100
101 /**
102 * Returns the list of prepared statement replacements.
103 *
104 * @return the modifiable list of prepared statement replacements, not null.
105 */
106 public List<Object> getPreparedStatementReplacements()
107 {
108 return preparedStatementReplacements;
109 }
110
111 /**
112 * Appends another PreparedStatementPart to this part.
113 *
114 * @param toAppend the part to append, not null.
115 *
116 * @return this PreparedStatementPart (with toAppend appended).
117 */
118 public PreparedStatementPart append(PreparedStatementPart toAppend)
119 {
120 sql.append(toAppend.sql);
121 preparedStatementReplacements.addAll(
122 toAppend.preparedStatementReplacements);
123 return this;
124 }
125
126 /**
127 * Appends a SqlEnum to this part.
128 *
129 * @param toAppend the part to append, not null.
130 *
131 * @return this PreparedStatementPart (with toAppend appended).
132 */
133 public PreparedStatementPart append(SqlEnum toAppend)
134 {
135 sql.append(toAppend);
136 return this;
137 }
138
139 @Override
140 public int hashCode()
141 {
142 HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
143 hashCodeBuilder.append(sql);
144 hashCodeBuilder.append(preparedStatementReplacements);
145 return hashCodeBuilder.toHashCode();
146 }
147
148 @Override
149 public boolean equals(Object obj)
150 {
151 if (this == obj)
152 {
153 return true;
154 }
155 if (obj == null)
156 {
157 return false;
158 }
159 if (getClass() != obj.getClass())
160 {
161 return false;
162 }
163 PreparedStatementPart other = (PreparedStatementPart) obj;
164 EqualsBuilder equalsBuilder = new EqualsBuilder();
165 equalsBuilder.append(other.sql, this.sql);
166 equalsBuilder.append(
167 other.preparedStatementReplacements,
168 this.preparedStatementReplacements);
169 return equalsBuilder.isEquals();
170 }
171
172 @Override
173 public String toString()
174 {
175 return sql + ", preparedStatementReplacements="
176 + preparedStatementReplacements;
177 }
178 }