Example #1
0
        public static void ASYNC_GetSchemaAndTestVariousTypes(ICRUDDataStore store)
        {
            var schema = store.GetSchemaAsync(new Query("CRUD.Types.Load")).Result;

            var row = new DynamicRow(schema);
            row["GDID"] = new GDID(0, 145);
            row["SCREEN_NAME"] = "User1";
            row["STRING_NAME"] = "Some user 1";
            row["CHAR_NAME"] = "Some user 2";
            row["BOOL_CHAR"] = 'T';
            row["BOOL_BOOL"] = true;

            row["AMOUNT"] = 145670.23m;

            row["DOB"] = new DateTime(1980,12,1);

            store.Insert( row );

            var row2 = store.LoadOneRow(new Query("CRUD.Types.Load", new GDID(0, 145)));

            Assert.NotNull(row2);
            Assert.AreEqual(145, row2["GDID"]);
            Assert.AreEqual("User1", row2["Screen_Name"]);
            Assert.AreEqual("Some user 1", row2["String_Name"]);
            Assert.AreEqual("Some user 2", row2["Char_Name"]);

            Assert.AreEqual(true, row2["BOOL_Char"].AsBool());
            Assert.AreEqual(true, row2["BOOL_BOOL"].AsBool());

            Assert.AreEqual(145670.23m, row2["Amount"]);

            Assert.AreEqual(1980, row2["DOB"].AsDateTime().Year);
        }
Example #2
0
        public void InferSchema()
        {
            var doc = new BSONDocument();
            doc.Set( new BSONStringElement("FullName", "Alex Bobby") );
            doc.Set( new BSONInt32Element("Age", 123) );
            doc.Set( new BSONBooleanElement("IsGood", true) );

            var c = new RowConverter();

            var schema = c.InferSchemaFromBSONDocument(doc);

            Assert.AreEqual(3, schema.FieldCount);

            Assert.AreEqual(0, schema["FullName"].Order);
            Assert.AreEqual(1, schema["Age"].Order);
            Assert.AreEqual(2, schema["IsGood"].Order);

            Assert.AreEqual(typeof(object), schema["FullName"].NonNullableType);
            Assert.AreEqual(typeof(object), schema["Age"].NonNullableType);
            Assert.AreEqual(typeof(object), schema["IsGood"].NonNullableType);

            var row = new DynamicRow(schema);
            c.BSONDocumentToRow(doc, row, null);

            Assert.AreEqual("Alex Bobby", row[0]);
            Assert.AreEqual(123,          row[1]);
            Assert.AreEqual(true,         row[2]);

            Assert.AreEqual("Alex Bobby", row["FullName"]);
            Assert.AreEqual(123,          row["Age"]);
            Assert.AreEqual(true,         row["IsGood"]);
        }
Example #3
0
        public void Equality()
        {
            var person1 = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person1["ID"] = "POP1";
            person1["FirstName"] = "Oleg";
            person1["LastName"] = "Popov";
            person1["DOB"] = new DateTime(1981, 2, 12);
            person1["YearsInSpace"] = 45;
            person1["Amount"] = 100;
            person1["Description"]="Wanted to go to the moon";

            var person2 = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person2["ID"] = "POP1";
            person2["FirstName"] = "Egor";
            person2["LastName"] = "Pedorov";
            person2["DOB"] = new DateTime(1982, 5, 2);
            person2["YearsInSpace"] = 4;
            person2["Amount"] = 1000000;

            Assert.IsTrue( person1.Equals(person2) );

            person2["ID"] = "POP2";

            Assert.IsFalse(person1.Equals(person2) );
        }
Example #4
0
        public void BuildUsingAdHockSchema()
        {
            var schema = new Schema("TEZT",
                           new Schema.FieldDef("ID", typeof(int), new List<FieldAttribute>{ new FieldAttribute(required: true, key: true)}),
                           new Schema.FieldDef("Description", typeof(string), new List<FieldAttribute>{ new FieldAttribute(required: true)})
            );

            var tbl = new Table(schema);

            for(var i=0; i<1000; i++)
            {
                 var row =  new DynamicRow(tbl.Schema);

                 row["ID"] = i;
                 row["Description"] = "Item-{0}".Args(i);

                 tbl.Insert( row );
            }

            Assert.AreEqual(1000, tbl.Count);

            var match = tbl.FindByKey(178);
            Assert.IsNotNull( match );
            Assert.AreEqual("Item-178", match["Description"]);
        }
