Project

General

Profile

« Previous | Next » 

Revision 757

Added by bojilova almost 23 years ago

- new db adapter for MS SQL Server
- changed the getUniqueID(conn, tableName) to return the last generated by the db unique ID
instead of generating unique ID first by the appl and then supplied to the insert,
because SQL Server doesn't have capabilities
unique ID to be generated on the client and then supplied to the insert.

View differences:

src/edu/ucsb/nceas/dbadapter/DBAdapter.java
50 50
  /**
51 51
   * Unique ID generator
52 52
   *
53
   * @param conn db connection in which to generate the unique id
54
   * @param tableName the table which unique id to generate
53
   * @param conn db connection in which the unique id was generated
54
   * @param tableName the table which unique id was generate
55 55
   * @exception SQLException <br/> any SQLException that can be thrown 
56 56
   *            during the db operation
57 57
   * @return return the generated unique id as a long type
58 58
   */
59 59
  public abstract long getUniqueID(Connection conn, String tableName) 
60 60
                                                  throws SQLException;
61

  
61 62
  /**
62 63
   * The function name that gets the current date and time
63 64
   * from the database server
src/edu/ucsb/nceas/dbadapter/DBPostgresql.java
39 39
   * The name of the sequence used to generate the unique id 
40 40
   * is made from the name of the table that uses the id by 
41 41
   * appending "_id_seq" to it.
42
   * 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.
42 45
   *
43
   * @param conn db connection from which to generate the unique id
44
   * @param tableName the name of table which unique id to generate
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
45 48
   * @exception SQLException any SQLException that can be thrown 
46 49
   *            during the db operation
47 50
   * @return return the generated unique id as a long type
......
50 53
                                         throws SQLException {
51 54
    long uniqueid = 0;
52 55
    Statement stmt = conn.createStatement();
53
    stmt.execute("SELECT nextval('" + tableName + "_id_seq')");
56
    stmt.execute("SELECT currval('" + tableName + "_id_seq')");
54 57
    ResultSet rs = stmt.getResultSet();
55 58
    if ( rs.next() ) 
56 59
		{
......
65 68
   * The PostgreSQL function name that gets the current date 
66 69
   * and time from the database server
67 70
   *
68
   * @return return the current date and time function name: "now()::date"
71
   * @return return the current date and time function name: "now()"
69 72
   */
70 73
  public String getDateTimeFunction() {
71 74

  
72
    //System.out.println("The date and time function: " + "now()::date");    
73
		//to return just the date use now()::date and for the time
74
		//use now()::time
75
    //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
75 78
    return "now()";
76 79
  }
77 80

  
src/edu/ucsb/nceas/dbadapter/DBOracle.java
39 39
   * The name of the sequence used to generate the unique id 
40 40
   * is made from the name of the table that uses the id by 
41 41
   * appending "_id_seq" to it.
42
   * 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.
42 45
   *
43
   * @param conn db connection in which to generate the unique id
44
   * @param tableName the name of table which unique id to generate
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
45 48
   * @exception SQLException <br/> any SQLException that can be thrown 
46 49
   *            during the db operation
47 50
   * @return return the generated unique id as a long type
......
50 53
                                         throws SQLException {
51 54
    long uniqueid = 0;
52 55
    Statement stmt = conn.createStatement();
53
    stmt.execute("SELECT " + tableName + "_id_seq.nextval FROM dual");
56
    stmt.execute("SELECT " + tableName + "_id_seq.currval FROM dual");
54 57
    ResultSet rs = stmt.getResultSet();
55 58
    if ( rs.next() ) {
56 59
      uniqueid = rs.getLong(1);
src/edu/ucsb/nceas/dbadapter/DBSqlServer.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A db adapter class for MS SQL Server 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

  
32
/**
33
 * The MS SQL Server db adapter implementation.
34
 */
35
public class DBSqlServer extends DBAdapter {
36

  
37
  /**
38
   * The SQL Server unique ID generator through use of IDENTITY key
39
   * The IDENTITY key is a column in the table. When record is inserted
40
   * in the table, SELECT @@IDENTITY can return the key generated in
41
   * that IDENTITY column in the same db connection.
42
   * This is the only way to get unique id: let the SQL Server assign
43
   * a value in IDENTITY column and get it afterwards for use in the 
44
   * application.
45
   *
46
   * @param conn db connection in which to generate the unique id
47
   * @param tableName the name of table which unique id to generate
48
   * @exception SQLException <br/> 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
    stmt.execute("SELECT @@IDENTITY");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) {
59
      uniqueid = rs.getLong(1);
60
    }
61
    stmt.close();
62
System.out.println("Unique ID: " + uniqueid);    
63
    return uniqueid;
64
  }
65

  
66
  /**
67
   * The SQL Server's function name that gets the current date and time
68
   * from the database server: "getdate()"
69
   *
70
   * @return return the current date and time function name: "getdate()"
71
   */
72
  public String getDateTimeFunction() {
73

  
74
    //System.out.println("The date and time function: " + "getdate()");    
75
    return "getdate()";
76
  }
77

  
78
  /**
79
   * The SQL Server's string delimiter character: single quote (')
80
   *
81
   * @return return the string delimiter: single quote (')
82
   */
83
  public String getStringDelimiter() {
84

  
85
    return "'";
86
  }
87
  
88
}
89
    
0 90

  

Also available in: Unified diff