Project

General

Profile

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

    
27
package edu.ucsb.nceas.dbadapter;
28

    
29
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
33

    
34
/**
35
 * The Oracle db adapter implementation.
36
 */
37
public class OracleAdapter extends AbstractDatabase {
38

    
39
  /**
40
   * The Oracle unique ID generator through use of sequences
41
   * The name of the sequence used to generate the unique id 
42
   * is made from the name of the table that uses the id by 
43
   * appending "_id_seq" to it.
44
   * When record is inserted in the table and insert trigger gets
45
   * a nextval from that sequence, select currval of that sequence
46
   * can return the generated key in the same db connection.
47
   *
48
   * @param conn db connection in which the unique id was generated
49
   * @param tableName the name of table which unique id was generate
50
   * @exception SQLException <br/> any SQLException that can be thrown 
51
   *            during the db operation
52
   * @return return the generated unique id as a long type
53
   */
54
  public long getUniqueID(Connection conn, String tableName) 
55
                                         throws SQLException {
56
    long uniqueid = 0;
57
    Statement stmt = null;
58
    
59
  
60
     stmt = conn.createStatement();
61
     stmt.execute("SELECT " + tableName + "_id_seq.currval FROM dual");
62
     ResultSet rs = stmt.getResultSet();
63
     if ( rs.next() ) {
64
        uniqueid = rs.getLong(1);
65
     }
66
     stmt.close();
67
 
68
     return uniqueid;
69
  }
70

    
71
  /**
72
   * The Oracle's function name that gets the current date and time
73
   * from the database server: "sysdate"
74
   *
75
   * @return return the current date and time function name: "sysdate"
76
   */
77
  public String getDateTimeFunction() {
78

    
79
    //System.out.println("The date and time function: " + "sysdate");    
80
    return "sysdate";
81
  }
82

    
83
  /**
84
   * The Oracle's function name that is used to return non-NULL value
85
   *
86
   * @return return the non-NULL function name: "nvl"
87
   */
88
  public String getIsNULLFunction() {
89
    
90
    return "nvl";
91
  }
92

    
93
  /**
94
   * The Oracles's string delimiter character: single quote (')
95
   *
96
   * @return return the string delimiter: single quote (')
97
   */
98
  public String getStringDelimiter() {
99

    
100
    return "'";
101
  }
102

    
103
  /**
104
   * The Oracles's syntax for doing a left join
105
   * Add 'a.' in front of the fields for first table and
106
   * 'b.' in front of the fields for the second table
107
   * 
108
   * @param selectFields fields that you want to be selected
109
   * @param tableA first table in the join
110
   * @param tableB second table in the join
111
   * @param joinCriteria the criteria based on which the join will be made
112
   * @param nonJoinCriteria all other criterias
113
   * @return return the string for teh select query
114
   */
115
  public String getLeftJoinQuery(String selectFields, String tableA, 
116
		  String tableB, String joinCriteria, String nonJoinCriteria){
117

    
118
	  return "SELECT " + selectFields + " FROM " + tableA + " a, " 
119
	         + tableB + " b WHERE " + joinCriteria + "(+) " + " AND (" 
120
	         + nonJoinCriteria +")";
121
  }
122
  
123
  
124
  /**
125
   * Return a hard code string to get xml_document list in timed replcation
126
   */
127
  public String getReplicationDocumentListSQL()
128
  {
129
      String sql ="select a.docid, a.rev, a.doctype from xml_documents a, xml_revisions b where a.docid=b.docid(+) and  a.rev<=b.rev(+) and b.docid is null ";
130
      return sql;
131
  }
132
  
133
  /**
134
   * @see http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
135
   */
136
  @Override
137
  public String getPagedQuery(String queryWithOrderBy, Integer start, Integer count) {
138
	  
139
	  // not limiting rows
140
	  StringBuffer query = new StringBuffer("SELECT " + queryWithOrderBy);
141
	  
142
	  // check for params
143
	  if (start != null) {
144
		  query = new StringBuffer();
145
		  query.append("SELECT * FROM ");
146
		  query.append("( SELECT a.*, ROWNUM rnum FROM ");
147
		  query.append("( " + queryWithOrderBy + " ) a ");
148
		  if (count != null) {
149
			  // both are limited
150
			  query.append(" WHERE ROWNUM <= " + count);
151
		  }
152
		  query.append(" ) ");
153
		  query.append("WHERE rnum  >= " + start);
154
	  }
155
	  
156
	  return query.toString();
157
  }
158
  
159
}
160
    
(2-2/4)