Example #5
0
        public static void ASYNC_QueryInsertQuery(ICRUDDataStore store)
        {
            var query = new Query("CRUD.Patient.List") { new Query.Param("LN", "%loff") };
            var task = store.LoadAsync( query );  

            Assert.AreEqual(1, task.Result.Count);
            var rowset = task.Result[0];
            Assert.AreEqual(0, rowset.Count);
                
            var row = new DynamicRow(rowset.Schema);

            row["ssn"] = "999-88-9012";
            row["First_Name"] = "Jack";
            row["Last_Name"] = "Kozloff";
            row["DOB"] = new DateTime(1980, 1, 12);

            Assert.IsNull( row.Validate());

            store.InsertAsync(row).Wait();   

                      
            task = store.LoadAsync( query );  

            Assert.AreEqual(1, task.Result.Count);
            rowset = task.Result[0];

            Assert.AreEqual(1, rowset.Count);
            Assert.AreEqual("Jack", rowset[0]["First_Name"]);
            
        }
Example #6
0
        public void Slim_SerializeTable_DynamicRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));
            
            for(var i=0; i<1000; i++)
            {
                var row = new DynamicRow( tbl.Schema );

                row["ID"] = "POP{0}".Args(i);
                row["FirstName"] = "Oleg";
                row["LastName"] = "Popov-{0}".Args(i);
                row["DOB"] = new DateTime(1953, 12, 10);
                row["YearsInSpace"] = 12;
            
                tbl.Insert( row );
            }

            var ser = new SlimSerializer();
            using(var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);
            Console.WriteLine("{0} rows took {1} bytes".Args(tbl.Count, ms.Position));
                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Assert.IsNotNull( tbl2 );
                Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) );

                Assert.AreEqual( 1000, tbl2.Count);
                Assert.IsTrue( tbl.SequenceEqual( tbl2 ) );
            }
        }
Example #7
0
        public void DynamicRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));

                var row = new DynamicRow( tbl.Schema );

                row["ID"] = "POP1";
                row["FirstName"] = "Oleg";
                row["LastName"] = "Popov-1";
                row["DOB"] = new DateTime(1953, 12, 10);
                row["YearsInSpace"] = 12;

                tbl.Insert( row );

            var ser = new SlimSerializer();
            using(var ms = new MemoryStream())
            {
                ser.Serialize(ms, tbl);

                ms.Position = 0;

                var tbl2 = ser.Deserialize(ms) as Table;

                Assert.IsNotNull( tbl2 );
                Assert.IsFalse( object.ReferenceEquals(tbl ,tbl2) );

                Assert.IsFalse( object.ReferenceEquals(tbl.Schema ,tbl2.Schema) );

                Assert.IsTrue( tbl.Schema.IsEquivalentTo(tbl2.Schema));
            }
        }
Example #8
0
    public Row ToRow()
    {
      var result = new DynamicRow(m_Schema);
      foreach(var field in this.Fields)
       result[field.FieldName] = field.ValueAsObject;

      return result;
    }
Example #9
0
        /// <summary>
        /// Reads either Table or Rowset from JSON created by WriteAsJSON. Metadata must be present.
        /// allMatched==false when some data did not match schema (i.e. too little fields or extra fields supplied)
        /// </summary>
        public static RowsetBase FromJSON(JSONDataMap jsonMap,
                                          out bool allMatched,
                                          bool schemaOnly           = false,
                                          bool readOnlySchema       = false,
                                          SetFieldFunc setFieldFunc = null)
        {
            if (jsonMap == null || jsonMap.Count == 0)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap=null)");
            }

            var schMap = jsonMap["Schema"] as JSONDataMap;

            if (schMap == null)
            {
                throw new CRUDException(StringConsts.ARGUMENT_ERROR + "RowsetBase.FromJSON(jsonMap!schema)");
            }

            var schema  = Schema.FromJSON(schMap, readOnlySchema);
            var isTable = jsonMap["IsTable"].AsBool();

            allMatched = true;
            var result = isTable ? (RowsetBase) new Table(schema) : (RowsetBase) new Rowset(schema);

            if (schemaOnly)
            {
                return(result);
            }

            var rows = jsonMap["Rows"] as JSONDataArray;

            if (rows == null)
            {
                return(result);
            }


            foreach (var jrow in rows)
            {
                var jdo = jrow as IJSONDataObject;
                if (jdo == null)
                {
                    allMatched = false;
                    continue;
                }

                var row = new DynamicRow(schema);

                if (!Row.TryFillFromJSON(row, jdo, setFieldFunc))
                {
                    allMatched = false;
                }

                result.Add(row);
            }

            return(result);
        }
