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.

298 lines
12 KiB

  1. // Copyright ?2004, 2013, Oracle and/or its affiliates. All rights reserved.
  2. //
  3. // MySQL Connector/NET is licensed under the terms of the GPLv2
  4. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  5. // MySQL Connectors. There are special exceptions to the terms and
  6. // conditions of the GPLv2 as it is applied to this software, see the
  7. // FLOSS License Exception
  8. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published
  12. // by the Free Software Foundation; version 2 of the License.
  13. //
  14. // This program is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. // for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License along
  20. // with this program; if not, write to the Free Software Foundation, Inc.,
  21. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #if NET40 || NET461
  23. using System;
  24. using System.ComponentModel;
  25. using System.Data.Common;
  26. using System.Data;
  27. using System.Text;
  28. using Externals.MySql.Data.Common;
  29. using System.Collections;
  30. using Externals.MySql.Data.Types;
  31. using System.Globalization;
  32. using Externals.MySql.Data.MySqlClient.Properties;
  33. using System.Collections.Generic;
  34. namespace Externals.MySql.Data.MySqlClient
  35. {
  36. [ToolboxItem(false)]
  37. [System.ComponentModel.DesignerCategory("Code")]
  38. internal sealed class MySqlCommandBuilder : DbCommandBuilder
  39. {
  40. /// <include file='Externals/MySql.Data-6.9.11/docs/mysqlCommandBuilder.xml' path='docs/Ctor/*'/>
  41. public MySqlCommandBuilder()
  42. {
  43. QuotePrefix = QuoteSuffix = "`";
  44. }
  45. /// <include file='Externals/MySql.Data-6.9.11/docs/mysqlCommandBuilder.xml' path='docs/Ctor2/*'/>
  46. public MySqlCommandBuilder(MySqlDataAdapter adapter)
  47. : this()
  48. {
  49. DataAdapter = adapter;
  50. }
  51. /// <include file='Externals/MySql.Data-6.9.11/docs/mysqlcommandBuilder.xml' path='docs/DataAdapter/*'/>
  52. public new MySqlDataAdapter DataAdapter
  53. {
  54. get { return (MySqlDataAdapter)base.DataAdapter; }
  55. set { base.DataAdapter = value; }
  56. }
  57. #region Public Methods
  58. /// <summary>
  59. /// Retrieves parameter information from the stored procedure specified
  60. /// in the MySqlCommand and populates the Parameters collection of the
  61. /// specified MySqlCommand object.
  62. /// This method is not currently supported since stored procedures are
  63. /// not available in MySql.
  64. /// </summary>
  65. /// <param name="command">The MySqlCommand referencing the stored
  66. /// procedure from which the parameter information is to be derived.
  67. /// The derived parameters are added to the Parameters collection of the
  68. /// MySqlCommand.</param>
  69. /// <exception cref="InvalidOperationException">The command text is not
  70. /// a valid stored procedure name.</exception>
  71. public static void DeriveParameters(MySqlCommand command)
  72. {
  73. if (command.CommandType != CommandType.StoredProcedure)
  74. throw new InvalidOperationException(Resources.CanNotDeriveParametersForTextCommands);
  75. // retrieve the proc definition from the cache.
  76. string spName = command.CommandText;
  77. if (spName.IndexOf(".") == -1)
  78. spName = command.Connection.Database + "." + spName;
  79. try
  80. {
  81. ProcedureCacheEntry entry = command.Connection.ProcedureCache.GetProcedure(command.Connection, spName, null);
  82. command.Parameters.Clear();
  83. foreach (MySqlSchemaRow row in entry.parameters.Rows)
  84. {
  85. MySqlParameter p = new MySqlParameter();
  86. p.ParameterName = String.Format("@{0}", row["PARAMETER_NAME"]);
  87. if (row["ORDINAL_POSITION"].Equals(0) && p.ParameterName == "@")
  88. p.ParameterName = "@RETURN_VALUE";
  89. p.Direction = GetDirection(row);
  90. bool unsigned = StoredProcedure.GetFlags(row["DTD_IDENTIFIER"].ToString()).IndexOf("UNSIGNED") != -1;
  91. bool real_as_float = entry.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1;
  92. p.MySqlDbType = MetaData.NameToType(row["DATA_TYPE"].ToString(),
  93. unsigned, real_as_float, command.Connection);
  94. if (row["CHARACTER_MAXIMUM_LENGTH"] != null)
  95. p.Size = (int)row["CHARACTER_MAXIMUM_LENGTH"];
  96. if (row["NUMERIC_PRECISION"] != null)
  97. p.Precision = Convert.ToByte(row["NUMERIC_PRECISION"]);
  98. if (row["NUMERIC_SCALE"] != null)
  99. p.Scale = Convert.ToByte(row["NUMERIC_SCALE"]);
  100. if (p.MySqlDbType == MySqlDbType.Set || p.MySqlDbType == MySqlDbType.Enum)
  101. p.PossibleValues = GetPossibleValues(row);
  102. command.Parameters.Add(p);
  103. }
  104. }
  105. catch (InvalidOperationException ioe)
  106. {
  107. throw new MySqlException(Resources.UnableToDeriveParameters, ioe);
  108. }
  109. }
  110. private static List<string> GetPossibleValues(MySqlSchemaRow row)
  111. {
  112. string[] types = new string[] { "ENUM", "SET" };
  113. string dtdIdentifier = row["DTD_IDENTIFIER"].ToString().Trim();
  114. int index = 0;
  115. for (; index < 2; index++)
  116. if (dtdIdentifier.StartsWith(types[index], StringComparison.OrdinalIgnoreCase))
  117. break;
  118. if (index == 2) return null;
  119. dtdIdentifier = dtdIdentifier.Substring(types[index].Length).Trim();
  120. dtdIdentifier = dtdIdentifier.Trim('(', ')').Trim();
  121. List<string> values = new List<string>();
  122. MySqlTokenizer tokenzier = new MySqlTokenizer(dtdIdentifier);
  123. string token = tokenzier.NextToken();
  124. int start = tokenzier.StartIndex;
  125. while (true)
  126. {
  127. if (token == null || token == ",")
  128. {
  129. int end = dtdIdentifier.Length - 1;
  130. if (token == ",")
  131. end = tokenzier.StartIndex;
  132. string value = dtdIdentifier.Substring(start, end - start).Trim('\'', '\"').Trim();
  133. values.Add(value);
  134. start = tokenzier.StopIndex;
  135. }
  136. if (token == null) break;
  137. token = tokenzier.NextToken();
  138. }
  139. return values;
  140. }
  141. private static ParameterDirection GetDirection(MySqlSchemaRow row)
  142. {
  143. string mode = row["PARAMETER_MODE"].ToString();
  144. int ordinal = Convert.ToInt32(row["ORDINAL_POSITION"]);
  145. if (0 == ordinal)
  146. return ParameterDirection.ReturnValue;
  147. else if (mode == "IN")
  148. return ParameterDirection.Input;
  149. else if (mode == "OUT")
  150. return ParameterDirection.Output;
  151. return ParameterDirection.InputOutput;
  152. }
  153. /// <summary>
  154. /// Gets the delete command.
  155. /// </summary>
  156. /// <returns></returns>
  157. public new MySqlCommand GetDeleteCommand()
  158. {
  159. return (MySqlCommand)base.GetDeleteCommand();
  160. }
  161. /// <summary>
  162. /// Gets the update command.
  163. /// </summary>
  164. /// <returns></returns>
  165. public new MySqlCommand GetUpdateCommand()
  166. {
  167. return (MySqlCommand)base.GetUpdateCommand();
  168. }
  169. /// <summary>
  170. /// Gets the insert command.
  171. /// </summary>
  172. /// <returns></returns>
  173. public new MySqlCommand GetInsertCommand()
  174. {
  175. return (MySqlCommand)GetInsertCommand(false);
  176. }
  177. public override string QuoteIdentifier(string unquotedIdentifier)
  178. {
  179. if (unquotedIdentifier == null) throw new
  180. ArgumentNullException("unquotedIdentifier");
  181. // don't quote again if it is already quoted
  182. if (unquotedIdentifier.StartsWith(QuotePrefix) &&
  183. unquotedIdentifier.EndsWith(QuoteSuffix))
  184. return unquotedIdentifier;
  185. unquotedIdentifier = unquotedIdentifier.Replace(QuotePrefix, QuotePrefix + QuotePrefix);
  186. return String.Format("{0}{1}{2}", QuotePrefix, unquotedIdentifier, QuoteSuffix);
  187. }
  188. public override string UnquoteIdentifier(string quotedIdentifier)
  189. {
  190. if (quotedIdentifier == null) throw new
  191. ArgumentNullException("quotedIdentifier");
  192. // don't unquote again if it is already unquoted
  193. if (!quotedIdentifier.StartsWith(QuotePrefix) ||
  194. !quotedIdentifier.EndsWith(QuoteSuffix))
  195. return quotedIdentifier;
  196. if (quotedIdentifier.StartsWith(QuotePrefix))
  197. quotedIdentifier = quotedIdentifier.Substring(1);
  198. if (quotedIdentifier.EndsWith(QuoteSuffix))
  199. quotedIdentifier = quotedIdentifier.Substring(0, quotedIdentifier.Length - 1);
  200. quotedIdentifier = quotedIdentifier.Replace(QuotePrefix + QuotePrefix, QuotePrefix);
  201. return quotedIdentifier;
  202. }
  203. #endregion
  204. protected override DataTable GetSchemaTable(DbCommand sourceCommand)
  205. {
  206. DataTable schemaTable = base.GetSchemaTable(sourceCommand);
  207. foreach (DataRow row in schemaTable.Rows)
  208. if (row["BaseSchemaName"].Equals(sourceCommand.Connection.Database))
  209. row["BaseSchemaName"] = null;
  210. return schemaTable;
  211. }
  212. /// <summary>
  213. ///
  214. /// </summary>
  215. /// <param name="parameterName"></param>
  216. /// <returns></returns>
  217. protected override string GetParameterName(string parameterName)
  218. {
  219. StringBuilder sb = new StringBuilder(parameterName);
  220. sb.Replace(" ", "");
  221. sb.Replace("/", "_per_");
  222. sb.Replace("-", "_");
  223. sb.Replace(")", "_cb_");
  224. sb.Replace("(", "_ob_");
  225. sb.Replace("%", "_pct_");
  226. sb.Replace("<", "_lt_");
  227. sb.Replace(">", "_gt_");
  228. sb.Replace(".", "_pt_");
  229. return String.Format("@{0}", sb.ToString());
  230. }
  231. protected override void ApplyParameterInfo(DbParameter parameter, DataRow row,
  232. StatementType statementType, bool whereClause)
  233. {
  234. ((MySqlParameter)parameter).MySqlDbType = (MySqlDbType)row["ProviderType"];
  235. }
  236. protected override string GetParameterName(int parameterOrdinal)
  237. {
  238. return String.Format("@p{0}", parameterOrdinal.ToString(CultureInfo.InvariantCulture));
  239. }
  240. protected override string GetParameterPlaceholder(int parameterOrdinal)
  241. {
  242. return String.Format("@p{0}", parameterOrdinal.ToString(CultureInfo.InvariantCulture));
  243. }
  244. protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
  245. {
  246. MySqlDataAdapter myAdapter = (adapter as MySqlDataAdapter);
  247. if (adapter != base.DataAdapter)
  248. myAdapter.RowUpdating += new MySqlRowUpdatingEventHandler(RowUpdating);
  249. else
  250. myAdapter.RowUpdating -= new MySqlRowUpdatingEventHandler(RowUpdating);
  251. }
  252. private void RowUpdating(object sender, MySqlRowUpdatingEventArgs args)
  253. {
  254. base.RowUpdatingHandler(args);
  255. }
  256. }
  257. }
  258. #endif