Exemplo n.º 1
0
        public void CanBeConstructed()
        {
            var regex = new MongoRegex("expression");

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual(string.Empty, regex.RawOptions);
        }
Exemplo n.º 2
0
        private MongoRegex ParseRegEx(char[] json, ref int index)
        {
            EatWhitespace(json, ref index);

            var c = json[index++];

            if (c != '/')
            {
                return(null);
            }

            var lastIndex  = GetLastIndexOfRegEx(json, index);
            int charLength = lastIndex - index;

            char[] charArray = new char[charLength];
            Array.Copy(json, index, charArray, 0, charLength);
            var mr = new MongoRegex(new string(charArray));

            index = lastIndex + 1;

            if (index < json.Length)
            {
                if (json[index] == 'i' || json[index] == 'm' || json[index] == 'g')
                {
                    mr.Options = json[index].ToString();
                    index++;
                }
            }

            return(mr);
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Calculates the size.
        /// </summary>
        /// <param name = "regex">The regex.</param>
        /// <returns></returns>
        public int CalculateSize(MongoRegex regex)
        {
            var size = CalculateSize(regex.Expression, false);

            size += CalculateSize(regex.RawOptions, false);
            return(size);
        }
Exemplo n.º 4
0
        public void CanBeConstructedWithMongoRegexOption()
        {
            var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("img", regex.RawOptions);
        }
Exemplo n.º 5
0
        public void CanBeConstructedWithRegexOptions()
        {
            var regex = new MongoRegex("expression", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("im", regex.RawOptions);
        }
Exemplo n.º 6
0
        public void CanBeCunstructedFromNullExpressionAndOptions()
        {
            var regex = new MongoRegex(null, null);

            Assert.IsNull(regex.Expression);
            Assert.IsNull(regex.RawOptions);
        }
Exemplo n.º 7
0
        public void MongoRegexOptionFlagsAreIndependent()
        {
            var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase);

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("i", regex.RawOptions);
        }
Exemplo n.º 8
0
        public void CanBeConstructedWithOption()
        {
            var regex = new MongoRegex("expression", "options");

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("options", regex.RawOptions);
        }
Exemplo n.º 9
0
        public void CanSetOptions()
        {
            var regex = new MongoRegex("expression", null)
            {
                Options = MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace
            };

            Assert.AreEqual("ig", regex.RawOptions);
        }
Exemplo n.º 10
0
        public void CanSetOptions()
        {
            var regex = new MongoRegex("expression", null)
            {
                Options = MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace
            };

            Assert.AreEqual("ig",regex.RawOptions);
        }
Exemplo n.º 11
0
        public void CanBuildNetRegex()
        {
            var regex    = new MongoRegex("expression", MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);
            var netRegex = regex.BuildRegex();

            Assert.IsNotNull(netRegex);
            Assert.AreEqual("expression", netRegex.ToString());
            Assert.AreEqual(RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline, netRegex.Options);
        }
Exemplo n.º 12
0
        public void TestWriteMongoRegex()
        {
            var regex = new MongoRegex("expression",
                                       MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);

            var bson = Serialize(new Document("regex", regex));

            Assert.AreEqual("GwAAAAtyZWdleABleHByZXNzaW9uAGltZwAA", bson);
        }
Exemplo n.º 13
0
        public void CanBeConstructedFromRegex()
        {
            const RegexOptions options = RegexOptions.IgnoreCase |
                                         RegexOptions.IgnorePatternWhitespace |
                                         RegexOptions.Multiline;

            var regex = new MongoRegex(new Regex("expression", options));
            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("img", regex.RawOptions);
        }
Exemplo n.º 14
0
        public void CanBeConstructedFromRegex()
        {
            const RegexOptions options = RegexOptions.IgnoreCase |
                                         RegexOptions.IgnorePatternWhitespace |
                                         RegexOptions.Multiline;

            var regex = new MongoRegex(new Regex("expression", options));

            Assert.AreEqual("expression", regex.Expression);
            Assert.AreEqual("img", regex.RawOptions);
        }
Exemplo n.º 15
0
        public void CanBeXmlSerialized()
        {
            var source = new MongoRegex("exp", "opt");
            var serializer = new XmlSerializer(typeof(MongoRegex));

            var writer = new StringWriter();
            serializer.Serialize(writer, source);
            var dest = (MongoRegex)serializer.Deserialize(new StringReader(writer.ToString()));

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 16
0
        public void CanBeXmlSerializedWhenNullPropertys()
        {
            var source     = new MongoRegex(null, null);
            var serializer = new XmlSerializer(typeof(MongoRegex));

            var writer = new StringWriter();

            serializer.Serialize(writer, source);
            var dest = (MongoRegex)serializer.Deserialize(new StringReader(writer.ToString()));

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 17
0
        public void CanBeXmlSerialized()
        {
            var source     = new MongoRegex("exp", "opt");
            var serializer = new XmlSerializer(typeof(MongoRegex));

            var writer = new StringWriter();

            serializer.Serialize(writer, source);
            var dest = (MongoRegex)serializer.Deserialize(new StringReader(writer.ToString()));

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 18
0
        public void CanBeBinarySerialized()
        {
            var source = new MongoRegex("exp", "opt");
            var formatter = new BinaryFormatter();

            var mem = new MemoryStream();
            formatter.Serialize(mem, source);
            mem.Position = 0;

            var dest = (MongoRegex)formatter.Deserialize(mem);

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 19
0
        public void CanBeBinarySerialized()
        {
            var source    = new MongoRegex("exp", "opt");
            var formatter = new BinaryFormatter();

            var mem = new MemoryStream();

            formatter.Serialize(mem, source);
            mem.Position = 0;

            var dest = (MongoRegex)formatter.Deserialize(mem);

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 20
0
 public void CanBeConstructedWithMongoRegexOption()
 {
     var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);
     Assert.AreEqual("expression", regex.Expression);
     Assert.AreEqual("img", regex.RawOptions);
 }
Exemplo n.º 21
0
        public int CalculateSize(Object val)
        {
            if (val == null)
            {
                return(0);
            }
            switch (TranslateToBsonType(val))
            {
            case BsonDataType.MinKey:
            case BsonDataType.MaxKey:
            case BsonDataType.Null:
                return(0);

            case BsonDataType.Boolean:
                return(1);

            case BsonDataType.Integer:
                return(4);

            case BsonDataType.Long:
            case BsonDataType.Date:
                return(8);

            case BsonDataType.Oid:
                return(12);

            case BsonDataType.Number:
                return(sizeof(Double));

            case BsonDataType.String:
                return(CalculateSize((string)val));

            case BsonDataType.Obj: {
                Type t = val.GetType();
                if (t == typeof(Document))
                {
                    return(CalculateSize((Document)val));
                }
                else if (t == typeof(DBRef))
                {
                    return(CalculateSize((Document)((DBRef)val)));
                }
                else
                {
                    throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented yet.", t.Name));
                }
            }

            case BsonDataType.Array:
                return(CalculateSize((IEnumerable)val));

            case BsonDataType.Regex: {
                MongoRegex r    = (MongoRegex)val;
                int        size = CalculateSize(r.Expression, false);
                size += CalculateSize(r.Options, false);
                return(size);
            }

            case BsonDataType.Code:
                Code c = (Code)val;
                return(CalculateSize(c.Value, true));

            case BsonDataType.CodeWScope: {
                CodeWScope cw   = (CodeWScope)val;
                int        size = 4;
                size += CalculateSize(cw.Value, true);
                size += CalculateSize(cw.Scope);
                return(size);
            }

            case BsonDataType.Binary: {
                if (val is Guid)
                {
                    return(21);
                }
                else
                {
                    Binary b    = (Binary)val;
                    int    size = 4; //size int
                    size += 1;       //subtype
                    if (b.Subtype == Binary.TypeCode.General)
                    {
                        size += 4;     //embedded size int
                    }
                    size += b.Bytes.Length;
                    return(size);
                }
            }

            default:
                throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented.", val.GetType().Name));
            }
        }
Exemplo n.º 22
0
 public void MongoRegexOptionFlagsAreIndependent()
 {
     var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase);
     Assert.AreEqual("expression", regex.Expression);
     Assert.AreEqual("i", regex.RawOptions);
 }
Exemplo n.º 23
0
 public void CanBeConstructed()
 {
     var regex = new MongoRegex("expression");
     Assert.AreEqual("expression", regex.Expression);
     Assert.AreEqual(string.Empty, regex.RawOptions);
 }
Exemplo n.º 24
0
 public void CanBeConstructedWithOption()
 {
     var regex = new MongoRegex("expression", "options");
     Assert.AreEqual("expression",regex.Expression);
     Assert.AreEqual("options",regex.RawOptions);
 }
Exemplo n.º 25
0
 public void CanBeCunstructedFromNullExpressionAndOptions()
 {
     var regex = new MongoRegex(null, null);
     Assert.IsNull(regex.Expression);
     Assert.IsNull(regex.RawOptions);
 }
Exemplo n.º 26
0
        public void CanBuildNetRegex()
        {
            var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase|MongoRegexOption.IgnorePatternWhitespace|MongoRegexOption.Multiline);
            var netRegex = regex.BuildRegex();

            Assert.IsNotNull(netRegex);
            Assert.AreEqual("expression",netRegex.ToString());
            Assert.AreEqual(RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline, netRegex.Options);
        }
Exemplo n.º 27
0
        public void WriteValue(BsonDataType dt, Object obj)
        {
            switch (dt)
            {
            case BsonDataType.MinKey:
            case BsonDataType.MaxKey:
            case BsonDataType.Null:
                return;

            case BsonDataType.Boolean:
                writer.Write((bool)obj);
                return;

            case BsonDataType.Integer:
                writer.Write((int)obj);
                return;

            case BsonDataType.Long:
                writer.Write((long)obj);
                return;

            case BsonDataType.Date:
                DateTime d    = (DateTime)obj;
                TimeSpan diff = d.ToUniversalTime() - epoch;
                double   time = Math.Floor(diff.TotalMilliseconds);
                writer.Write((long)time);
                return;

            case BsonDataType.Oid:
                Oid id = (Oid)obj;
                writer.Write(id.Value);
                return;

            case BsonDataType.Number:
                writer.Write((double)obj);
                return;

            case BsonDataType.String: {
                String str = (String)obj;
                writer.Write(CalculateSize(str, false));
                this.WriteString(str);
                return;
            }

            case BsonDataType.Obj:
                if (obj is Document)
                {
                    this.Write((Document)obj);
                }
                else if (obj is DBRef)
                {
                    this.Write((Document)((DBRef)obj));
                }
                return;

            case BsonDataType.Array:
                this.WriteArray((IEnumerable)obj);
                return;

            case BsonDataType.Regex: {
                MongoRegex r = (MongoRegex)obj;
                this.WriteString(r.Expression);
                this.WriteString(r.Options);
                return;
            }

            case BsonDataType.Code: {
                Code c = (Code)obj;
                this.WriteValue(BsonDataType.String, c.Value);
                return;
            }

            case BsonDataType.CodeWScope: {
                CodeWScope cw = (CodeWScope)obj;
                writer.Write(CalculateSize(cw));
                this.WriteValue(BsonDataType.String, cw.Value);
                this.WriteValue(BsonDataType.Obj, cw.Scope);
                return;
            }

            case BsonDataType.Binary: {
                if (obj is Guid)
                {
                    writer.Write((int)16);
                    writer.Write((byte)3);
                    writer.Write(((Guid)obj).ToByteArray());
                }
                else
                {
                    Binary b = (Binary)obj;
                    if (b.Subtype == Binary.TypeCode.General)
                    {
                        writer.Write(b.Bytes.Length + 4);
                        writer.Write((byte)b.Subtype);
                        writer.Write(b.Bytes.Length);
                    }
                    else
                    {
                        writer.Write(b.Bytes.Length);
                        writer.Write((byte)b.Subtype);
                    }
                    writer.Write(b.Bytes);
                }
                return;
            }

            default:
                throw new NotImplementedException(String.Format("Writing {0} types not implemented.", obj.GetType().Name));
            }
        }
Exemplo n.º 28
0
        public void CanBeXmlSerializedWhenNullPropertys()
        {
            var source = new MongoRegex(null, null);
            var serializer = new XmlSerializer(typeof(MongoRegex));

            var writer = new StringWriter();
            serializer.Serialize(writer, source);
            var dest = (MongoRegex)serializer.Deserialize(new StringReader(writer.ToString()));

            Assert.AreEqual(source, dest);
        }
Exemplo n.º 29
0
        public void CanReadOptions()
        {
            var regex = new MongoRegex("expression", "img");

            Assert.AreEqual(MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline, regex.Options);
        }
Exemplo n.º 30
0
        public void TestWriteMongoRegex()
        {
            var regex = new MongoRegex("expression",
                MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);

            var bson = Serialize(new Document("regex", regex));

            Assert.AreEqual("GwAAAAtyZWdleABleHByZXNzaW9uAGltZwAA",bson);
        }
Exemplo n.º 31
0
 /// <summary>
 ///   Writes the specified regex.
 /// </summary>
 /// <param name = "regex">The regex.</param>
 private void Write(MongoRegex regex)
 {
     Write(regex.Expression, false);
     Write(regex.RawOptions, false);
 }
Exemplo n.º 32
0
 /// <summary>
 ///   Calculates the size.
 /// </summary>
 /// <param name = "regex">The regex.</param>
 /// <returns></returns>
 public int CalculateSize(MongoRegex regex)
 {
     var size = CalculateSize(regex.Expression, false);
     size += CalculateSize(regex.RawOptions, false);
     return size;
 }
Exemplo n.º 33
0
 /// <summary>
 ///   Writes the specified regex.
 /// </summary>
 /// <param name = "regex">The regex.</param>
 private void Write(MongoRegex regex)
 {
     Write(regex.Expression, false);
     Write(regex.RawOptions, false);
 }
Exemplo n.º 34
0
 public object ToNative()
 {
     MongoRegex mr = new MongoRegex();
     mr.Expression = this.Expression;
     mr.Options = this.Options;
     return mr;
 }
Exemplo n.º 35
0
 public void CanBeConstructedWithRegexOptions()
 {
     var regex = new MongoRegex("expression", RegexOptions.IgnoreCase | RegexOptions.Multiline);
     Assert.AreEqual("expression", regex.Expression);
     Assert.AreEqual("im", regex.RawOptions);
 }
Exemplo n.º 36
0
 public void CanReadOptions()
 {
     var regex = new MongoRegex("expression", "img");
     Assert.AreEqual(MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline, regex.Options);
 }
Exemplo n.º 37
0
        public Object ReadElementType(sbyte typeNum)
        {
            switch ((BsonDataType)typeNum) {
            case BsonDataType.Null:
            case BsonDataType.Undefined:
                return DBNull.Value;
            case BsonDataType.MinKey:
                return MongoMinKey.Value;
            case BsonDataType.MaxKey:
                return MongoMaxKey.Value;
            case BsonDataType.Boolean:
                position++;
                return reader.ReadBoolean ();
            case BsonDataType.Integer:
                position += 4;
                return reader.ReadInt32 ();
            case BsonDataType.Long:
                position += 8;
                return reader.ReadInt64 ();
            case BsonDataType.Date:
                position += 8;
                long millis = reader.ReadInt64 ();
                return BsonInfo.Epoch.AddMilliseconds(millis);
            case BsonDataType.Oid:
                position += 12;
                return new Oid (reader.ReadBytes (12));
            case BsonDataType.Number:
                position += 8;
                return reader.ReadDouble ();
            case BsonDataType.String:{
                return ReadLenString ();
            }
            case BsonDataType.Obj:{
                Document doc = this.ReadDocument();
                if(DBRef.IsDocumentDBRef(doc)){
                    return DBRef.FromDocument(doc);
                }
                return doc;
            }

            case BsonDataType.Array:{
                Document doc = this.ReadDocument();
                return ConvertToArray (doc);
            }
            case BsonDataType.Regex:{
                MongoRegex r = new MongoRegex ();
                r.Expression = this.ReadString ();
                r.Options = this.ReadString ();
                return r;
            }
            case BsonDataType.Code:{
                Code c = new Code ();
                c.Value = ReadLenString();
                return c;
            }
            case BsonDataType.CodeWScope:{
                int startpos = position;
                int size = reader.ReadInt32 ();
                position += 4;

                String val = this.ReadLenString();
                Document scope = this.ReadDocument();
                if (size != position - startpos) {
                    throw new System.IO.InvalidDataException (string.Format ("Should have read {0} bytes from stream but read {1} in CodeWScope", size, position - startpos));
                }
                return new CodeWScope (val, scope);
            }
            case BsonDataType.Binary:{
                int size = reader.ReadInt32 ();
                position += 4;
                byte subtype = reader.ReadByte ();
                position ++;
                if (subtype == (byte)Binary.TypeCode.General) {
                    size = reader.ReadInt32 ();
                    position += 4;
                }
                byte[] bytes = reader.ReadBytes (size);
                position += size;

                // From http://en.wikipedia.org/wiki/Universally_Unique_Identifier
                // The most widespread use of this standard is in Microsoft's Globally Unique Identifiers (GUIDs).
                if (subtype == 3 && 16 == size)
                {
                    return new Guid(bytes);
                }

                Binary b = new Binary();
                b.Bytes = bytes;
                b.Subtype = (Binary.TypeCode)subtype;
                return b;
            }
            default:
                throw new ArgumentOutOfRangeException (String.Format ("Type Number: {0} not recognized", typeNum));
            }
        }
Exemplo n.º 38
0
        public EndUser[] SelectEnduserByName(string iEnduserName, out string msg)
        {
            Mongo mongo = new Mongo(connectionString);
            MongoDatabase mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
            MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;

            try
            {
                mongo.Connect();
                List<EndUser> userList = new List<EndUser>();
                MongoRegex reg = new MongoRegex(".*" + iEnduserName + ".*", MongoRegexOption.IgnoreCase);
                Document docEnduserName = new Document { { "name", reg } };
                IEnumerable<Document> doc = mongoCollection.Find(docEnduserName).Documents;
                foreach (Document d in doc)
                {
                    EndUser user = new EndUser();
                    user.AutoID = d["AutoID"].ToString();
                    user.EnduserName = d["EnduserName"].ToString();
                    user.Identities = d["Identities"].ToString().Split('|');
                    user.IsValid = bool.Parse(d["IsValid"].ToString());
                    user.LastModifyTime = DateTime.Parse(d["LastModifyTime"].ToString());
                    user.SimCardNo = d["SimCardNo"].ToString();
                    userList.Add(user);
                }
                msg = "查询成功";
                mongo.Disconnect();
                return userList.ToArray();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                mongo.Disconnect();
                return null;
            }
        }
Exemplo n.º 39
0
        public Object ReadElementType(sbyte typeNum)
        {
            switch ((BsonDataType)typeNum)
            {
            case BsonDataType.Null:
            case BsonDataType.Undefined:
                return(MongoDBNull.Value);

            case BsonDataType.MinKey:
                return(MongoMinKey.Value);

            case BsonDataType.MaxKey:
                return(MongoMaxKey.Value);

            case BsonDataType.Boolean:
                position++;
                return(reader.ReadBoolean());

            case BsonDataType.Integer:
                position += 4;
                return(reader.ReadInt32());

            case BsonDataType.Long:
                position += 8;
                return(reader.ReadInt64());

            case BsonDataType.Date:
                position += 8;
                long millis = reader.ReadInt64();
                return(epoch.AddMilliseconds(millis));

            case BsonDataType.Oid:
                position += 12;
                return(new Oid(reader.ReadBytes(12)));

            case BsonDataType.Number:
                position += 8;
                return(reader.ReadDouble());

            case BsonDataType.String: {
                return(ReadLenString());
            }

            case BsonDataType.Obj: {
                Document doc = this.ReadDocument();
                if (DBRef.IsDocumentDBRef(doc))
                {
                    return(DBRef.FromDocument(doc));
                }
                return(doc);
            }

            case BsonDataType.Array: {
                Document doc = this.ReadDocument();
                return(ConvertToArray(doc));
            }

            case BsonDataType.Regex: {
                MongoRegex r = new MongoRegex();
                r.Expression = this.ReadString();
                r.Options    = this.ReadString();
                return(r);
            }

            case BsonDataType.Code: {
                Code c = new Code();
                c.Value = ReadLenString();
                return(c);
            }

            case BsonDataType.CodeWScope: {
                int startpos = position;
                int size     = reader.ReadInt32();
                position += 4;

                String   val   = this.ReadLenString();
                Document scope = this.ReadDocument();
                if (size != position - startpos)
                {
                    throw new System.IO.InvalidDataException(string.Format("Should have read {0} bytes from stream but read {1} in CodeWScope", size, position - startpos));
                }
                return(new CodeWScope(val, scope));
            }

            case BsonDataType.Binary: {
                int size = reader.ReadInt32();
                position += 4;
                byte subtype = reader.ReadByte();
                position++;
                if (subtype == (byte)Binary.TypeCode.General)
                {
                    size      = reader.ReadInt32();
                    position += 4;
                }
                byte[] bytes = reader.ReadBytes(size);
                position += size;

                // From http://en.wikipedia.org/wiki/Universally_Unique_Identifier
                // The most widespread use of this standard is in Microsoft's Globally Unique Identifiers (GUIDs).
                if (subtype == 3 && 16 == size)
                {
                    return(new Guid(bytes));
                }

                Binary b = new Binary();
                b.Bytes   = bytes;
                b.Subtype = (Binary.TypeCode)subtype;
                return(b);
            }

            default:
                throw new ArgumentOutOfRangeException(String.Format("Type Number: {0} not recognized", typeNum));
            }
        }