Source code: org/hibernate/dialect/SybaseDialect.java
1 //$Id: SybaseDialect.java,v 1.15 2005/04/27 04:12:34 oneovthafew Exp $
2 package org.hibernate.dialect;
3
4 import java.sql.CallableStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Types;
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, false) );
55 registerFunction( "current_time", new NoArgSQLFunction("getdate", Hibernate.TIME, false) );
56 registerFunction( "current_date", new NoArgSQLFunction("getdate", Hibernate.DATE, false) );
57 registerFunction( "getdate", new NoArgSQLFunction("getdate", Hibernate.TIMESTAMP) );
58 registerFunction( "getutcdate", new NoArgSQLFunction("getutcdate", Hibernate.TIMESTAMP) );
59 registerFunction( "day", new StandardSQLFunction("day", Hibernate.INTEGER) );
60 registerFunction( "month", new StandardSQLFunction("month", Hibernate.INTEGER) );
61 registerFunction( "year", new StandardSQLFunction("year", Hibernate.INTEGER) );
62 registerFunction( "datename", new StandardSQLFunction("datename", Hibernate.STRING) );
63
64 registerFunction( "abs", new StandardSQLFunction("abs") );
65 registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
66
67 registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) );
68 registerFunction( "asin", new StandardSQLFunction("asin", Hibernate.DOUBLE) );
69 registerFunction( "atan", new StandardSQLFunction("atan", Hibernate.DOUBLE) );
70 registerFunction( "cos", new StandardSQLFunction("cos", Hibernate.DOUBLE) );
71 registerFunction( "cot", new StandardSQLFunction("cot", Hibernate.DOUBLE) );
72 registerFunction( "exp", new StandardSQLFunction("exp", Hibernate.DOUBLE) );
73 registerFunction( "log", new StandardSQLFunction( "log", Hibernate.DOUBLE) );
74 registerFunction( "log10", new StandardSQLFunction("log10", Hibernate.DOUBLE) );
75 registerFunction( "sin", new StandardSQLFunction("sin", Hibernate.DOUBLE) );
76 registerFunction( "sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE) );
77 registerFunction( "tan", new StandardSQLFunction("tan", Hibernate.DOUBLE) );
78 registerFunction( "pi", new NoArgSQLFunction("pi", Hibernate.DOUBLE) );
79 registerFunction( "square", new StandardSQLFunction("square") );
80 registerFunction( "rand", new StandardSQLFunction("rand", Hibernate.FLOAT) );
81
82 registerFunction("radians", new StandardSQLFunction("radians", Hibernate.DOUBLE) );
83 registerFunction("degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE) );
84
85 registerFunction( "round", new StandardSQLFunction("round") );
86 registerFunction( "ceiling", new StandardSQLFunction("ceiling") );
87 registerFunction( "floor", new StandardSQLFunction("floor") );
88
89 registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","+",")" ) );
90
91 getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, NO_BATCH);
92 }
93
94 public String getAddColumnString() {
95 return "add";
96 }
97 public String getNullColumnString() {
98 return " null";
99 }
100 public boolean qualifyIndexName() {
101 return false;
102 }
103
104 public String getForUpdateString() {
105 return "";
106 }
107
108 public boolean supportsIdentityColumns() {
109 return true;
110 }
111 public String getIdentitySelectString() {
112 return "select @@identity";
113 }
114 public String getIdentityColumnString() {
115 return "identity not null"; //starts with 1, implicitly
116 }
117
118 public boolean supportsInsertSelectIdentity() {
119 return true;
120 }
121
122 public String appendIdentitySelectToInsert(String insertSQL) {
123 return insertSQL + "\nselect @@identity";
124 }
125
126 public String appendLockHint(LockMode mode, String tableName) {
127 if ( mode.greaterThan(LockMode.READ) ) {
128 return tableName + " holdlock";
129 }
130 else {
131 return tableName;
132 }
133 }
134
135 public int registerResultSetOutParameter(CallableStatement statement,int col) throws SQLException {
136 return col; // sql server just returns automatically.
137 }
138
139 public ResultSet getResultSet(CallableStatement ps) throws SQLException {
140 // This will work with SQL Server/Sybase
141 boolean isResultSet = ps.execute();
142 // This assumes you will want to ignore any update counts
143 while (!isResultSet && ps.getUpdateCount() != -1) {
144 isResultSet = ps.getMoreResults();
145 }
146 ResultSet rs = ps.getResultSet();
147 // You may still have other ResultSets or update counts left to process here
148 // but you can't do it now or the ResultSet you just got will be closed
149 return rs;
150 }
151
152 }