Example #10
0
        public void BuildUsingAdHockSchema()
        {
            var schema = new Schema("TEZT",
                           new Schema.FieldDef("ID", typeof(int), new List<FieldAttribute>{ new FieldAttribute(required: true, key: true)}),
                           new Schema.FieldDef("Description", typeof(string), new List<FieldAttribute>{ new FieldAttribute(required: true)})
            );

            var person = new DynamicRow(schema);

            person["ID"] = 123;
            person["Description"] = "Tank";

            Assert.AreEqual( 123,                       person["ID"] );
            Assert.AreEqual( "Tank",                    person["Description"] );
        }
Example #11
0
        public void BuildUsingTypedSchema()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));

            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] = "Popov";
            person["DOB"] = new DateTime(1953, 12, 10);
            person["YearsInSpace"] = 12;

            Assert.AreEqual( "POP1",                       person["ID"] );
            Assert.AreEqual( "Oleg",                       person["FirstName"] );
            Assert.AreEqual( "Popov",                      person["LastName"] );
            Assert.AreEqual( new DateTime(1953, 12, 10),   person["DOB"] );
            Assert.AreEqual( 12,                           person["YearsInSpace"] );
        }
Example #12
0
        /// <summary>
        /// Creates key row out of field values for keys
        /// </summary>
        public Row KeyRowFromValues(params object[] keys)
        {
            var krow = new DynamicRow(Schema);
            var idx  = 0;

            foreach (var kdef in Schema.AnyTargetKeyFieldDefs)
            {
                if (idx > keys.Length - 1)
                {
                    throw new CRUDException(StringConsts.CRUD_FIND_BY_KEY_LENGTH_ERROR);
                }
                krow[kdef.Order] = keys[idx];
                idx++;
            }

            return(krow);
        }
Example #13
0
        public void CopyFields_AmorphousDynamicRow_To_DynamicRow()
        {
            var from = new AmorphousDynamicRow(Schema.GetForTypedRow(typeof(Person)));
            from["FirstName"] = "Ivan";
            from["LuckRatio"] = 12345.6789D;
            from.AmorphousData["field1"] = "some data";

            var to = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            to["Description"] = "descr";
            to["YearsWithCompany"] = 30;

            from.CopyFields(to);

            Assert.AreEqual(to["FirstName"], from["FirstName"]);
            Assert.AreEqual(to["LuckRatio"], from["LuckRatio"]);
            Assert.AreEqual(to["Description"], from["Description"]);
            Assert.AreEqual(to["YearsWithCompany"], from["YearsWithCompany"]);
            Assert.AreEqual(null, to.Schema["field1"]);
        }
Example #14
0
        public void PopulateAndFindKey_MixedRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));

            for(var i=0; i<1000; i++)
            {
                 var row =  new DynamicRow(tbl.Schema);

                 row["ID"] = "DYN{0}".Args(i);
                 row["FirstName"] = "Oleg";
                 row["LastName"] = "DynamicPopov-{0}".Args(i);
                 row["DOB"] = new DateTime(1953, 12, 10);
                 row["YearsInSpace"] = 12;

                 tbl.Insert( row );

                 tbl.Insert( new Person{
                                    ID = "TYPED{0}".Args(i),
                                    FirstName = "Oleg",
                                    LastName = "TypedPopov-{0}".Args(i),
                                    DOB = new DateTime(1953, 12, 10),
                                    YearsInSpace = 12
                                   });
            }

            Assert.AreEqual(2000, tbl.Count);

            var match1 = tbl.FindByKey("DYN35");
            Assert.IsNotNull( match1 );
            Assert.IsTrue( match1 is DynamicRow );
            Assert.AreEqual("DynamicPopov-35", match1["LastName"]);

            var match2 = tbl.FindByKey("TYPED36") as Person;
            Assert.IsNotNull( match2 );
            Assert.AreEqual("TypedPopov-36", match2["LastName"]);

            var match3 = tbl.FindByKey("DoesNotExist");
            Assert.IsNull( match3 );
        }
