KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SybaseDialect.java,v 1.20 2005/05/26 15:24:15 oneovthafew Exp $
2
package org.hibernate.dialect;
3
4 import java.sql.CallableStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Types JavaDoc;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.LockMode;
11 import org.hibernate.cfg.Environment;
12 import org.hibernate.dialect.function.NoArgSQLFunction;
13 import org.hibernate.dialect.function.StandardSQLFunction;
14 import org.hibernate.dialect.function.VarArgsSQLFunction;
15
16 /**
17  * An SQL dialect compatible with Sybase and MS SQL Server.
18  * @author Gavin King
19  */

20
21 public class SybaseDialect extends Dialect {
22     public SybaseDialect() {
23         super();
24         registerColumnType( Types.BIT, "tinyint" ); //Sybase BIT type does not support null values
25
registerColumnType( Types.BIGINT, "numeric(19,0)" );
26         registerColumnType( Types.SMALLINT, "smallint" );
27         registerColumnType( Types.TINYINT, "tinyint" );
28         registerColumnType( Types.INTEGER, "int" );
29         registerColumnType( Types.CHAR, "char(1)" );
30         registerColumnType( Types.VARCHAR, "varchar($l)" );
31         registerColumnType( Types.FLOAT, "float" );
32         registerColumnType( Types.DOUBLE, "double precision" );
33         registerColumnType( Types.DATE, "datetime" );
34         registerColumnType( Types.TIME, "datetime" );
35         registerColumnType( Types.TIMESTAMP, "datetime" );
36         registerColumnType( Types.VARBINARY, "varbinary($l)" );
37         registerColumnType( Types.NUMERIC, "numeric($p,$s)" );
38         registerColumnType( Types.BLOB, "image" );
39         registerColumnType( Types.CLOB, "text" );
40
41         registerFunction( "ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER) );
42         registerFunction( "char", new StandardSQLFunction("char", Hibernate.CHARACTER) );
43         registerFunction( "len", new StandardSQLFunction("len", Hibernate.LONG) );
44         registerFunction( "lower", new StandardSQLFunction("lower") );
45         registerFunction( "upper", new StandardSQLFunction("upper") );
46         registerFunction( "str", new StandardSQLFunction("str", Hibernate.STRING) );
47         registerFunction( "ltrim", new StandardSQLFunction("ltrim") );
48         registerFunction( "rtrim", new StandardSQLFunction("rtrim") );
49         registerFunction( "reverse", new StandardSQLFunction("reverse") );
50         registerFunction( "space", new StandardSQLFunction("space", Hibernate.STRING) );
51
52         registerFunction( "user", new NoArgSQLFunction("user", Hibernate.STRING) );
53
54         registerFunction( "current_timestamp", new NoArgSQLFunction("getdate", Hibernate.TIMESTAMP) );
55         registerFunction( "current_time", new NoArgSQLFunction("getdate", Hibernate.TIME) );
56         registerFunction( "current_date", new NoArgSQLFunction("getdate", Hibernate.DATE) );
57         
58         registerFunction( "getdate", new NoArgSQLFunction("getdate", Hibernate.TIMESTAMP) );
59         registerFunction( "getutcdate", new NoArgSQLFunction("getutcdate", Hibernate.TIMESTAMP) );
60         registerFunction( "day", new StandardSQLFunction("day", Hibernate.INTEGER) );
61         registerFunction( "month", new StandardSQLFunction("month", Hibernate.INTEGER) );
62         registerFunction( "year", new StandardSQLFunction("year", Hibernate.INTEGER) );
63         registerFunction( "datename", new StandardSQLFunction("datename", Hibernate.STRING) );
64
65         registerFunction( "abs", new StandardSQLFunction("abs") );
66         registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
67
68         registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) );
69         registerFunction( "asin", new StandardSQLFunction("asin", Hibernate.DOUBLE) );
70         registerFunction( "atan", new StandardSQLFunction("atan", Hibernate.DOUBLE) );
71         registerFunction( "cos", new StandardSQLFunction("cos", Hibernate.DOUBLE) );
72         registerFunction( "cot", new StandardSQLFunction("cot", Hibernate.DOUBLE) );
73         registerFunction( "exp", new StandardSQLFunction("exp", Hibernate.DOUBLE) );
74         registerFunction( "log", new StandardSQLFunction( "log", Hibernate.DOUBLE) );
75         registerFunction( "log10", new StandardSQLFunction("log10", Hibernate.DOUBLE) );
76         registerFunction( "sin", new StandardSQLFunction("sin", Hibernate.DOUBLE) );
77         registerFunction( "sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE) );
78         registerFunction( "tan", new StandardSQLFunction("tan", Hibernate.DOUBLE) );
79         registerFunction( "pi", new NoArgSQLFunction("pi", Hibernate.DOUBLE) );
80         registerFunction( "square", new StandardSQLFunction("square") );
81         registerFunction( "rand", new StandardSQLFunction("rand", Hibernate.FLOAT) );
82
83         registerFunction("radians", new StandardSQLFunction("radians", Hibernate.DOUBLE) );
84         registerFunction("degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE) );
85
86         registerFunction( "round", new StandardSQLFunction("round") );
87         registerFunction( "ceiling", new StandardSQLFunction("ceiling") );
88         registerFunction( "floor", new StandardSQLFunction("floor") );
89
90         registerFunction( "isnull", new StandardSQLFunction("isnull") );
91
92         registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","+",")" ) );
93         
94         registerFunction( "length", new StandardSQLFunction( "len", Hibernate.INTEGER ) );
95
96         getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, NO_BATCH);
97     }
98
99     public String JavaDoc getAddColumnString() {
100         return "add";
101     }
102     public String JavaDoc getNullColumnString() {
103         return " null";
104     }
105     public boolean qualifyIndexName() {
106         return false;
107     }
108
109     public String JavaDoc getForUpdateString() {
110         return "";
111     }
112
113     public boolean supportsIdentityColumns() {
114         return true;
115     }
116     public String JavaDoc getIdentitySelectString() {
117         return "select @@identity";
118     }
119     public String JavaDoc getIdentityColumnString() {
120         return "identity not null"; //starts with 1, implicitly
121
}
122
123     public boolean supportsInsertSelectIdentity() {
124         return true;
125     }
126
127     public String JavaDoc appendIdentitySelectToInsert(String JavaDoc insertSQL) {
128         return insertSQL + "\nselect @@identity";
129     }
130
131     public String JavaDoc appendLockHint(LockMode mode, String JavaDoc tableName) {
132         if ( mode.greaterThan(LockMode.READ) ) {
133             return tableName + " holdlock";
134         }
135         else {
136             return tableName;
137         }
138     }
139     
140     public int registerResultSetOutParameter(CallableStatement JavaDoc statement, int col) throws SQLException JavaDoc {
141         return col; // sql server just returns automatically
142
}
143     
144     public ResultSet JavaDoc getResultSet(CallableStatement JavaDoc ps) throws SQLException JavaDoc {
145         boolean isResultSet = ps.execute();
146 // This assumes you will want to ignore any update counts
147
while (!isResultSet && ps.getUpdateCount() != -1) {
148             isResultSet = ps.getMoreResults();
149         }
150         ResultSet JavaDoc rs = ps.getResultSet();
151 // You may still have other ResultSets or update counts left to process here
152
// but you can't do it now or the ResultSet you just got will be closed
153
return rs;
154     }
155
156 }
157
Popular Tags