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.

252 lines
8.0 KiB

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