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
 *
8
 *   '$Author: jones $'
9
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
10
 * '$Revision: 3077 $'
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.*;
30
import edu.ucsb.nceas.metacat.*;
31

    
32
/**
33
 * The MS SQL Server db adapter implementation.
34
 */
35
public class SqlserverAdapter extends AbstractDatabase {
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 = null;
56
    stmt = conn.createStatement();
57
    stmt.execute("SELECT @@IDENTITY");
58
    ResultSet rs = stmt.getResultSet();
59
    if ( rs.next() ) {
60
        uniqueid = rs.getLong(1);
61
    }
62
      stmt.close();
63

    
64
    return uniqueid;
65
  }
66

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

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

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

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

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

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

    
128
/**
129
   * Return a hard code string to get xml_document list in timed replcation
130
   */
131
  public String getReplicationDocumentListSQL()
132
  {
133
      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 ";
134
      return sql;
135
  }
136
}
137
    
(4-4/4)