001: package org.apache.torque.adapter;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.text.DateFormat;
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027:
028: import org.apache.torque.util.Query;
029:
030: /**
031: * This is used to connect to PostgresQL databases.
032: *
033: * <a href="http://www.postgresql.org/">http://www.postgresql.org/</a>
034: *
035: * @author <a href="mailto:hakan42@gmx.de">Hakan Tandogan</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @version $Id: DBPostgres.java 522044 2007-03-24 16:00:57Z tfischer $
038: */
039: public class DBPostgres extends AbstractDBAdapter {
040: /**
041: * Serial version
042: */
043: private static final long serialVersionUID = 7643304924262475272L;
044:
045: /** A specialized date format for PostgreSQL. */
046: private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
047:
048: /**
049: * Empty constructor.
050: */
051: protected DBPostgres() {
052: }
053:
054: /**
055: * This method is used to ignore case.
056: *
057: * @param in The string to transform to upper case.
058: * @return The upper case string.
059: */
060: public String toUpperCase(String in) {
061: String s = new StringBuffer("UPPER(").append(in).append(")")
062: .toString();
063: return s;
064: }
065:
066: /**
067: * This method is used to ignore case.
068: *
069: * @param in The string whose case to ignore.
070: * @return The string in a case that can be ignored.
071: */
072: public String ignoreCase(String in) {
073: String s = new StringBuffer("UPPER(").append(in).append(")")
074: .toString();
075: return s;
076: }
077:
078: /**
079: * @see org.apache.torque.adapter.DB#getIDMethodType()
080: */
081: public String getIDMethodType() {
082: return SEQUENCE;
083: }
084:
085: /**
086: * @param name The name of the field (should be of type
087: * <code>String</code>).
088: * @return SQL to retreive the next database key.
089: * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object)
090: */
091: public String getIDMethodSQL(Object name) {
092: return ("select nextval('" + name + "')");
093: }
094:
095: /**
096: * Locks the specified table.
097: *
098: * @param con The JDBC connection to use.
099: * @param table The name of the table to lock.
100: * @exception SQLException No Statement could be created or executed.
101: */
102: public void lockTable(Connection con, String table)
103: throws SQLException {
104: }
105:
106: /**
107: * Unlocks the specified table.
108: *
109: * @param con The JDBC connection to use.
110: * @param table The name of the table to unlock.
111: * @exception SQLException No Statement could be created or executed.
112: */
113: public void unlockTable(Connection con, String table)
114: throws SQLException {
115: }
116:
117: /**
118: * This method is used to chek whether the database supports
119: * limiting the size of the resultset.
120: *
121: * @return LIMIT_STYLE_POSTGRES.
122: * @deprecated This should not be exposed to the outside
123: */
124: public int getLimitStyle() {
125: return DB.LIMIT_STYLE_POSTGRES;
126: }
127:
128: /**
129: * Return true for PostgreSQL
130: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeLimit()
131: */
132: public boolean supportsNativeLimit() {
133: return true;
134: }
135:
136: /**
137: * Return true for PostgreSQL
138: * @see org.apache.torque.adapter.AbstractDBAdapter#supportsNativeOffset()
139: */
140: public boolean supportsNativeOffset() {
141: return true;
142: }
143:
144: /**
145: * Generate a LIMIT limit OFFSET offset clause if offset > 0
146: * or an LIMIT limit clause if limit is > 0 and offset
147: * is 0.
148: *
149: * @param query The query to modify
150: * @param offset the offset Value
151: * @param limit the limit Value
152: */
153: public void generateLimits(Query query, int offset, int limit) {
154: if (offset > 0) {
155: query.setOffset(Integer.toString(offset));
156: }
157: if (limit >= 0) {
158: query.setLimit(Integer.toString(limit));
159: }
160:
161: query.setPreLimit(null);
162: query.setPostLimit(null);
163: }
164:
165: /**
166: * Override the default behavior to associate b with null?
167: *
168: * @see DB#getBooleanString(Boolean)
169: */
170: public String getBooleanString(Boolean b) {
171: return (b == null) ? "FALSE" : (Boolean.TRUE.equals(b) ? "TRUE"
172: : "FALSE");
173: }
174:
175: /**
176: * This method overrides the JDBC escapes used to format dates
177: * using a <code>DateFormat</code>.
178: *
179: * This generates the timedate format defined in
180: * http://www.postgresql.org/docs/7.3/static/datatype-datetime.html
181: * which defined PostgreSQL dates as YYYY-MM-DD hh:mm:ss
182: * @param date the date to format
183: * @return The properly formatted date String.
184: */
185: public String getDateString(Date date) {
186: DateFormat df = new SimpleDateFormat(DATE_FORMAT);
187: StringBuffer dateBuf = new StringBuffer();
188: char delim = getStringDelimiter();
189: dateBuf.append(delim);
190: dateBuf.append(df.format(date));
191: dateBuf.append(delim);
192: return dateBuf.toString();
193: }
194:
195: /**
196: * Whether ILIKE should be used for case insensitive like clauses.
197: *
198: * As postgres uses ILIKE, this mimplementation returns true.
199: *
200: * @return true if ilike should be used for case insensitive likes,
201: * false if ignoreCase should be applied to the compared strings.
202: */
203: public boolean useIlike() {
204: return true;
205: }
206: }
|