Example #15
0
        public void PopulateAndFindKey_DynamicRows()
        {
            var tbl = new Table(Schema.GetForTypedRow(typeof(Person)));

            for(var i=0; i<1000; i++)
            {
                 var row =  new DynamicRow(tbl.Schema);

                 row["ID"] = "POP{0}".Args(i);
                 row["FirstName"] = "Oleg";
                 row["LastName"] = "Popov-{0}".Args(i);
                 row["DOB"] = new DateTime(1953, 12, 10);
                 row["YearsInSpace"] = 12;

                 tbl.Insert( row );
            }

            Assert.AreEqual(1000, tbl.Count);

            var match1 = tbl.FindByKey("POP35");
            Assert.IsNotNull( match1 );
            Assert.AreEqual("Popov-35", match1["LastName"]);

            var match2 = tbl.FindByKey("POP36") as DynamicRow;
            Assert.IsNotNull( match2 );
            Assert.AreEqual("Popov-36", match2["LastName"]);

            var match3 = tbl.FindByKey("DoesNotExist");
            Assert.IsNull( match3 );
        }
Example #16
0
        public void ErlInsertManyAndQuery()
        {
            var schema = store.GetSchema(new Query("CRUD.SECDEF"));

              for (var i = 0; i < CCY_PAIRS.Length; i++)
              {
            var row = new DynamicRow(schema);

            row.ApplyDefaultFieldValues(store.TargetName);

            var ccy1 = CCY_PAIRS[i].Substring(0, 3);
            var ccy2 = CCY_PAIRS[i].Substring(4, 3);

            row["xchg"]       = "CLE";
            row["symbol"]     = ccy1 + ccy2;
            row["instr"]      = CCY_PAIRS[i];
            row["secid"]      = i;
            row["xchg_secid"] = 1000 + i;
            row["ccy"]        = ccy1;
            row["settl_ccy"]  = ccy2;

            row["contr_mult"] = 1.0d;
            row["px_step"]    = 10e-5d;

            Assert.IsNull(row.Validate());

            var affected = store.Insert(row);
            Assert.AreEqual(1, affected);
              }

              var qry = new Query("CRUD.SecDef.ByExchange")
              {
              new Query.Param("Exchange", "CLE")
              };

              var data = store.LoadOneRowset(qry);

              Assert.IsNotNull(data);

              Console.WriteLine(data.ToJSON(JSONWritingOptions.PrettyPrintRowsAsMap));

              Assert.AreEqual(CCY_PAIRS.Length, data.Count);
        }
Example #17
0
        public void ErlInsert()
        {
            var schema = store.GetSchema(new Query("CRUD.SECDEF"));
              var row = new DynamicRow(schema);

              row.ApplyDefaultFieldValues(store.TargetName);

              row["xchg"]       = "HS";
              row["symbol"]     = "EURUSD";
              row["instr"]      = "EUR/USD";
              row["secid"]      = 1;
              row["xchg_secid"] = 1010;
              row["ccy"]        = "EUR";
              row["settl_ccy"]  = "USD";

              row["contr_mult"] = 1.0d;
              row["px_step"]    = 10e-5d;

              Assert.IsNull(row.Validate());

              var affected = store.Insert(row);
              Assert.AreEqual(1, affected);
        }
Example #18
0
        public void GetSchema_ROW_JSON_ROW()
        {
            var row =
               new MyPerzon
            {
               GDID = new GDID(1, 1, 980),
               Name = "Lenin Grib",
               Age = 100
            };

             store.Insert(row);

             var qry = new Query("CRUD.LoadPerzon", typeof(MyPerzon))
                           {
                             new Query.Param("id", new GDID(1,1,980))
                           };

             var schema = store.GetSchema(qry);

             Assert.IsNotNull(schema);
             Assert.AreEqual(3, schema.FieldCount);

             Assert.AreEqual(0, schema["_id"].Order);
             Assert.AreEqual(1, schema["Name"].Order);
             Assert.AreEqual(2, schema["Age"].Order);

             var row2 = new DynamicRow(schema);//Notice: We are creating dynamic row with schema taken from Mongo

             row2["_id"] = new GDID(10,10,10);
             row2["Name"] = "Kozloff";
             row2["Age"] = "199";

             var json = row2.ToJSON(JSONWritingOptions.PrettyPrintRowsAsMap);
             Console.WriteLine(json);

             var dyn = json.JSONToDynamic();

             Assert.AreEqual("10:10:10", dyn._id);
             Assert.AreEqual("Kozloff", dyn.Name);
             Assert.AreEqual("199", dyn.Age);
        }
