Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An abstract class that encapsulates access to any RDBMS.
4
 *             This allows to swap easily between databases without any
5
 *             modification to the application.
6
 *  Copyright: 2000 Regents of the University of California and the
7
 *             National Center for Ecological Analysis and Synthesis
8
 *    Authors: Jivka Bojilova
9
 *
10
 *   '$Author: jones $'
11
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
12
 * '$Revision: 3077 $'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

    
29
package edu.ucsb.nceas.dbadapter;
30

    
31
import java.sql.*;
32
import edu.ucsb.nceas.metacat.MetaCatUtil;
33

    
34
/**
35
 * Java offers uniform database access through the use of JDBC.
36
 * But many databases still use different SQL implementations and 
37
 * conventions. Thus this class offers extended programming interface
38
 * that all subclasses should implement to gain access to different
39
 * databases.
40
 *
41
 * To add a new database adapter class you must create a new class 
42
 * <dbname>Adapter that extends edu.ucsb.nceas.dbadapter.AbstarctDatabase
43
 * (where dbname is the name of the database or database driver you wish
44
 * to add to your application). AbstarctDatabase is an abstract class,
45
 * thus the subclasses need to implement the abstract methods.
46
 */
47
public abstract class AbstractDatabase {
48

    
49
  /**
50
   * Unique ID generator
51
   *
52
   * @param conn db connection in which the unique id was generated
53
   * @param tableName the table which unique id was generate
54
   * @exception SQLException <br/> any SQLException that can be thrown 
55
   *            during the db operation
56
   * @return return the generated unique id as a long type
57
   */
58
  public abstract long getUniqueID(Connection conn, String tableName) 
59
                                                  throws SQLException;
60

    
61
  /**
62
   * The function name that gets the current date and time
63
   * from the database server
64
   *
65
   * @return return the current date and time function name
66
   */
67
  public abstract String getDateTimeFunction();
68

    
69
  /**
70
   * The function name that is used to return non-NULL value
71
   *
72
   * @return return the non-NULL function name
73
   */
74
  public abstract String getIsNULLFunction();
75

    
76
  /**
77
   * The character that the specific database implementation uses to 
78
   * indicate string literals in SQL. This will usually be a single
79
   * qoute (').
80
   *
81
   * @return return the string delimiter
82
   */
83
  public abstract String getStringDelimiter();
84
  
85
  /**
86
   * MSSQL didn't support to_date function which to transfer a text string
87
   * to date type. But Oracle and Postsql do.
88
   */
89
  public String toDate(String dateString, String format)
90
  {
91
    return "to_date(" + "'"+ dateString + "', '" + format + "')";
92
  }
93
  
94
  
95
  /**
96
   * Syntax for doing a left join
97
   * Add 'a.' in front of the fields for first table and
98
   * 'b.' in front of the fields for the second table
99
   * 
100
   * @param selectFields fields that you want to be selected
101
   * @param tableA first table in the join
102
   * @param tableB second table in the join
103
   * @param joinCriteria the criteria based on which the join will be made
104
   * @param nonJoinCriteria all other criterias
105
   * @return return the string for teh select query
106
   */
107
  public abstract String getLeftJoinQuery(String selectFields, String tableA, 
108
		  String tableB, String joinCriteria, String nonJoinCriteria);
109
  
110
  /**
111
   * Instantiate a class using the name of the class at runtime
112
   *
113
   * @param className the fully qualified name of the class to instantiate
114
   */
115
  static public Object createObject(String className) throws Exception {
116
 
117
    Object object = null;
118
    try {
119
      Class classDefinition = Class.forName(className);
120
      object = classDefinition.newInstance();
121
    } catch (InstantiationException e) {
122
      throw e;
123
    } catch (IllegalAccessException e) {
124
      throw e;
125
    } catch (ClassNotFoundException e) {
126
      throw e;
127
    }
128
    return object;
129
  }
130

    
131
  /**
132
   * the main routine used to test the dbadapter utility.
133
   */
134
  static public void main(String[] args) {
135
    
136
    // Determine our db adapter class and
137
    // create an instance of that class
138
    try {
139
      String dbAdapter = MetaCatUtil.getOption("dbAdapter");
140
      AbstractDatabase dbAdapterObj = (AbstractDatabase)createObject(dbAdapter);
141
      
142
      // test if they work correctly
143
      String date = dbAdapterObj.getDateTimeFunction();
144

    
145
    } catch (Exception e) {
146
      System.out.println(e);
147
    }
148
  }
149
  
150
  
151
  /**
152
   * This method will return the sql command to get document list in xml_document
153
   * in replication. Because it involes outer join, so this method is very flexible.
154
   * @return
155
   */
156
  public abstract String getReplicationDocumentListSQL();
157
}
158
    
(1-1/4)