1   /*
2    *  DBHelper.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Marin Dimitrov, 18/Sep/2001
12   *
13   *  $Id: DBHelper.java,v 1.25 2001/11/14 13:20:43 marin Exp $
14   */
15  
16  package gate.persist;
17  
18  import java.sql.*;
19  import java.net.*;
20  import java.util.*;
21  
22  import gate.util.*;
23  
24  public class DBHelper {
25  
26    /** --- */
27    private static final String jdbcOracleDriverName = "oracle.jdbc.driver.OracleDriver";
28    private static final String jdbcPostgresDriverName = "postgresql.Driver";
29    private static final String jdbcSapDBDriverName = "com.sap.dbtech.jdbc.DriverSapDB";
30  
31  
32    //ACHTUNG!
33    //DO NOT EDIT THESE CONSTANTS WITHOUT
34    //SYNCHRONIZING WITH ERROR.SPC PL/SQL PACKAGE
35    //note that while Oracle returns negative error numbers
36    //the SQLException::getErrorCode() returns positive ones
37    //
38    public static final int X_ORACLE_START = 20100;
39    public static final int X_ORACLE_DUPLICATE_GROUP_NAME =      X_ORACLE_START + 1 ;
40    public static final int X_ORACLE_DUPLICATE_USER_NAME =       X_ORACLE_START + 2 ;
41    public static final int X_ORACLE_INVALID_USER_NAME =         X_ORACLE_START + 3 ;
42    public static final int X_ORACLE_INVALID_USER_PASS =         X_ORACLE_START + 4 ;
43    public static final int X_ORACLE_INVALID_USER_GROUP =        X_ORACLE_START + 5 ;
44    public static final int X_ORACLE_INVALID_LR =                X_ORACLE_START + 6 ;
45    public static final int X_ORACLE_INVALID_ACCESS_MODE =       X_ORACLE_START + 7 ;
46    public static final int X_ORACLE_INVALID_ARGUMENT =          X_ORACLE_START + 8 ;
47    public static final int X_ORACLE_NOT_IMPLEMENTED =           X_ORACLE_START + 9 ;
48    public static final int X_ORACLE_GROUP_OWNS_RESOURCES =      X_ORACLE_START + 10 ;
49    public static final int X_ORACLE_USER_OWNS_RESOURCES =       X_ORACLE_START + 11 ;
50    public static final int X_ORACLE_INCOMPLETE_DATA  =          X_ORACLE_START + 12 ;
51    public static final int X_ORACLE_INVALID_LR_TYPE  =          X_ORACLE_START + 13 ;
52    public static final int X_ORACLE_INVALID_ANNOTATION_TYPE =   X_ORACLE_START + 14 ;
53    public static final int X_ORACLE_INVALID_FEATURE_TYPE =      X_ORACLE_START + 15 ;
54    public static final int X_ORACLE_INVALID_CONTENT_TYPE =      X_ORACLE_START + 16 ;
55    public static final int X_ORACLE_INVALID_ANNOTATION =        X_ORACLE_START + 17 ;
56    public static final int X_ORACLE_INSUFFICIENT_PRIVILEGES =   X_ORACLE_START + 18 ;
57    public static final int X_ORACLE_INVALID_ANNOTATION_SET  =   X_ORACLE_START + 19 ;
58  
59    public static final int TRUE = 1;
60    public static final int FALSE = 0;
61  
62    public static final int CHARACTER_CONTENT = 1;
63    public static final int BINARY_CONTENT = 2;
64    public static final int EMPTY_CONTENT = 3;
65  
66    public static final String DOCUMENT_CLASS = "gate.corpora.DatabaseDocumentImpl";
67    public static final String CORPUS_CLASS =  "gate.corpora.DatabaseCorpusImpl";
68  
69    public static final String  DB_PARAMETER_GUID = "DB_GUID";
70  
71    //dummy key
72    //hopefully no one will create a feature with such key
73    public static final String DUMMY_FEATURE_KEY =  "--NO--SUCH--KEY--";
74    //dummy ID
75    public static final Long DUMMY_ID;
76  
77  
78    //!!! ACHTUNG !!!
79    // these 4 constants should *always* be synchronzied with the ones in the
80    // related SQL packages/scripts [for Oracle - security.spc]
81    // i.e. if u don't have a serious reason do *not* change anything
82  
83    /** used to store corpus' features */
84    protected static final int FEATURE_OWNER_CORPUS  = 1;
85    /** used to store document's features */
86    protected static final int FEATURE_OWNER_DOCUMENT  = 2;
87    /** used to store annotation's features */
88    protected static final int FEATURE_OWNER_ANNOTATION  = 3;
89  
90    /** feature value is int  */
91    public static final int VALUE_TYPE_NULL              = 100;
92    /** feature value is int  */
93    public static final int VALUE_TYPE_INTEGER           = 101;
94    /** feature value is long */
95    public static final int VALUE_TYPE_LONG              = 102;
96    /** feature value is boolean */
97    public static final int VALUE_TYPE_BOOLEAN           = 103;
98    /** feature value is string less than 4000 bytes */
99    public static final int VALUE_TYPE_STRING            = 104;
100   /** feature value is binary */
101   public static final int VALUE_TYPE_BINARY            = 105;
102   /** feature value is float */
103   public static final int VALUE_TYPE_FLOAT             = 106;
104   /** feature value is array of ints */
105   public static final int VALUE_TYPE_INTEGER_ARR       = 107;
106   /** feature value is array of longs */
107   public static final int VALUE_TYPE_LONG_ARR          = 108;
108   /** feature value is array of bools */
109   public static final int VALUE_TYPE_BOOLEAN_ARR       = 109;
110   /** feature value is array of strings */
111   public static final int VALUE_TYPE_STRING_ARR        = 110;
112   /** feature value is array of binary values */
113   public static final int VALUE_TYPE_BINARY_ARR        = 111;
114   /** feature value is array of floats */
115   public static final int VALUE_TYPE_FLOAT_ARR         = 112;
116   /** feature value is array of floats */
117   public static final int VALUE_TYPE_EMPTY_ARR         = 113;
118 
119 
120   private static final boolean DEBUG = false;
121 
122 
123 
124   private static boolean  driversLoaded;
125 
126   static {
127     DUMMY_ID = new Long(Long.MIN_VALUE);
128     driversLoaded = false;
129   }
130 
131 
132   protected DBHelper() {
133 
134     //no way
135     //contains only static methods
136   }
137 
138   /** --- */
139   private static synchronized void loadDrivers()
140     throws ClassNotFoundException {
141 
142     if (!driversLoaded) {
143       Class.forName(jdbcOracleDriverName);
144       Class.forName(jdbcPostgresDriverName);
145       Class.forName(jdbcSapDBDriverName);
146 
147       driversLoaded = true;
148     }
149   }
150 
151 
152   /** --- */
153   public static void cleanup(ResultSet rs)
154     throws PersistenceException {
155 
156     try {
157       if (rs!=null)
158         rs.close();
159     }
160     catch(SQLException sqle) {
161       throw new PersistenceException("an SQL exception occured ["+ sqle.getMessage()+"]");
162     }
163   }
164 
165   /** --- */
166   public static void cleanup(Statement stmt)
167     throws PersistenceException {
168     try {
169       if (stmt!=null)
170         stmt.close();
171     }
172     catch(SQLException sqle) {
173       throw new PersistenceException("an SQL exception occured ["+ sqle.getMessage()+"]");
174     }
175   }
176 
177   /** --- */
178   public static Connection connect(String connectURL)
179     throws SQLException,ClassNotFoundException{
180 
181     loadDrivers();
182     Connection conn = DriverManager.getConnection(connectURL);
183 
184     if (DEBUG) {
185       DatabaseMetaData meta = conn.getMetaData();
186       gate.util.Err.println(
187             "JDBC driver name=["+meta.getDriverName() +
188             "] version=["+ meta.getDriverVersion() +"]");
189     }
190 
191     return conn;
192   }
193 
194   /** --- */
195   public static void readCLOB(java.sql.Clob src, StringBuffer dest)
196     throws SQLException {
197 
198     throw new MethodNotImplementedException();
199   }
200 
201 
202   /** --- */
203   public static void writeCLOB(StringBuffer src,java.sql.Clob dest)
204     throws SQLException {
205 
206     throw new MethodNotImplementedException();
207   }
208 
209 }
210