Example #19
0
        public void SetValuesAsDifferentTypes()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));

            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] = "Popov";
            person["DOB"] = new DateTime(1953, 12, 10);
            person["YearsInSpace"] = 12;

            Assert.AreEqual( "POP1",                       person["ID"] );
            Assert.AreEqual( "Oleg",                       person["FirstName"] );
            Assert.AreEqual( "Popov",                      person["LastName"] );
            Assert.AreEqual( new DateTime(1953, 12, 10),   person["DOB"] );
            Assert.AreEqual( 12,                           person["YearsInSpace"] );

            person["DOB"] = "05/15/2009 18:00";
            Assert.AreEqual( new DateTime(2009, 5, 15, 18, 0, 0),   person["DOB"] );

            person["DOB"] = null;
            Assert.IsNull( person["DOB"] );

            person["YearsInSpace"] = "-190";
            Assert.AreEqual( -190,   person["YearsInSpace"] );

            person["Description"] = 2+2;
            Assert.AreEqual( "4",   person["Description"] );

            person["Amount"] = "180.12";
            Assert.AreEqual( 180.12m,   person["Amount"] );

            person["GoodPerson"] = "true";
            Assert.AreEqual( true,   person["GoodPerson"] );

            person["LuckRatio"] = 123;
            Assert.AreEqual( 123d,   person["LuckRatio"] );
        }
Example #20
0
        public static void GetSchemaAndTestFullGDID(ICRUDDataStore store)
        {
            var schema = store.GetSchema(new Query("CRUD.FullGDID.Load"));
                
            var row = new DynamicRow(schema);

            var key = new GDID(179, 1, 1234567890);
            Console.WriteLine( key.Bytes.ToDumpString(DumpFormat.Hex));

            row["GDID"] = new GDID(179, 1, 1234567890);
            Console.WriteLine( ((byte[])row["GDID"]).ToDumpString(DumpFormat.Hex) );

            row["VARGDID"] = new GDID(12, 9, 9876543210);
            row["STRING_NAME"] = "DA DA DA!";

            store.Insert( row );

            var row2 = store.LoadOneRow(new Query("CRUD.FullGDID.Load", key, typeof(FullGDID))) as FullGDID;

            Assert.NotNull(row2);
            Assert.AreEqual(key, row2.GDID);
            Assert.AreEqual(new GDID(12, 9, 9876543210), row2.VARGDID);
            Assert.AreEqual("DA DA DA!", row2["String_Name"]);
        }
Example #21
0
        public void Validate_Error_ValueList()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] ="Popov";
            person["DOB"] = new DateTime(1981, 2, 12);
            person["YearsInSpace"] = 45;
            person["Amount"] = 100;
            person["Description"] = "0123";
            person["Classification"] = "INVALID";

            var error = person.Validate();
            Console.WriteLine( error );
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("Classification", ((CRUDFieldValidationException)error).FieldName);
            Assert.IsTrue( error.Message.Contains("not in list of permitted values") );

            person["Classification"] = "good";
            Assert.IsNull( person.Validate() );
            person["Classification"] = "bad";
            Assert.IsNull( person.Validate() );
            person["Classification"] = "ugly";
            Assert.IsNull( person.Validate() );
        }
