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.

239 lines
8.9 KiB

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 Externals.MySql.Data.Common;
  24. using System;
  25. using System.Collections.Generic;
  26. namespace Externals.MySql.Data.MySqlClient
  27. {
  28. internal abstract class Statement
  29. {
  30. protected MySqlCommand command;
  31. private readonly List<MySqlPacket> _buffers;
  32. protected string commandText;
  33. private Statement(MySqlCommand cmd)
  34. {
  35. command = cmd;
  36. _buffers = new List<MySqlPacket>();
  37. }
  38. protected Statement(MySqlCommand cmd, string text)
  39. : this(cmd)
  40. {
  41. commandText = text;
  42. }
  43. #region Properties
  44. public virtual string ResolvedCommandText
  45. {
  46. get { return commandText; }
  47. }
  48. protected Driver Driver => command.Connection.driver;
  49. protected MySqlConnection Connection => command.Connection;
  50. protected MySqlParameterCollection Parameters => command.Parameters;
  51. #endregion
  52. public virtual void Close(MySqlDataReader reader)
  53. {
  54. }
  55. public virtual void Resolve(bool preparing)
  56. {
  57. }
  58. public virtual void Execute()
  59. {
  60. // we keep a reference to this until we are done
  61. BindParameters();
  62. ExecuteNext();
  63. }
  64. public virtual bool ExecuteNext()
  65. {
  66. if (_buffers.Count == 0)
  67. return false;
  68. MySqlPacket packet = _buffers[0];
  69. //MemoryStream ms = stream.InternalBuffer;
  70. Driver.SendQuery(packet);
  71. _buffers.RemoveAt(0);
  72. return true;
  73. }
  74. protected virtual void BindParameters()
  75. {
  76. MySqlParameterCollection parameters = command.Parameters;
  77. int index = 0;
  78. while (true)
  79. {
  80. InternalBindParameters(ResolvedCommandText, parameters, null);
  81. // if we are not batching, then we are done. This is only really relevant the
  82. // first time through
  83. if (command.Batch == null) return;
  84. while (index < command.Batch.Count)
  85. {
  86. MySqlCommand batchedCmd = command.Batch[index++];
  87. MySqlPacket packet = (MySqlPacket)_buffers[_buffers.Count - 1];
  88. // now we make a guess if this statement will fit in our current stream
  89. long estimatedCmdSize = batchedCmd.EstimatedSize();
  90. if (((packet.Length - 4) + estimatedCmdSize) > Connection.driver.MaxPacketSize)
  91. {
  92. // it won't, so we setup to start a new run from here
  93. parameters = batchedCmd.Parameters;
  94. break;
  95. }
  96. // looks like we might have room for it so we remember the current end of the stream
  97. _buffers.RemoveAt(_buffers.Count - 1);
  98. //long originalLength = packet.Length - 4;
  99. // and attempt to stream the next command
  100. string text = ResolvedCommandText;
  101. if (text.StartsWith("(", StringComparison.Ordinal))
  102. packet.WriteStringNoNull(", ");
  103. else
  104. packet.WriteStringNoNull("; ");
  105. InternalBindParameters(text, batchedCmd.Parameters, packet);
  106. if ((packet.Length - 4) > Connection.driver.MaxPacketSize)
  107. {
  108. //TODO
  109. //stream.InternalBuffer.SetLength(originalLength);
  110. parameters = batchedCmd.Parameters;
  111. break;
  112. }
  113. }
  114. if (index == command.Batch.Count)
  115. return;
  116. }
  117. }
  118. private void InternalBindParameters(string sql, MySqlParameterCollection parameters, MySqlPacket packet)
  119. {
  120. bool sqlServerMode = command.Connection.Settings.SqlServerMode;
  121. if (packet == null)
  122. {
  123. packet = new MySqlPacket(Driver.Encoding) { Version = Driver.Version };
  124. packet.WriteByte(0);
  125. }
  126. MySqlTokenizer tokenizer = new MySqlTokenizer(sql)
  127. {
  128. ReturnComments = true,
  129. SqlServerMode = sqlServerMode
  130. };
  131. int pos = 0;
  132. string token = tokenizer.NextToken();
  133. int parameterCount = 0;
  134. while (token != null)
  135. {
  136. // serialize everything that came before the token (i.e. whitespace)
  137. packet.WriteStringNoNull(sql.Substring(pos, tokenizer.StartIndex - pos));
  138. pos = tokenizer.StopIndex;
  139. if (MySqlTokenizer.IsParameter(token))
  140. {
  141. if ((!parameters.containsUnnamedParameters && token.Length == 1 && parameterCount > 0) || parameters.containsUnnamedParameters && token.Length > 1)
  142. throw new MySqlException(Resources.MixedParameterNamingNotAllowed);
  143. parameters.containsUnnamedParameters = token.Length == 1;
  144. if (SerializeParameter(parameters, packet, token, parameterCount))
  145. token = null;
  146. parameterCount++;
  147. }
  148. if (token != null)
  149. {
  150. if (sqlServerMode && tokenizer.Quoted && token.StartsWith("[", StringComparison.Ordinal))
  151. token = String.Format("`{0}`", token.Substring(1, token.Length - 2));
  152. packet.WriteStringNoNull(token);
  153. }
  154. token = tokenizer.NextToken();
  155. }
  156. _buffers.Add(packet);
  157. }
  158. protected virtual bool ShouldIgnoreMissingParameter(string parameterName)
  159. {
  160. if (Connection.Settings.AllowUserVariables)
  161. return true;
  162. if (parameterName.StartsWith("@" + StoredProcedure.ParameterPrefix, StringComparison.OrdinalIgnoreCase))
  163. return true;
  164. if (parameterName.Length > 1 &&
  165. (parameterName[1] == '`' || parameterName[1] == '\''))
  166. return true;
  167. return false;
  168. }
  169. /// <summary>
  170. /// Serializes the given parameter to the given memory stream
  171. /// </summary>
  172. /// <remarks>
  173. /// <para>This method is called by PrepareSqlBuffers to convert the given
  174. /// parameter to bytes and write those bytes to the given memory stream.
  175. /// </para>
  176. /// </remarks>
  177. /// <returns>True if the parameter was successfully serialized, false otherwise.</returns>
  178. private bool SerializeParameter(MySqlParameterCollection parameters,
  179. MySqlPacket packet, string parmName, int parameterIndex)
  180. {
  181. MySqlParameter parameter = null;
  182. if (!parameters.containsUnnamedParameters)
  183. parameter = parameters.GetParameterFlexible(parmName, false);
  184. else
  185. {
  186. if (parameterIndex <= parameters.Count)
  187. parameter = parameters[parameterIndex];
  188. else
  189. throw new MySqlException(Resources.ParameterIndexNotFound);
  190. }
  191. if (parameter == null)
  192. {
  193. // if we are allowing user variables and the parameter name starts with @
  194. // then we can't throw an exception
  195. if (parmName.StartsWith("@", StringComparison.Ordinal) && ShouldIgnoreMissingParameter(parmName))
  196. return false;
  197. throw new MySqlException(
  198. String.Format(Resources.ParameterMustBeDefined, parmName));
  199. }
  200. parameter.Serialize(packet, false, Connection.Settings);
  201. return true;
  202. }
  203. }
  204. }
  205. #endif