Records database.
Наследование: ObservableObject
Пример #1
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="columns">The record columns.</param>
 /// <param name="values">The record values.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, ImmutableArray<XColumn> columns, ImmutableArray<XValue> values)
 {
     return new XRecord()
     {
         Id = Guid.NewGuid(),
         Columns = columns,
         Values = values,
         Owner = owner
     };
 }
Пример #2
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="id">The record Id.</param>
 /// <param name="columns">The record columns.</param>
 /// <param name="values">The record values.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, string id, ImmutableArray <XColumn> columns, ImmutableArray <XValue> values)
 {
     return(new XRecord()
     {
         Id = string.IsNullOrWhiteSpace(id) ? Guid.NewGuid() : Guid.Parse(id),
         Columns = columns,
         Values = values,
         Owner = owner
     });
 }
Пример #3
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="columns">The record columns.</param>
 /// <param name="values">The record values.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, ImmutableArray <XColumn> columns, ImmutableArray <XValue> values)
 {
     return(new XRecord()
     {
         Id = Guid.NewGuid(),
         Columns = columns,
         Values = values,
         Owner = owner
     });
 }
Пример #4
0
 /// <summary>
 /// Creates a new <see cref="XColumn"/> instance.
 /// </summary>
 /// <param name="owner">The owner instance.</param>
 /// <param name="name">The column name.</param>
 /// <param name="width">The column width.</param>
 /// <param name="isVisible">The flag indicating whether column is visible.</param>
 /// <returns>The new instance of the <see cref="XColumn"/> class.</returns>
 public static XColumn Create(XDatabase owner, string name, double width = double.NaN, bool isVisible = true)
 {
     return new XColumn()
     {
         Id = Guid.NewGuid(),
         Name = name,
         Width = width,
         IsVisible = isVisible,
         Owner = owner
     };
 }
Пример #5
0
 /// <summary>
 /// Creates a new <see cref="XColumn"/> instance.
 /// </summary>
 /// <param name="owner">The owner instance.</param>
 /// <param name="name">The column name.</param>
 /// <param name="width">The column width.</param>
 /// <param name="isVisible">The flag indicating whether column is visible.</param>
 /// <returns>The new instance of the <see cref="XColumn"/> class.</returns>
 public static XColumn Create(XDatabase owner, string name, double width = double.NaN, bool isVisible = true)
 {
     return(new XColumn()
     {
         Id = Guid.NewGuid(),
         Name = name,
         Width = width,
         IsVisible = isVisible,
         Owner = owner
     });
 }
Пример #6
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="value">The record value.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, string value)
 {
     return(new XRecord()
     {
         Id = Guid.NewGuid(),
         Columns = owner.Columns,
         Values = ImmutableArray.CreateRange(
             Enumerable.Repeat(
                 value,
                 owner.Columns.Length).Select(c => XValue.Create(c))),
         Owner = owner
     });
 }
Пример #7
0
        /// <summary>
        /// Creates a new <see cref="XDatabase"/> instance.
        /// </summary>
        /// <param name="name">The database name.</param>
        /// <param name="fields">The fields collection.</param>
        /// <param name="idColumnName">The Id column name.</param>
        /// <returns>The new instance of the <see cref="XDatabase"/> class.</returns>
        public static XDatabase FromFields(string name, IEnumerable <string[]> fields, string idColumnName = DefaultIdColumnName)
        {
            var db          = XDatabase.Create(name, idColumnName);
            var tempColumns = fields.FirstOrDefault().Select(c => XColumn.Create(db, c));
            var columns     = ImmutableArray.CreateRange <XColumn>(tempColumns);

            if (columns.Length >= 1 && columns[0].Name == idColumnName)
            {
                // Use existing record Id.
                var tempRecords = fields
                                  .Skip(1)
                                  .Select(v =>
                                          XRecord.Create(
                                              db,
                                              v.FirstOrDefault(),
                                              columns,
                                              ImmutableArray.CreateRange <XValue>(v.Select(c => XValue.Create(c)))));
                var records = ImmutableArray.CreateRange <XRecord>(tempRecords);

                db.Columns = columns;
                db.Records = records;
            }
            else
            {
                // Create records with new Id.
                var tempRecords = fields
                                  .Skip(1)
                                  .Select(v =>
                                          XRecord.Create(
                                              db,
                                              columns,
                                              ImmutableArray.CreateRange <XValue>(v.Select(c => XValue.Create(c)))));
                var records = ImmutableArray.CreateRange <XRecord>(tempRecords);

                db.Columns = columns;
                db.Records = records;
            }

            return(db);
        }
