Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An 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: tao $'
10
 *     '$Date: 2002-05-23 20:39:35 -0700 (Thu, 23 May 2002) $'
11
 * '$Revision: 1129 $'
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
import edu.ucsb.nceas.metacat.*;
32

    
33
/**
34
 * The MS SQL Server db adapter implementation.
35
 */
36
public class SqlserverAdapter extends AbstractDatabase {
37

    
38
  /**
39
   * The SQL Server unique ID generator through use of IDENTITY key
40
   * The IDENTITY key is a column in the table. When record is inserted
41
   * in the table, SELECT @@IDENTITY can return the key generated in
42
   * that IDENTITY column in the same db connection.
43
   * This is the only way to get unique id: let the SQL Server assign
44
   * a value in IDENTITY column and get it afterwards for use in the 
45
   * application.
46
   *
47
   * @param conn db connection in which to generate the unique id
48
   * @param tableName the name of table which unique id to generate
49
   * @exception SQLException <br/> any SQLException that can be thrown 
50
   *            during the db operation
51
   * @return return the generated unique id as a long type
52
   */
53
  public long getUniqueID(String tableName) 
54
                                         throws SQLException {
55
    long uniqueid = 0;
56
    DBConnection conn = null;
57
    int serialNumber = -1;
58
    Statement stmt = null;
59
    try
60
    {
61
      //check out DBConnection
62
      conn=DBConnectionPool.getDBConnection("SqlserverAdapter.getUniqueID");
63
      serialNumber=conn.getCheckOutSerialNumber();
64
      stmt = conn.createStatement();
65
      stmt.execute("SELECT @@IDENTITY");
66
      ResultSet rs = stmt.getResultSet();
67
      if ( rs.next() ) {
68
        uniqueid = rs.getLong(1);
69
      }
70
      stmt.close();
71
    }
72
    finally
73
    {
74
      DBConnectionPool.returnDBConnection(conn, serialNumber);
75
    }
76
    //System.out.println("Unique ID: " + uniqueid);    
77
    return uniqueid;
78
  }
79

    
80
  /**
81
   * The SQL Server's function name that gets the current date and time
82
   * from the database server: "getdate()"
83
   *
84
   * @return return the current date and time function name: "getdate()"
85
   */
86
  public String getDateTimeFunction() {
87

    
88
    //System.out.println("The date and time function: " + "getdate()");    
89
    return "getdate()";
90
  }
91

    
92
  /**
93
   * The SQL Server's function name that is used to return non-NULL value
94
   *
95
   * @return return the non-NULL function name: "isnull"
96
   */
97
  public String getIsNULLFunction() {
98
    
99
    return "isnull";
100
  }
101

    
102
  /**
103
   * The SQL Server's string delimiter character: single quote (')
104
   *
105
   * @return return the string delimiter: single quote (')
106
   */
107
  public String getStringDelimiter() {
108

    
109
    return "'";
110
  }
111
  
112
}
113
    
(4-4/4)