public static void ReadValue(this IBedValueWriter writer, BinaryReader stream) { TypeStorage storage = (TypeStorage)stream.ReadByte(); switch (storage) { case TypeStorage.Null: writer.SetNull(); break; case TypeStorage.Boolean: writer.SetBoolean(stream.ReadBoolean()); break; case TypeStorage.Byte: writer.SetByte(stream.ReadByte()); break; case TypeStorage.Int16: writer.SetInt16(stream.ReadInt16()); break; case TypeStorage.Int32: writer.SetInt32(stream.ReadInt32()); break; case TypeStorage.Int64: writer.SetInt64(stream.ReadInt64()); break; case TypeStorage.SByte: writer.SetSByte(stream.ReadSByte()); break; case TypeStorage.UInt16: writer.SetUInt16(stream.ReadUInt16()); break; case TypeStorage.UInt32: writer.SetUInt32(stream.ReadUInt32()); break; case TypeStorage.UInt64: writer.SetUInt64(stream.ReadUInt64()); break; case TypeStorage.Float: writer.SetFloat(stream.ReadSingle()); break; case TypeStorage.Double: writer.SetDouble(stream.ReadDouble()); break; case TypeStorage.Decimal: writer.SetDecimal(stream.ReadDecimal()); break; case TypeStorage.DateTime: writer.SetDateTime(DateTime.FromBinary(stream.ReadInt64())); break; case TypeStorage.DateTimeEx: writer.SetDateTimeEx(DateTimeEx.FromStream(stream)); break; case TypeStorage.DateEx: writer.SetDateEx(DateEx.FromStream(stream)); break; case TypeStorage.TimeEx: writer.SetTimeEx(TimeEx.FromStream(stream)); break; case TypeStorage.ByteArray: { int len = stream.Read7BitEncodedInt(); writer.SetByteArray(stream.ReadBytes(len)); } break; case TypeStorage.Guid: writer.SetGuid(new Guid(stream.ReadBytes(16))); break; case TypeStorage.String: writer.SetString(stream.ReadString()); break; //case TypeStorage.Array: // writer.SetArray(BedArray.FromStream(stream, true)); // break; } }
//public static void WriteTo(this IBedValueReader reader, BinaryWriter stream) //{ // switch (reader.GetFieldType()) // { // case TypeStorage.Null: // stream.Write((byte)TypeStorage.Null); // break; // case TypeStorage.Boolean: // stream.Write((byte)TypeStorage.Boolean); // stream.Write(reader.GetBoolean()); // break; // case TypeStorage.Byte: // stream.Write((byte)TypeStorage.Byte); // stream.Write(reader.GetByte()); // break; // case TypeStorage.Int16: // stream.Write((byte)TypeStorage.Int16); // stream.Write(reader.GetInt16()); // break; // case TypeStorage.Int32: // stream.Write((byte)TypeStorage.Int32); // stream.Write(reader.GetInt32()); // break; // case TypeStorage.Int64: // stream.Write((byte)TypeStorage.Int64); // stream.Write(reader.GetInt64()); // break; // case TypeStorage.Float: // stream.Write((byte)TypeStorage.Float); // stream.Write(reader.GetFloat()); // break; // case TypeStorage.Double: // stream.Write((byte)TypeStorage.Double); // stream.Write(reader.GetDouble()); // break; // case TypeStorage.Decimal: // stream.Write((byte)TypeStorage.Decimal); // stream.Write(reader.GetDecimal()); // break; // case TypeStorage.DateTime: // stream.Write((byte)TypeStorage.DateTime); // stream.Write(reader.GetDateTime().ToBinary()); // break; // case TypeStorage.DateTimeEx: // stream.Write((byte)TypeStorage.DateTime); // reader.GetDateTimeEx().WriteTo(stream); // break; // case TypeStorage.ByteArray: // { // byte[] val = reader.GetByteArray(); // stream.Write((byte)TypeStorage.ByteArray); // stream.Write7BitEncodedInt(val.Length); // stream.Write(val); // } // break; // case TypeStorage.Guid: // stream.Write((byte)TypeStorage.Guid); // stream.Write(reader.GetGuid().ToByteArray()); // break; // case TypeStorage.String: // stream.Write((byte)TypeStorage.String); // stream.Write(reader.GetString()); // break; // } //} public static void WriteTo(this IBedValueReader reader, IBedValueWriter writer) { switch (reader.GetFieldType()) { case TypeStorage.Null: writer.SetNull(); break; case TypeStorage.Boolean: writer.SetBoolean(reader.GetBoolean()); break; case TypeStorage.Byte: writer.SetByte(reader.GetByte()); break; case TypeStorage.Int16: writer.SetInt16(reader.GetInt16()); break; case TypeStorage.Int32: writer.SetInt32(reader.GetInt32()); break; case TypeStorage.Int64: writer.SetInt64(reader.GetInt64()); break; case TypeStorage.SByte: writer.SetSByte(reader.GetSByte()); break; case TypeStorage.UInt16: writer.SetUInt16(reader.GetUInt16()); break; case TypeStorage.UInt32: writer.SetUInt32(reader.GetUInt32()); break; case TypeStorage.UInt64: writer.SetUInt64(reader.GetUInt64()); break; case TypeStorage.Float: writer.SetFloat(reader.GetFloat()); break; case TypeStorage.Double: writer.SetDouble(reader.GetDouble()); break; case TypeStorage.Decimal: writer.SetDecimal(reader.GetDecimal()); break; case TypeStorage.DateTime: writer.SetDateTime(reader.GetDateTime()); break; case TypeStorage.DateTimeEx: writer.SetDateTimeEx(reader.GetDateTimeEx()); break; case TypeStorage.DateEx: writer.SetDateEx(reader.GetDateEx()); break; case TypeStorage.TimeEx: writer.SetTimeEx(reader.GetTimeEx()); break; case TypeStorage.ByteArray: writer.SetByteArray(reader.GetByteArray()); break; case TypeStorage.Guid: writer.SetGuid(reader.GetGuid()); break; case TypeStorage.String: writer.SetString(reader.GetString()); break; //case TypeStorage.Array: // writer.SetArray(reader.GetArray()); // break; } }
public static void ReadFrom(this IBedValueWriter writer, IDataRecord record, int index) { if (record.IsDBNull(index)) { writer.SetNull(); return; } Type type = record.GetFieldType(index); try { switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: writer.SetBoolean(record.GetBoolean(index)); break; case TypeCode.Byte: writer.SetByte(record.GetByte(index)); break; case TypeCode.Int16: writer.SetInt16(record.GetInt16(index)); break; case TypeCode.Int32: writer.SetInt32(record.GetInt32(index)); break; case TypeCode.Int64: writer.SetInt64(record.GetInt64(index)); break; case TypeCode.SByte: unchecked { writer.SetSByte((sbyte)record.GetByte(index)); } break; case TypeCode.UInt16: unchecked { writer.SetUInt16((ushort)record.GetInt16(index)); } break; case TypeCode.UInt32: unchecked { writer.SetUInt32((uint)record.GetInt32(index)); } break; case TypeCode.UInt64: unchecked { writer.SetUInt64((ulong)record.GetInt64(index)); } break; case TypeCode.DateTime: writer.SetDateTime(record.GetDateTime(index)); break; case TypeCode.Decimal: writer.SetDecimal(record.GetDecimal(index)); break; case TypeCode.Single: writer.SetFloat(record.GetFloat(index)); break; case TypeCode.Double: writer.SetDouble(record.GetDouble(index)); break; case TypeCode.String: writer.SetString(record.GetString(index)); break; default: if (type == typeof(Guid)) { writer.SetGuid(record.GetGuid(index)); } else if (type == typeof(byte[])) { writer.SetByteArray((byte[])record.GetValue(index)); } else { writer.SetString(record.GetValue(index).ToString()); } break; } } catch { try { object val = record[index]; // try to read from boxed value (not very effective) writer.ReadFrom(val); } catch { try { writer.SetString(record.GetString(index)); } catch (Exception err) { // add information to exception try { err.Data["data_type"] = record.GetFieldType(index).FullName; } catch (Exception err2) { err.Data["data_type"] = err2.ToString(); } try { err.Data["data_isnull"] = record.IsDBNull(index).ToString(); } catch (Exception err2) { err.Data["data_isnull"] = err2.ToString(); } throw; } } } }
public static void ReadFrom(this IBedValueWriter writer, object value) { if (value == null || value == DBNull.Value) { writer.SetNull(); return; } var valreader = value as IBedValueReader; if (valreader != null) { writer.ReadFrom(valreader); return; } Type type = value.GetType(); switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: writer.SetBoolean((bool)value); break; case TypeCode.Byte: writer.SetByte((byte)value); break; case TypeCode.Int16: writer.SetInt16((short)value); break; case TypeCode.Int32: writer.SetInt32((int)value); break; case TypeCode.Int64: writer.SetInt64((long)value); break; case TypeCode.SByte: writer.SetSByte((sbyte)value); break; case TypeCode.UInt16: writer.SetUInt16((ushort)value); break; case TypeCode.UInt32: writer.SetUInt32((uint)value); break; case TypeCode.UInt64: writer.SetUInt64((ulong)value); break; case TypeCode.DateTime: writer.SetDateTime((DateTime)value); break; case TypeCode.Decimal: writer.SetDecimal((decimal)value); break; case TypeCode.Single: writer.SetFloat((float)value); break; case TypeCode.Double: writer.SetDouble((double)value); break; case TypeCode.String: writer.SetString((string)value); break; default: if (type == typeof(Guid)) { writer.SetGuid((Guid)value); } else if (type == typeof(byte[])) { writer.SetByteArray((byte[])value); } else if (type == typeof(DateTimeEx)) { writer.SetDateTimeEx((DateTimeEx)value); } else if (type == typeof(DateEx)) { writer.SetDateEx((DateEx)value); } else if (type == typeof(TimeEx)) { writer.SetTimeEx((TimeEx)value); } else { writer.SetString(value.ToString()); } break; } }
public void ParseValue(string text, TypeStorage type, IBedValueWriter writer) { foreach (string nulltext in m_settings.GetNullValues()) { if (text == nulltext) { writer.SetNull(); return; } } NumberStyles floatStyle = NumberStyles.Float | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite; NumberStyles intStyle = NumberStyles.Integer | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite; NumberStyles decStyle = NumberStyles.Number | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite; switch (type) { case TypeStorage.Null: writer.SetNull(); break; case TypeStorage.Boolean: { bool?val = m_settings.ParseBoolean(text); if (val != null) { writer.SetBoolean(val.Value); } else { writer.SetNull(); } } break; case TypeStorage.Byte: { byte val; if (Byte.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetByte(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Int16: { short val; if (Int16.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetInt16(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Int32: { int val; if (Int32.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetInt32(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Int64: { long val; if (Int64.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetInt64(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.SByte: { sbyte val; if (SByte.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetSByte(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.UInt16: { ushort val; if (UInt16.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetUInt16(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.UInt32: { uint val; if (UInt32.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetUInt32(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.UInt64: { ulong val; if (UInt64.TryParse(text, intStyle, m_numberFormat, out val)) { writer.SetUInt64(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Float: { float val; if (Single.TryParse(text, floatStyle, m_numberFormat, out val)) { writer.SetFloat(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Double: { double val; if (Double.TryParse(text, floatStyle, m_numberFormat, out val)) { writer.SetDouble(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.Decimal: { decimal val; if (Decimal.TryParse(text, decStyle, m_numberFormat, out val)) { writer.SetDecimal(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.DateTime: { DateTime val; if (DateTime.TryParseExact(text, m_settings.DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out val)) { writer.SetDateTime(val); } else if (DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out val)) { writer.SetDateTime(val); } else { ParseError(text, type, writer); } } break; case TypeStorage.DateTimeEx: try { writer.SetDateTimeEx(DateTimeEx.Parse(text, m_settings.DateTimeFormat, CultureInfo.InvariantCulture)); } catch { writer.SetDateTimeEx(DateTimeEx.FromDateTime(DateTime.Parse(text, CultureInfo.InvariantCulture))); } break; case TypeStorage.DateEx: try { writer.SetDateEx(DateTimeEx.Parse(text, m_settings.DateFormat, CultureInfo.InvariantCulture).DatePart); } catch { writer.SetDateEx(DateTimeEx.FromDateTime(DateTime.Parse(text, CultureInfo.InvariantCulture)).DatePart); } break; case TypeStorage.TimeEx: try { writer.SetTimeEx(DateTimeEx.Parse(text, m_settings.TimeFormat, CultureInfo.InvariantCulture).TimePart); } catch { writer.SetTimeEx(DateTimeEx.FromDateTime(DateTime.Parse(text, CultureInfo.InvariantCulture)).TimePart); } break; case TypeStorage.ByteArray: { if (text.EndsWith("=")) { writer.SetByteArray(Convert.FromBase64String(text.Replace("=", ""))); } else { writer.SetNull(); } } break; case TypeStorage.Guid: writer.SetGuid(new Guid(text)); break; case TypeStorage.String: writer.SetString(text); break; //case TypeStorage.Array: // writer.SetArray(BedArray.Parse(text)); // break; } }