KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > dialect > SQLServerDialect


1 //$Id: SQLServerDialect.java,v 1.18 2005/05/02 19:55:18 oneovthafew Exp $
2
package org.hibernate.dialect;
3
4 import java.sql.Types JavaDoc;
5
6 import org.hibernate.Hibernate;
7 import org.hibernate.LockMode;
8 import org.hibernate.dialect.function.SQLFunctionTemplate;
9
10 /**
11  * A dialect for Microsoft SQL Server 2000
12  * @author Gavin King
13  */

14 public class SQLServerDialect extends SybaseDialect {
15
16     public SQLServerDialect() {
17         registerColumnType( Types.VARBINARY, "image" );
18         registerColumnType( Types.VARBINARY, 8000, "varbinary($l)" );
19         
20         registerFunction( "second", new SQLFunctionTemplate(Hibernate.INTEGER, "datepart(second, ?1)") );
21         registerFunction( "minute", new SQLFunctionTemplate(Hibernate.INTEGER, "datepart(minute, ?1)") );
22         registerFunction( "hour", new SQLFunctionTemplate(Hibernate.INTEGER, "datepart(hour, ?1)") );
23     }
24
25     public String JavaDoc getNoColumnsInsertString() {
26         return "default values";
27     }
28
29     static int getAfterSelectInsertPoint(String JavaDoc sql) {
30         int selectIndex = sql.toLowerCase().indexOf( "select" );
31         final int selectDistinctIndex = sql.toLowerCase().indexOf( "select distinct" );
32         return selectIndex + ( selectDistinctIndex == selectIndex ? 15 : 6 );
33     }
34
35     public String JavaDoc getLimitString(String JavaDoc querySelect, int offset, int limit) {
36         if (offset>0) throw new UnsupportedOperationException JavaDoc("sql server has no offset");
37         return new StringBuffer JavaDoc( querySelect.length()+8 )
38             .append(querySelect)
39             .insert( getAfterSelectInsertPoint(querySelect), " top " + limit )
40             .toString();
41     }
42
43     /**
44      * Use <tt>insert table(...) values(...) select SCOPE_IDENTITY()</tt>
45      *
46      * @author <a HREF="mailto:jkristian@docent.com">John Kristian</a>
47      */

48     public String JavaDoc appendIdentitySelectToInsert(String JavaDoc insertSQL) {
49         return insertSQL + " select scope_identity()";
50     }
51
52     public boolean supportsLimit() {
53         return true;
54     }
55
56     public boolean useMaxForLimit() {
57         return true;
58     }
59
60     public boolean supportsLimitOffset() {
61         return false;
62     }
63
64     public boolean supportsVariableLimit() {
65         return false;
66     }
67
68     public char closeQuote() {
69         return ']';
70     }
71
72     public char openQuote() {
73         return '[';
74     }
75
76     public String JavaDoc appendLockHint(LockMode mode, String JavaDoc tableName) {
77         if ( mode.greaterThan(LockMode.READ) ) {
78             return tableName + " with (updlock, rowlock)";
79         }
80         else {
81             return tableName;
82         }
83     }
84     
85     public String JavaDoc getSelectGUIDString() {
86         return "select newid()";
87     }
88     
89 }
90
Popular Tags