0
|
1 using System;
|
|
2 using System.Data;
|
|
3 using System.Data.Common;
|
|
4 using System.Diagnostics;
|
|
5 using System.Text;
|
|
6 using System.Xml;
|
|
7
|
|
8 // System.Data.SQLite.dll must be referenced.
|
|
9 // http://sqlite.phxsoftware.com/
|
|
10 //
|
|
11 using Mono.Data.Sqlite;
|
|
12
|
|
13 namespace BLToolkit.Data.DataProvider
|
|
14 {
|
|
15 /// <summary>
|
|
16 /// Implements access to the Data Provider for SQLite.
|
|
17 /// </summary>
|
|
18 /// <remarks>
|
|
19 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
20 /// </remarks>
|
|
21 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
|
|
22 public sealed class SQLiteDataProvider: DataProviderBase
|
|
23 {
|
|
24 /// <summary>
|
|
25 /// Creates the database connection object.
|
|
26 /// </summary>
|
|
27 /// <remarks>
|
|
28 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
29 /// </remarks>
|
|
30 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
|
|
31 /// <returns>The database connection object.</returns>
|
|
32 public override IDbConnection CreateConnectionObject()
|
|
33 {
|
|
34 return new SqliteConnection();
|
|
35 }
|
|
36
|
|
37 /// <summary>
|
|
38 /// Creates the data adapter object.
|
|
39 /// </summary>
|
|
40 /// <remarks>
|
|
41 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
42 /// </remarks>
|
|
43 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
|
|
44 /// <returns>A data adapter object.</returns>
|
|
45 public override DbDataAdapter CreateDataAdapterObject()
|
|
46 {
|
|
47 return new SqliteDataAdapter();
|
|
48 }
|
|
49
|
|
50 /// <summary>
|
|
51 /// Populates the specified IDbCommand object's Parameters collection with
|
|
52 /// parameter information for the stored procedure specified in the IDbCommand.
|
|
53 /// </summary>
|
|
54 /// <remarks>
|
|
55 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
56 /// </remarks>
|
|
57 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
|
|
58 /// <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>
|
|
59 public override bool DeriveParameters(IDbCommand command)
|
|
60 {
|
|
61 // SQLiteCommandBuilder does not implement DeriveParameters.
|
|
62 // This is not surprising, since SQLite has no support for stored procs.
|
|
63 //
|
|
64 return false;
|
|
65 }
|
|
66
|
|
67 public override object Convert(object value, ConvertType convertType)
|
|
68 {
|
|
69 switch (convertType)
|
|
70 {
|
|
71 case ConvertType.NameToQueryParameter:
|
|
72 case ConvertType.NameToParameter:
|
|
73 return "@" + value;
|
|
74
|
|
75 case ConvertType.NameToQueryField:
|
|
76 case ConvertType.NameToQueryTable:
|
|
77 {
|
|
78 string name = (string)value;
|
|
79
|
|
80 if (name.Length > 0 && name[0] == '[')
|
|
81 return value;
|
|
82
|
|
83 if (name.IndexOf('.') > 0)
|
|
84 value = string.Join("].[", name.Split('.'));
|
|
85
|
|
86 return "[" + value + "]";
|
|
87 }
|
|
88
|
|
89 case ConvertType.ParameterToName:
|
|
90 {
|
|
91 string name = (string)value;
|
|
92 return name.Length > 0 && name[0] == '@'? name.Substring(1): name;
|
|
93 }
|
|
94
|
|
95 case ConvertType.ExceptionToErrorNumber:
|
|
96 {
|
|
97 if (value is SqliteException)
|
|
98 return ((SqliteException)value).ErrorCode;
|
|
99 break;
|
|
100 }
|
|
101 }
|
|
102
|
|
103 return value;
|
|
104 }
|
|
105
|
|
106 public override void AttachParameter(IDbCommand command, IDbDataParameter parameter)
|
|
107 {
|
|
108 if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput)
|
|
109 {
|
|
110 if (parameter.Value is XmlDocument)
|
|
111 {
|
|
112 parameter.Value = Encoding.UTF8.GetBytes(((XmlDocument) parameter.Value).InnerXml);
|
|
113 parameter.DbType = DbType.Binary;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 base.AttachParameter(command, parameter);
|
|
118 }
|
|
119
|
|
120 /// <summary>
|
|
121 /// Returns connection type.
|
|
122 /// </summary>
|
|
123 /// <remarks>
|
|
124 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
125 /// </remarks>
|
|
126 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataManager Method</seealso>
|
|
127 /// <value>An instance of the <see cref="Type"/> class.</value>
|
|
128 public override Type ConnectionType
|
|
129 {
|
|
130 get { return typeof(SqliteConnection); }
|
|
131 }
|
|
132
|
|
133 /// <summary>
|
|
134 /// Returns the data provider name.
|
|
135 /// </summary>
|
|
136 /// <remarks>
|
|
137 /// See the <see cref="DbManager.AddDataProvider(DataProviderBase)"/> method to find an example.
|
|
138 /// </remarks>
|
|
139 /// <seealso cref="DbManager.AddDataProvider(DataProviderBase)">AddDataProvider Method</seealso>
|
|
140 /// <value>Data provider name.</value>
|
|
141 public override string Name
|
|
142 {
|
|
143 get { return "SQLite"; }
|
|
144 }
|
|
145
|
|
146 public class SQLiteMappingSchema : Mapping.MappingSchema
|
|
147 {
|
|
148 #region Convert
|
|
149
|
|
150 public override XmlReader ConvertToXmlReader(object value)
|
|
151 {
|
|
152 if (value is byte[])
|
|
153 value = Encoding.UTF8.GetString((byte[])value);
|
|
154
|
|
155 return base.ConvertToXmlReader(value);
|
|
156 }
|
|
157
|
|
158 public override XmlDocument ConvertToXmlDocument(object value)
|
|
159 {
|
|
160 if (value is byte[])
|
|
161 value = Encoding.UTF8.GetString((byte[])value);
|
|
162
|
|
163 return base.ConvertToXmlDocument(value);
|
|
164 }
|
|
165
|
|
166 #endregion
|
|
167 }
|
|
168 }
|
|
169 }
|