KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > SQLObjectSource


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util;
37
38 import java.util.*;
39 import java.util.regex.*;
40 import java.sql.*;
41 import com.micronova.util.*;
42
43 /** ObjectSource for SQL query template rendering */
44
45 public class SQLObjectSource implements ObjectSource
46 {
47     protected Connection _connection;
48     protected List _list;
49     protected ObjectSource _objectSource;
50
51     public SQLObjectSource(Connection connection, ObjectSource objectSource, List list)
52     {
53         _connection = connection;
54         _list = list;
55         _objectSource = objectSource;
56     }
57
58     public SQLObjectSource(Connection connection, ObjectSource objectSource)
59     {
60         this(connection, objectSource, new ArrayList());
61     }
62
63     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
64     {
65         try
66         {
67             Object JavaDoc replacement = _objectSource.getObject(this, key);
68             
69             if (replacement != null)
70             {
71                 // expand into "?,?,?,..." if replacement is a List
72

73                 List list = TypeUtil.isList(replacement);
74                 
75                 if (list != null)
76                 {
77                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
78                     String JavaDoc separator = "";
79                     
80                     Iterator iterator = list.iterator();
81                     
82                     while (iterator.hasNext())
83                     {
84                         _list.add(iterator.next());
85
86                         buffer.append(separator);
87                         buffer.append("?");
88                     
89                         separator = ",";
90                     }
91                     
92                     return buffer.toString();
93                 }
94             }
95             
96             _list.add(replacement);
97             return "?";
98         }
99         catch (Exception JavaDoc e)
100         {
101             throw new RuntimeException JavaDoc(e);
102         }
103     }
104
105     public PreparedStatement apply(PreparedStatement preparedStatement) throws Exception JavaDoc
106     {
107         List list = _list;
108
109         for (int i = 0; i < list.size(); i ++)
110         {
111             Object JavaDoc replacement = list.get(i);
112             
113             if (replacement != null)
114             {
115                 if (replacement instanceof java.util.Date JavaDoc)
116                 {
117                     replacement = new java.sql.Timestamp JavaDoc(((java.util.Date JavaDoc)replacement).getTime());
118                 }
119
120                 preparedStatement.setObject(i + 1, replacement);
121             }
122             else
123             {
124                 preparedStatement.setNull(i + 1, Types.CHAR);
125             }
126         }
127         
128         return preparedStatement;
129     }
130
131     public PreparedStatement render(String JavaDoc query, Pattern pattern, boolean isRecursive, boolean doesAllowGroup) throws Exception JavaDoc
132     {
133         String JavaDoc renderedQuery = (String JavaDoc)Template.render(query, pattern, 1, this, this, isRecursive, doesAllowGroup);
134
135         return apply(_connection.prepareStatement(renderedQuery));
136     }
137
138     public PreparedStatement render(String JavaDoc query, Pattern pattern) throws Exception JavaDoc
139     {
140         return render(query, pattern, false, false);
141     }
142
143     public static PreparedStatement prepareStatement(Connection connection, String JavaDoc query, Pattern pattern, ObjectSource objectSource) throws Exception JavaDoc
144     {
145         return prepareStatement(connection, query, pattern, objectSource, false, false);
146     }
147
148     public static PreparedStatement prepareStatement(Connection connection, String JavaDoc query, Pattern pattern, ObjectSource objectSource, boolean isRecursive, boolean doesAllowGroup) throws Exception JavaDoc
149     {
150         SQLObjectSource sqlObjectSource = new SQLObjectSource(connection, objectSource);
151
152         return sqlObjectSource.render(query, pattern, isRecursive, doesAllowGroup);
153     }
154 }
155
156
157
Popular Tags