public AutoTrackingTable (DatabaseFront database, string name, double trackIntervalTime, IObservable <TRecord> itemAddedObservable, int version) { this.Table = new TypedTable <TRecord, TKey>(database, name) { IsIdAuto = false, Version = version, }; this.Tracker = new Tracker <TRecord, TKey>(this.Table).AddTo(this.Disposables); itemAddedObservable .Buffer(itemAddedObservable.Throttle(TimeSpan.FromMilliseconds(trackIntervalTime))) .Subscribe(items => { Task.Run(async() => { await this.Table.Parent.RequestThreadSafeTransactionAsync (context => this.Table.AddRangeAsync(items, context.Connection, context.Transaction)) .ConfigureAwait(false); items.ForEach(x => this.Tracker.Track(x)); }); }) .AddTo(this.Disposables); }
public TypedTable(DatabaseFront database, string name) { this.Parent = database; this.Name = name; if (typeof(TKey) == typeof(string)) { this.IsTextKey = true; } this.Parent.Add(this); this.columnOptions = new Dictionary <string, string>(); this.properties = new Lazy <Dictionary <string, Type> >(() => typeof(TRecord) .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(property => property.CustomAttributes.Any (atr => atr.AttributeType.Equals(typeof(RecordMemberAttribute)))) .ToDictionary(x => x.Name, x => x.PropertyType)); var propertiesWithoutId = new Lazy <KeyValuePair <string, Type>[]>(() => this.properties.Value .Where(x => !x.Key.Equals(IdName)) .OrderBy(x => x.Key) .ToArray()); this.values = new Lazy <string>(() => propertiesWithoutId.Value .Select(x => x.Key) .Join(", @")); this.targets = new Lazy <string>(() => propertiesWithoutId.Value .Select(x => x.Key) .Join(",\n ")); this.tableSchema = new Lazy <string>(() => { var columns = propertiesWithoutId.Value .Select(x => { var schema = $"{x.Key} {TypeConversion[x.Value].ToString()}"; if (this.columnOptions.ContainsKey(x.Key)) { return($"{schema} {this.columnOptions[x.Key]}"); } return(schema); }) .Join(",\n "); var idType = TypeConversion[this.properties.Value[IdName]].TypeName; return($"{IdName} {idType} primary key,\n {columns}"); }); this.updateSchema = new Lazy <string>(() => propertiesWithoutId.Value .Select(x => $"{x.Key} = @{x.Key}") .Join(", ")); }