KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > StatementText


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: StatementText.java,v 1.5 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import java.sql.Connection JavaDoc;
15 import java.sql.PreparedStatement JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23 import javax.jdo.JDOFatalInternalException;
24 import org.apache.log4j.Category;
25
26
27 class StatementText
28 {
29     private static final Category LOG = Category.getInstance(StatementText.class);
30
31     private static int nextParamID = 0;
32
33     private StringBuffer JavaDoc sql;
34     private ArrayList JavaDoc parameterNames = null;
35     private HashMap JavaDoc parameterMappingsByName = null;
36     private HashMap JavaDoc parameterValuesByName = null;
37     private HashSet JavaDoc referencedColumns = null;
38
39
40     public StatementText()
41     {
42         sql = new StringBuffer JavaDoc();
43     }
44
45     public StatementText(String JavaDoc initialSql)
46     {
47         sql = new StringBuffer JavaDoc(initialSql);
48     }
49
50     private void initParameters()
51     {
52         if (parameterNames == null)
53         {
54             parameterNames = new ArrayList JavaDoc();
55             parameterMappingsByName = new HashMap JavaDoc();
56             parameterValuesByName = new HashMap JavaDoc();
57         }
58     }
59
60     private void initReferencedColumns()
61     {
62         if (referencedColumns == null)
63             referencedColumns = new HashSet JavaDoc();
64     }
65
66     public Set JavaDoc getReferencedColumns()
67     {
68         if (referencedColumns == null)
69             return Collections.EMPTY_SET;
70         else
71             return Collections.unmodifiableSet(referencedColumns);
72     }
73
74     public StatementText prepend(char c)
75     {
76         sql.insert(0, c);
77         return this;
78     }
79
80     public StatementText prepend(String JavaDoc s)
81     {
82         sql.insert(0, s);
83         return this;
84     }
85
86     public StatementText append(char c)
87     {
88         sql.append(c);
89         return this;
90     }
91
92     public StatementText append(String JavaDoc s)
93     {
94         sql.append(s);
95         return this;
96     }
97
98     public StatementText append(QueryStatement.QueryColumn qsc)
99     {
100         sql.append(qsc);
101
102         initReferencedColumns();
103         referencedColumns.add(qsc);
104         return this;
105     }
106
107     public StatementText append(StatementText st)
108     {
109         sql.append(st);
110
111         if (st.parameterNames != null)
112         {
113             initParameters();
114             parameterNames.addAll(st.parameterNames);
115             parameterMappingsByName.putAll(st.parameterMappingsByName);
116             parameterValuesByName.putAll(st.parameterValuesByName);
117         }
118
119         if (st.referencedColumns != null)
120         {
121             initReferencedColumns();
122             referencedColumns.addAll(st.referencedColumns);
123         }
124
125         return this;
126     }
127
128     public StatementText append(SQLExpression expr)
129     {
130         return append(expr.toStatementText());
131     }
132
133     public StatementText append(Object JavaDoc o)
134     {
135         sql.append(o);
136         return this;
137     }
138
139     public String JavaDoc appendParameter(ColumnMapping mapping, Object JavaDoc value)
140     {
141         String JavaDoc name = "param-" + nextParamID++;
142
143         sql.append('?');
144
145         initParameters();
146         parameterNames.add(name);
147         parameterMappingsByName.put(name, mapping);
148         parameterValuesByName.put(name, value);
149
150         return name;
151     }
152
153     public void setParameterValue(String JavaDoc name, Object JavaDoc value)
154     {
155         initParameters();
156
157         if (!parameterValuesByName.containsKey(name))
158             throw new JDOFatalInternalException("No such statement parameter: " + name);
159
160         parameterValuesByName.put(name, value);
161     }
162
163     public PreparedStatement JavaDoc prepareStatement(PersistenceManager pm,
164                                               Connection JavaDoc conn) throws SQLException JavaDoc
165     {
166         String JavaDoc stmtText = toString();
167
168         LOG.debug(stmtText);
169
170         PreparedStatement JavaDoc ps = conn.prepareStatement(stmtText);
171         boolean done = false;
172
173         try
174         {
175             setParameters(pm, ps);
176             done = true;
177         }
178         finally
179         {
180             if (!done)
181                 ps.close();
182         }
183
184         return ps;
185     }
186
187     public PreparedStatement JavaDoc prepareStatement(PersistenceManager pm,
188                                               Connection JavaDoc conn,
189                                               int resultSetType,
190                                               int resultSetConcurrency) throws SQLException JavaDoc
191     {
192         String JavaDoc stmtText = toString();
193
194         LOG.debug(stmtText);
195
196         PreparedStatement JavaDoc ps = conn.prepareStatement(stmtText, resultSetType, resultSetConcurrency);
197         boolean done = false;
198
199         try
200         {
201             setParameters(pm, ps);
202             done = true;
203         }
204         finally
205         {
206             if (!done)
207                 ps.close();
208         }
209
210         return ps;
211     }
212     
213     private void setParameters(PersistenceManager pm, PreparedStatement JavaDoc ps)
214     {
215         if (parameterNames != null)
216         {
217             Iterator JavaDoc i = parameterNames.iterator();
218             int stmtParamNum = 1;
219
220             while (i.hasNext())
221             {
222                 String JavaDoc name = (String JavaDoc)i.next();
223                 ColumnMapping mapping = (ColumnMapping)parameterMappingsByName.get(name);
224                 Object JavaDoc value = parameterValuesByName.get(name);
225
226                 mapping.setObject(pm, ps, stmtParamNum++, value);
227             }
228         }
229     }
230
231     public String JavaDoc toString()
232     {
233         return sql.toString();
234     }
235 }
236
Popular Tags