View Javadoc

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;
23  import java.sql.SQLException;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Date;
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 522044 2007-03-24 16:00:57Z 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 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 toUpperCase(String in)
63      {
64          String s = new StringBuffer("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 ignoreCase(String in)
75      {
76          String s = new StringBuffer("UPPER(").append(in).append(")").toString();
77          return s;
78      }
79  
80      /***
81       * @see org.apache.torque.adapter.DB#getIDMethodType()
82       */
83      public String 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 getIDMethodSQL(Object 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 con, String table) throws SQLException
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 con, String table) throws SQLException
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         if (offset > 0)
163         {
164             query.setOffset(Integer.toString(offset));
165         }
166         if (limit >= 0)
167         {
168             query.setLimit(Integer.toString(limit));
169         }
170 
171         query.setPreLimit(null);
172         query.setPostLimit(null);
173     }
174 
175     /***
176      * Override the default behavior to associate b with null?
177      *
178      * @see DB#getBooleanString(Boolean)
179      */
180     public String getBooleanString(Boolean b)
181     {
182         return (b == null) ? "FALSE" : (Boolean.TRUE.equals(b) ? "TRUE" : "FALSE");
183     }
184 
185     /***
186      * This method overrides the JDBC escapes used to format dates
187      * using a <code>DateFormat</code>.
188      *
189      * This generates the timedate format defined in
190      * http://www.postgresql.org/docs/7.3/static/datatype-datetime.html
191      * which defined PostgreSQL dates as YYYY-MM-DD hh:mm:ss
192      * @param date the date to format
193      * @return The properly formatted date String.
194      */
195     public String getDateString(Date date)
196     {
197         DateFormat df = new SimpleDateFormat(DATE_FORMAT);
198         StringBuffer dateBuf = new StringBuffer();
199         char delim = getStringDelimiter();
200         dateBuf.append(delim);
201         dateBuf.append(df.format(date));
202         dateBuf.append(delim);
203         return dateBuf.toString();
204     }
205 
206     /***
207      * Whether ILIKE should be used for case insensitive like clauses.
208      *
209      * As postgres uses ILIKE, this mimplementation returns true.
210      *
211      * @return true if ilike should be used for case insensitive likes,
212      *         false if ignoreCase should be applied to the compared strings.
213      */
214     public boolean useIlike()
215     {
216         return true;
217     }
218 }