You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
12 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2004, 2013, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.ComponentModel;
  25. using System.ComponentModel.Design.Serialization;
  26. using System.Data;
  27. using System.Data.Common;
  28. using System.Globalization;
  29. using System.Reflection;
  30. using ParameterDirection = System.Data.ParameterDirection;
  31. namespace Externals.MySql.Data.MySqlClient
  32. {
  33. [TypeConverter(typeof(MySqlParameterConverter))]
  34. internal sealed partial class MySqlParameter : DbParameter, IDataParameter, IDbDataParameter
  35. {
  36. private DbType dbType;
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="MySqlParameter"/> class with the parameter name, the <see cref="MySqlDbType"/>, the size, and the source column name.
  39. /// </summary>
  40. /// <param name="parameterName">The name of the parameter to map. </param>
  41. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  42. /// <param name="size">The length of the parameter. </param>
  43. /// <param name="sourceColumn">The name of the source column. </param>
  44. public MySqlParameter(string parameterName, MySqlDbType dbType, int size, string sourceColumn) : this(parameterName, dbType)
  45. {
  46. Size = size;
  47. Direction = ParameterDirection.Input;
  48. SourceColumn = sourceColumn;
  49. SourceVersion = DataRowVersion.Current;
  50. }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="MySqlParameter"/> class with the parameter name, the type of the parameter, the size of the parameter, a <see cref="ParameterDirection"/>, the precision of the parameter, the scale of the parameter, the source column, a <see cref="DataRowVersion"/> to use, and the value of the parameter.
  53. /// </summary>
  54. /// <param name="parameterName">The name of the parameter to map. </param>
  55. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  56. /// <param name="size">The length of the parameter. </param>
  57. /// <param name="direction">One of the <see cref="ParameterDirection"/> values. </param>
  58. /// <param name="isNullable">true if the value of the field can be null, otherwise false. </param>
  59. /// <param name="precision">The total number of digits to the left and right of the decimal point to which <see cref="MySqlParameter.Value"/> is resolved.</param>
  60. /// <param name="scale">The total number of decimal places to which <see cref="MySqlParameter.Value"/> is resolved. </param>
  61. /// <param name="sourceColumn">The name of the source column. </param>
  62. /// <param name="sourceVersion">One of the <see cref="DataRowVersion"/> values. </param>
  63. /// <param name="value">An <see cref="Object"/> that is the value of the <see cref="MySqlParameter"/>. </param>
  64. /// <exception cref="ArgumentException"/>
  65. public MySqlParameter(string parameterName, MySqlDbType dbType, int size, ParameterDirection direction,
  66. bool isNullable, byte precision, byte scale, string sourceColumn,
  67. DataRowVersion sourceVersion,
  68. object value)
  69. : this(parameterName, dbType, size, sourceColumn)
  70. {
  71. Direction = direction;
  72. SourceVersion = sourceVersion;
  73. IsNullable = isNullable;
  74. Precision = precision;
  75. Scale = scale;
  76. Value = value;
  77. }
  78. internal MySqlParameter(string name, MySqlDbType type, ParameterDirection dir, string col, DataRowVersion ver, object val)
  79. : this(name, type)
  80. {
  81. Direction = dir;
  82. SourceColumn = col;
  83. SourceVersion = ver;
  84. Value = val;
  85. }
  86. partial void Init()
  87. {
  88. SourceVersion = DataRowVersion.Current;
  89. Direction = ParameterDirection.Input;
  90. }
  91. /// <summary>
  92. /// Gets or sets the <see cref="DataRowVersion"/> to use when loading <see cref="Value"/>.
  93. /// </summary>
  94. [Category("Data")]
  95. public override DataRowVersion SourceVersion { get; set; }
  96. /// <summary>
  97. /// Gets or sets the name of the source column that is mapped to the <see cref="DataSet"/> and used for loading or returning the <see cref="Value"/>.
  98. /// </summary>
  99. [Category("Data")]
  100. public override String SourceColumn { get; set; }
  101. /// <summary>
  102. /// Resets the <b>DbType</b> property to its original settings.
  103. /// </summary>
  104. public override void ResetDbType()
  105. {
  106. inferType = true;
  107. }
  108. /// <summary>
  109. /// Sets or gets a value which indicates whether the source column is nullable.
  110. /// This allows <see cref="DbCommandBuilder"/> to correctly generate Update statements
  111. /// for nullable columns.
  112. /// </summary>
  113. public override bool SourceColumnNullMapping { get; set; }
  114. /// <summary>
  115. /// Gets or sets the <see cref="DbType"/> of the parameter.
  116. /// </summary>
  117. public override DbType DbType
  118. {
  119. get { return dbType; }
  120. set
  121. {
  122. SetDbType(value);
  123. inferType = false;
  124. }
  125. }
  126. partial void SetDbTypeFromMySqlDbType()
  127. {
  128. switch (mySqlDbType)
  129. {
  130. case MySqlDbType.NewDecimal:
  131. case MySqlDbType.Decimal:
  132. dbType = DbType.Decimal;
  133. break;
  134. case MySqlDbType.Byte:
  135. dbType = DbType.SByte;
  136. break;
  137. case MySqlDbType.UByte:
  138. dbType = DbType.Byte;
  139. break;
  140. case MySqlDbType.Int16:
  141. dbType = DbType.Int16;
  142. break;
  143. case MySqlDbType.UInt16:
  144. dbType = DbType.UInt16;
  145. break;
  146. case MySqlDbType.Int24:
  147. case MySqlDbType.Int32:
  148. dbType = DbType.Int32;
  149. break;
  150. case MySqlDbType.UInt24:
  151. case MySqlDbType.UInt32:
  152. dbType = DbType.UInt32;
  153. break;
  154. case MySqlDbType.Int64:
  155. dbType = DbType.Int64;
  156. break;
  157. case MySqlDbType.UInt64:
  158. dbType = DbType.UInt64;
  159. break;
  160. case MySqlDbType.Bit:
  161. dbType = DbType.UInt64;
  162. break;
  163. case MySqlDbType.Float:
  164. dbType = DbType.Single;
  165. break;
  166. case MySqlDbType.Double:
  167. dbType = DbType.Double;
  168. break;
  169. case MySqlDbType.Timestamp:
  170. case MySqlDbType.DateTime:
  171. dbType = DbType.DateTime;
  172. break;
  173. case MySqlDbType.Date:
  174. case MySqlDbType.Newdate:
  175. case MySqlDbType.Year:
  176. dbType = DbType.Date;
  177. break;
  178. case MySqlDbType.Time:
  179. dbType = DbType.Time;
  180. break;
  181. case MySqlDbType.Enum:
  182. case MySqlDbType.Set:
  183. case MySqlDbType.VarChar:
  184. dbType = DbType.String;
  185. break;
  186. case MySqlDbType.TinyBlob:
  187. case MySqlDbType.MediumBlob:
  188. case MySqlDbType.LongBlob:
  189. case MySqlDbType.Blob:
  190. dbType = DbType.Object;
  191. break;
  192. case MySqlDbType.String:
  193. dbType = DbType.StringFixedLength;
  194. break;
  195. case MySqlDbType.Guid:
  196. dbType = DbType.Guid;
  197. break;
  198. }
  199. }
  200. private void SetDbType(DbType db_type)
  201. {
  202. dbType = db_type;
  203. switch (dbType)
  204. {
  205. case DbType.Guid:
  206. mySqlDbType = MySqlDbType.Guid;
  207. break;
  208. case DbType.AnsiString:
  209. case DbType.String:
  210. mySqlDbType = MySqlDbType.VarChar;
  211. break;
  212. case DbType.AnsiStringFixedLength:
  213. case DbType.StringFixedLength:
  214. mySqlDbType = MySqlDbType.String;
  215. break;
  216. case DbType.Boolean:
  217. case DbType.Byte:
  218. mySqlDbType = MySqlDbType.UByte;
  219. break;
  220. case DbType.SByte:
  221. mySqlDbType = MySqlDbType.Byte;
  222. break;
  223. case DbType.Date:
  224. mySqlDbType = MySqlDbType.Date;
  225. break;
  226. case DbType.DateTime:
  227. mySqlDbType = MySqlDbType.DateTime;
  228. break;
  229. case DbType.Time:
  230. mySqlDbType = MySqlDbType.Time;
  231. break;
  232. case DbType.Single:
  233. mySqlDbType = MySqlDbType.Float;
  234. break;
  235. case DbType.Double:
  236. mySqlDbType = MySqlDbType.Double;
  237. break;
  238. case DbType.Int16:
  239. mySqlDbType = MySqlDbType.Int16;
  240. break;
  241. case DbType.UInt16:
  242. mySqlDbType = MySqlDbType.UInt16;
  243. break;
  244. case DbType.Int32:
  245. mySqlDbType = MySqlDbType.Int32;
  246. break;
  247. case DbType.UInt32:
  248. mySqlDbType = MySqlDbType.UInt32;
  249. break;
  250. case DbType.Int64:
  251. mySqlDbType = MySqlDbType.Int64;
  252. break;
  253. case DbType.UInt64:
  254. mySqlDbType = MySqlDbType.UInt64;
  255. break;
  256. case DbType.Decimal:
  257. case DbType.Currency:
  258. mySqlDbType = MySqlDbType.Decimal;
  259. break;
  260. case DbType.Object:
  261. case DbType.VarNumeric:
  262. case DbType.Binary:
  263. default:
  264. mySqlDbType = MySqlDbType.Blob;
  265. break;
  266. }
  267. if (dbType == DbType.Object)
  268. {
  269. var value = this.paramValue as byte[];
  270. if (value != null && value.Length == GEOMETRY_LENGTH)
  271. mySqlDbType = MySqlDbType.Geometry;
  272. }
  273. ValueObject = MySqlField.GetIMySqlValue(mySqlDbType);
  274. }
  275. }
  276. internal class MySqlParameterConverter : TypeConverter
  277. {
  278. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  279. {
  280. if (destinationType == typeof(InstanceDescriptor))
  281. {
  282. return true;
  283. }
  284. // Always call the base to see if it can perform the conversion.
  285. return base.CanConvertTo(context, destinationType);
  286. }
  287. public override object ConvertTo(ITypeDescriptorContext context,
  288. CultureInfo culture, object value, Type destinationType)
  289. {
  290. if (destinationType == typeof(InstanceDescriptor))
  291. {
  292. ConstructorInfo ci = typeof(MySqlParameter).GetConstructor(
  293. new Type[]
  294. {
  295. typeof (string), typeof (MySqlDbType), typeof (int), typeof (ParameterDirection),
  296. typeof (bool), typeof (byte), typeof (byte), typeof (string), typeof (DataRowVersion),
  297. typeof (object)
  298. });
  299. MySqlParameter p = (MySqlParameter)value;
  300. return new InstanceDescriptor(ci, new object[]
  301. {
  302. p.ParameterName, p.DbType, p.Size, p.Direction,
  303. p.IsNullable, p.Precision,
  304. p.Scale, p.SourceColumn, p.SourceVersion, p.Value
  305. });
  306. }
  307. // Always call base, even if you can't convert.
  308. return base.ConvertTo(context, culture, value, destinationType);
  309. }
  310. }
  311. }
  312. #endif