View Javadoc

1   package org.apache.torque.adapter;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.sql.Connection;
20  import java.sql.SQLException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  
24  /***
25   * This is used to connect to PostgresQL databases.
26   *
27   * <a href="http://www.postgresql.org/">http://www.postgresql.org/</a>
28   *
29   * @author <a href="mailto:hakan42@gmx.de">Hakan Tandogan</a>
30   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
31   * @version $Id: DBPostgres.java 239636 2005-08-24 12:38:09Z henning $
32   */
33  public class DBPostgres extends DB
34  {
35  
36      /*** A specialized date format for PostgreSQL. */
37      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
38  
39      private SimpleDateFormat sdf = null;
40  
41      /***
42       * Empty constructor.
43       */
44      protected DBPostgres()
45      {
46          sdf = new SimpleDateFormat(DATE_FORMAT);
47      }
48  
49      /***
50       * This method is used to ignore case.
51       *
52       * @param in The string to transform to upper case.
53       * @return The upper case string.
54       */
55      public String toUpperCase(String in)
56      {
57          String s = new StringBuffer("UPPER(").append(in).append(")").toString();
58          return s;
59      }
60  
61      /***
62       * This method is used to ignore case.
63       *
64       * @param in The string whose case to ignore.
65       * @return The string in a case that can be ignored.
66       */
67      public String ignoreCase(String in)
68      {
69          String s = new StringBuffer("UPPER(").append(in).append(")").toString();
70          return s;
71      }
72  
73      /***
74       * @see org.apache.torque.adapter.DB#getIDMethodType()
75       */
76      public String getIDMethodType()
77      {
78          return SEQUENCE;
79      }
80  
81      /***
82       * @param name The name of the field (should be of type
83       *      <code>String</code>).
84       * @return SQL to retreive the next database key.
85       * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object)
86       */
87      public String getIDMethodSQL(Object name)
88      {
89          return ("select nextval('" + name + "')");
90      }
91  
92      /***
93       * Locks the specified table.
94       *
95       * @param con The JDBC connection to use.
96       * @param table The name of the table to lock.
97       * @exception SQLException No Statement could be created or executed.
98       */
99      public void lockTable(Connection con, String table) throws SQLException
100     {
101     }
102 
103     /***
104      * Unlocks the specified table.
105      *
106      * @param con The JDBC connection to use.
107      * @param table The name of the table to unlock.
108      * @exception SQLException No Statement could be created or executed.
109      */
110     public void unlockTable(Connection con, String table) throws SQLException
111     {
112     }
113 
114     /***
115      * This method is used to chek whether the database natively
116      * supports limiting the size of the resultset.
117      *
118      * @return True.
119      */
120     public boolean supportsNativeLimit()
121     {
122         return true;
123     }
124 
125     /***
126      * This method is used to chek whether the database natively
127      * supports returning results starting at an offset position other
128      * than 0.
129      *
130      * @return True.
131      */
132     public boolean supportsNativeOffset()
133     {
134         return true;
135     }
136 
137     /***
138      * This method is used to chek whether the database supports
139      * limiting the size of the resultset.
140      *
141      * @return LIMIT_STYLE_POSTGRES.
142      */
143     public int getLimitStyle()
144     {
145         return DB.LIMIT_STYLE_POSTGRES;
146     }
147 
148     /***
149      * Override the default behavior to associate b with null?
150      *
151      * @see DB#getBooleanString(Boolean)
152      */
153     public String getBooleanString(Boolean b)
154     {
155         return (b == null) ? "FALSE" : (Boolean.TRUE.equals(b) ? "TRUE" : "FALSE");
156     }
157 
158     /***
159      * This method overrides the JDBC escapes used to format dates
160      * using a <code>DateFormat</code>.
161      *
162      * This generates the timedate format defined in
163      * http://www.postgresql.org/docs/7.3/static/datatype-datetime.html
164      * which defined PostgreSQL dates as YYYY-MM-DD hh:mm:ss
165      * @param date the date to format
166      * @return The properly formatted date String.
167      */
168     public String getDateString(Date date)
169     {
170         StringBuffer dateBuf = new StringBuffer();
171         char delim = getStringDelimiter();
172         dateBuf.append(delim);
173         dateBuf.append(sdf.format(date));
174         dateBuf.append(delim);
175         return dateBuf.toString();
176     }
177 }