void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object v, int length) { uint val = (v is uint) ? (uint)v : Convert.ToUInt32(v); if (binary) packet.WriteInteger((long)val, is24Bit ? 3 : 4); else packet.WriteStringNoNull(val.ToString()); }
void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length) { ulong v = (val is ulong) ? (ulong)val : Convert.ToUInt64(val); if (binary) packet.WriteInteger((long)v, 8); else packet.WriteStringNoNull(v.ToString()); }
void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length) { int v = (val is Int32) ? (int)val : Convert.ToInt32(val); if (binary) packet.WriteInteger((long)v, 2); else packet.WriteStringNoNull(v.ToString()); }
void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length) { if (!(val is TimeSpan)) throw new MySqlException("Only TimeSpan objects can be serialized by MySqlTimeSpan"); TimeSpan ts = (TimeSpan)val; bool negative = ts.TotalMilliseconds < 0; ts = ts.Duration(); if (binary) { if (ts.Milliseconds > 0) packet.WriteByte(12); else packet.WriteByte(8); packet.WriteByte((byte)(negative ? 1 : 0)); packet.WriteInteger(ts.Days, 4); packet.WriteByte((byte)ts.Hours); packet.WriteByte((byte)ts.Minutes); packet.WriteByte((byte)ts.Seconds); if (ts.Milliseconds > 0) { long mval = ts.Milliseconds*1000; packet.WriteInteger(mval, 4); } } else { String s = String.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5}'", negative ? "-" : "", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds); packet.WriteStringNoNull(s); } }
public void Open() { // connect to one of our specified hosts baseStream = new RemoteConnection(); baseStream.Connect(MySqlConnection.CurrentStringBuilder.ServerEndPoint, MySqlConnection.CurrentStringBuilder.ServerPort); int maxSinglePacket = 255 * 255 * 255; stream = new MySqlStream(baseStream, Encoding, false); stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000); // read off the welcome packet and parse out it's values packet = stream.ReadPacket(); int protocol = packet.ReadByte(); string versionString = packet.ReadString(); version = DBVersion.Parse(versionString); if (!version.isAtLeast(5, 0, 0)) throw new NotSupportedException(Resources.ServerTooOld); threadId = packet.ReadInteger(4); encryptionSeed = packet.ReadString(); maxSinglePacket = (256 * 256 * 256) - 1; // read in Server capabilities if they are provided ClientFlags serverCaps = 0; if (packet.HasMoreData) serverCaps = (ClientFlags)packet.ReadInteger(2); /* New protocol with 16 bytes to describe server characteristics */ owner.ConnectionCharSetIndex = (int)packet.ReadByte(); serverStatus = (ServerStatusFlags)packet.ReadInteger(2); // Since 5.5, high bits of server caps are stored after status. // Previously, it was part of reserved always 0x00 13-byte filler. uint serverCapsHigh = (uint)packet.ReadInteger(2); serverCaps |= (ClientFlags)(serverCapsHigh << 16); packet.Position += 11; string seedPart2 = packet.ReadString(); encryptionSeed += seedPart2; string authenticationMethod = ""; if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0) { authenticationMethod = packet.ReadString(); } else { // Some MySql versions like 5.1, don't give name of plugin, default to native password. authenticationMethod = "mysql_native_password"; } // based on our settings, set our connection flags SetConnectionFlags(serverCaps); packet.Clear(); packet.WriteInteger((int)connectionFlags, 4); packet.WriteInteger(maxSinglePacket, 4); packet.WriteByte(8); packet.Write(new byte[23]); Authenticate(authenticationMethod, false); // if we are using compression, then we use our CompressedStream class // to hide the ugliness of managing the compression if ((connectionFlags & ClientFlags.COMPRESS) != 0) stream = new MySqlStream(baseStream, Encoding, true); // give our stream the server version we are connected to. // We may have some fields that are read differently based // on the version of the server we are connected to. packet.Version = version; stream.MaxBlockSize = maxSinglePacket; }
public virtual void Prepare() { // strip out names from parameter markers string text; List<string> parameter_names = PrepareCommandText(out text); // ask our connection to send the prepare command MySqlField[] paramList = null; statementId = Driver.PrepareStatement(text, ref paramList); // now we need to assign our field names since we stripped them out // for the prepare for (int i = 0; i < parameter_names.Count; i++) { //paramList[i].ColumnName = (string) parameter_names[i]; string parameterName = (string)parameter_names[i]; MySqlParameter p = Parameters.GetParameterFlexible(parameterName, false); if (p == null) throw new InvalidOperationException( String.Format(Resources.ParameterNotFoundDuringPrepare, parameterName)); p.Encoding = paramList[i].Encoding; parametersToSend.Add(p); } // now prepare our null map int numNullBytes = 0; if (paramList != null && paramList.Length > 0) { nullMap = new BitArray(paramList.Length); numNullBytes = (nullMap.Count + 7) / 8; } packet = new MySqlPacket(Driver.Encoding); // write out some values that do not change run to run packet.WriteByte(0); packet.WriteInteger(statementId, 4); packet.WriteByte((byte)0); // flags; always 0 for 4.1 packet.WriteInteger(1, 4); // interation count; 1 for 4.1 nullMapPosition = packet.Position; packet.Position += numNullBytes; // leave room for our null map packet.WriteByte(1); // rebound flag // write out the parameter types foreach (MySqlParameter p in parametersToSend) packet.WriteInteger(p.GetPSType(), 2); dataPosition = packet.Position; }
void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object value, int length) { MySqlDateTime dtValue; string valueAsString = value as string; if (value is DateTime) dtValue = new MySqlDateTime(type, (DateTime)value); else if (valueAsString != null) dtValue = MySqlDateTime.Parse(valueAsString); else if (value is MySqlDateTime) dtValue = (MySqlDateTime)value; else throw new MySqlException("Unable to serialize date/time value."); if (!binary) { SerializeText(packet, dtValue); return; } if (dtValue.Millisecond > 0) packet.WriteByte(11); else packet.WriteByte(7); packet.WriteInteger(dtValue.Year, 2); packet.WriteByte((byte)dtValue.Month); packet.WriteByte((byte)dtValue.Day); if (type == MySqlDbType.Date) { packet.WriteByte(0); packet.WriteByte(0); packet.WriteByte(0); } else { packet.WriteByte((byte)dtValue.Hour); packet.WriteByte((byte)dtValue.Minute); packet.WriteByte((byte)dtValue.Second); } if (dtValue.Millisecond > 0) { long val = dtValue.Millisecond < 1000 ? dtValue.Millisecond * 1000 : dtValue.Millisecond; for (int x = 0; x < 4; x++) { packet.WriteByte((byte)(val & 0xff)); val >>= 8; } } }
public void WriteValue(MySqlPacket packet, bool binary, object value, int length) { ulong v = (value is UInt64) ? (UInt64)value : Convert.ToUInt64(value); if (binary) packet.WriteInteger((long)v, 8); else packet.WriteStringNoNull(v.ToString()); }