|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Apache.Ignite.Core.Binary; |
| 4 | + |
| 5 | +namespace OptimaJet.Workflow.Ignite |
| 6 | +{ |
| 7 | + public class ColumnInfo |
| 8 | + { |
| 9 | + public string Name; |
| 10 | + public Type Type = typeof(string); |
| 11 | + public bool IsKey = false; |
| 12 | + } |
| 13 | + |
| 14 | + public class DbObject<T> : IBinarizable where T : DbObject<T>, new() |
| 15 | + { |
| 16 | + public static List<ColumnInfo> DbColumns = new List<ColumnInfo>(); |
| 17 | + |
| 18 | + public virtual object GetValue(string key) |
| 19 | + { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + public virtual void SetValue(string key, object value) |
| 24 | + { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + public static object ConvertToDbCompatibilityType(object obj) |
| 29 | + { |
| 30 | + return obj; |
| 31 | + } |
| 32 | + |
| 33 | + private static bool IsNullable<T1>(T1 obj) |
| 34 | + { |
| 35 | + if (obj == null) return true; |
| 36 | + Type type = typeof (T1); |
| 37 | + if (!type.IsValueType) return true; |
| 38 | + if (Nullable.GetUnderlyingType(type) != null) return true; |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + public virtual void WriteBinary(IBinaryWriter writer) |
| 43 | + { |
| 44 | + foreach(var c in DbColumns) |
| 45 | + { |
| 46 | + if(c.Type == typeof(string)) |
| 47 | + { |
| 48 | + writer.WriteString(c.Name, (string)GetValue(c.Name)); |
| 49 | + } |
| 50 | + else if(c.Type == typeof(Guid)) |
| 51 | + { |
| 52 | + writer.WriteGuid(c.Name, (Guid?)GetValue(c.Name)); |
| 53 | + } |
| 54 | + else if (c.Type == typeof(byte)) |
| 55 | + { |
| 56 | + writer.WriteByte(c.Name, (byte)GetValue(c.Name)); |
| 57 | + } |
| 58 | + else if (c.Type == typeof(bool)) |
| 59 | + { |
| 60 | + writer.WriteBoolean(c.Name, (bool)GetValue(c.Name)); |
| 61 | + } |
| 62 | + else if (c.Type == typeof(DateTime)) |
| 63 | + { |
| 64 | + DateTime? dt = (DateTime?)GetValue(c.Name); |
| 65 | + if (dt.HasValue) |
| 66 | + dt = dt.Value.ToUniversalTime(); |
| 67 | + writer.WriteTimestamp(c.Name, dt); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + throw new Exception(string.Format("Unknow type {0}", c.Type)); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public virtual void ReadBinary(IBinaryReader reader) |
| 77 | + { |
| 78 | + foreach (var c in DbColumns) |
| 79 | + { |
| 80 | + if (c.Type == typeof(string)) |
| 81 | + { |
| 82 | + SetValue(c.Name, reader.ReadString(c.Name)); |
| 83 | + } |
| 84 | + else if (c.Type == typeof(Guid)) |
| 85 | + { |
| 86 | + Guid? tmp = reader.ReadGuid(c.Name); |
| 87 | + if (tmp.HasValue) |
| 88 | + SetValue(c.Name, tmp.Value); |
| 89 | + SetValue(c.Name, tmp); |
| 90 | + } |
| 91 | + else if (c.Type == typeof(byte)) |
| 92 | + { |
| 93 | + SetValue(c.Name, reader.ReadByte(c.Name)); |
| 94 | + } |
| 95 | + else if (c.Type == typeof(bool)) |
| 96 | + { |
| 97 | + SetValue(c.Name, reader.ReadBoolean(c.Name)); |
| 98 | + } |
| 99 | + else if (c.Type == typeof(DateTime)) |
| 100 | + { |
| 101 | + DateTime? tmp = reader.ReadTimestamp(c.Name); |
| 102 | + if (tmp.HasValue) |
| 103 | + SetValue(c.Name, tmp.Value); |
| 104 | + SetValue(c.Name, tmp); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + throw new Exception(string.Format("Unknow type {0}", c.Type)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments