Esempio n. 1
0
        public QueryResult(IEnumerable <BsonValue> bsonValues, ICultureFormat cultureFormat)
        {
            _cultureFormat = cultureFormat;
            InstanceId     = Guid.NewGuid().ToString("D");

            Initialize(bsonValues);
        }
Esempio n. 2
0
 public static void EnsureValue(ref ICultureFormat cultureFormat)
 {
     if (cultureFormat == null)
     {
         cultureFormat = Invariant;
     }
 }
        public static DataTable ToDataTable(this IEnumerable <BsonValue> bsonValues,
                                            ICultureFormat cultureFormat)
        {
            DefaultCultureFormat.EnsureValue(ref cultureFormat);

            var table = new DataTable();

            if (bsonValues == null)
            {
                return(table);
            }

            foreach (var value in bsonValues)
            {
                var row = table.NewRow();
                var doc = value.IsDocument ?
                          value.AsDocument :
                          new BsonDocument {
                    ["[value]"] = value
                };

                if (doc.Keys.Count == 0)
                {
                    doc["[root]"] = "{}";
                }

                foreach (var key in doc.Keys)
                {
                    var col = table.Columns[key];
                    if (col == null)
                    {
                        table.Columns.Add(key);
                        var readOnly = key == "_id";
                        col            = table.Columns[key];
                        col.ColumnName = key;
                        col.Caption    = key;
                        col.ReadOnly   = readOnly;
                    }
                }

                foreach (var key in doc.Keys)
                {
                    var bsonValue = doc[key];

                    if (bsonValue.IsNull || bsonValue.IsArray || bsonValue.IsDocument || bsonValue.IsBinary) // convertToString ||
                    {
                        row[key] = bsonValue.ToDisplayValue(null, cultureFormat);
                    }
                    else
                    {
                        row[key] = bsonValue.RawValue;
                    }
                }

                table.Rows.Add(row);
            }

            return(table);
        }
Esempio n. 4
0
        public static string ToDisplayValue(this BsonValue bsonValue, int?maxLength = null, ICultureFormat cultureFormat = null)
        {
            if (bsonValue == null)
            {
                return(string.Empty);
            }

            if (cultureFormat == null)
            {
                cultureFormat = DefaultCultureFormat.Invariant;
            }

            var cultureInfo = cultureFormat.Culture ?? CultureInfo.InvariantCulture;

            try
            {
                string result;

                switch (bsonValue.Type)
                {
                case BsonType.MinValue:
                    result = @"-∞";
                    break;

                case BsonType.MaxValue:
                    result = @"+∞";
                    break;

                case BsonType.Boolean:
                    result = bsonValue.AsBoolean.ToString(cultureInfo).ToLower();
                    break;

                case BsonType.DateTime:
                    result = !string.IsNullOrEmpty(cultureFormat.DateTimeFormat) ? bsonValue.AsDateTime.ToString(cultureFormat.DateTimeFormat, cultureInfo) : bsonValue.AsDateTime.ToString(cultureInfo);
                    break;

                case BsonType.Null:
                    result = "(null)";
                    break;

                case BsonType.Binary:
                    result = Convert.ToBase64String(bsonValue.AsBinary);
                    break;

                case BsonType.Int32:
                case BsonType.Int64:
                case BsonType.Double:
                case BsonType.Decimal:
                case BsonType.String:
                case BsonType.ObjectId:
                case BsonType.Guid:
                    result = Convert.ToString(bsonValue.RawValue, cultureInfo);
                    break;

                case BsonType.Document:
                    result = @"[Document]";
                    break;

                case BsonType.Array:
                    result = @"[Array]";
                    break;

                default:
                    result = JsonSerializer.Serialize(bsonValue);
                    break;
                }

                if (maxLength.HasValue)
                {
                    return(result?.Truncate(maxLength.Value));
                }

                return(result);
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
        public static FrameworkElement GetBsonValueEditor(BsonValueEditorContext editorContext, ICultureFormat cultureFormat)
        {
            var binding = new Binding
            {
                Path                = new PropertyPath(editorContext.BindingPath),
                Source              = editorContext.BindingSource,
                Mode                = BindingMode.TwoWay,
                Converter           = new BsonValueToNetValueConverter(),
                UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
            };

            void AddValueChangedListener(FrameworkElement associatedObject, DependencyProperty dependencyProperty)
            {
                if (associatedObject == null || dependencyProperty == null)
                {
                    return;
                }

                var descriptor =
                    DependencyPropertyDescriptor.FromProperty(dependencyProperty, associatedObject.GetType());

                descriptor?.AddValueChanged(associatedObject, (sender, args) =>
                {
                    if (sender is FrameworkElement frameworkElement && !frameworkElement.IsLoaded)
                    {
                        return;
                    }

                    // Logger.Debug("BsonValue changed: {senderType}, {propertyName}", sender.GetType(), dependencyProperty.Name);

                    editorContext.SetChanged(sender);
                });
            }
        public static LookupTable ToLookupTable(this IEnumerable <BsonValue> bsonValues, ICultureFormat cultureFormat)
        {
            DefaultCultureFormat.EnsureValue(ref cultureFormat);

            var table = new LookupTable();

            if (bsonValues == null)
            {
                return(table);
            }

            foreach (var value in bsonValues)
            {
                var row = table.NewRow();
                var doc = value.IsDocument ?
                          value.AsDocument :
                          new BsonDocument {
                    ["[value]"] = value
                };

                if (doc.Keys.Count == 0)
                {
                    doc["[root]"] = "{}";
                }

                foreach (var key in doc.Keys)
                {
                    var col = table.Columns[key];
                    if (col == null)
                    {
                        table.Columns.Add(new LookupDataColumn {
                            ColumnName = key
                        });
                    }
                }

                foreach (var key in doc.Keys)
                {
                    var bsonValue = doc[key];
                    if (bsonValue.IsNull || bsonValue.IsArray || bsonValue.IsDocument || bsonValue.IsBinary)
                    {
                        row[key] = bsonValue.ToDisplayValue(null, cultureFormat);
                    }
                    else
                    {
                        row[key] = bsonValue.RawValue;
                    }
                }

                table.Rows.Add(row);
            }

            return(table);
        }