Mercurial > pub > bltoolkit
comparison Source/Data/DataProvider/PostgreSQLDataProvider.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.Data; | |
3 using System.Data.Common; | |
4 | |
5 using Npgsql; | |
6 | |
7 namespace BLToolkit.Data.DataProvider | |
8 { | |
9 using Sql.SqlProvider; | |
10 using BLToolkit.Mapping; | |
11 | |
12 public class PostgreSQLDataProvider : DataProviderBase | |
13 { | |
14 #region Configurable | |
15 | |
16 public enum CaseConvert | |
17 { | |
18 None, | |
19 Lower, | |
20 Upper | |
21 } | |
22 | |
23 public static CaseConvert QueryCaseConvert = CaseConvert.None; | |
24 | |
25 public static bool QuoteIdentifiers | |
26 { | |
27 get { return PostgreSQLSqlProvider.QuoteIdentifiers; } | |
28 set { PostgreSQLSqlProvider.QuoteIdentifiers = value; } | |
29 } | |
30 | |
31 public override void Configure(System.Collections.Specialized.NameValueCollection attributes) | |
32 { | |
33 var quoteIdentifiers = attributes["QuoteIdentifiers"]; | |
34 | |
35 if (quoteIdentifiers != null) | |
36 QuoteIdentifiers = Common.Convert.ToBoolean(quoteIdentifiers); | |
37 | |
38 var queryCaseConcert = attributes["QueryCaseConvert"]; | |
39 if (queryCaseConcert != null) | |
40 { | |
41 try | |
42 { | |
43 QueryCaseConvert = (CaseConvert)Enum.Parse(typeof(CaseConvert), queryCaseConcert, true); | |
44 } | |
45 catch { } | |
46 } | |
47 | |
48 base.Configure(attributes); | |
49 } | |
50 | |
51 #endregion | |
52 | |
53 public override IDbConnection CreateConnectionObject() | |
54 { | |
55 return new NpgsqlConnection(); | |
56 } | |
57 | |
58 public override DbDataAdapter CreateDataAdapterObject() | |
59 { | |
60 return new NpgsqlDataAdapter(); | |
61 } | |
62 | |
63 public override bool DeriveParameters(IDbCommand command) | |
64 { | |
65 NpgsqlCommandBuilder.DeriveParameters((NpgsqlCommand)command); | |
66 return true; | |
67 } | |
68 | |
69 public override void SetParameterValue(IDbDataParameter parameter, object value) | |
70 { | |
71 if(value is Enum) | |
72 { | |
73 var type = Enum.GetUnderlyingType(value.GetType()); | |
74 value = (MappingSchema ?? Map.DefaultSchema).ConvertChangeType(value, type); | |
75 | |
76 } | |
77 base.SetParameterValue(parameter, value); | |
78 } | |
79 | |
80 public override object Convert(object value, ConvertType convertType) | |
81 { | |
82 switch (convertType) | |
83 { | |
84 case ConvertType.ExceptionToErrorNumber: | |
85 if (value is NpgsqlException) | |
86 { | |
87 var ex = (NpgsqlException)value; | |
88 | |
89 foreach (NpgsqlError error in ex.Errors) | |
90 return error.Code; | |
91 | |
92 return 0; | |
93 } | |
94 | |
95 break; | |
96 } | |
97 | |
98 return SqlProvider.Convert(value, convertType); | |
99 } | |
100 | |
101 public override Type ConnectionType | |
102 { | |
103 get { return typeof(NpgsqlConnection); } | |
104 } | |
105 | |
106 public override string Name | |
107 { | |
108 get { return DataProvider.ProviderName.PostgreSQL; } | |
109 } | |
110 | |
111 public override int MaxBatchSize | |
112 { | |
113 get { return 0; } | |
114 } | |
115 | |
116 public override ISqlProvider CreateSqlProvider() | |
117 { | |
118 return new PostgreSQLSqlProvider(); | |
119 } | |
120 | |
121 public override void PrepareCommand(ref CommandType commandType, ref string commandText, ref IDbDataParameter[] commandParameters) | |
122 { | |
123 if (QueryCaseConvert == CaseConvert.Lower) | |
124 commandText = commandText.ToLower(); | |
125 else if (QueryCaseConvert == CaseConvert.Upper) | |
126 commandText = commandText.ToUpper(); | |
127 | |
128 base.PrepareCommand(ref commandType, ref commandText, ref commandParameters); | |
129 } | |
130 | |
131 public override bool CanReuseCommand(IDbCommand command, CommandType newCommandType) | |
132 { | |
133 return command.CommandType == newCommandType; | |
134 } | |
135 | |
136 public override IDataReader GetDataReader(MappingSchema schema, IDataReader dataReader) | |
137 { | |
138 return | |
139 dataReader is NpgsqlDataReader | |
140 ? new NpgsqlDataReaderEx(schema, (NpgsqlDataReader)dataReader) | |
141 : base.GetDataReader(schema, dataReader); | |
142 } | |
143 | |
144 class NpgsqlDataReaderEx : IDataReader | |
145 { | |
146 private readonly NpgsqlDataReader _reader; | |
147 private readonly MappingSchema _schema; | |
148 | |
149 public NpgsqlDataReaderEx(MappingSchema schema, NpgsqlDataReader reader) | |
150 { | |
151 _reader = reader; | |
152 _schema = schema; | |
153 } | |
154 | |
155 #region IDataReader Members | |
156 | |
157 public void Close() | |
158 { | |
159 _reader.Close(); | |
160 } | |
161 | |
162 public int Depth | |
163 { | |
164 get { return _reader.Depth; } | |
165 } | |
166 | |
167 public DataTable GetSchemaTable() | |
168 { | |
169 return _reader.GetSchemaTable(); | |
170 } | |
171 | |
172 public bool IsClosed | |
173 { | |
174 get { return _reader.IsClosed; } | |
175 } | |
176 | |
177 public bool NextResult() | |
178 { | |
179 return _reader.NextResult(); | |
180 } | |
181 | |
182 public bool Read() | |
183 { | |
184 return _reader.Read(); | |
185 } | |
186 | |
187 public int RecordsAffected | |
188 { | |
189 get { return _reader.RecordsAffected; } | |
190 } | |
191 | |
192 #endregion | |
193 | |
194 #region IDisposable Members | |
195 | |
196 public void Dispose() | |
197 { | |
198 _reader.Dispose(); | |
199 } | |
200 | |
201 #endregion | |
202 | |
203 #region IDataRecord Members | |
204 | |
205 public int FieldCount | |
206 { | |
207 get { return _reader.FieldCount; } | |
208 } | |
209 | |
210 public bool GetBoolean(int i) | |
211 { | |
212 return _reader.GetBoolean(i); | |
213 } | |
214 | |
215 public byte GetByte(int i) | |
216 { | |
217 return _schema.ConvertToByte(_reader.GetValue(i)); | |
218 } | |
219 | |
220 public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) | |
221 { | |
222 return _reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length); | |
223 } | |
224 | |
225 public char GetChar(int i) | |
226 { | |
227 return _reader.GetChar(i); | |
228 } | |
229 | |
230 public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) | |
231 { | |
232 return _reader.GetChars(i, fieldoffset, buffer, bufferoffset, length); | |
233 } | |
234 | |
235 public IDataReader GetData(int i) | |
236 { | |
237 return _reader.GetData(i); | |
238 } | |
239 | |
240 public string GetDataTypeName(int i) | |
241 { | |
242 return _reader.GetDataTypeName(i); | |
243 } | |
244 | |
245 public DateTime GetDateTime(int i) | |
246 { | |
247 return _reader.GetDateTime(i); | |
248 } | |
249 | |
250 public decimal GetDecimal(int i) | |
251 { | |
252 return _reader.GetDecimal(i); | |
253 } | |
254 | |
255 public double GetDouble(int i) | |
256 { | |
257 return _reader.GetDouble(i); | |
258 } | |
259 | |
260 public Type GetFieldType(int i) | |
261 { | |
262 return _reader.GetFieldType(i); | |
263 } | |
264 | |
265 public float GetFloat(int i) | |
266 { | |
267 return _reader.GetFloat(i); | |
268 } | |
269 | |
270 public Guid GetGuid(int i) | |
271 { | |
272 return _reader.GetGuid(i); | |
273 } | |
274 | |
275 public short GetInt16(int i) | |
276 { | |
277 return _reader.GetInt16(i); | |
278 } | |
279 | |
280 public int GetInt32(int i) | |
281 { | |
282 return _reader.GetInt32(i); | |
283 } | |
284 | |
285 public long GetInt64(int i) | |
286 { | |
287 return _reader.GetInt64(i); | |
288 } | |
289 | |
290 public string GetName(int i) | |
291 { | |
292 return _reader.GetName(i); | |
293 } | |
294 | |
295 public int GetOrdinal(string name) | |
296 { | |
297 return _reader.GetOrdinal(name); | |
298 } | |
299 | |
300 public string GetString(int i) | |
301 { | |
302 return _reader.GetString(i); | |
303 } | |
304 | |
305 public object GetValue(int i) | |
306 { | |
307 return _reader.GetValue(i); | |
308 } | |
309 | |
310 public int GetValues(object[] values) | |
311 { | |
312 return _reader.GetValues(values); | |
313 } | |
314 | |
315 public bool IsDBNull(int i) | |
316 { | |
317 return _reader.IsDBNull(i); | |
318 } | |
319 | |
320 public object this[string name] | |
321 { | |
322 get { return _reader[name]; } | |
323 } | |
324 | |
325 public object this[int i] | |
326 { | |
327 get { return _reader[i]; } | |
328 } | |
329 | |
330 #endregion | |
331 } | |
332 } | |
333 } |