Example #22
0
        /// <summary>
        /// Convert an Erlang hierarchical term representing a schema to a Row.
        /// </summary>
        /// <param name="row">Row to update</param>
        /// <param name="data">
        ///   Data to update row with.
        ///   The data must be in the form {SchemaName::atom, [{FieldName::atom(), Value}]}.
        /// </param>
        /// <param name="schema">Alternative schema to use in place of row.Schema</param>
        /// <param name="targetName">Name of the target for looking up field attributes</param>
        /// <param name="schemaName">Alternative name of the top-most 'SchemaName' atom used in the "data".</param>
        /// <param name="knownSchemas">List of known schemas to use when initializing a field a DynamicRow type.</param>
        public static void Update(this Row row, IErlObject data, Schema schema = null, string targetName = null,
            string schemaName = null, Registry<Schema> knownSchemas = null)
        {
            if (schema == null)
            schema = row.Schema;

              if (schemaName == null)
            schemaName = schema.Name;

              if (data == null)
            data = new ErlTuple(new ErlAtom(schemaName), new ErlList());

              // Input data must be in the form: {SchemaName::atom(), [{FieldName::atom(), Value}]}
              // where Value can be any primitive value or a hierarchical value with another Row type.
              if (!checkKeyValueTuple(data) || ((ErlTuple)data)[1].TypeOrder != ErlTypeOrder.ErlList)
            throw new ErlDataAccessException(
              StringConsts.ERL_DS_CRUD_RESP_SCH_MISMATCH_ERROR.Args(data.ToString(), schema.Name));

              var dataList = ((ErlTuple)data)[1] as ErlList;

              // Make sure that the first element of the tuple matches the schema name
              if (!((ErlTuple)data)[0].ValueAsString.Equals(schemaName))
            throw new ErlDataAccessException(
              StringConsts.ERL_DS_CRUD_RESP_SCH_MISMATCH_ERROR.Args(data.ToString(), schema.Name));

              // Contains a set of field names that are present in configuration
              var presentFieldNames = new HashSet<string>();
              foreach (var item in dataList.Where(checkKeyValueTuple).Cast<ErlTuple>())
            presentFieldNames.Add(item[0].ValueAsString);

              ErlList newList = null;

              foreach (var fld in schema.Where(fd => typeof(Row).IsAssignableFrom(fd.Type)))
            if (!presentFieldNames.Contains(fld.Name))
            {
              if (newList == null)
            newList = (ErlList)dataList.Clone();
              // Add: {FieldName::atom(), []}
              newList.Add(new ErlTuple(new ErlAtom(fld.Name), new ErlList()));
            }

              // If no new items were added to the list use current list:
              if (newList == null) newList = dataList;

              foreach (var item in newList.Where(checkKeyValueTuple).Cast<ErlTuple>())
              {
            var name  = item[0].ValueAsString;
            var value = item[1];
            var fdef  = schema[name];
            var attr  = fdef[targetName];

            if (!attr.Visible || (attr.Metadata != null && attr.Metadata.Navigate("$readonly|$read-only|$read_only").ValueAsBool()))
              continue;

            // If this field is defined in the schema as a Row type, then we need to descend into the
            // value's hierarchical structure and treat it as a nested row
            if (typeof(Row).IsAssignableFrom(fdef.Type))
            {
              // Get the row associated
              Schema chldSch;
              var chldRow = row[fdef.Order] as Row;

              // If the row has a field of Row type initialized, use its Schema value.
              if (chldRow != null)
            chldSch = chldRow.Schema;
              else
              {
            // Otherwise lookup the schema from the given registry
            if (!knownSchemas.TryGetValue(name, out chldSch))
              throw new ErlDataAccessException(
                StringConsts.ERL_DS_SCHEMA_NOT_KNOWN_ERROR.Args(name, data.ToString()));
            // Construct the field's value as dynmiac row of the looked up schema type
            chldRow = new DynamicRow(chldSch);
            chldRow.ApplyDefaultFieldValues();
            row[fdef.Order] = chldRow;
              }

              if (value.TypeOrder != ErlTypeOrder.ErlList)
            throw new ErlDataAccessException(
              StringConsts.ERL_DS_SCHEMA_INVALID_VALUE_ERROR.Args(chldSch.Name, value));

              // Recursively update the field's value from given data by using the field's schema:
              chldRow.Update(item, chldSch, targetName, knownSchemas: knownSchemas);
            }
            else
            {
              // This is a primitive value type
              var clr = SchemaMap.ErlToClrValue(value, schema, fdef, null, data);
              row.SetFieldValue(fdef, clr);
            }
              }
        }
