|
1 | 1 | using System;
|
2 |
| -using System.Text; |
| 2 | +using System.Collections; |
3 | 3 | using System.Data;
|
| 4 | +using System.Text; |
4 | 5 |
|
| 6 | +using NHibernate.SqlCommand; |
| 7 | +using NHibernate.SqlTypes; |
5 | 8 | using NHibernate.Util;
|
6 | 9 |
|
7 |
| -namespace NHibernate.Dialect { |
8 |
| - |
| 10 | +namespace NHibernate.Dialect |
| 11 | +{ |
9 | 12 | /// <summary>
|
10 |
| - /// An SQL dialect for Oracle 9 (uses ANSI-style syntax where possible). |
| 13 | + /// It's a immature version, it just work. |
| 14 | + /// An SQL dialect for Oracle 9 |
11 | 15 | /// </summary>
|
12 |
| - public class Oracle9Dialect : Dialect { |
13 |
| - |
14 |
| - public Oracle9Dialect() : base() { |
15 |
| - /* Type mapping in Java was: |
16 |
| - |
17 |
| - Types.BIT, "NUMBER(1,0)" |
18 |
| - Types.BIGINT, "NUMBER(19,0)" |
19 |
| - Types.SMALLINT, "NUMBER(5,0)" |
20 |
| - Types.TINYINT, "NUMBER(3,0)" |
21 |
| - Types.INTEGER, "NUMBER(10,0)" |
22 |
| - Types.CHAR, "CHAR(1)" |
23 |
| - Types.VARCHAR, "VARCHAR2($l)" |
24 |
| - Types.FLOAT, "FLOAT" |
25 |
| - Types.DOUBLE, "DOUBLE PRECISION" |
26 |
| - Types.DATE, "DATE" |
27 |
| - Types.TIME, "DATE" |
28 |
| - Types.TIMESTAMP, "DATE" |
29 |
| - Types.VARBINARY, "RAW($l)" |
30 |
| - Types.NUMERIC, "NUMBER(19, $l)" |
31 |
| - Types.BLOB, "BLOB" |
32 |
| - Types.CLOB, "CLOB" |
33 |
| - */ |
34 |
| - |
35 |
| - Register( DbType.Bit, "NUMBER(1,0)" ); |
36 |
| - Register( DbType.BigInt, "NUMBER(19,0)" ); |
37 |
| - Register( DbType.SmallInt, "NUMBER(5,0)" ); |
38 |
| - Register( DbType.TinyInt, "NUMBER(3,0)" ); |
39 |
| - Register( DbType.Int, "NUMBER(10,0)" ); |
40 |
| - Register( DbType.Char, "CHAR(1)" ); |
41 |
| - Register( DbType.VarChar, "VARCHAR2($l)" ); |
42 |
| - Register( DbType.Float, "FLOAT" ); |
43 |
| - Register( DbType.Real, "DOUBLE PRECISION" ); |
44 |
| - Register( DbType.DateTime, "DATE" ); |
45 |
| - Register( DbType.SmallDateTime, "DATE" ); |
46 |
| - Register( DbType.Timestamp, "DATE" ); |
47 |
| - Register( DbType.VarBinary, "RAW($l)" ); |
48 |
| - Register( DbType.Decimal, "NUMBER(19, $l)" ); |
49 |
| - Register( DbType.Image, "BLOB" ); |
50 |
| - Register( DbType.Text, "CLOB" ); |
| 16 | + public class Oracle9Dialect : Dialect |
| 17 | + { |
51 | 18 |
|
| 19 | + private readonly IDictionary aggregateFunctions = new Hashtable(); |
| 20 | + |
| 21 | + public Oracle9Dialect() : base() |
| 22 | + { |
52 | 23 | /* TODO:
|
53 | 24 | getDefaultProperties().setProperty(Environment.USE_STREAMS_FOR_BINARY, "true");
|
54 | 25 | getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
|
55 | 26 | getDefaultProperties().setProperty(Environment.OUTER_JOIN, "true");
|
56 | 27 | */
|
| 28 | + |
| 29 | + // add all the functions from the base into this instance |
| 30 | + foreach(DictionaryEntry de in base.AggregateFunctions) |
| 31 | + { |
| 32 | + aggregateFunctions[de.Key] = de.Value; |
| 33 | + } |
| 34 | + aggregateFunctions["trunc"] = new QueryFunctionStandard(); |
| 35 | + aggregateFunctions["round"] = new QueryFunctionStandard(); |
| 36 | + aggregateFunctions["abs"] = new QueryFunctionStandard(NHibernate.Int32); |
| 37 | + aggregateFunctions["sign"] = new QueryFunctionStandard(NHibernate.Int32); |
| 38 | + aggregateFunctions["ceil"] = new QueryFunctionStandard(NHibernate.Int32); |
| 39 | + aggregateFunctions["floor"] = new QueryFunctionStandard(NHibernate.Int32); |
| 40 | + aggregateFunctions["sqrt"] = new QueryFunctionStandard(); |
| 41 | + aggregateFunctions["exp"] = new QueryFunctionStandard(); |
| 42 | + aggregateFunctions["ln"] = new QueryFunctionStandard(); |
| 43 | + aggregateFunctions["sin"] = new QueryFunctionStandard(); |
| 44 | + aggregateFunctions["sinh"] = new QueryFunctionStandard(); |
| 45 | + aggregateFunctions["cos"] = new QueryFunctionStandard(); |
| 46 | + aggregateFunctions["cosh"] = new QueryFunctionStandard(); |
| 47 | + aggregateFunctions["tan"] = new QueryFunctionStandard(); |
| 48 | + aggregateFunctions["tanh"] = new QueryFunctionStandard(); |
| 49 | + aggregateFunctions["stddev"] = new QueryFunctionStandard(); |
| 50 | + aggregateFunctions["variance"] = new QueryFunctionStandard(); |
| 51 | + //TODO: aggregateFunctions["sysdate"] = new TODO; |
| 52 | + aggregateFunctions["lastday"] = new QueryFunctionStandard(NHibernate.Date); |
57 | 53 | }
|
58 | 54 |
|
59 |
| - public override string AddColumnString { |
| 55 | + public override string AddColumnString |
| 56 | + { |
60 | 57 | get { return "add"; }
|
61 | 58 | }
|
62 |
| - |
63 |
| - public override string GetSequenceNextValString(string sequenceName) { |
64 |
| - return string.Concat( "select ", sequenceName, ".nextval from dual" ); |
| 59 | + |
| 60 | + public override string GetSequenceNextValString(string sequenceName) |
| 61 | + { |
| 62 | + return "select " + sequenceName + ".nextval from dual"; |
65 | 63 | }
|
66 |
| - public override string GetCreateSequenceString(string sequenceName) { |
67 |
| - return "create sequence " + sequenceName; |
| 64 | + |
| 65 | + public override string GetCreateSequenceString(string sequenceName) |
| 66 | + { |
| 67 | + return "create sequence " + sequenceName + " INCREMENT BY 1 START WITH 1 MAXVALUE 1.0E28 MINVALUE 1 NOCYCLE CACHE 20 NOORDER"; |
68 | 68 | }
|
69 |
| - public override string GetDropSequenceString(string sequenceName) { |
| 69 | + |
| 70 | + public override string GetDropSequenceString(string sequenceName) |
| 71 | + { |
70 | 72 | return "drop sequence " + sequenceName;
|
71 | 73 | }
|
72 | 74 |
|
73 |
| - public string CascadeConstraintsString { |
| 75 | + public override string CascadeConstraintsString |
| 76 | + { |
74 | 77 | get { return " cascade constraints"; }
|
75 | 78 | }
|
76 | 79 |
|
77 |
| - public bool SupportsForUpdateNowait { |
| 80 | + public override bool SupportsForUpdateNoWait |
| 81 | + { |
78 | 82 | get { return true; }
|
79 | 83 | }
|
80 |
| - |
81 |
| - public override bool SupportsSequences { |
| 84 | + |
| 85 | + public override bool SupportsSequences |
| 86 | + { |
82 | 87 | get { return true; }
|
| 88 | + } |
| 89 | + |
| 90 | + public override bool SupportsLimit |
| 91 | + { |
| 92 | + get { return true; } |
| 93 | + } |
| 94 | + |
| 95 | + public override string GetLimitString(String querySelect) |
| 96 | + { |
| 97 | + StringBuilder pagingSelect = new StringBuilder(100); |
| 98 | + pagingSelect.Append("select * from ( select row_.*, rownum rownum_ from ( "); |
| 99 | + pagingSelect.Append(querySelect); |
| 100 | + pagingSelect.Append(" ) row_ where rownum <= ?) where rownum_ > ?"); |
| 101 | + return pagingSelect.ToString(); |
83 | 102 | }
|
84 | 103 |
|
85 |
| - public override bool UseNamedParameters { |
| 104 | + public override SqlString GetLimitString(SqlString querySqlString) |
| 105 | + { |
| 106 | + Parameter p1 = new Parameter(); |
| 107 | + Parameter p2 = new Parameter(); |
| 108 | + |
| 109 | + p1.Name = "p1"; |
| 110 | + p1.DbType = DbType.Int16; |
| 111 | + |
| 112 | + p2.Name = "p2"; |
| 113 | + p2.DbType = DbType.Int16; |
| 114 | + |
| 115 | + /* |
| 116 | + * "select * from ( select row_.*, rownum rownum_ from ( " |
| 117 | + * sql |
| 118 | + * " ) row_ where rownum <= ?) where rownum_ > ?" |
| 119 | + */ |
| 120 | + SqlStringBuilder pagingBuilder = new SqlStringBuilder(); |
| 121 | + pagingBuilder.Add("select * from ( select row_.*, rownum rownum_ from ( "); |
| 122 | + pagingBuilder.Add(querySqlString); |
| 123 | + pagingBuilder.Add(" ) row_ where rownum <= "); |
| 124 | + pagingBuilder.Add(p1); |
| 125 | + pagingBuilder.Add(") where rownum_ > "); |
| 126 | + pagingBuilder.Add(p2); |
| 127 | + |
| 128 | + return pagingBuilder.ToSqlString(); |
| 129 | + } |
| 130 | + |
| 131 | + public override bool BindLimitParametersInReverseOrder |
| 132 | + { |
| 133 | + get { return true; } |
| 134 | + } |
| 135 | + |
| 136 | + public override bool SupportsForUpdateOf |
| 137 | + { |
| 138 | + get { return true; } |
| 139 | + } |
| 140 | + |
| 141 | + public override IDictionary AggregateFunctions |
| 142 | + { |
| 143 | + get { return aggregateFunctions; } |
| 144 | + } |
| 145 | + |
| 146 | + public override bool UseMaxForLimit |
| 147 | + { |
| 148 | + get { return true; } |
| 149 | + } |
| 150 | + |
| 151 | + [Obsolete("See the Dialect class for reason")] |
| 152 | + public override bool UseNamedParameters |
| 153 | + { |
86 | 154 | get { return true; }
|
87 | 155 | }
|
88 | 156 |
|
89 |
| - public override string NamedParametersPrefix { |
| 157 | + [Obsolete("See the Dialect class for reason")] |
| 158 | + public override string NamedParametersPrefix |
| 159 | + { |
90 | 160 | get { return ":"; }
|
91 | 161 | }
|
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + private string SqlTypeToString(string name, int length) |
| 166 | + { |
| 167 | + return name + "(" + length + ")"; |
| 168 | + } |
| 169 | + |
| 170 | + private string SqlTypeToString(string name, int precision, int scale) |
| 171 | + { |
| 172 | + if (precision > 19) precision = 19; |
| 173 | + return name + "(" + precision + ", " + scale + ")"; |
| 174 | + } |
| 175 | + |
| 176 | + protected override string SqlTypeToString(AnsiStringFixedLengthSqlType sqlType) |
| 177 | + { |
| 178 | + return SqlTypeToString("NVARCHAR2", 1000); |
| 179 | + } |
| 180 | + |
| 181 | + protected override string SqlTypeToString(BinarySqlType sqlType) |
| 182 | + { |
| 183 | + if(sqlType.Length <= 8000) |
| 184 | + { |
| 185 | + return SqlTypeToString("RAW", sqlType.Length); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + return "BLOB"; // should use the IType.BlobType |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + protected override string SqlTypeToString(BooleanSqlType sqlType) |
| 194 | + { |
| 195 | + return "NUMBER(1,0)"; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + protected override string SqlTypeToString(ByteSqlType sqlType) |
| 200 | + { |
| 201 | + return "NUMBER(3,0)"; |
| 202 | + } |
| 203 | + |
| 204 | + protected override string SqlTypeToString(CurrencySqlType sqlType) |
| 205 | + { |
| 206 | + return "NUMBER(19, 1)"; |
| 207 | + } |
| 208 | + |
| 209 | + protected override string SqlTypeToString(DateSqlType sqlType) |
| 210 | + { |
| 211 | + return "DATE"; |
| 212 | + } |
| 213 | + |
| 214 | + protected override string SqlTypeToString(DateTimeSqlType sqlType) |
| 215 | + { |
| 216 | + return "DATE"; |
| 217 | + } |
| 218 | + |
| 219 | + protected override string SqlTypeToString(DecimalSqlType sqlType) |
| 220 | + { |
| 221 | + return SqlTypeToString("NUMBER", sqlType.Precision, sqlType.Scale); |
| 222 | + } |
| 223 | + |
| 224 | + protected override string SqlTypeToString(DoubleSqlType sqlType) |
| 225 | + { |
| 226 | + return "DOUBLE PRECISION"; |
| 227 | + } |
| 228 | + |
| 229 | + protected override string SqlTypeToString(Int16SqlType sqlType) |
| 230 | + { |
| 231 | + return "NUMBER(5,0)"; |
| 232 | + } |
| 233 | + |
| 234 | + protected override string SqlTypeToString(Int32SqlType sqlType) |
| 235 | + { |
| 236 | + return "NUMBER(10,0)"; |
| 237 | + } |
| 238 | + |
| 239 | + protected override string SqlTypeToString(Int64SqlType sqlType) |
| 240 | + { |
| 241 | + return "NUMBER(20,0)"; |
| 242 | + } |
| 243 | + |
| 244 | + protected override string SqlTypeToString(SingleSqlType sqlType) |
| 245 | + { |
| 246 | + return "FLOAT"; |
| 247 | + } |
| 248 | + |
| 249 | + protected override string SqlTypeToString(StringFixedLengthSqlType sqlType) |
| 250 | + { |
| 251 | + if(sqlType.Length <= 2000) |
| 252 | + { |
| 253 | + return SqlTypeToString("NVARCHAR2", sqlType.Length); |
| 254 | + } |
| 255 | + else |
| 256 | + { |
| 257 | + return string.Empty; // should use the IType.ClobType |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + protected override string SqlTypeToString(StringSqlType sqlType) |
| 262 | + { |
| 263 | + if(sqlType.Length <= 2000) |
| 264 | + { |
| 265 | + return SqlTypeToString("NVARCHAR2", sqlType.Length); |
| 266 | + } |
| 267 | + else |
| 268 | + { |
| 269 | + return string.Empty; // should use the IType.ClobType |
| 270 | + } |
| 271 | + } |
92 | 272 | }
|
93 | 273 | }
|
| 274 | + |
0 commit comments