Mercurial > pub > bltoolkit
comparison Source/Data/DataProvider/SQLiteDataProvider.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 using System.Data.SQLite; | |
5 using System.Diagnostics; | |
6 using System.Text; | |
7 using System.Xml; | |
8 | |
9 using BLToolkit.Data.Sql.SqlProvider; | |
10 using BLToolkit.Mapping; | |
11 // System.Data.SQLite.dll must be referenced. | |
12 // http://sqlite.phxsoftware.com/ | |
13 // | |
14 | |
15 namespace BLToolkit.Data.DataProvider | |
16 { | |
17 /// <summary> | |
18 /// Implements access to the Data Provider for SQLite. | |
19 /// </summary> | |
20 /// <remarks> | |
21 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
22 /// </remarks> | |
23 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso> | |
24 public sealed class SQLiteDataProvider : DataProviderBase | |
25 { | |
26 /// <summary> | |
27 /// Returns connection type. | |
28 /// </summary> | |
29 /// <remarks> | |
30 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
31 /// </remarks> | |
32 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso> | |
33 /// <value>An instance of the <see cref="Type"/> class.</value> | |
34 public override Type ConnectionType | |
35 { | |
36 get { return typeof (SQLiteConnection); } | |
37 } | |
38 | |
39 /// <summary> | |
40 /// Returns the data provider name. | |
41 /// </summary> | |
42 /// <remarks> | |
43 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
44 /// </remarks> | |
45 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataProvider Method</seealso> | |
46 /// <value>Data provider name.</value> | |
47 public override string Name | |
48 { | |
49 get { return DataProvider.ProviderName.SQLite; } | |
50 } | |
51 | |
52 /// <summary> | |
53 /// Creates the database connection object. | |
54 /// </summary> | |
55 /// <remarks> | |
56 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
57 /// </remarks> | |
58 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso> | |
59 /// <returns>The database connection object.</returns> | |
60 public override IDbConnection CreateConnectionObject() | |
61 { | |
62 return new SQLiteConnection(); | |
63 } | |
64 | |
65 /// <summary> | |
66 /// Creates the data adapter object. | |
67 /// </summary> | |
68 /// <remarks> | |
69 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
70 /// </remarks> | |
71 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso> | |
72 /// <returns>A data adapter object.</returns> | |
73 public override DbDataAdapter CreateDataAdapterObject() | |
74 { | |
75 return new SQLiteDataAdapter(); | |
76 } | |
77 | |
78 /// <summary> | |
79 /// Populates the specified IDbCommand object's Parameters collection with | |
80 /// parameter information for the stored procedure specified in the IDbCommand. | |
81 /// </summary> | |
82 /// <remarks> | |
83 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example. | |
84 /// </remarks> | |
85 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso> | |
86 /// <param name="command">The IDbCommand referencing the stored procedure for which the parameter information is to be derived. The derived parameters will be populated into the Parameters of this command.</param> | |
87 public override bool DeriveParameters(IDbCommand command) | |
88 { | |
89 // SQLiteCommandBuilder does not implement DeriveParameters. | |
90 // This is not surprising, since SQLite has no support for stored procs. | |
91 // | |
92 return false; | |
93 } | |
94 | |
95 public override object Convert(object value, ConvertType convertType) | |
96 { | |
97 switch (convertType) | |
98 { | |
99 case ConvertType.ExceptionToErrorNumber: | |
100 { | |
101 if (value is SQLiteException) | |
102 return ((SQLiteException) value).ErrorCode; | |
103 break; | |
104 } | |
105 } | |
106 | |
107 return SqlProvider.Convert(value, convertType); | |
108 } | |
109 | |
110 public override DataExceptionType ConvertErrorNumberToDataExceptionType(int number) | |
111 { | |
112 switch (number) | |
113 { | |
114 case 19: return DataExceptionType.ConstraintViolation; | |
115 } | |
116 return DataExceptionType.Undefined; | |
117 } | |
118 | |
119 public override void AttachParameter(IDbCommand command, IDbDataParameter parameter) | |
120 { | |
121 if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput) | |
122 { | |
123 if (parameter.Value is XmlDocument) | |
124 { | |
125 parameter.Value = Encoding.UTF8.GetBytes(((XmlDocument) parameter.Value).InnerXml); | |
126 parameter.DbType = DbType.Binary; | |
127 } | |
128 } | |
129 | |
130 base.AttachParameter(command, parameter); | |
131 } | |
132 | |
133 public override void SetParameterValue(IDbDataParameter parameter, object value) | |
134 { | |
135 if (parameter.DbType == DbType.DateTime2) | |
136 parameter.DbType = DbType.DateTime; | |
137 | |
138 base.SetParameterValue(parameter, value); | |
139 } | |
140 | |
141 public override ISqlProvider CreateSqlProvider() | |
142 { | |
143 return new SQLiteSqlProvider(); | |
144 } | |
145 | |
146 #region Nested type: LoverFunction | |
147 /// <summary> | |
148 /// SQLite built-in text processor is ANSI-only Just override it. | |
149 /// </summary> | |
150 [SQLiteFunction(Name = "lower", Arguments = 1, FuncType = FunctionType.Scalar)] | |
151 internal class LoverFunction : SQLiteFunction | |
152 { | |
153 public override object Invoke(object[] args) | |
154 { | |
155 Debug.Assert(args != null && args.Length == 1); | |
156 var arg = args[0]; | |
157 | |
158 Debug.Assert(arg is string || arg is DBNull || arg is byte[]); | |
159 return | |
160 arg is string | |
161 ? ((string) arg).ToLower() | |
162 : arg is byte[] | |
163 ? Encoding.UTF8.GetString((byte[]) arg).ToLower() | |
164 : arg; | |
165 } | |
166 } | |
167 #endregion | |
168 | |
169 #region Nested type: SQLiteMappingSchema | |
170 public class SQLiteMappingSchema : MappingSchema | |
171 { | |
172 #region Convert | |
173 public override XmlReader ConvertToXmlReader(object value) | |
174 { | |
175 if (value is byte[]) | |
176 value = Encoding.UTF8.GetString((byte[]) value); | |
177 | |
178 return base.ConvertToXmlReader(value); | |
179 } | |
180 | |
181 public override XmlDocument ConvertToXmlDocument(object value) | |
182 { | |
183 if (value is byte[]) | |
184 value = Encoding.UTF8.GetString((byte[]) value); | |
185 | |
186 return base.ConvertToXmlDocument(value); | |
187 } | |
188 #endregion | |
189 } | |
190 #endregion | |
191 | |
192 #region Nested type: UpperFunction | |
193 /// <summary> | |
194 /// SQLite built-in text processor is ANSI-only Just override it. | |
195 /// </summary> | |
196 [SQLiteFunction(Name = "upper", Arguments = 1, FuncType = FunctionType.Scalar)] | |
197 internal class UpperFunction : SQLiteFunction | |
198 { | |
199 public override object Invoke(object[] args) | |
200 { | |
201 Debug.Assert(args != null && args.Length == 1); | |
202 var arg = args[0]; | |
203 | |
204 Debug.Assert(arg is string || arg is DBNull || arg is byte[]); | |
205 return | |
206 arg is string | |
207 ? ((string) arg).ToUpper() | |
208 : arg is byte[] | |
209 ? Encoding.UTF8.GetString((byte[]) arg).ToUpper() | |
210 : arg; | |
211 } | |
212 } | |
213 #endregion | |
214 } | |
215 } |