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.

355 lines
12 KiB

  1. #if NET461
  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 Externals.MySql.Data.Types;
  25. using System.ComponentModel;
  26. using System.Globalization;
  27. using System.Reflection;
  28. using System.Text;
  29. using System.Collections;
  30. using System.Data;
  31. using System.Data.Common;
  32. namespace Externals.MySql.Data.MySqlClient
  33. {
  34. /// <summary>
  35. /// Represents a parameter to a <see cref="MySqlCommand"/>, and optionally, its mapping to <see cref="DataSet"/> columns. This class cannot be inherited.
  36. /// </summary>
  37. internal sealed partial class MySqlParameter : ICloneable
  38. {
  39. private const int UNSIGNED_MASK = 0x8000;
  40. private object paramValue;
  41. private string paramName;
  42. private MySqlDbType mySqlDbType;
  43. private bool inferType = true;
  44. private const int GEOMETRY_LENGTH = 25;
  45. #region Constructors
  46. public MySqlParameter()
  47. {
  48. Init();
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="MySqlParameter"/> class with the parameter name and a value of the new MySqlParameter.
  52. /// </summary>
  53. /// <param name="parameterName">The name of the parameter to map. </param>
  54. /// <param name="value">An <see cref="Object"/> that is the value of the <see cref="MySqlParameter"/>. </param>
  55. public MySqlParameter(string parameterName, object value) : this()
  56. {
  57. ParameterName = parameterName;
  58. Value = value;
  59. }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="MySqlParameter"/> class with the parameter name and the data type.
  62. /// </summary>
  63. /// <param name="parameterName">The name of the parameter to map. </param>
  64. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  65. public MySqlParameter(string parameterName, MySqlDbType dbType) : this(parameterName, null)
  66. {
  67. MySqlDbType = dbType;
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of the <see cref="MySqlParameter"/> class with the parameter name, the <see cref="MySqlDbType"/>, and the size.
  71. /// </summary>
  72. /// <param name="parameterName">The name of the parameter to map. </param>
  73. /// <param name="dbType">One of the <see cref="MySqlDbType"/> values. </param>
  74. /// <param name="size">The length of the parameter. </param>
  75. public MySqlParameter(string parameterName, MySqlDbType dbType, int size) : this(parameterName, dbType)
  76. {
  77. Size = size;
  78. }
  79. partial void Init();
  80. #endregion
  81. #region Properties
  82. [Category("Misc")]
  83. public override String ParameterName
  84. {
  85. get { return paramName; }
  86. set { SetParameterName(value); }
  87. }
  88. internal MySqlParameterCollection Collection { get; set; }
  89. internal Encoding Encoding { get; set; }
  90. internal bool TypeHasBeenSet
  91. {
  92. get { return inferType == false; }
  93. }
  94. internal string BaseName
  95. {
  96. get
  97. {
  98. if (ParameterName.StartsWith("@", StringComparison.Ordinal) || ParameterName.StartsWith("?", StringComparison.Ordinal))
  99. return ParameterName.Substring(1);
  100. return ParameterName;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets a value indicating whether the parameter is input-only, output-only, bidirectional, or a stored procedure return value parameter.
  105. /// As of MySql version 4.1 and earlier, input-only is the only valid choice.
  106. /// </summary>
  107. [Category("Data")]
  108. public override ParameterDirection Direction { get; set; }
  109. /// <summary>
  110. /// Gets or sets a value indicating whether the parameter accepts null values.
  111. /// </summary>
  112. [Browsable(false)]
  113. public override Boolean IsNullable { get; set; }
  114. /// <summary>
  115. /// Gets or sets the MySqlDbType of the parameter.
  116. /// </summary>
  117. [Category("Data")]
  118. [DbProviderSpecificTypeProperty(true)]
  119. public MySqlDbType MySqlDbType
  120. {
  121. get { return mySqlDbType; }
  122. set
  123. {
  124. SetMySqlDbType(value);
  125. inferType = false;
  126. }
  127. }
  128. /// <summary>
  129. /// Gets or sets the maximum number of digits used to represent the <see cref="Value"/> property.
  130. /// </summary>
  131. [Category("Data")]
  132. public override byte Precision { get; set; }
  133. /// <summary>
  134. /// Gets or sets the number of decimal places to which <see cref="Value"/> is resolved.
  135. /// </summary>
  136. [Category("Data")]
  137. public override byte Scale { get; set; }
  138. /// <summary>
  139. /// Gets or sets the maximum size, in bytes, of the data within the column.
  140. /// </summary>
  141. [Category("Data")]
  142. public override int Size { get; set; }
  143. /// <summary>
  144. /// Gets or sets the value of the parameter.
  145. /// </summary>
  146. [TypeConverter(typeof(StringConverter))]
  147. [Category("Data")]
  148. public override object Value
  149. {
  150. get { return paramValue; }
  151. set
  152. {
  153. paramValue = value;
  154. byte[] valueAsByte = value as byte[];
  155. string valueAsString = value as string;
  156. if (valueAsByte != null)
  157. Size = valueAsByte.Length;
  158. else if (valueAsString != null)
  159. Size = valueAsString.Length;
  160. if (inferType)
  161. SetTypeFromValue();
  162. }
  163. }
  164. private IMySqlValue _valueObject;
  165. internal IMySqlValue ValueObject
  166. {
  167. get { return _valueObject; }
  168. private set
  169. {
  170. _valueObject = value;
  171. }
  172. }
  173. /// <summary>
  174. /// Returns the possible values for this parameter if this parameter is of type
  175. /// SET or ENUM. Returns null otherwise.
  176. /// </summary>
  177. public IList PossibleValues { get; internal set; }
  178. #endregion
  179. private void SetParameterName(string name)
  180. {
  181. if (Collection != null)
  182. Collection.ParameterNameChanged(this, paramName, name);
  183. paramName = name;
  184. }
  185. /// <summary>
  186. /// Overridden. Gets a string containing the <see cref="ParameterName"/>.
  187. /// </summary>
  188. /// <returns></returns>
  189. public override string ToString()
  190. {
  191. return paramName;
  192. }
  193. internal int GetPSType()
  194. {
  195. switch (mySqlDbType)
  196. {
  197. case MySqlDbType.Bit:
  198. return (int)MySqlDbType.Int64 | UNSIGNED_MASK;
  199. case MySqlDbType.UByte:
  200. return (int)MySqlDbType.Byte | UNSIGNED_MASK;
  201. case MySqlDbType.UInt64:
  202. return (int)MySqlDbType.Int64 | UNSIGNED_MASK;
  203. case MySqlDbType.UInt32:
  204. return (int)MySqlDbType.Int32 | UNSIGNED_MASK;
  205. case MySqlDbType.UInt24:
  206. return (int)MySqlDbType.Int32 | UNSIGNED_MASK;
  207. case MySqlDbType.UInt16:
  208. return (int)MySqlDbType.Int16 | UNSIGNED_MASK;
  209. default:
  210. return (int)mySqlDbType;
  211. }
  212. }
  213. internal void Serialize(MySqlPacket packet, bool binary, MySqlConnectionStringBuilder settings)
  214. {
  215. if (!binary && (paramValue == null || paramValue == DBNull.Value))
  216. packet.WriteStringNoNull("NULL");
  217. else
  218. {
  219. if (ValueObject.MySqlDbType == MySqlDbType.Guid)
  220. {
  221. MySqlGuid g = (MySqlGuid)ValueObject;
  222. g.OldGuids = settings.OldGuids;
  223. ValueObject = g;
  224. }
  225. if (ValueObject.MySqlDbType == MySqlDbType.Geometry)
  226. {
  227. MySqlGeometry v = (MySqlGeometry)ValueObject;
  228. if (v.IsNull && Value != null)
  229. {
  230. MySqlGeometry.TryParse(Value.ToString(), out v);
  231. }
  232. ValueObject = v;
  233. }
  234. ValueObject.WriteValue(packet, binary, paramValue, Size);
  235. }
  236. }
  237. partial void SetDbTypeFromMySqlDbType();
  238. private void SetMySqlDbType(MySqlDbType mysql_dbtype)
  239. {
  240. mySqlDbType = mysql_dbtype;
  241. ValueObject = MySqlField.GetIMySqlValue(mySqlDbType);
  242. SetDbTypeFromMySqlDbType();
  243. }
  244. private void SetTypeFromValue()
  245. {
  246. if (paramValue == null || paramValue == DBNull.Value) return;
  247. if (paramValue is Guid)
  248. MySqlDbType = MySqlDbType.Guid;
  249. else if (paramValue is TimeSpan)
  250. MySqlDbType = MySqlDbType.Time;
  251. else if (paramValue is bool)
  252. MySqlDbType = MySqlDbType.Byte;
  253. else
  254. {
  255. Type t = paramValue.GetType();
  256. switch (t.Name)
  257. {
  258. case "SByte": MySqlDbType = MySqlDbType.Byte; break;
  259. case "Byte": MySqlDbType = MySqlDbType.UByte; break;
  260. case "Int16": MySqlDbType = MySqlDbType.Int16; break;
  261. case "UInt16": MySqlDbType = MySqlDbType.UInt16; break;
  262. case "Int32": MySqlDbType = MySqlDbType.Int32; break;
  263. case "UInt32": MySqlDbType = MySqlDbType.UInt32; break;
  264. case "Int64": MySqlDbType = MySqlDbType.Int64; break;
  265. case "UInt64": MySqlDbType = MySqlDbType.UInt64; break;
  266. case "DateTime": MySqlDbType = MySqlDbType.DateTime; break;
  267. case "String": MySqlDbType = MySqlDbType.VarChar; break;
  268. case "Single": MySqlDbType = MySqlDbType.Float; break;
  269. case "Double": MySqlDbType = MySqlDbType.Double; break;
  270. case "Decimal": MySqlDbType = MySqlDbType.Decimal; break;
  271. case "Object":
  272. default:
  273. if (t.BaseType == typeof(Enum))
  274. MySqlDbType = MySqlDbType.Int32;
  275. else
  276. MySqlDbType = MySqlDbType.Blob;
  277. break;
  278. }
  279. }
  280. }
  281. #region ICloneable
  282. public MySqlParameter Clone()
  283. {
  284. MySqlParameter clone = new MySqlParameter(paramName, mySqlDbType, Direction, SourceColumn, SourceVersion, paramValue);
  285. // if we have not had our type set yet then our clone should not either
  286. clone.inferType = inferType;
  287. return clone;
  288. }
  289. object ICloneable.Clone()
  290. {
  291. return this.Clone();
  292. }
  293. #endregion
  294. // this method is pretty dumb but we want it to be fast. it doesn't return size based
  295. // on value and type but just on the value.
  296. internal long EstimatedSize()
  297. {
  298. if (Value == null || Value == DBNull.Value)
  299. return 4; // size of NULL
  300. if (Value is byte[])
  301. return (Value as byte[]).Length;
  302. if (Value is string)
  303. return (Value as string).Length * 4; // account for UTF-8 (yeah I know)
  304. if (Value is decimal || Value is float)
  305. return 64;
  306. return 32;
  307. }
  308. }
  309. }
  310. #endif