KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > adapter > DBPostgres


1 package org.apache.torque.adapter;
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.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import org.apache.torque.util.Query;
29
30 /**
31  * This is used to connect to PostgresQL databases.
32  *
33  * <a HREF="http://www.postgresql.org/">http://www.postgresql.org/</a>
34  *
35  * @author <a HREF="mailto:hakan42@gmx.de">Hakan Tandogan</a>
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @version $Id: DBPostgres.java 476497 2006-11-18 12:11:30Z tfischer $
38  */

39 public class DBPostgres extends AbstractDBAdapter
40 {
41     /**
42      * Serial version
43      */

44     private static final long serialVersionUID = 7643304924262475272L;
45
46     /** A specialized date format for PostgreSQL. */
47     private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
48
49     /**
50      * Empty constructor.
51      */

52     protected DBPostgres()
53     {
54     }
55
56     /**
57      * This method is used to ignore case.
58      *
59      * @param in The string to transform to upper case.
60      * @return The upper case string.
61      */

62     public String JavaDoc toUpperCase(String JavaDoc in)
63     {
64         String JavaDoc s = new StringBuffer JavaDoc("UPPER(").append(in).append(")").toString();
65         return s;
66     }
67
68     /**
69      * This method is used to ignore case.
70      *
71      * @param in The string whose case to ignore.
72      * @return The string in a case that can be ignored.
73      */

74     public String JavaDoc ignoreCase(String JavaDoc in)
75     {
76         String JavaDoc s = new StringBuffer JavaDoc("UPPER(").append(in).append(")").toString();
77         return s;
78     }
79
80     /**
81      * @see org.apache.torque.adapter.DB#getIDMethodType()
82      */

83     public String JavaDoc getIDMethodType()
84     {
85         return SEQUENCE;
86     }
87
88     /**
89      * @param name The name of the field (should be of type
90      * <code>String</code>).
91      * @return SQL to retreive the next database key.
92      * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object)
93      */

94     public String JavaDoc getIDMethodSQL(Object JavaDoc name)
95     {
96         return ("select nextval('" + name + "')");
97     }
98
99     /**
100      * Locks the specified table.
101      *
102      * @param con The JDBC connection to use.
103      * @param table The name of the table to lock.
104      * @exception SQLException No Statement could be created or executed.
105      */

106     public void lockTable(Connection JavaDoc con, String JavaDoc table) throws SQLException JavaDoc
107     {
108     }
109
110     /**
111      * Unlocks the specified table.
112      *
113      * @param con The JDBC connection to use.
114      * @param table The name of the table to unlock.
115      * @exception SQLException No Statement could be created or executed.
116      */

117     public void unlockTable(Connection JavaDoc con, String JavaDoc table) throws SQLException JavaDoc
118     {
119     }
120
121     /**
122      * This method is used to chek whether the database supports
123      * limiting the size of the resultset.
124      *
125      * @return LIMIT_STYLE_POSTGRES.
126      * @deprecated This should not be exposed to the outside
127      */

128     public int getLimitStyle()
129     {
130         return DB.LIMIT_STYLE_POSTGRES;
131     }
132
133     /**
134      * Return true for PostgreSQL
135      * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
136      */

137     public boolean supportsNativeLimit()
138     {
139         return true;
140     }
141
142     /**
143      * Return true for PostgreSQL
144      * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
145      */

146     public boolean supportsNativeOffset()
147     {
148         return true;
149     }
150
151     /**
152      * Generate a LIMIT limit OFFSET offset clause if offset &gt; 0
153      * or an LIMIT limit clause if limit is &gt; 0 and offset
154      * is 0.
155      *
156      * @param query The query to modify
157      * @param offset the offset Value
158      * @param limit the limit Value
159      */

160     public void generateLimits(Query query, int offset, int limit)
161     {
162         StringBuffer JavaDoc limitStringBuffer = new StringBuffer JavaDoc();
163
164         if (offset > 0)
165         {
166             limitStringBuffer.append(limit)
167                     .append(" offset ")
168                     .append(offset);
169         }
170         else
171         {
172             if (limit >= 0)
173             {
174                 limitStringBuffer.append(limit);
175             }
176         }
177
178         query.setLimit(limitStringBuffer.toString());
179         query.setPreLimit(null);
180         query.setPostLimit(null);
181     }
182
183     /**
184      * Override the default behavior to associate b with null?
185      *
186      * @see DB#getBooleanString(Boolean)
187      */

188     public String JavaDoc getBooleanString(Boolean JavaDoc b)
189     {
190         return (b == null) ? "FALSE" : (Boolean.TRUE.equals(b) ? "TRUE" : "FALSE");
191     }
192
193     /**
194      * This method overrides the JDBC escapes used to format dates
195      * using a <code>DateFormat</code>.
196      *
197      * This generates the timedate format defined in
198      * http://www.postgresql.org/docs/7.3/static/datatype-datetime.html
199      * which defined PostgreSQL dates as YYYY-MM-DD hh:mm:ss
200      * @param date the date to format
201      * @return The properly formatted date String.
202      */

203     public String JavaDoc getDateString(Date JavaDoc date)
204     {
205         DateFormat JavaDoc df = new SimpleDateFormat JavaDoc(DATE_FORMAT);
206         StringBuffer JavaDoc dateBuf = new StringBuffer JavaDoc();
207         char delim = getStringDelimiter();
208         dateBuf.append(delim);
209         dateBuf.append(df.format(date));
210         dateBuf.append(delim);
211         return dateBuf.toString();
212     }
213
214     /**
215      * Whether ILIKE should be used for case insensitive like clauses.
216      *
217      * As postgres uses ILIKE, this mimplementation returns true.
218      *
219      * @return true if ilike should be used for case insensitive likes,
220      * false if ignoreCase should be applied to the compared strings.
221      */

222     public boolean useIlike()
223     {
224         return true;
225     }
226 }
227
Popular Tags