Project

General

Profile

1 775 bojilova
/**
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
 *    Release: @release@
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
28
package edu.ucsb.nceas.dbadapter;
29
30
import java.sql.*;
31 1128 tao
import edu.ucsb.nceas.metacat.*;
32 775 bojilova
33
/**
34
 * The PostgreSQL db adapter implementation.
35
 */
36
public class PostgresqlAdapter extends AbstractDatabase {
37
38
  /**
39
   * The PostgreSQL unique ID /sequence generator
40
   * The name of the sequence used to generate the unique id
41
   * is made from the name of the table that uses the id by
42
   * appending "_id_seq" to it.
43
   * When record is inserted in the table and insert trigger gets
44
   * a nextval from that sequence, select currval of that sequence
45
   * can return the generated key in the same db connection.
46
   *
47
   * @param conn db connection in which the unique id was generated
48
   * @param tableName the name of table which unique id was generate
49
   * @exception SQLException any SQLException that can be thrown
50
   *            during the db operation
51
   * @return return the generated unique id as a long type
52
   */
53 1135 tao
  public long getUniqueID(Connection conn, String tableName)
54 775 bojilova
                                         throws SQLException {
55
    long uniqueid = 0;
56 1128 tao
    Statement stmt = null;
57 1135 tao
    stmt = conn.createStatement();
58
    stmt.execute("SELECT currval('" + tableName + "_id_seq')");
59
    ResultSet rs = stmt.getResultSet();
60
    if ( rs.next() )
61
	  {
62 1128 tao
        uniqueid = rs.getLong(1);
63 1135 tao
    }
64
    stmt.close();
65
66 775 bojilova
    return uniqueid;
67
  }
68
69
  /**
70
   * The PostgreSQL function name that gets the current date
71
   * and time from the database server
72
   *
73
   * @return return the current date and time function name: "now()"
74
   */
75
  public String getDateTimeFunction() {
76
77
    //System.out.println("The date and time function: " + "now()");
78
		//to return just the date use now()::date
79
		//and for the time use now()::time
80
    return "now()";
81
  }
82
83
  /**
84
   * The PostgreSQL function name that is used to return non-NULL value
85
   *
86
   * @return return the non-NULL function name: "coalesce"
87
   */
88
  public String getIsNULLFunction() {
89
90
    return "coalesce";
91
  }
92
93
  /**
94
   * PostgreSQL's string delimiter character: single quote (')
95
   *
96
   * @return return the string delimiter: single quote (')
97
   */
98
  public String getStringDelimiter() {
99
100 899 berkley
    return "\"";
101 775 bojilova
  }
102
103 2722 tao
  /**
104
   * Return a hard code string to get xml_document list in timed replcation
105
   */
106
  public String getReplicationDocumentListSQL()
107
  {
108
      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 ";
109
      return sql;
110
  }
111 775 bojilova
}