ConvertFieldName() public static method

public static ConvertFieldName ( string ObjName, List FieldNames ) : List
ObjName string
FieldNames List
return List
        private void AddProjetion()
        {
            ProjectionDefinition <T> fields = null;

            if (_includedFields.Any())
            {
                var includeFieldList = MongoMapperHelper.ConvertFieldName(typeof(T).Name, _includedFields).ToList();
                fields = this.Project.Include(includeFieldList.First());
                foreach (var field in includeFieldList.Skip(1))
                {
                    fields = this.Project.Include(field);
                }
            }


            if (_excludedFields.Any())
            {
                var excludeFieldList = MongoMapperHelper.ConvertFieldName(typeof(T).Name, _excludedFields).ToList();
                fields = this.Project.Exclude(excludeFieldList.First());
                foreach (var field in excludeFieldList.Skip(1))
                {
                    fields = this.Project.Exclude(field);
                }
            }

            if (fields != null)
            {
                Cursor.Project <T>(fields);
            }
        }
Esempio n. 2
0
 public static FilterDefinition <T> RegEx(string ObjName, string FieldName, string expresion)
 {
     FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName);
     return(Builders <T> .Filter.Regex(FieldName, expresion));
 }
Esempio n. 3
0
        public static FilterDefinition <T> Ne(string ObjName, string FieldName, object Value)
        {
            object defaultValue = MongoMapperHelper.GetFieldDefaultValue(ObjName, FieldName);

            FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName);

            FilterDefinition <T> query = null;
            Type type = Value.GetType();

            if (type.BaseType != null && type.BaseType.Name == "Enum")
            {
                type = typeof(int);
            }

            if (type == typeof(DateTime))
            {
                query = Builders <T> .Filter.Ne(FieldName, (DateTime)Value);

                if (defaultValue != null && (DateTime)defaultValue != (DateTime)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(int))
            {
                query = Builders <T> .Filter.Ne(FieldName, (int)Value);

                if (defaultValue != null && (int)defaultValue != (int)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(string))
            {
                query = Builders <T> .Filter.Ne(FieldName, (string)Value);

                if (defaultValue != null && (string)defaultValue != (string)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(long))
            {
                query = Builders <T> .Filter.Ne(FieldName, (long)Value);

                if (defaultValue != null && (long)defaultValue != (long)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(bool))
            {
                query = Builders <T> .Filter.Ne(FieldName, (bool)Value);

                if (defaultValue != null && (bool)defaultValue != (bool)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(double))
            {
                query = Builders <T> .Filter.Ne(FieldName, (double)Value);

                if (defaultValue != null && (double)defaultValue != (double)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }

            return(query);
        }
Esempio n. 4
0
        public static FilterDefinition <T> Eq(string ObjName, string FieldName, object Value)
        {
            object defaultValue = MongoMapperHelper.GetFieldDefaultValue(ObjName, FieldName);

            FieldName = MongoMapperHelper.ConvertFieldName(ObjName, FieldName);

            FilterDefinition <T> query = null;
            Type type = Value.GetType();

            if (type == typeof(BsonNull))
            {
                query = Builders <T> .Filter.Eq(FieldName, BsonNull.Value);

                return(query);
            }

            if (type.BaseType != null && type.BaseType.Name == "Enum")
            {
                type = typeof(int);
                if ((int)Value < 0)
                {
                    return(new BsonDocument());
                }
            }

            if (type == typeof(string))
            {
                bool   isLike   = false;
                string txtValue = Value.ToString();

                if (txtValue.Trim() == "%")
                {
                    return(new BsonDocument());
                }

                if (txtValue.StartsWith("%") && txtValue.EndsWith("%"))
                {
                    Value  = String.Format("/{0}/", txtValue);
                    isLike = true;
                }
                else if (txtValue.StartsWith("%"))
                {
                    Value  = String.Format("/{0}$/", txtValue);
                    isLike = true;
                }
                else if (txtValue.EndsWith("%"))
                {
                    Value  = String.Format("/^{0}/", txtValue);
                    isLike = true;
                }

                if (isLike)
                {
                    query = Builders <T> .Filter.Regex(FieldName, new BsonRegularExpression(string.Format("{0}i", Value.ToString().Replace("%", ""))));

                    return(query);
                }
            }

            if (type == typeof(DateTime))
            {
                query = Builders <T> .Filter.Eq(FieldName, (DateTime)Value);

                if (defaultValue != null && (DateTime)defaultValue == (DateTime)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(int))
            {
                query = Builders <T> .Filter.Eq(FieldName, (int)Value);

                if (defaultValue != null && (int)defaultValue == (int)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(string))
            {
                query = Builders <T> .Filter.Eq(FieldName, (string)Value);

                if (defaultValue != null && (string)defaultValue == (string)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(long))
            {
                query = Builders <T> .Filter.Eq(FieldName, (long)Value);

                if (defaultValue != null && (long)defaultValue == (long)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(bool))
            {
                query = Builders <T> .Filter.Eq(FieldName, (bool)Value);

                if (defaultValue != null && (bool)defaultValue == (bool)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(double))
            {
                query = Builders <T> .Filter.Eq(FieldName, (double)Value);

                if (defaultValue != null && (double)defaultValue == (double)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }
            else if (type == typeof(Guid))
            {
                query = Builders <T> .Filter.Eq(FieldName, (Guid)Value);

                if (defaultValue != null && (Guid)defaultValue == (Guid)Value)
                {
                    query = Builders <T> .Filter.Or(query, Builders <T> .Filter.Exists(FieldName, false));
                }
            }

            return(query);
        }
        internal static void CreateIndexes(Type ClassType)
        {
            var existingIndexNames = GetExistinIndexNames(ClassType);

            foreach (string index in GetIndexes(ClassType))
            {
                if (index.StartsWith("2D|"))
                {
                    var mongoIndex = Builders <BsonDocument> .IndexKeys.Geo2D(MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split('|')[1]).Trim());

                    var indexName = "2D" + "_" + index.Split('|')[1];

                    if (!existingIndexNames.Contains(indexName))
                    {
                        Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName);
                        CollectionsManager.GetCollection <BsonDocument>(ClassType.Name)
                        .Indexes.CreateOneAsync(mongoIndex, new CreateIndexOptions()
                        {
                            Name = indexName
                        })
                        .GetAwaiter()
                        .GetResult();
                    }
                }
                else if (index.StartsWith("2DSphere|"))
                {
                    var indexName = "2DSphere" + "_" + index.Split('|')[1];

                    if (!existingIndexNames.Contains(indexName))
                    {
                        Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName);
                        var mongoIndex =
                            Builders <BsonDocument> .IndexKeys.Geo2DSphere(
                                MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split('|')[1]).Trim());

                        CollectionsManager.GetCollection <BsonDocument>(ClassType.Name)
                        .Indexes.CreateOneAsync(mongoIndex, new CreateIndexOptions()
                        {
                            Name = indexName
                        })
                        .GetAwaiter()
                        .GetResult();
                    }
                }
                else
                {
                    var indexFieldnames = MongoMapperHelper.ConvertFieldName(ClassType.Name, index.Split(',').ToList()).Select(IndexField => IndexField.Trim());

                    var fieldnames = indexFieldnames as IList <string> ?? indexFieldnames.ToList();

                    if (fieldnames.Any())
                    {
                        var indexName = "IX" + "_" + string.Join("_", fieldnames);

                        if (!existingIndexNames.Contains(indexName))
                        {
                            Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName);

                            var indexFields = Builders <BsonDocument> .IndexKeys.Ascending(fieldnames.First());

                            indexFields = fieldnames.Skip(1)
                                          .Aggregate(indexFields, (Current, FieldName) => Current.Ascending(FieldName));

                            CollectionsManager.GetCollection <BsonDocument>(ClassType.Name)
                            .Indexes.CreateOneAsync(indexFields, new CreateIndexOptions()
                            {
                                Name = indexName
                            })
                            .GetAwaiter()
                            .GetResult();
                        }
                    }
                }
            }

            string[] pk = GetPrimaryKey(ClassType).ToArray();
            if (pk.Count(K => K == "m_id") == 0)
            {
                var indexFieldnames = MongoMapperHelper.ConvertFieldName(ClassType.Name, pk.ToList()).Select(PkField => PkField.Trim());

                var fieldnames = indexFieldnames as IList <string> ?? indexFieldnames.ToList();
                if (fieldnames.Any())
                {
                    var indexName = "PK_" + string.Join("_", fieldnames);

                    if (!existingIndexNames.Contains(indexName))
                    {
                        Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName);

                        var indexFields = Builders <BsonDocument> .IndexKeys.Ascending(fieldnames.First());

                        indexFields = fieldnames.Skip(1)
                                      .Aggregate(indexFields, (Current, FieldName) => Current.Ascending(FieldName));

                        CollectionsManager.GetCollection <BsonDocument>(ClassType.Name)
                        .Indexes.CreateOneAsync(indexFields,
                                                new CreateIndexOptions()
                        {
                            Unique = true, Name = indexName
                        })
                        .GetAwaiter()
                        .GetResult();
                    }
                }
            }

            string ttlIndex = GetTTLIndex(ClassType);

            if (ttlIndex != string.Empty)
            {
                var tmpIndex  = ttlIndex.Split(',');
                var indexName = "TTL_" + tmpIndex[0].Trim();

                if (!existingIndexNames.Contains(indexName))
                {
                    Console.WriteLine("CREATING INDEX IN" + ClassType.Name + " => " + indexName);

                    var keys = Builders <BsonDocument> .IndexKeys.Ascending(tmpIndex[0].Trim());

                    CollectionsManager.GetCollection <BsonDocument>(ClassType.Name).Indexes.CreateOneAsync(
                        keys,
                        new CreateIndexOptions()
                    {
                        Name        = indexName,
                        ExpireAfter = TimeSpan.FromSeconds(int.Parse(tmpIndex[1].Trim()))
                    })
                    .GetAwaiter()
                    .GetResult();
                }
            }
        }