1 |
750
|
harris
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
751
|
bojilova
|
* Purpose: A db adapter class for PostgreSQL RDBMS.
|
4 |
750
|
harris
|
* 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 |
|
|
|
32 |
|
|
/**
|
33 |
751
|
bojilova
|
* The PostgreSQL db adapter implementation.
|
34 |
750
|
harris
|
*/
|
35 |
|
|
public class DBPostgresql extends DBAdapter {
|
36 |
|
|
|
37 |
|
|
/**
|
38 |
751
|
bojilova
|
* The PostgreSQL unique ID /sequence generator
|
39 |
750
|
harris
|
* 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 |
757
|
bojilova
|
* When record is inserted in the table and before insert trigger
|
43 |
|
|
* gets a nextval from that sequence, select currval of that
|
44 |
|
|
* sequence can return the generated key in the same db connection.
|
45 |
750
|
harris
|
*
|
46 |
757
|
bojilova
|
* @param conn db connection in which the unique id was generated
|
47 |
|
|
* @param tableName the name of table which unique id was generate
|
48 |
750
|
harris
|
* @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 = conn.createStatement();
|
56 |
757
|
bojilova
|
stmt.execute("SELECT currval('" + tableName + "_id_seq')");
|
57 |
750
|
harris
|
ResultSet rs = stmt.getResultSet();
|
58 |
|
|
if ( rs.next() )
|
59 |
|
|
{
|
60 |
|
|
uniqueid = rs.getLong(1);
|
61 |
|
|
}
|
62 |
|
|
stmt.close();
|
63 |
|
|
//System.out.println("Unique ID: " + uniqueid);
|
64 |
|
|
return uniqueid;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
751
|
bojilova
|
* The PostgreSQL function name that gets the current date
|
69 |
|
|
* and time from the database server
|
70 |
750
|
harris
|
*
|
71 |
757
|
bojilova
|
* @return return the current date and time function name: "now()"
|
72 |
750
|
harris
|
*/
|
73 |
753
|
harris
|
public String getDateTimeFunction() {
|
74 |
750
|
harris
|
|
75 |
757
|
bojilova
|
//System.out.println("The date and time function: " + "now()");
|
76 |
|
|
//to return just the date use now()::date
|
77 |
|
|
//and for the time use now()::time
|
78 |
753
|
harris
|
return "now()";
|
79 |
750
|
harris
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
751
|
bojilova
|
* PostgreSQL's string delimiter character: single quote (')
|
83 |
750
|
harris
|
*
|
84 |
|
|
* @return return the string delimiter: single quote (')
|
85 |
|
|
*/
|
86 |
|
|
public String getStringDelimiter() {
|
87 |
|
|
|
88 |
|
|
return "'";
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
}
|