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.sql.Statement;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  
25  /***
26   * This code should be used for an Oracle database pool.
27   *
28   * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
29   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
30   * @author <a href="mailto:bschneider@vecna.com">Bill Schneider</a>
31   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
32   * @version $Id: DBOracle.java 239636 2005-08-24 12:38:09Z henning $
33   */
34  public class DBOracle extends DB
35  {
36      /*** date format used in getDateString() */
37      private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss";
38  
39      /***
40       * Empty constructor.
41       */
42      protected DBOracle()
43      {
44      }
45  
46      /***
47       * This method is used to ignore case.
48       *
49       * @param in The string to transform to upper case.
50       * @return The upper case string.
51       */
52      public String toUpperCase(String in)
53      {
54          return new StringBuffer("UPPER(").append(in).append(")").toString();
55      }
56  
57      /***
58       * This method is used to ignore case.
59       *
60       * @param in The string whose case to ignore.
61       * @return The string in a case that can be ignored.
62       */
63      public String ignoreCase(String in)
64      {
65          return new StringBuffer("UPPER(").append(in).append(")").toString();
66      }
67  
68      /***
69       * This method is used to format any date string.
70       *
71       * @param date the Date to format
72       * @return The date formatted String for Oracle.
73       */
74      public String getDateString(Date date)
75      {
76          return "TO_DATE('" + new SimpleDateFormat(DATE_FORMAT).format(date)
77                  + "', 'DD-MM-YYYY HH24:MI:SS')";
78      }
79  
80      /***
81       * @see org.apache.torque.adapter.DB#getIDMethodType()
82       */
83      public String getIDMethodType()
84      {
85          return SEQUENCE;
86      }
87  
88      /***
89       * Returns the next key from a sequence.  Uses the following
90       * implementation:
91       *
92       * <blockquote><code><pre>
93       * select sequenceName.nextval from dual
94       * </pre></code></blockquote>
95       *
96       * @param sequenceName The name of the sequence (should be of type
97       * <code>String</code>).
98       * @return SQL to retreive the next database key.
99       * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object)
100      */
101     public String getIDMethodSQL(Object sequenceName)
102     {
103         return ("select " + sequenceName + ".nextval from dual");
104     }
105 
106     /***
107      * Locks the specified table.
108      *
109      * @param con The JDBC connection to use.
110      * @param table The name of the table to lock.
111      * @exception SQLException No Statement could be created or executed.
112      */
113     public void lockTable(Connection con, String table) throws SQLException
114     {
115         Statement statement = con.createStatement();
116 
117         StringBuffer stmt = new StringBuffer();
118         stmt.append("SELECT next_id FROM ")
119                 .append(table)
120                 .append(" FOR UPDATE");
121 
122         statement.executeQuery(stmt.toString());
123     }
124 
125     /***
126      * Unlocks the specified table.
127      *
128      * @param con The JDBC connection to use.
129      * @param table The name of the table to unlock.
130      * @exception SQLException No Statement could be created or executed.
131      */
132     public void unlockTable(Connection con, String table) throws SQLException
133     {
134         // Tables in Oracle are unlocked when a commit is issued.  The
135         // user may have issued a commit but do it here to be sure.
136         con.commit();
137     }
138 
139     /***
140      * This method is used to check whether the database natively
141      * supports limiting the size of the resultset.
142      *
143      * @return True.
144      */
145     public boolean supportsNativeLimit()
146     {
147         return true;
148     }
149 
150     /***
151      * This method is used to check whether the database supports
152      * limiting the size of the resultset.
153      *
154      * @return LIMIT_STYLE_ORACLE.
155      */
156     public int getLimitStyle()
157     {
158         return DB.LIMIT_STYLE_ORACLE;
159     }
160 
161     /***
162      * This method is for the SqlExpression.quoteAndEscape rules.  The rule is,
163      * any string in a SqlExpression with a BACKSLASH will either be changed to
164      * "//" or left as "\".  SapDB does not need the escape character.
165      *
166      * @return false.
167      */
168     public boolean escapeText()
169     {
170         return false;
171     }
172 }