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.util.Date;
23  import java.text.SimpleDateFormat;
24  
25  /***
26   * This is used to connect to a Sybase database using Sybase's
27   * JConnect JDBC driver.
28   *
29   * <B>NOTE:</B><I>Currently JConnect does not implement the required
30   * methods for ResultSetMetaData, and therefore the village API's may
31   * not function.  For connection pooling, everything works.</I>
32   *
33   * @author <a href="mailto:ekkerbj@netscape.net">Jeff Brekke</a>
34   * @version $Id: DBSybase.java 239630 2005-08-24 12:25:32Z henning $
35   */
36  public class DBSybase extends DB
37  {
38      /*** date format */
39      private static final String DATE_FORMAT = "yyyyMMdd HH:mm:ss";
40  
41      /***
42       * Empty constructor.
43       */
44      protected DBSybase()
45      {
46      }
47  
48      /***
49       * This method is used to ignore case.
50       *
51       * @param in The string to transform to upper case.
52       * @return The upper case string.
53       */
54      public String toUpperCase(String in)
55      {
56          return new StringBuffer("UPPER(").append(in).append(")").toString();
57      }
58  
59      /***
60       * This method is used to ignore case.
61       *
62       * @param in The string whose case to ignore.
63       * @return The string in a case that can be ignored.
64       */
65      public String ignoreCase(String in)
66      {
67          return new StringBuffer("UPPER(").append(in).append(")").toString();
68      }
69  
70      /***
71       * @see org.apache.torque.adapter.DB#getIDMethodType()
72       */
73      public String getIDMethodType()
74      {
75          return AUTO_INCREMENT;
76      }
77  
78      /***
79       * Returns the last value from an identity column (available on a
80       * per-session basis from the global variable
81       * <code>@@identity</code>).
82       *
83       * @see org.apache.torque.adapter.DB#getIDMethodSQL(Object obj)
84       */
85      public String getIDMethodSQL(Object unused)
86      {
87          return "select @@identity";
88      }
89  
90      /***
91       * Locks the specified table.
92       *
93       * @param con The JDBC connection to use.
94       * @param table The name of the table to lock.
95       * @throws SQLException No Statement could be created or executed.
96       */
97      public void lockTable(Connection con, String table) throws SQLException
98      {
99          Statement statement = con.createStatement();
100 
101         StringBuffer stmt = new StringBuffer();
102         stmt.append("SELECT next_id FROM ")
103         .append(table)
104         .append(" FOR UPDATE");
105 
106         statement.executeQuery(stmt.toString());
107     }
108 
109     /***
110      * Unlocks the specified table.
111      *
112      * @param con The JDBC connection to use.
113      * @param table The name of the table to unlock.
114      * @throws SQLException No Statement could be created or executed.
115      */
116     public void unlockTable(Connection con, String table) throws SQLException
117     {
118         // Tables in Sybase are unlocked when a commit is issued.  The
119         // user may have issued a commit but do it here to be sure.
120         con.commit();
121     }
122 
123     /***
124      * This method is used to chek whether the database natively
125      * supports limiting the size of the resultset.
126      *
127      * @return True.
128      */
129     public boolean supportsNativeLimit()
130     {
131         return true;
132     }
133 
134     /***
135      * This method is used to chek whether the database supports
136      * limiting the size of the resultset.
137      *
138      * @return LIMIT_STYLE_SYBASE.
139      */
140     public int getLimitStyle()
141     {
142         return DB.LIMIT_STYLE_SYBASE;
143     }
144 
145     /***
146      * This method overrides the JDBC escapes used to format dates
147      * using a <code>DateFormat</code>.  As of version 11, the Sybase
148      * JDBC driver does not implement JDBC 3.0 escapes.
149      *
150      * @param date the date to format
151      * @return The properly formatted date String.
152      */
153     public String getDateString(Date date)
154     {
155         char delim = getStringDelimiter();
156         return (delim + new SimpleDateFormat(DATE_FORMAT).format(date) + delim);
157     }
158 }