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.

294 lines
11 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2004, 2016, 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.Data.Common;
  26. using System.Data;
  27. using System.Text;
  28. using Externals.MySql.Data.Types;
  29. using System.Globalization;
  30. using System.Collections.Generic;
  31. using Externals.MySql.Data.Common;
  32. namespace Externals.MySql.Data.MySqlClient
  33. {
  34. [ToolboxItem(false)]
  35. [System.ComponentModel.DesignerCategory("Code")]
  36. internal sealed class MySqlCommandBuilder : DbCommandBuilder
  37. {
  38. public MySqlCommandBuilder()
  39. {
  40. QuotePrefix = QuoteSuffix = "`";
  41. }
  42. public MySqlCommandBuilder(MySqlDataAdapter adapter)
  43. : this()
  44. {
  45. DataAdapter = adapter;
  46. }
  47. public new MySqlDataAdapter DataAdapter
  48. {
  49. get { return (MySqlDataAdapter)base.DataAdapter; }
  50. set { base.DataAdapter = value; }
  51. }
  52. #region Public Methods
  53. /// <summary>
  54. /// Retrieves parameter information from the stored procedure specified
  55. /// in the MySqlCommand and populates the Parameters collection of the
  56. /// specified MySqlCommand object.
  57. /// This method is not currently supported since stored procedures are
  58. /// not available in MySql.
  59. /// </summary>
  60. /// <param name="command">The MySqlCommand referencing the stored
  61. /// procedure from which the parameter information is to be derived.
  62. /// The derived parameters are added to the Parameters collection of the
  63. /// MySqlCommand.</param>
  64. /// <exception cref="InvalidOperationException">The command text is not
  65. /// a valid stored procedure name.</exception>
  66. public static void DeriveParameters(MySqlCommand command)
  67. {
  68. if (command.CommandType != CommandType.StoredProcedure)
  69. throw new InvalidOperationException(Resources.CanNotDeriveParametersForTextCommands);
  70. // retrieve the proc definition from the cache.
  71. string spName = command.CommandText;
  72. if (spName.IndexOf(".") == -1)
  73. spName = command.Connection.Database + "." + spName;
  74. try
  75. {
  76. ProcedureCacheEntry entry = command.Connection.ProcedureCache.GetProcedure(command.Connection, spName, null);
  77. command.Parameters.Clear();
  78. foreach (MySqlSchemaRow row in entry.parameters.Rows)
  79. {
  80. MySqlParameter p = new MySqlParameter();
  81. p.ParameterName = String.Format("@{0}", row["PARAMETER_NAME"]);
  82. if (row["ORDINAL_POSITION"].Equals(0) && p.ParameterName == "@")
  83. p.ParameterName = "@RETURN_VALUE";
  84. p.Direction = GetDirection(row);
  85. bool unsigned = StoredProcedure.GetFlags(row["DTD_IDENTIFIER"].ToString()).IndexOf("UNSIGNED") != -1;
  86. bool real_as_float = entry.procedure.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1;
  87. p.MySqlDbType = MetaData.NameToType(row["DATA_TYPE"].ToString(),
  88. unsigned, real_as_float, command.Connection);
  89. if (row["CHARACTER_MAXIMUM_LENGTH"] != null)
  90. p.Size = (int)row["CHARACTER_MAXIMUM_LENGTH"];
  91. if (row["NUMERIC_PRECISION"] != null)
  92. p.Precision = Convert.ToByte(row["NUMERIC_PRECISION"]);
  93. if (row["NUMERIC_SCALE"] != null)
  94. p.Scale = Convert.ToByte(row["NUMERIC_SCALE"]);
  95. if (p.MySqlDbType == MySqlDbType.Set || p.MySqlDbType == MySqlDbType.Enum)
  96. p.PossibleValues = GetPossibleValues(row);
  97. command.Parameters.Add(p);
  98. }
  99. }
  100. catch (InvalidOperationException ioe)
  101. {
  102. throw new MySqlException(Resources.UnableToDeriveParameters, ioe);
  103. }
  104. }
  105. private static List<string> GetPossibleValues(MySqlSchemaRow row)
  106. {
  107. string[] types = new string[] { "ENUM", "SET" };
  108. string dtdIdentifier = row["DTD_IDENTIFIER"].ToString().Trim();
  109. int index = 0;
  110. for (; index < 2; index++)
  111. if (dtdIdentifier.StartsWith(types[index], StringComparison.OrdinalIgnoreCase))
  112. break;
  113. if (index == 2) return null;
  114. dtdIdentifier = dtdIdentifier.Substring(types[index].Length).Trim();
  115. dtdIdentifier = dtdIdentifier.Trim('(', ')').Trim();
  116. List<string> values = new List<string>();
  117. MySqlTokenizer tokenzier = new MySqlTokenizer(dtdIdentifier);
  118. string token = tokenzier.NextToken();
  119. int start = tokenzier.StartIndex;
  120. while (true)
  121. {
  122. if (token == null || token == ",")
  123. {
  124. int end = dtdIdentifier.Length - 1;
  125. if (token == ",")
  126. end = tokenzier.StartIndex;
  127. string value = dtdIdentifier.Substring(start, end - start).Trim('\'', '\"').Trim();
  128. values.Add(value);
  129. start = tokenzier.StopIndex;
  130. }
  131. if (token == null) break;
  132. token = tokenzier.NextToken();
  133. }
  134. return values;
  135. }
  136. private static ParameterDirection GetDirection(MySqlSchemaRow row)
  137. {
  138. string mode = row["PARAMETER_MODE"].ToString();
  139. int ordinal = Convert.ToInt32(row["ORDINAL_POSITION"]);
  140. if (0 == ordinal)
  141. return ParameterDirection.ReturnValue;
  142. else if (mode == "IN")
  143. return ParameterDirection.Input;
  144. else if (mode == "OUT")
  145. return ParameterDirection.Output;
  146. return ParameterDirection.InputOutput;
  147. }
  148. /// <summary>
  149. /// Gets the delete command.
  150. /// </summary>
  151. /// <returns></returns>
  152. public new MySqlCommand GetDeleteCommand()
  153. {
  154. return (MySqlCommand)base.GetDeleteCommand();
  155. }
  156. /// <summary>
  157. /// Gets the update command.
  158. /// </summary>
  159. /// <returns></returns>
  160. public new MySqlCommand GetUpdateCommand()
  161. {
  162. return (MySqlCommand)base.GetUpdateCommand();
  163. }
  164. /// <summary>
  165. /// Gets the insert command.
  166. /// </summary>
  167. /// <returns></returns>
  168. public new MySqlCommand GetInsertCommand()
  169. {
  170. return (MySqlCommand)GetInsertCommand(false);
  171. }
  172. public override string QuoteIdentifier(string unquotedIdentifier)
  173. {
  174. if (unquotedIdentifier == null) throw new
  175. ArgumentNullException("unquotedIdentifier");
  176. // don't quote again if it is already quoted
  177. if (unquotedIdentifier.StartsWith(QuotePrefix) &&
  178. unquotedIdentifier.EndsWith(QuoteSuffix))
  179. return unquotedIdentifier;
  180. unquotedIdentifier = unquotedIdentifier.Replace(QuotePrefix, QuotePrefix + QuotePrefix);
  181. return String.Format("{0}{1}{2}", QuotePrefix, unquotedIdentifier, QuoteSuffix);
  182. }
  183. public override string UnquoteIdentifier(string quotedIdentifier)
  184. {
  185. if (quotedIdentifier == null) throw new
  186. ArgumentNullException("quotedIdentifier");
  187. // don't unquote again if it is already unquoted
  188. if (!quotedIdentifier.StartsWith(QuotePrefix) ||
  189. !quotedIdentifier.EndsWith(QuoteSuffix))
  190. return quotedIdentifier;
  191. if (quotedIdentifier.StartsWith(QuotePrefix))
  192. quotedIdentifier = quotedIdentifier.Substring(1);
  193. if (quotedIdentifier.EndsWith(QuoteSuffix))
  194. quotedIdentifier = quotedIdentifier.Substring(0, quotedIdentifier.Length - 1);
  195. quotedIdentifier = quotedIdentifier.Replace(QuotePrefix + QuotePrefix, QuotePrefix);
  196. return quotedIdentifier;
  197. }
  198. #endregion
  199. protected override DataTable GetSchemaTable(DbCommand sourceCommand)
  200. {
  201. DataTable schemaTable = base.GetSchemaTable(sourceCommand);
  202. foreach (DataRow row in schemaTable.Rows)
  203. if (row["BaseSchemaName"].Equals(sourceCommand.Connection.Database))
  204. row["BaseSchemaName"] = null;
  205. return schemaTable;
  206. }
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. /// <param name="parameterName"></param>
  211. /// <returns></returns>
  212. protected override string GetParameterName(string parameterName)
  213. {
  214. StringBuilder sb = new StringBuilder(parameterName);
  215. sb.Replace(" ", "");
  216. sb.Replace("/", "_per_");
  217. sb.Replace("-", "_");
  218. sb.Replace(")", "_cb_");
  219. sb.Replace("(", "_ob_");
  220. sb.Replace("%", "_pct_");
  221. sb.Replace("<", "_lt_");
  222. sb.Replace(">", "_gt_");
  223. sb.Replace(".", "_pt_");
  224. return String.Format("@{0}", sb.ToString());
  225. }
  226. protected override void ApplyParameterInfo(DbParameter parameter, DataRow row,
  227. StatementType statementType, bool whereClause)
  228. {
  229. ((MySqlParameter)parameter).MySqlDbType = (MySqlDbType)row["ProviderType"];
  230. }
  231. protected override string GetParameterName(int parameterOrdinal)
  232. {
  233. return String.Format("@p{0}", parameterOrdinal.ToString(CultureInfo.InvariantCulture));
  234. }
  235. protected override string GetParameterPlaceholder(int parameterOrdinal)
  236. {
  237. return String.Format("@p{0}", parameterOrdinal.ToString(CultureInfo.InvariantCulture));
  238. }
  239. protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
  240. {
  241. MySqlDataAdapter myAdapter = (adapter as MySqlDataAdapter);
  242. if (adapter != base.DataAdapter)
  243. myAdapter.RowUpdating += new MySqlRowUpdatingEventHandler(RowUpdating);
  244. else
  245. myAdapter.RowUpdating -= new MySqlRowUpdatingEventHandler(RowUpdating);
  246. }
  247. private void RowUpdating(object sender, MySqlRowUpdatingEventArgs args)
  248. {
  249. base.RowUpdatingHandler(args);
  250. }
  251. }
  252. }
  253. #endif