Пример #8
0
        private void Remove(XDatabase database)
        {
            if (database == null)
                return;

            database.PropertyChanged -= ObserveDatabase;

            if (database.Columns != null)
            {
                Remove(database.Columns);
            }

            if (database.Records != null)
            {
                Remove(database.Records);
            }
        }
Пример #9
0
        private void Add(XDatabase database)
        {
            if (database == null)
                return;

            database.PropertyChanged += ObserveDatabase;

            if (database.Columns != null)
            {
                Add(database.Columns);
            }

            if (database.Records != null)
            {
                Add(database.Records);
            }
        }
Пример #10
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="id">The record Id.</param>
 /// <param name="columns">The record columns.</param>
 /// <param name="values">The record values.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, string id, ImmutableArray<XColumn> columns, ImmutableArray<XValue> values)
 {
     return new XRecord()
     {
         Id = string.IsNullOrWhiteSpace(id) ? Guid.NewGuid() : Guid.Parse(id),
         Columns = columns,
         Values = values,
         Owner = owner
     };
 }
Пример #11
0
 /// <summary>
 /// Creates a new <see cref="XRecord"/> instance.
 /// </summary>
 /// <param name="owner">The record owner.</param>
 /// <param name="value">The record value.</param>
 /// <returns>The new instance of the <see cref="XRecord"/> class.</returns>
 public static XRecord Create(XDatabase owner, string value)
 {
     return new XRecord()
     {
         Id = Guid.NewGuid(),
         Columns = owner.Columns,
         Values = ImmutableArray.CreateRange(
             Enumerable.Repeat(
                 value, 
                 owner.Columns.Length).Select(c => XValue.Create(c))),
         Owner = owner
     };
 }
Пример #12
0
        /// <summary>
        /// Update the destination database using data from source database using Id column as identification.
        /// </summary>
        /// <param name="destination">The destination database.</param>
        /// <param name="source">The source database.</param>
        /// <param name="records">The updated records from destination database.</param>
        /// <returns>True if destination database was updated.</returns>
        public static bool Update(XDatabase destination, XDatabase source, out ImmutableArray<XRecord>.Builder records)
        {
            bool isDirty = false;
            records = null;

            if (source == null || destination == null)
            {
                return isDirty;
            }

            // Check the number of source database columns.
            if (source.Columns.Length <= 1)
            {
                return isDirty;
            }

            // Check for presence of the Id column in the source database.
            if (source.Columns[0].Name != destination.IdColumnName)
            {
                return isDirty;
            }

            // Check for matching columns length.
            if (source.Columns.Length - 1 != destination.Columns.Length)
            {
                return isDirty;
            }

            // Check for matching column names.
            for (int i = 1; i < source.Columns.Length; i++)
            {
                if (source.Columns[i].Name != destination.Columns[i - 1].Name)
                {
                    return isDirty;
                }
            }

            // Create updated records builder.
            records = destination.Records.ToBuilder();

            // Update or remove existing records.
            for (int i = 0; i < destination.Records.Length; i++)
            {
                var record = destination.Records[i];
                var result = source.Records.FirstOrDefault(r => r.Id == record.Id);
                if (result != null)
                {
                    // Update existing record.
                    for (int j = 1; j < result.Values.Length; j++)
                    {
                        var valuesBuilder = record.Values.ToBuilder();
                        valuesBuilder[j - 1] = result.Values[j];
                        record.Values = valuesBuilder.ToImmutable();
                    }
                    isDirty = true;
                }
                else
                {
                    // Remove existing record.
                    records.Remove(record);
                    isDirty = true;
                }
            }

            // Add new records.
            for (int i = 0; i < source.Records.Length; i++)
            {
                var record = source.Records[i];
                var result = destination.Records.FirstOrDefault(r => r.Id == record.Id);
                if (result == null)
                {
                    var r = source.Records[i];

                    // Use existing columns.
                    r.Columns = destination.Columns;

                    // Skip Id column.
                    r.Values = r.Values.Skip(1).ToImmutableArray();

                    // Add new record.
                    records.Add(r);
                    isDirty = true;
                }
            }

            return isDirty;
        }
