Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An adapter class for PostgreSQL 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: jones $'
9
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3077 $'
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.*;
30
import edu.ucsb.nceas.metacat.*;
31

    
32
/**
33
 * The PostgreSQL db adapter implementation.
34
 */
35
public class PostgresqlAdapter extends AbstractDatabase {
36

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

    
68
  /**
69
   * The PostgreSQL function name that gets the current date 
70
   * and time from the database server
71
   *
72
   * @return return the current date and time function name: "now()"
73
   */
74
  public String getDateTimeFunction() {
75

    
76
    //System.out.println("The date and time function: " + "now()");    
77
		//to return just the date use now()::date
78
		//and for the time use now()::time
79
    return "now()";
80
  }
81

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

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

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

    
117
	  return "SELECT " + selectFields + " FROM " + tableA + " a LEFT JOIN " 
118
	         + tableB + " b ON " + joinCriteria + " WHERE (" 
119
	         + nonJoinCriteria +")";
120
  }
121

    
122
  /**
123
   * Return a hard code string to get xml_document list in timed replcation
124
   */
125
  public String getReplicationDocumentListSQL()
126
  {
127
      String sql ="select a.docid, a.rev, a.doctype from ( xml_documents as a left outer join  xml_revisions as b on (a.docid=b.docid and  a.rev<=b.rev)) where b.docid is null ";
128
      return sql;
129
  }
130
}
131
    
(3-3/4)