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: leinfelder $'
9
 *     '$Date: 2012-11-29 16:12:17 -0800 (Thu, 29 Nov 2012) $'
10
 * '$Revision: 7437 $'
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 PostgreSQL db adapter implementation.
36
 */
37
public class PostgresqlAdapter extends AbstractDatabase {
38

    
39
  /**
40
   * The PostgreSQL unique ID /sequence generator
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 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
    stmt = conn.createStatement();
59
    stmt.execute("SELECT currval('" + tableName + "_id_seq')");
60
    ResultSet rs = stmt.getResultSet();
61
    if ( rs.next() ) 
62
	  {
63
        uniqueid = rs.getLong(1);
64
    }
65
    stmt.close();
66
 
67
    return uniqueid;
68
  }
69

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

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

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

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

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

    
119
	  return "SELECT " + selectFields + " FROM " + tableA + " a LEFT JOIN " 
120
	         + tableB + " b ON " + joinCriteria + " WHERE (" 
121
	         + nonJoinCriteria +")";
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 as a left outer join  xml_revisions as b on (a.docid=b.docid and  a.rev<=b.rev)) where b.docid is null ";
130
      return sql;
131
  }
132
  
133
  public String getPagedQuery(String queryWithOrderBy, Integer start, Integer count) {
134
	  String query = queryWithOrderBy;
135
	  if (count != null) {
136
		  query = query + " LIMIT " + count;
137
	  }
138
	  if (start != null) {
139
		  query = query + " OFFSET " + start;
140
	  }
141
	  return query;
142
  }
143
}
144
    
(3-3/4)