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: daigle $'
|
11
|
* '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
|
12
|
* '$Revision: 4080 $'
|
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
|
|
33
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
34
|
|
35
|
/**
|
36
|
* Java offers uniform database access through the use of JDBC.
|
37
|
* But many databases still use different SQL implementations and
|
38
|
* conventions. Thus this class offers extended programming interface
|
39
|
* that all subclasses should implement to gain access to different
|
40
|
* databases.
|
41
|
*
|
42
|
* To add a new database adapter class you must create a new class
|
43
|
* <dbname>Adapter that extends edu.ucsb.nceas.dbadapter.AbstarctDatabase
|
44
|
* (where dbname is the name of the database or database driver you wish
|
45
|
* to add to your application). AbstarctDatabase is an abstract class,
|
46
|
* thus the subclasses need to implement the abstract methods.
|
47
|
*/
|
48
|
public abstract class AbstractDatabase {
|
49
|
|
50
|
/**
|
51
|
* Unique ID generator
|
52
|
*
|
53
|
* @param conn db connection in which the unique id was generated
|
54
|
* @param tableName the table which unique id was generate
|
55
|
* @exception SQLException <br/> any SQLException that can be thrown
|
56
|
* during the db operation
|
57
|
* @return return the generated unique id as a long type
|
58
|
*/
|
59
|
public abstract long getUniqueID(Connection conn, String tableName)
|
60
|
throws SQLException;
|
61
|
|
62
|
/**
|
63
|
* The function name that gets the current date and time
|
64
|
* from the database server
|
65
|
*
|
66
|
* @return return the current date and time function name
|
67
|
*/
|
68
|
public abstract String getDateTimeFunction();
|
69
|
|
70
|
/**
|
71
|
* The function name that is used to return non-NULL value
|
72
|
*
|
73
|
* @return return the non-NULL function name
|
74
|
*/
|
75
|
public abstract String getIsNULLFunction();
|
76
|
|
77
|
/**
|
78
|
* The character that the specific database implementation uses to
|
79
|
* indicate string literals in SQL. This will usually be a single
|
80
|
* qoute (').
|
81
|
*
|
82
|
* @return return the string delimiter
|
83
|
*/
|
84
|
public abstract String getStringDelimiter();
|
85
|
|
86
|
/**
|
87
|
* MSSQL didn't support to_date function which to transfer a text string
|
88
|
* to date type. But Oracle and Postsql do.
|
89
|
*/
|
90
|
public String toDate(String dateString, String format)
|
91
|
{
|
92
|
return "to_date(" + "'"+ dateString + "', '" + format + "')";
|
93
|
}
|
94
|
|
95
|
|
96
|
/**
|
97
|
* Syntax for doing a left join
|
98
|
* Add 'a.' in front of the fields for first table and
|
99
|
* 'b.' in front of the fields for the second table
|
100
|
*
|
101
|
* @param selectFields fields that you want to be selected
|
102
|
* @param tableA first table in the join
|
103
|
* @param tableB second table in the join
|
104
|
* @param joinCriteria the criteria based on which the join will be made
|
105
|
* @param nonJoinCriteria all other criterias
|
106
|
* @return return the string for teh select query
|
107
|
*/
|
108
|
public abstract String getLeftJoinQuery(String selectFields, String tableA,
|
109
|
String tableB, String joinCriteria, String nonJoinCriteria);
|
110
|
|
111
|
/**
|
112
|
* Instantiate a class using the name of the class at runtime
|
113
|
*
|
114
|
* @param className the fully qualified name of the class to instantiate
|
115
|
*/
|
116
|
static public Object createObject(String className) throws Exception {
|
117
|
|
118
|
Object object = null;
|
119
|
try {
|
120
|
Class classDefinition = Class.forName(className);
|
121
|
object = classDefinition.newInstance();
|
122
|
} catch (InstantiationException e) {
|
123
|
throw e;
|
124
|
} catch (IllegalAccessException e) {
|
125
|
throw e;
|
126
|
} catch (ClassNotFoundException e) {
|
127
|
throw e;
|
128
|
}
|
129
|
return object;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* the main routine used to test the dbadapter utility.
|
134
|
*/
|
135
|
static public void main(String[] args) {
|
136
|
|
137
|
// Determine our db adapter class and
|
138
|
// create an instance of that class
|
139
|
try {
|
140
|
String dbAdapter = PropertyService.getProperty("database.adapter");
|
141
|
AbstractDatabase dbAdapterObj = (AbstractDatabase)createObject(dbAdapter);
|
142
|
|
143
|
// test if they work correctly
|
144
|
String date = dbAdapterObj.getDateTimeFunction();
|
145
|
|
146
|
} catch (Exception e) {
|
147
|
System.out.println(e);
|
148
|
}
|
149
|
}
|
150
|
|
151
|
|
152
|
/**
|
153
|
* This method will return the sql command to get document list in xml_document
|
154
|
* in replication. Because it involes outer join, so this method is very flexible.
|
155
|
* @return
|
156
|
*/
|
157
|
public abstract String getReplicationDocumentListSQL();
|
158
|
}
|
159
|
|