Project

General

Profile

1 775 bojilova
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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 7437 leinfelder
import java.sql.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
33 775 bojilova
34
/**
35
 * The MS SQL Server db adapter implementation.
36
 */
37
public class SqlserverAdapter extends AbstractDatabase {
38
39
  /**
40
   * The SQL Server unique ID generator through use of IDENTITY key
41
   * The IDENTITY key is a column in the table. When record is inserted
42
   * in the table, SELECT @@IDENTITY can return the key generated in
43
   * that IDENTITY column in the same db connection.
44
   * This is the only way to get unique id: let the SQL Server assign
45
   * a value in IDENTITY column and get it afterwards for use in the
46
   * application.
47
   *
48
   * @param conn db connection in which to generate the unique id
49
   * @param tableName the name of table which unique id to generate
50
   * @exception SQLException <br/> any SQLException that can be thrown
51
   *            during the db operation
52
   * @return return the generated unique id as a long type
53
   */
54 1136 tao
  public long getUniqueID(Connection conn, String tableName)
55 775 bojilova
                                         throws SQLException {
56
    long uniqueid = 0;
57 1129 tao
    Statement stmt = null;
58 1136 tao
    stmt = conn.createStatement();
59
    stmt.execute("SELECT @@IDENTITY");
60
    ResultSet rs = stmt.getResultSet();
61
    if ( rs.next() ) {
62 1129 tao
        uniqueid = rs.getLong(1);
63 1136 tao
    }
64 1129 tao
      stmt.close();
65 1136 tao
66 775 bojilova
    return uniqueid;
67
  }
68
69
  /**
70
   * The SQL Server's function name that gets the current date and time
71
   * from the database server: "getdate()"
72
   *
73
   * @return return the current date and time function name: "getdate()"
74
   */
75
  public String getDateTimeFunction() {
76
77
    //System.out.println("The date and time function: " + "getdate()");
78
    return "getdate()";
79
  }
80
81
  /**
82
   * The SQL Server's function name that is used to return non-NULL value
83
   *
84
   * @return return the non-NULL function name: "isnull"
85
   */
86
  public String getIsNULLFunction() {
87
88
    return "isnull";
89
  }
90
91
  /**
92
   * The SQL Server's string delimiter character: single quote (')
93
   *
94
   * @return return the string delimiter: single quote (')
95
   */
96
  public String getStringDelimiter() {
97
98
    return "'";
99
  }
100
101 1745 tao
 /**
102 1749 tao
  * MSSQL doesn't support the to_date function, so we transfer text directly.
103
  * This method will overwrite the method in AbstarctDatabase class
104 1745 tao
  */
105
  public String toDate(String dateString, String format)
106
  {
107 1749 tao
    return "'" + dateString +"'";
108 1745 tao
  }
109
110 2723 tao
  /**
111 2728 sgarg
   * MSSQL's syntax for doing a left join
112
   * Add 'a.' in front of the fields for first table and
113
   * 'b.' in front of the fields for the second table
114
   *
115
   * @param selectFields fields that you want to be selected
116
   * @param tableA first table in the join
117
   * @param tableB second table in the join
118
   * @param joinCriteria the criteria based on which the join will be made
119
   * @param nonJoinCriteria all other criterias
120
   * @return return the string for teh select query
121
   */
122
  public String getLeftJoinQuery(String selectFields, String tableA,
123
		  String tableB, String joinCriteria, String nonJoinCriteria){
124
125
	  return "SELECT " + selectFields + " FROM " + tableA + " a LEFT JOIN "
126
	         + tableB + " b ON " + joinCriteria + " WHERE ("
127
	         + nonJoinCriteria +")";
128
  }
129
130
/**
131 2723 tao
   * Return a hard code string to get xml_document list in timed replcation
132
   */
133
  public String getReplicationDocumentListSQL()
134
  {
135
      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 ";
136
      return sql;
137
  }
138 7437 leinfelder
139
  public String getPagedQuery(String queryFieldsWithOrderBy, Integer start, Integer count) {
140
	  // TODO: implement MSSQL server
141
	  return null;
142
143
  }
144 775 bojilova
}