private void AssertAnnotations(IMutableAnnotatable element)
 {
     foreach (var annotationName in GetAnnotationNames())
     {
         Assert.Equal("Value", (string)element[annotationName]);
     }
 }
Exemplo n.º 2
0
        public static PostgresCollation GetOrAddCollation(
            [NotNull] IMutableAnnotatable annotatable,
            [CanBeNull] string schema,
            [NotNull] string name,
            [NotNull] string lcCollate,
            [NotNull] string lcCtype,
            [CanBeNull] string provider = null,
            bool?deterministic          = null)
        {
            Check.NotNull(annotatable, nameof(annotatable));
            Check.NullButNotEmpty(schema, nameof(schema));
            Check.NotEmpty(name, nameof(name));

            if (FindCollation(annotatable, schema, name) is PostgresCollation collation)
            {
                return(collation);
            }

            var annotationName = BuildAnnotationName(schema, name);

            return(new PostgresCollation(annotatable, annotationName)
            {
                LcCollate = lcCollate,
                LcCtype = lcCtype,
                Provider = provider,
                IsDeterministic = deterministic
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets or adds a <see cref="PostgresRange"/> from or to the <see cref="IMutableAnnotatable"/>.
        /// </summary>
        /// <param name="annotatable">The annotatable from which to get or add the range.</param>
        /// <param name="schema">The range schema or null to use the model's default schema.</param>
        /// <param name="name">The range name.</param>
        /// <param name="subtype">The range subtype.</param>
        /// <param name="canonicalFunction"></param>
        /// <param name="subtypeOpClass"></param>
        /// <param name="collation"></param>
        /// <param name="subtypeDiff"></param>
        /// <returns>
        /// The <see cref="PostgresRange"/> from the <see cref="IMutableAnnotatable"/>.
        /// </returns>
        /// <exception cref="ArgumentException"><paramref name="schema"/></exception>
        /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception>
        /// <exception cref="ArgumentNullException"><paramref name="name"/></exception>
        /// <exception cref="ArgumentNullException"><paramref name="subtype"/></exception>
        public static PostgresRange GetOrAddPostgresRange(
            IMutableAnnotatable annotatable,
            string?schema,
            string name,
            string subtype,
            string?canonicalFunction = null,
            string?subtypeOpClass    = null,
            string?collation         = null,
            string?subtypeDiff       = null)
        {
            Check.NotNull(annotatable, nameof(annotatable));
            Check.NullButNotEmpty(schema, nameof(schema));
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(subtype, nameof(subtype));

            if (FindPostgresRange(annotatable, schema, name) is PostgresRange postgresRange)
            {
                return(postgresRange);
            }

            var annotationName = BuildAnnotationName(schema, name);

            return(new PostgresRange(annotatable, annotationName)
            {
                Subtype = subtype,
                CanonicalFunction = canonicalFunction,
                SubtypeOpClass = subtypeOpClass,
                Collation = collation,
                SubtypeDiff = subtypeDiff,
            });
        }
Exemplo n.º 4
0
 internal static void AddAnnotations([NotNull] IMutableAnnotatable annotatable, [NotNull] IEnumerable <IAnnotation> annotations)
 {
     foreach (var annotation in annotations)
     {
         annotatable.AddAnnotation(annotation.Name, annotation.Value);
     }
 }
 private void AddAnnotations(IMutableAnnotatable element)
 {
     foreach (var annotationName in GetAnnotationNames()
              .Select(a => "Unicorn" + a.Substring(RelationalAnnotationNames.Prefix.Length - 1)))
     {
         element[annotationName] = "Value";
     }
 }
        public static IMutableAnnotatable HasExtendedProperty(IMutableAnnotatable annotatable, String key, String value)
        {
            var annotation       = annotatable.FindAnnotation(nameof(ExtendedProperty)) ?? new Annotation(nameof(ExtendedProperty), "{}");
            var annotationValues = JsonSerializer.Deserialize <Dictionary <String, String> >(annotation.Value as String);

            annotationValues.Add(key, value);

            annotatable.SetAnnotation(nameof(ExtendedProperty), JsonSerializer.Serialize(annotationValues));
            return(annotatable);
        }
        internal static ExtendedProperty[] GetExtendedProperties(this IMutableAnnotatable operation)
        {
            var annotation = operation.FindAnnotation(nameof(ExtendedProperty));

            if (annotation == null)
            {
                return(new ExtendedProperty[0]);
            }
            var annotationValues = JsonSerializer.Deserialize <Dictionary <String, String> >(annotation.Value as String);

            return(annotationValues.Select(x => new ExtendedProperty(x.Key, x.Value)).ToArray());
        }
        private static void SetComment(this IMutableAnnotatable annotatable, XPathNavigator navigator, MemberInfo memberInfo)
        {
            var comment = navigator.GetComment(memberInfo);

            if (!string.IsNullOrWhiteSpace(comment))
            {
                annotatable.SetAnnotation("ClrComment", comment);
            }
            else
            {
                annotatable.RemoveAnnotation("ClrComment");
            }
        }
        public static PostgresExtension GetOrAddPostgresExtension(
            [NotNull] IMutableAnnotatable annotatable,
            [NotNull] string extensionName)
        {
            var extension = (PostgresExtension)FindPostgresExtension(annotatable, extensionName);

            if (extension != null)
            {
                return(extension);
            }

            extension = new PostgresExtension(annotatable, BuildAnnotationName(extensionName));
            extension.SetData(new PostgresExtensionData {
                Name = extensionName
            });
            return(extension);
        }
Exemplo n.º 10
0
        public static PostgresEnum GetOrAddPostgresEnum(
            [NotNull] IMutableAnnotatable annotatable,
            [CanBeNull] string schema,
            [NotNull] string name,
            [NotNull] string[] labels)
        {
            var extension = FindPostgresEnum(annotatable, schema, name);

            if (extension != null)
            {
                return(extension);
            }

            extension = new PostgresEnum(annotatable, BuildAnnotationName(schema, name));
            extension.SetData(labels);
            return(extension);
        }
Exemplo n.º 11
0
        public static PostgresExtension GetOrAddPostgresExtension(
            [NotNull] IMutableAnnotatable annotatable,
            [CanBeNull] string schema,
            [NotNull] string name,
            [CanBeNull] string version)
        {
            Check.NotNull(annotatable, nameof(annotatable));
            Check.NullButNotEmpty(schema, nameof(schema));
            Check.NotNull(name, nameof(name));

            if (FindPostgresExtension(annotatable, schema, name) is PostgresExtension postgresExtension)
            {
                return(postgresExtension);
            }

            var annotationName = BuildAnnotationName(schema, name);

            return(new PostgresExtension(annotatable, annotationName)
            {
                Version = version
            });
        }
Exemplo n.º 12
0
        public static PostgresEnum GetOrAddPostgresEnum(
            [NotNull] IMutableAnnotatable annotatable,
            [CanBeNull] string schema,
            [NotNull] string name,
            [NotNull] string[] labels)
        {
            Check.NotNull(annotatable, nameof(annotatable));
            Check.NullButNotEmpty(schema, nameof(schema));
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(labels, nameof(labels));

            if (FindPostgresEnum(annotatable, schema, name) is PostgresEnum enumType)
            {
                return(enumType);
            }

            var annotationName = BuildAnnotationName(schema, name);

            return(new PostgresEnum(annotatable, annotationName)
            {
                Labels = labels
            });
        }
 private static string GetComment([NotNull] IMutableAnnotatable annotatable)
 {
     return(annotatable[CoRelationalAnnotationNames.Comment]?.ToString());
 }
Exemplo n.º 14
0
 public static PostgresExtension GetOrAddPostgresExtension(
     [NotNull] IMutableAnnotatable annotatable,
     [NotNull] string name,
     [CanBeNull] string version)
 => GetOrAddPostgresExtension(annotatable, null, name, version);
 protected virtual void CopyAnnotations([NotNull] IEnumerable <IAnnotation> annotations, [NotNull] IMutableAnnotatable annotatable)
 {
     foreach (var annotation in annotations)
     {
         annotatable.AddAnnotation(annotation.Name, annotation.Value);
     }
 }
Exemplo n.º 16
0
 public static PostgresEnum GetOrAddPostgresEnum(
     [NotNull] IMutableAnnotatable annotatable,
     [NotNull] string name,
     [NotNull] string[] labels)
 => GetOrAddPostgresEnum(annotatable, null, name, labels);
Exemplo n.º 17
0
 /// <summary>
 /// Gets or adds a <see cref="PostgresExtension"/> from or to the <see cref="IMutableAnnotatable"/>.
 /// </summary>
 /// <param name="annotatable">The annotatable from which to get or add the extension.</param>
 /// <param name="name">The extension name.</param>
 /// <param name="version">The extension version.</param>
 /// <returns>
 /// The <see cref="PostgresExtension"/> from the <see cref="IMutableAnnotatable"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException"><paramref name="annotatable"/></exception>
 /// <exception cref="ArgumentNullException"><paramref name="name"/></exception>
 public static PostgresExtension GetOrAddPostgresExtension(
     IMutableAnnotatable annotatable,
     string name,
     string?version)
 => GetOrAddPostgresExtension(annotatable, null, name, version);
Exemplo n.º 18
0
 /// <summary>
 ///     Sets the annotation stored under the given name. Overwrites the existing annotation if an
 ///     annotation with the specified name already exists. Removes the existing annotation if <see langword="null" /> is supplied.
 /// </summary>
 /// <param name="annotatable"> The object to set the annotation for. </param>
 /// <param name="name"> The name of the annotation to be added. </param>
 /// <param name="value"> The value to be stored in the annotation. </param>
 public static void SetOrRemoveAnnotation(
     [NotNull] this IMutableAnnotatable annotatable,
     [NotNull] string name,
     [CanBeNull] object value)
 => ((ConventionAnnotatable)annotatable).SetOrRemoveAnnotation(name, value, ConfigurationSource.Explicit);
 /// <summary>
 ///     Gets the existing annotation with a given key, or adds a new annotation if one does not exist.
 /// </summary>
 /// <param name="annotatable"> The object to find or add the annotation to. </param>
 /// <param name="annotationName"> The key of the annotation to be found or added. </param>
 /// <param name="value"> The value to be stored in the annotation if a new one is created. </param>
 /// <returns> The found or added annotation. </returns>
 public static Annotation GetOrAddAnnotation(
     [NotNull] this IMutableAnnotatable annotatable, [NotNull] string annotationName, [NotNull] string value)
 => annotatable.FindAnnotation(annotationName) ?? annotatable.AddAnnotation(annotationName, value);
Exemplo n.º 20
0
 /// <summary>
 ///     Gets the annotation with the given name, throwing if it does not exist.
 /// </summary>
 /// <param name="annotatable"> The object to find the annotation on. </param>
 /// <param name="annotationName"> The key of the annotation to find. </param>
 /// <returns> The annotation with the specified name. </returns>
 public static IAnnotation GetAnnotation([NotNull] this IMutableAnnotatable annotatable, [NotNull] string annotationName)
 => ((IAnnotatable)annotatable).GetAnnotation(annotationName);
        public static IMutableAnnotatable UseSnakeCase([NotNull] this IMutableAnnotatable annotatable)
        {
            if (annotatable == null)
            {
                throw new ArgumentNullException(nameof(annotatable));
            }

            switch (annotatable)
            {
            case IMutableModel m:
                return(MutateModel(m));

            case IMutableEntityType e:
                e.Relational().TableName = e.Relational().TableName.ConvertToSnakeCase();
                return(e);

            case IMutableForeignKey f:
                f.Relational().Name = f.Relational().Name.ConvertToSnakeCase();
                return(f);

            case IMutableIndex i:
                i.Relational().Name = i.Relational().Name.ConvertToSnakeCase();
                return(i);

            case IMutableKey k:
                k.Relational().Name = k.Relational().Name.ConvertToSnakeCase();
                return(k);

            case IMutableProperty p:
                p.Relational().ColumnName = p.Relational().ColumnName.ConvertToSnakeCase();
                return(p);

            case IMutableNavigation _:
            case IMutableServiceProperty _:
            case IMutablePropertyBase _:
            case IMutableTypeBase _:
            default:
                return(annotatable);
            }

            // Mutates the model in place to avoid recursive calls.
            IMutableModel MutateModel(IMutableModel m)
            {
                foreach (IMutableEntityType e in m.GetEntityTypes())
                {
                    e.UseSnakeCase();

                    foreach (IMutableForeignKey f in e.GetForeignKeys())
                    {
                        f.UseSnakeCase();
                    }

                    foreach (IMutableIndex i in e.GetIndexes())
                    {
                        i.UseSnakeCase();
                    }

                    foreach (IMutableKey k in e.GetKeys())
                    {
                        k.UseSnakeCase();
                    }

                    foreach (IMutableProperty p in e.GetProperties())
                    {
                        p.UseSnakeCase();
                    }
                }

                return(m);
            }
        }