Пример #13
0
        /// <summary>
        /// Update the destination database using data from source database using Id column as identification.
        /// </summary>
        /// <param name="destination">The destination database.</param>
        /// <param name="source">The source database.</param>
        /// <param name="records">The updated records from destination database.</param>
        /// <returns>True if destination database was updated.</returns>
        public static bool Update(XDatabase destination, XDatabase source, out ImmutableArray <XRecord> .Builder records)
        {
            bool isDirty = false;

            records = null;

            if (source == null || destination == null)
            {
                return(isDirty);
            }

            // Check the number of source database columns.
            if (source.Columns.Length <= 1)
            {
                return(isDirty);
            }

            // Check for presence of the Id column in the source database.
            if (source.Columns[0].Name != destination.IdColumnName)
            {
                return(isDirty);
            }

            // Check for matching columns length.
            if (source.Columns.Length - 1 != destination.Columns.Length)
            {
                return(isDirty);
            }

            // Check for matching column names.
            for (int i = 1; i < source.Columns.Length; i++)
            {
                if (source.Columns[i].Name != destination.Columns[i - 1].Name)
                {
                    return(isDirty);
                }
            }

            // Create updated records builder.
            records = destination.Records.ToBuilder();

            // Update or remove existing records.
            for (int i = 0; i < destination.Records.Length; i++)
            {
                var record = destination.Records[i];
                var result = source.Records.FirstOrDefault(r => r.Id == record.Id);
                if (result != null)
                {
                    // Update existing record.
                    for (int j = 1; j < result.Values.Length; j++)
                    {
                        var valuesBuilder = record.Values.ToBuilder();
                        valuesBuilder[j - 1] = result.Values[j];
                        record.Values        = valuesBuilder.ToImmutable();
                    }
                    isDirty = true;
                }
                else
                {
                    // Remove existing record.
                    records.Remove(record);
                    isDirty = true;
                }
            }

            // Add new records.
            for (int i = 0; i < source.Records.Length; i++)
            {
                var record = source.Records[i];
                var result = destination.Records.FirstOrDefault(r => r.Id == record.Id);
                if (result == null)
                {
                    var r = source.Records[i];

                    // Use existing columns.
                    r.Columns = destination.Columns;

                    // Skip Id column.
                    r.Values = r.Values.Skip(1).ToImmutableArray();

                    // Add new record.
                    records.Add(r);
                    isDirty = true;
                }
            }

            return(isDirty);
        }
Пример #14
0
 public void Records_Not_Null()
 {
     var target = new XDatabase();
     Assert.NotNull(target.Records);
 }
Пример #15
0
 public void Columns_Not_Null()
 {
     var target = new XDatabase();
     Assert.NotNull(target.Columns);
 }
Пример #16
0
 public void Inherits_From_ObservableObject()
 {
     var target = new XDatabase();
     Assert.True(target is ObservableObject);
 }