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