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: sgarg $'
10
 *     '$Date: 2005-11-10 13:22:57 -0800 (Thu, 10 Nov 2005) $'
11
 * '$Revision: 2728 $'
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(Connection conn, String tableName) 
54
                                         throws SQLException {
55
    long uniqueid = 0;
56
    Statement stmt = null;
57
    stmt = conn.createStatement();
58
    stmt.execute("SELECT @@IDENTITY");
59
    ResultSet rs = stmt.getResultSet();
60
    if ( rs.next() ) {
61
        uniqueid = rs.getLong(1);
62
    }
63
      stmt.close();
64

    
65
    return uniqueid;
66
  }
67

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

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

    
80
  /**
81
   * The SQL Server's function name that is used to return non-NULL value
82
   *
83
   * @return return the non-NULL function name: "isnull"
84
   */
85
  public String getIsNULLFunction() {
86
    
87
    return "isnull";
88
  }
89

    
90
  /**
91
   * The SQL Server's string delimiter character: single quote (')
92
   *
93
   * @return return the string delimiter: single quote (')
94
   */
95
  public String getStringDelimiter() {
96

    
97
    return "'";
98
  }
99
  
100
 /**
101
  * MSSQL doesn't support the to_date function, so we transfer text directly.
102
  * This method will overwrite the method in AbstarctDatabase class
103
  */
104
  public String toDate(String dateString, String format)
105
  {
106
    return "'" + dateString +"'";
107
  }
108
  
109
  /**
110
   * MSSQL's syntax for doing a left join
111
   * Add 'a.' in front of the fields for first table and
112
   * 'b.' in front of the fields for the second table
113
   * 
114
   * @param selectFields fields that you want to be selected
115
   * @param tableA first table in the join
116
   * @param tableB second table in the join
117
   * @param joinCriteria the criteria based on which the join will be made
118
   * @param nonJoinCriteria all other criterias
119
   * @return return the string for teh select query
120
   */
121
  public String getLeftJoinQuery(String selectFields, String tableA, 
122
		  String tableB, String joinCriteria, String nonJoinCriteria){
123

    
124
	  return "SELECT " + selectFields + " FROM " + tableA + " a LEFT JOIN " 
125
	         + tableB + " b ON " + joinCriteria + " WHERE (" 
126
	         + nonJoinCriteria +")";
127
  }
128

    
129
/**
130
   * Return a hard code string to get xml_document list in timed replcation
131
   */
132
  public String getReplicationDocumentListSQL()
133
  {
134
      String sql ="select a.docid, a.rev, a.doctype from ( xml_documents a left outer join  xml_revisions b on (a.docid=b.docid and  a.rev<=b.rev)) where b.docid is null ";
135
      return sql;
136
  }
137
}
138
    
(4-4/4)