Example #23
0
    public void ErlInsert()
    {
      var schema = store.GetSchema(new Query("CRUD.SECDEF"));
      var row = new DynamicRow(schema);

      row.ApplyDefaultFieldValues(store.TargetName);

      row["Exchange"] = "HS";
      row["Symbol"] = "EURUSD";
      row["Instr"] =  "EUR/USD";
      row["SecID"] = 1;
      row["ExchSecID"] = 1010;
      row["Ccy"] = "EUR";
      row["SettlCcy"] = "USD";

      row["ContractMult"] = 1.0d;
      row["PriceStep"] = 10e-5d;

      Assert.IsNull(row.Validate());

      var affected = store.Insert(row);
      Assert.AreEqual(1, affected);
    }
Example #24
0
        public void Validate_Error_DecimalMinMax_2()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] ="Popov";
            person["DOB"] = new DateTime(1953, 12, 10);
            person["YearsInSpace"] = 45;
            person["Amount"] = 2000000;

            var error = person.Validate();
            Console.WriteLine( error );
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("Amount", ((CRUDFieldValidationException)error).FieldName);
            Assert.IsTrue( error.Message.Contains("is above") );
        }
Example #25
0
        public void Validate_Error_DateTimeMinMax_DifferentTarget()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] ="Popov";
            person["DOB"] = new DateTime(1899, 12, 10);
            person["YearsInSpace"] = 45;
            person["Amount"] = 100;

            var error = person.Validate("sparta_system");
            Assert.IsNull (error);
        }
Example #26
0
      public void T_09_DynamicRow()
      {
          var BYTES = new byte[] {0x00, 0x79, 0x14};

          var schema = new Schema("Dynamic Schema", 
                new Schema.FieldDef("ID", typeof(int), new List<FieldAttribute>{ new FieldAttribute(required: true, key: true)}),
                new Schema.FieldDef("Description", typeof(string), new List<FieldAttribute>{ new FieldAttribute(required: true)}),
                new Schema.FieldDef("Bytes", typeof(byte[]), new List<FieldAttribute>{ new FieldAttribute(required: true)})
          );

          var row = new DynamicRow(schema);
            
          row["ID"] = 123;
          row["Description"] = "T-90 Tank";
          row["Bytes"] = BYTES;
     
          var rc = new RowConverter();
      
          var doc = rc.RowToBSONDocument( row, "A" );

          Console.WriteLine(doc.ToString());

          var row2 = new DynamicRow(schema);
          rc.BSONDocumentToRow(doc, row2, "A");

          Assert.AreEqual(123, row2["ID"]);
          Assert.AreEqual("T-90 Tank", row2["Description"]);
          Assert.IsTrue(BYTES.SequenceEqual((byte[])row2["Bytes"]));
      }
Example #27
0
        public void CopyFields_ExtendedDynamicRow_To_DynamicRow()
        {
            var schema = Schema.GetForTypedRow(typeof(Person));
            var fieldDefs = schema.FieldDefs.ToList();
            fieldDefs.Add(new Schema.FieldDef("Info", typeof(string), new QuerySource.ColumnDef("Info")));
            fieldDefs.Add(new Schema.FieldDef("Count", typeof(long), new QuerySource.ColumnDef("Info")));
            var extendedSchema = new Schema("sname", fieldDefs.ToArray());

            var from = new DynamicRow(extendedSchema);
            from["FirstName"] = "Ivan";
            from["Amount"] = 10;
            from["DOB"] = new DateTime(1990, 2, 16);
            from["GoodPerson"] = true;
            from["Info"] = "extended info";
            from["Count"] = long.MaxValue;

            var to = new DynamicRow(schema);

            from.CopyFields(to);

            Assert.AreEqual(to["FirstName"], from["FirstName"]);
            Assert.AreEqual(to["Amount"], from["Amount"]);
            Assert.AreEqual(to["DOB"], from["DOB"]);
            Assert.AreEqual(to["GoodPerson"], from["GoodPerson"]);
        }
Example #28
0
        public void SetGetAndValidate_NoError()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));

            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] = "Popov";
            person["DOB"] = new DateTime(1953, 12, 10);
            person["YearsInSpace"] = 12;

            var error = person.Validate();
            Assert.IsNull( error );

            Assert.AreEqual( "POP1",                       person["ID"] );
            Assert.AreEqual( "Oleg",                       person["FirstName"] );
            Assert.AreEqual( "Popov",                      person["LastName"] );
            Assert.AreEqual( new DateTime(1953, 12, 10),   person["DOB"] );
            Assert.AreEqual( 12,                           person["YearsInSpace"] );
        }
