Example #1
0
        public Dictionary<string, object> Insert(string tableName, Dictionary<string, object> row, ITransaction transaction = null)
        {
            if (!DataStore.ContainsKey(tableName))
                DataStore.Add(tableName, new FakeDBTable());

            var table = DataStore[tableName];

            var dictToUse = row.Copy();
            //var id = DataStore[tableName].Count + 1;
            var primaryKeys = Analyzer.GetPrimaryKeys(tableName);
            var autoKey = Analyzer.GetAutoNumberKey(tableName);

            if (autoKey != null)
            {
                if (!dictToUse.ContainsKey(autoKey))
                    dictToUse.Add(autoKey, table.NextAutoKey++);
                else
                    dictToUse[autoKey] = table.NextAutoKey++;
            }

            var invalid = primaryKeys.Where(key => dictToUse[key] == null);
            if (invalid.Any())
                throw new KeyNotSetException(tableName, invalid);

            table.Add(dictToUse);
            return dictToUse.WhereKeys(key => primaryKeys.Contains(key));
        }
Example #2
0
        public void DictionaryIsCopied()
        {
            var dict = new Dictionary<int, Exception>();
            dict.Add(0, new Exception("Test"));

            var copy = (Dictionary<int, Exception>) dict.Copy();
            Assert.AreNotSame(copy[0], dict[0]);
            Assert.AreEqual(copy[0].Message, dict[0].Message);
        }
 public void Copy()
 {
     Dictionary<string, string> dict = new Dictionary<string, string>();
     dict.Add("Key 1", "Value 1");
     dict.Add("Key 2", "Value 2");
     dict.Add("Key 3", "Value 3");
     var dictCopy = dict.Copy();
     Assert.AreEqual(dictCopy, dict);
 }
        public void Diff()
        {
            var a = new Dictionary<string, int>() { { "A", 1 }, { "B", 2 }, {"C", 3}};
            var b = a.Assoc("B", -2);

            var diff = a.Diff(b);
            Assert.AreEqual("([B,(2, -2)])", diff.Print());

            diff = a.Diff(b, "A".And("B"));
            Assert.AreEqual("([B,(2, -2)])", diff.Print());

            diff = a.Diff(b, "A".And("C"));
            Assert.AreEqual("()", diff.Print());

            var copyA = a.Copy();
            diff = a.Diff(copyA);
            Assert.AreEqual("()", diff.Print());
        }
      public void Copy_Extension_Test()
      {
         short a = 1;
         int b = 2;
         long c = 3;
         float d = 4.0f;
         double e = 5.0;
         string f = "foo";
         var g = new ClientRetrievalTask { Enabled = false };
         var h = new List<string>(new[] { "foo" });
         var i = new Dictionary<string, object> { { "foo", new object() } };

         object objA = (short)1;
         object objB = 2;
         object objC = (long)3;
         object objD = 4.0f;
         object objE = 5.0;
         object objF = "foo";
         object objG = new ClientRetrievalTask { Enabled = false };
         object objH = new List<string>(new[] { "foo" });
         object objI = new Dictionary<string, object> { { "foo", new object() } };

         Assert.AreEqual(1, a.Copy());
         Assert.AreEqual(2, b.Copy());
         Assert.AreEqual(3, c.Copy());
         Assert.AreEqual(4.0f, d.Copy());
         Assert.AreEqual(5.0, e.Copy());
         Assert.AreEqual("foo", f.Copy());
         Assert.AreNotSame(g, g.Copy());
         Assert.AreNotSame(h, h.Copy());
         Assert.AreNotSame(i, i.Copy());

         Assert.AreEqual(1, objA.Copy());
         Assert.AreEqual(2, objB.Copy());
         Assert.AreEqual(3, objC.Copy());
         Assert.AreEqual(4.0f, objD.Copy());
         Assert.AreEqual(5.0, objE.Copy());
         Assert.AreEqual("foo", objF.Copy());
         Assert.AreNotSame(g, objG.Copy());
         Assert.AreNotSame(h, objH.Copy());
         Assert.AreNotSame(i, objI.Copy());
      }
Example #6
0
 public UpdateOp(string table, Dictionary<string, object> data, Dictionary<string, object> keys)
     : base(table)
 {
     Keys = keys.Copy();
     NewValues = data.Copy();
 }
Example #7
0
 /// <summary>
 /// Note: row should include any autokeys that the table defines as well,
 /// since InsertOp cannot determine them internally
 /// </summary>
 /// <param name="table"></param>
 /// <param name="row"></param>
 public InsertOp(string table, Dictionary<string, object> row)
     : base(table)
 {
     Row = row.Copy();
 }
Example #8
0
 public DeleteOp(string table, Dictionary<string, object> keys)
     : base(table)
 {
     Keys = keys.Copy();
 }
Example #9
0
        /// <summary>
        /// 获取枚举字典列表
        /// </summary>
        /// <param name="enType">枚举类型</param>
        /// <param name="isIntValue">返回枚举值是否是int类型</param>
        /// <returns></returns>
        public static Dictionary<string, string> ToEnumDirs(this Type enType, bool isIntValue=true)
        {
            if (!enType.IsEnum)
            {
                throw new ArgumentException("获取枚举字典,参数必须是枚举类型!");
            }
            string key = string.Concat(enType.FullName, isIntValue);
            Dictionary<string, string> dirs;
            enumDirs.TryGetValue(key, out dirs);

            if (dirs != null)
                return dirs.Copy();

            dirs=new Dictionary<string, string>();

            var values = Enum.GetValues(enType);

            foreach (var value in values)
            {
                var name = Enum.GetName(enType, value);
                string resultValue = isIntValue ? ((int)value).ToString() : value.ToString();
                var attrList = enType.GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
                if (attrList != null && attrList.Length > 0)
                {
                    dirs.Add(resultValue, attrList[0].Description);
                    continue;
                }
                dirs.Add(resultValue, name);
            }

            enumDirs.TryAdd(key, dirs);
            return dirs.Copy();
        }