Mercurial > pub > bltoolkit
comparison Source/Data/DataProvider/SybaseDataProvider.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Data; | |
4 using System.Data.Common; | |
5 using System.Text; | |
6 | |
7 using BLToolkit.Mapping; | |
8 | |
9 using Sybase.Data.AseClient; | |
10 | |
11 namespace BLToolkit.Data.DataProvider | |
12 { | |
13 using Sql.SqlProvider; | |
14 | |
15 public class SybaseDataProvider : DataProviderBase | |
16 { | |
17 public override IDbConnection CreateConnectionObject() | |
18 { | |
19 return new AseConnection(); | |
20 } | |
21 | |
22 public override DbDataAdapter CreateDataAdapterObject() | |
23 { | |
24 return new AseDataAdapter(); | |
25 } | |
26 | |
27 public override bool DeriveParameters(IDbCommand command) | |
28 { | |
29 AseCommandBuilder.DeriveParameters((AseCommand)command); | |
30 return true; | |
31 } | |
32 | |
33 public override object Convert(object value, ConvertType convertType) | |
34 { | |
35 switch (convertType) | |
36 { | |
37 case ConvertType.ExceptionToErrorNumber: | |
38 if (value is AseException) | |
39 { | |
40 var ex = (AseException)value; | |
41 | |
42 foreach (AseError error in ex.Errors) | |
43 if (error.IsError) | |
44 return error.MessageNumber; | |
45 | |
46 foreach (AseError error in ex.Errors) | |
47 if (error.MessageNumber != 0) | |
48 return error.MessageNumber; | |
49 | |
50 return 0; | |
51 } | |
52 | |
53 break; | |
54 | |
55 case ConvertType.ExceptionToErrorMessage: | |
56 if (value is AseException) | |
57 { | |
58 try | |
59 { | |
60 var ex = (AseException)value; | |
61 var sb = new StringBuilder(); | |
62 | |
63 foreach (AseError error in ex.Errors) | |
64 if (error.IsError) | |
65 sb.AppendFormat("{0} Ln: {1}{2}", | |
66 error.Message.TrimEnd('\n', '\r'), error.LineNum, Environment.NewLine); | |
67 | |
68 foreach (AseError error in ex.Errors) | |
69 if (!error.IsError) | |
70 sb.AppendFormat("* {0}{1}", error.Message, Environment.NewLine); | |
71 | |
72 return sb.Length == 0 ? ex.Message : sb.ToString(); | |
73 } | |
74 catch | |
75 { | |
76 } | |
77 } | |
78 | |
79 break; | |
80 } | |
81 | |
82 return SqlProvider.Convert(value, convertType); | |
83 } | |
84 | |
85 public override void AttachParameter(IDbCommand command, IDbDataParameter parameter) | |
86 { | |
87 if (parameter.Value is string && parameter.DbType == DbType.Guid) | |
88 parameter.DbType = DbType.AnsiString; | |
89 | |
90 base.AttachParameter(command, parameter); | |
91 | |
92 var p = (AseParameter)parameter; | |
93 | |
94 if (p.AseDbType == AseDbType.Unsupported && p.Value is DBNull) | |
95 parameter.DbType = DbType.AnsiString; | |
96 } | |
97 | |
98 public override Type ConnectionType | |
99 { | |
100 get { return typeof(AseConnection); } | |
101 } | |
102 | |
103 public override string Name | |
104 { | |
105 get { return DataProvider.ProviderName.Sybase; } | |
106 } | |
107 | |
108 public override ISqlProvider CreateSqlProvider() | |
109 { | |
110 return new SybaseSqlProvider(); | |
111 } | |
112 | |
113 public override bool InitParameter(IDbDataParameter parameter) | |
114 { | |
115 if (parameter.Value is Guid) | |
116 { | |
117 parameter.Value = parameter.Value.ToString(); | |
118 parameter.DbType = DbType.StringFixedLength; | |
119 parameter.Size = 36; | |
120 | |
121 return true; | |
122 } | |
123 | |
124 return false; | |
125 } | |
126 | |
127 public override void PrepareCommand(ref CommandType commandType, ref string commandText, ref IDbDataParameter[] commandParameters) | |
128 { | |
129 base.PrepareCommand(ref commandType, ref commandText, ref commandParameters); | |
130 | |
131 List<IDbDataParameter> list = null; | |
132 | |
133 if (commandParameters != null) for (var i = 0; i < commandParameters.Length; i++) | |
134 { | |
135 var p = commandParameters[i]; | |
136 | |
137 if (p.Value is Guid) | |
138 { | |
139 p.Value = p.Value.ToString(); | |
140 p.DbType = DbType.StringFixedLength; | |
141 p.Size = 36; | |
142 } | |
143 | |
144 if (commandType == CommandType.Text) | |
145 { | |
146 if (commandText.IndexOf(p.ParameterName) < 0) | |
147 { | |
148 if (list == null) | |
149 { | |
150 list = new List<IDbDataParameter>(commandParameters.Length); | |
151 | |
152 for (var j = 0; j < i; j++) | |
153 list.Add(commandParameters[j]); | |
154 } | |
155 } | |
156 else | |
157 { | |
158 if (list != null) | |
159 list.Add(p); | |
160 } | |
161 } | |
162 } | |
163 | |
164 if (list != null) | |
165 commandParameters = list.ToArray(); | |
166 } | |
167 | |
168 public override string EndOfSql { get { return ""; } } | |
169 | |
170 public override DbType GetDbType(Type systemType) | |
171 { | |
172 if (systemType == typeof(byte[])) | |
173 return DbType.Object; | |
174 | |
175 return base.GetDbType(systemType); | |
176 } | |
177 | |
178 #region DataReaderEx | |
179 | |
180 public override IDataReader GetDataReader(MappingSchema schema, IDataReader dataReader) | |
181 { | |
182 return dataReader is AseDataReader? | |
183 new DataReaderEx((AseDataReader)dataReader): | |
184 base.GetDataReader(schema, dataReader); | |
185 } | |
186 | |
187 class DataReaderEx : DataReaderBase<AseDataReader>, IDataReader | |
188 { | |
189 public DataReaderEx(AseDataReader rd): base(rd) | |
190 { | |
191 } | |
192 | |
193 public new object GetValue(int i) | |
194 { | |
195 var value = DataReader.GetValue(i); | |
196 | |
197 if (value is DateTime) | |
198 { | |
199 var dt = (DateTime)value; | |
200 | |
201 if (dt.Year == 1900 && dt.Month == 1 && dt.Day == 1) | |
202 return new DateTime(1, 1, 1, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); | |
203 } | |
204 | |
205 return value; | |
206 } | |
207 | |
208 public new DateTime GetDateTime(int i) | |
209 { | |
210 var dt = DataReader.GetDateTime(i); | |
211 | |
212 if (dt.Year == 1900 && dt.Month == 1 && dt.Day == 1) | |
213 return new DateTime(1, 1, 1, dt.Hour, dt.Minute, dt.Second, dt.Millisecond); | |
214 | |
215 return dt; | |
216 } | |
217 } | |
218 | |
219 #endregion | |
220 } | |
221 } |