0
|
1 // MySql Connector/Net
|
|
2 // http://dev.mysql.com/downloads/connector/net/
|
|
3 //
|
|
4 using System;
|
|
5 using System.Collections.Generic;
|
|
6 using System.Data;
|
|
7 using System.Data.Common;
|
|
8
|
|
9 using MySql.Data.MySqlClient;
|
|
10
|
|
11 namespace BLToolkit.Data.DataProvider
|
|
12 {
|
|
13 using Sql.SqlProvider;
|
|
14 using Common;
|
|
15
|
|
16 public class MySqlDataProvider : DataProviderBase
|
|
17 {
|
|
18 #region Static configuration
|
|
19
|
|
20 public static char ParameterSymbol
|
|
21 {
|
|
22 get { return MySqlSqlProvider.ParameterSymbol; }
|
|
23 set { MySqlSqlProvider.ParameterSymbol = value; }
|
|
24 }
|
|
25
|
|
26 public static bool TryConvertParameterSymbol
|
|
27 {
|
|
28 get { return MySqlSqlProvider.TryConvertParameterSymbol; }
|
|
29 set { MySqlSqlProvider.TryConvertParameterSymbol = value; }
|
|
30 }
|
|
31
|
|
32 public static string CommandParameterPrefix
|
|
33 {
|
|
34 get { return MySqlSqlProvider.CommandParameterPrefix; }
|
|
35 set { MySqlSqlProvider.CommandParameterPrefix = value; }
|
|
36 }
|
|
37
|
|
38 public static string SprocParameterPrefix
|
|
39 {
|
|
40 get { return MySqlSqlProvider.SprocParameterPrefix; }
|
|
41 set { MySqlSqlProvider.SprocParameterPrefix = value; }
|
|
42 }
|
|
43
|
|
44 public static List<char> ConvertParameterSymbols
|
|
45 {
|
|
46 get { return MySqlSqlProvider.ConvertParameterSymbols; }
|
|
47 set { MySqlSqlProvider.ConvertParameterSymbols = value; }
|
|
48 }
|
|
49
|
|
50 [Obsolete("Use CommandParameterPrefix or SprocParameterPrefix instead.")]
|
|
51 public static string ParameterPrefix
|
|
52 {
|
|
53 get { return MySqlSqlProvider.SprocParameterPrefix; }
|
|
54 set { SprocParameterPrefix = CommandParameterPrefix = string.IsNullOrEmpty(value) ? string.Empty : value; }
|
|
55 }
|
|
56
|
|
57 public static void ConfigureOldStyle()
|
|
58 {
|
|
59 ParameterSymbol = '?';
|
|
60 ConvertParameterSymbols = new List<char>(new[] { '@' });
|
|
61 TryConvertParameterSymbol = true;
|
|
62 }
|
|
63
|
|
64 public static void ConfigureNewStyle()
|
|
65 {
|
|
66 ParameterSymbol = '@';
|
|
67 ConvertParameterSymbols = null;
|
|
68 TryConvertParameterSymbol = false;
|
|
69 }
|
|
70
|
|
71 static MySqlDataProvider()
|
|
72 {
|
|
73 ConfigureOldStyle();
|
|
74 }
|
|
75
|
|
76 #endregion
|
|
77
|
|
78 public override IDbConnection CreateConnectionObject()
|
|
79 {
|
|
80 return new MySqlConnection();
|
|
81 }
|
|
82
|
|
83 public override DbDataAdapter CreateDataAdapterObject()
|
|
84 {
|
|
85 return new MySqlDataAdapter();
|
|
86 }
|
|
87
|
|
88 private void ConvertParameterNames(IDbCommand command)
|
|
89 {
|
|
90 foreach (IDataParameter p in command.Parameters)
|
|
91 {
|
|
92 if (p.ParameterName[0] != ParameterSymbol)
|
|
93 p.ParameterName =
|
|
94 Convert(
|
|
95 Convert(p.ParameterName, ConvertType.SprocParameterToName),
|
|
96 command.CommandType == CommandType.StoredProcedure ? ConvertType.NameToSprocParameter : ConvertType.NameToCommandParameter).ToString();
|
|
97 }
|
|
98 }
|
|
99
|
|
100 public override bool DeriveParameters(IDbCommand command)
|
|
101 {
|
|
102 if (command is MySqlCommand)
|
|
103 {
|
|
104 MySqlCommandBuilder.DeriveParameters((MySqlCommand)command);
|
|
105
|
|
106 if (TryConvertParameterSymbol && ConvertParameterSymbols.Count > 0)
|
|
107 ConvertParameterNames(command);
|
|
108
|
|
109 return true;
|
|
110 }
|
|
111
|
|
112 return false;
|
|
113 }
|
|
114
|
|
115 public override IDbDataParameter GetParameter(
|
|
116 IDbCommand command,
|
|
117 NameOrIndexParameter nameOrIndex)
|
|
118 {
|
|
119 if (nameOrIndex.ByName)
|
|
120 {
|
|
121 // if we have a stored procedure, then maybe command paramaters were formatted
|
|
122 // (SprocParameterPrefix added). In this case we need to format given parameter name first
|
|
123 // and only then try to take parameter by formatted parameter name
|
|
124 var parameterName = command.CommandType == CommandType.StoredProcedure
|
|
125 ? Convert(nameOrIndex.Name, ConvertType.NameToSprocParameter).ToString()
|
|
126 : nameOrIndex.Name;
|
|
127
|
|
128 return (IDbDataParameter)(command.Parameters[parameterName]);
|
|
129 }
|
|
130 return (IDbDataParameter)(command.Parameters[nameOrIndex.Index]);
|
|
131 }
|
|
132
|
|
133 public override object Convert(object value, ConvertType convertType)
|
|
134 {
|
|
135 if (value == null)
|
|
136 throw new ArgumentNullException("value");
|
|
137
|
|
138 switch (convertType)
|
|
139 {
|
|
140 case ConvertType.ExceptionToErrorNumber:
|
|
141 if (value is MySqlException)
|
|
142 return ((MySqlException)value).Number;
|
|
143 break;
|
|
144
|
|
145 case ConvertType.ExceptionToErrorMessage:
|
|
146 if (value is MySqlException)
|
|
147 return ((MySqlException)value).Message;
|
|
148 break;
|
|
149 }
|
|
150
|
|
151 return SqlProvider.Convert(value, convertType);
|
|
152 }
|
|
153
|
|
154 public override DataExceptionType ConvertErrorNumberToDataExceptionType(int number)
|
|
155 {
|
|
156 switch (number)
|
|
157 {
|
|
158 case 1213: return DataExceptionType.Deadlock;
|
|
159 case 1205: return DataExceptionType.Timeout;
|
|
160 case 1216:
|
|
161 case 1217: return DataExceptionType.ForeignKeyViolation;
|
|
162 case 1169: return DataExceptionType.UniqueIndexViolation;
|
|
163 }
|
|
164
|
|
165 return DataExceptionType.Undefined;
|
|
166 }
|
|
167
|
|
168 public override Type ConnectionType
|
|
169 {
|
|
170 get { return typeof(MySqlConnection); }
|
|
171 }
|
|
172
|
|
173 public override string Name
|
|
174 {
|
|
175 get { return DataProvider.ProviderName.MySql; }
|
|
176 }
|
|
177
|
|
178 public override ISqlProvider CreateSqlProvider()
|
|
179 {
|
|
180 return new MySqlSqlProvider();
|
|
181 }
|
|
182
|
|
183 public override void Configure(System.Collections.Specialized.NameValueCollection attributes)
|
|
184 {
|
|
185 var paremeterPrefix = attributes["ParameterPrefix"];
|
|
186 if (paremeterPrefix != null)
|
|
187 CommandParameterPrefix = SprocParameterPrefix = paremeterPrefix;
|
|
188
|
|
189 paremeterPrefix = attributes["CommandParameterPrefix"];
|
|
190 if (paremeterPrefix != null)
|
|
191 CommandParameterPrefix = paremeterPrefix;
|
|
192
|
|
193 paremeterPrefix = attributes["SprocParameterPrefix"];
|
|
194 if (paremeterPrefix != null)
|
|
195 SprocParameterPrefix = paremeterPrefix;
|
|
196
|
|
197 var configName = attributes["ParameterSymbolConfig"];
|
|
198 if (configName != null)
|
|
199 {
|
|
200 switch (configName)
|
|
201 {
|
|
202 case "OldStyle":
|
|
203 ConfigureOldStyle();
|
|
204 break;
|
|
205 case "NewStyle":
|
|
206 ConfigureNewStyle();
|
|
207 break;
|
|
208 }
|
|
209 }
|
|
210
|
|
211 var parameterSymbol = attributes["ParameterSymbol"];
|
|
212 if (parameterSymbol != null && parameterSymbol.Length == 1)
|
|
213 ParameterSymbol = parameterSymbol[0];
|
|
214
|
|
215 var convertParameterSymbols = attributes["ConvertParameterSymbols"];
|
|
216 if (convertParameterSymbols != null)
|
|
217 ConvertParameterSymbols = new List<char>(convertParameterSymbols.ToCharArray());
|
|
218
|
|
219 var tryConvertParameterSymbol = attributes["TryConvertParameterSymbol"];
|
|
220 if (tryConvertParameterSymbol != null)
|
|
221 TryConvertParameterSymbol = BLToolkit.Common.Convert.ToBoolean(tryConvertParameterSymbol);
|
|
222
|
|
223 base.Configure(attributes);
|
|
224 }
|
|
225 }
|
|
226 }
|