/// <summary> /// Serializes the given parameter to the given memory stream /// </summary> /// <remarks> /// <para>This method is called by PrepareSqlBuffers to convert the given /// parameter to bytes and write those bytes to the given memory stream. /// </para> /// </remarks> /// <returns>True if the parameter was successfully serialized, false otherwise.</returns> private bool SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, string parmName, int parameterIndex) { MySqlParameter parameter = null; if (!parameters.containsUnnamedParameters) { parameter = parameters.GetParameterFlexible(parmName, false); } else { if (parameterIndex <= parameters.Count) { parameter = parameters[parameterIndex]; } else { throw new MySqlException(MySqlResources.ParameterIndexNotFound); } } if (parameter == null) { // if we are allowing user variables and the parameter name starts with @ // then we can't throw an exception if (parmName.StartsWith("@", StringComparison.Ordinal) && ShouldIgnoreMissingParameter(parmName)) { return(false); } throw new MySqlException( String.Format(MySqlResources.ParameterMustBeDefined, parmName)); } parameter.Serialize(packet, false, Connection.Settings); return(true); }
public override void Execute() { // if we are not prepared, then call down to our base if (!IsPrepared) { base.Execute(); return; } //support long data here // create our null bitmap // we check this because Mono doesn't ignore the case where nullMapBytes // is zero length. // if (nullMapBytes.Length > 0) // { // byte[] bits = packet.Buffer; // nullMap.CopyTo(bits, // nullMap.CopyTo(nullMapBytes, 0); // start constructing our packet // if (Parameters.Count > 0) // nullMap.CopyTo(packet.Buffer, nullMapPosition); //if (parameters != null && parameters.Count > 0) //else // packet.WriteByte( 0 ); //only send rebound if parms change // now write out all non-null values packet.Position = dataPosition; for (int i = 0; i < parametersToSend.Count; i++) { MySqlParameter p = parametersToSend[i]; nullMap[i] = (p.Value == DBNull.Value || p.Value == null) || p.Direction == ParameterDirection.Output; if (nullMap[i]) { continue; } packet.Encoding = p.Encoding; p.Serialize(packet, true, Connection.Settings); } if (nullMap != null) { nullMap.CopyTo(packet.Buffer, nullMapPosition); } executionCount++; Driver.ExecuteStatement(packet); }
/// <summary> /// Serializes the given parameter to the given memory stream /// </summary> /// <remarks> /// <para>This method is called by PrepareSqlBuffers to convert the given /// parameter to bytes and write those bytes to the given memory stream. /// </para> /// </remarks> /// <returns>True if the parameter was successfully serialized, false otherwise.</returns> private bool SerializeParameter(MySqlParameterCollection parameters, MySqlStream stream, string parmName) { MySqlParameter parameter = GetParameter(parameters, parmName); if (parameter == null) { // if we are using old syntax, we can't throw exceptions for parameters // not defined. if (Connection.Settings.UseOldSyntax) { return(false); } throw new MySqlException( String.Format(System.Data.MySqlClient.Properties.Resources.ParameterMustBeDefined, parmName)); } parameter.Serialize(stream, false); return(true); }
public override void Execute() { // if we are not prepared, then call down to our base if (!IsPrepared) { base.Execute(); return; } MySqlStream stream = new MySqlStream(Driver.Encoding); //TODO: support long data here // create our null bitmap BitArray nullMap = new BitArray(Parameters.Count); // now we run through the parameters that PREPARE sent back and use // those names to index into the parameters the user gave us. // if the user set that parameter to NULL, then we set the null map // accordingly if (paramList != null) { for (int x = 0; x < paramList.Length; x++) { MySqlParameter p = Parameters[paramList[x].ColumnName]; if (p.Value == DBNull.Value || p.Value == null) { nullMap[x] = true; } } } byte[] nullMapBytes = new byte[(Parameters.Count + 7) / 8]; // we check this because Mono doesn't ignore the case where nullMapBytes // is zero length. if (nullMapBytes.Length > 0) { nullMap.CopyTo(nullMapBytes, 0); } // start constructing our packet stream.WriteInteger(statementId, 4); stream.WriteByte((byte)0); // flags; always 0 for 4.1 stream.WriteInteger(1, 4); // interation count; 1 for 4.1 stream.Write(nullMapBytes); //if (parameters != null && parameters.Count > 0) stream.WriteByte(1); // rebound flag //else // packet.WriteByte( 0 ); //TODO: only send rebound if parms change // write out the parameter types if (paramList != null) { foreach (MySqlField param in paramList) { MySqlParameter parm = Parameters[param.ColumnName]; stream.WriteInteger(parm.GetPSType(), 2); } // now write out all non-null values foreach (MySqlField param in paramList) { int index = Parameters.IndexOf(param.ColumnName); if (index == -1) { throw new MySqlException("Parameter '" + param.ColumnName + "' is not defined."); } MySqlParameter parm = Parameters[index]; if (parm.Value == DBNull.Value || parm.Value == null) { continue; } stream.Encoding = param.Encoding; parm.Serialize(stream, true); } } executionCount++; Driver.ExecuteStatement(stream.InternalBuffer.ToArray()); }