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: leinfelder $'
9
 *     '$Date: 2012-11-29 16:12:17 -0800 (Thu, 29 Nov 2012) $'
10
 * '$Revision: 7437 $'
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.Connection;
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
import java.sql.Statement;
33

    
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
  public long getUniqueID(Connection conn, String tableName) 
55
                                         throws SQLException {
56
    long uniqueid = 0;
57
    Statement stmt = null;
58
    stmt = conn.createStatement();
59
    stmt.execute("SELECT @@IDENTITY");
60
    ResultSet rs = stmt.getResultSet();
61
    if ( rs.next() ) {
62
        uniqueid = rs.getLong(1);
63
    }
64
      stmt.close();
65

    
66
    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
 /**
102
  * MSSQL doesn't support the to_date function, so we transfer text directly.
103
  * This method will overwrite the method in AbstarctDatabase class
104
  */
105
  public String toDate(String dateString, String format)
106
  {
107
    return "'" + dateString +"'";
108
  }
109
  
110
  /**
111
   * 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
   * 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
  
139
  public String getPagedQuery(String queryFieldsWithOrderBy, Integer start, Integer count) {
140
	  // TODO: implement MSSQL server
141
	  return null;
142

    
143
  }
144
}
145
    
(4-4/4)