Example #29
0
        public void Validate_Error_MaxLength()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] ="Popov";
            person["DOB"] = new DateTime(1981, 2, 12);
            person["YearsInSpace"] = 45;
            person["Amount"] = 100;
            person["Description"] = "0123456789012345678901234567890";

            var error = person.Validate();
            Console.WriteLine( error );
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("Description", ((CRUDFieldValidationException)error).FieldName);
            Assert.IsTrue( error.Message.Contains("exceeds max length") );
        }
Example #30
0
        public void ToDynamicRow_FromString()
        {
            var str = @"{name: ""Orlov"", dob: ""02/12/2007 6:45 PM"", certified: true, serviceyears: 12, salary: 145000}";

              var row = new DynamicRow(Schema.GetForTypedRow(typeof(MySimpleData)));

              JSONReader.ToRow(row, str.JSONToDataObject() as JSONDataMap);

              Assert.AreEqual("Orlov", row["Name"]);
              Assert.AreEqual(new DateTime(2007, 2, 12, 18, 45, 0), row["DOB"]);
              Assert.AreEqual(true, row["Certified"]);
              Assert.AreEqual(12, row["ServiceYears"]);
              Assert.AreEqual(145000m, row["Salary"]);
        }
Example #31
0
    public void ErlInsertManyDeleteAndQuery()
    {
      var schema = store.GetSchema(new Query("CRUD.SECDEF"));

      for (var i = 0; i < CCY_PAIRS.Length; i++)
      {
        var row = new DynamicRow(schema);

        row.ApplyDefaultFieldValues(store.TargetName);

        var ccy1 = CCY_PAIRS[i].Substring(0, 3);
        var ccy2 = CCY_PAIRS[i].Substring(4, 3);

        row["Exchange"]     = "NYSE";
        row["Symbol"]       = ccy1 + ccy2;
        row["Instr"]        = CCY_PAIRS[i];
        row["SecID"]        = i;
        row["ExchSecID"]    = 1000 + i;
        row["Ccy"]          = ccy1;
        row["SettlCcy"]     = ccy2;

        row["ContractMult"] = 1.0d;
        row["PriceStep"]    = 10e-5d;

        Assert.IsNull(row.Validate());

        var affected = store.Upsert(row);
        Assert.AreEqual(1, affected);
      }

      var qry = new Query("CRUD.SecDef.ByExchange")
      {
          new Query.Param("Exchange", "NYSE")
      };

      var data = store.LoadOneRowset(qry);

      Assert.IsNotNull(data);

      Console.WriteLine(data.ToJSON(JSONWritingOptions.PrettyPrintRowsAsMap));

      Assert.AreEqual(CCY_PAIRS.Length, data.Count);

      var del = new DynamicRow(schema);

      del["Exchange"]  = "CLE";
      del["Symbol"]  = "USDMXN";

      Assert.AreEqual(1, store.Delete(del));
      
      data = store.LoadOneRowset(qry);//NYSE

      Assert.IsNotNull(data);
      Assert.AreEqual(CCY_PAIRS.Length, data.Count); 

      qry = new Query("CRUD.SecDef.ByExchange")
      {
          new Query.Param("Exchange", "CLE")
      };

      data = store.LoadOneRowset(qry);//Requery for CLE

      Assert.IsNotNull(data);
      Assert.AreEqual(CCY_PAIRS.Length - 1, data.Count);//1 was deleted!!!!!!!!!!!
    }
Example #32
0
        public void Validate_Error_StringRequired()
        {
            var person = new DynamicRow(Schema.GetForTypedRow(typeof(Person)));
            person["ID"] = "POP1";
            person["FirstName"] = "Oleg";
            person["LastName"] = null;
            person["DOB"] = new DateTime(1953, 12, 10);
            person["YearsInSpace"] = 12;

            var error = person.Validate();
            Console.WriteLine( error );
            Assert.IsInstanceOf(typeof(CRUDFieldValidationException), error);
            Assert.AreEqual("LastName", ((CRUDFieldValidationException)error).FieldName);
        }