Пример #1
0
        public override RowsetBase Execute(ICRUDQueryExecutionContext context, Query query, bool oneRow = false)
        {
            var ctx = (MongoDBCRUDQueryExecutionContext)context;

              NFX.DataAccess.MongoDB.Connector.Collection collection;
              var qry = MakeQuery(ctx.Database, query, out collection);

              var rrow = new TResult();

              var sw = Stopwatch.StartNew();

              rrow.Count = collection.Count(qry);//Performs server-side count over query

              rrow.Interval = sw.Elapsed;

              var result = new Rowset(Schema.GetForTypedRow(typeof(TResult)));
              result.Add(rrow);
              return result;
        }
        public void Rowset_FromJSON(bool rowsAsMap)
        {
            var row = new TeztRow();
            var src = new Rowset(row.Schema);

            row.BoolField = true;
            row.CharField = 'a';
            row.StringField = "aaa";
            row.DateTimeField = new DateTime(2016, 1, 2);
            row.GDIDField = new GDID(1, 2, 3);

            row.ByteField = 100;
            row.ShortField = -100;
            row.IntField = -999;

            row.UIntField = 254869;
            row.LongField = -267392;

            row.FloatField = 32768.32768F;
            row.DoubleField = -1048576.1048576D;

            row.DecimalField = 1.0529M;

            row.NullableField = null;

            row.ArrayInt = new int[] {-1, 0, 1};
            row.ListString = new List<string> {"one", "two", "three"};
            row.DictionaryIntStr = new Dictionary<int, string>
            {
              {1, "first"},
              {2, "second"}
            };

            row.RowField = new Person { Name = "John", Age = 20 };

            src.Add(row);

            row.BoolField = false;
            row.CharField = 'b';
            row.StringField = "bbb";
            row.DateTimeField = new DateTime(2016, 2, 1);
            row.GDIDField = new GDID(4, 5, 6);

            row.ByteField = 101;
            row.ShortField = 100;
            row.IntField = 999;

            row.UIntField = 109876;
            row.LongField = 267392;

            row.FloatField = -32768.32768F;
            row.DoubleField = -048576.1048576D;

            row.DecimalField = -1.0529M;

            row.NullableField = null;

            row.ArrayInt = new int[] {1, 0, -1};
            row.ListString = new List<string> { "three","two", "one" };
            row.DictionaryIntStr = new Dictionary<int, string>
            {
              {0, "zero"},
              {1, "first"},
              {2, "second"}
            };

            row.RowField = new Person { Name = "Ann", Age = 19 };

            src.Add(row);

            var options = new NFX.Serialization.JSON.JSONWritingOptions
                          {
                            RowsetMetadata = true,
                            SpaceSymbols = true,
                            IndentWidth = 2,
                            MemberLineBreak = true,
                            ObjectLineBreak = true,
                            RowsAsMap = rowsAsMap
                          };
            var json = src.ToJSON(options);

            var trg = RowsetBase.FromJSON(json);

            schemaAssertions(trg.Schema, src.Schema);
            rowsAssertions(src, trg, rowsAsMap);
        }
        public void Rowset_FromJSON_FieldMissed(bool rowsAsMap)
        {
            var row = new Person { Name = "Henry", Age = 43 };
            var rowSet = new Rowset(row.Schema);
            rowSet.Add(row);
            var options = new NFX.Serialization.JSON.JSONWritingOptions
                          {
                            RowsetMetadata = true,
                            RowsAsMap = rowsAsMap
                          };
            var json = rowSet.ToJSON(options);
            var map = JSONReader.DeserializeDataObject( json ) as JSONDataMap;
            var rows = (map["Rows"] as IList<object>);
            if (rowsAsMap)
            {
              var pers = rows[0] as IDictionary<string, object>;
              pers.Remove("Age");
            }
            else
            {
              var pers = rows[0] as IList<object>;
              pers.RemoveAt(1);
            }

            bool allMatched;
            var trg = RowsetBase.FromJSON(map, out allMatched);

            Assert.IsFalse(allMatched);
            var trgRow = trg[0];
            Assert.AreEqual(trgRow.Schema.FieldCount, 2);
            Assert.AreEqual(trgRow["Name"], "Henry");
            Assert.IsNull(trgRow["Age"]);
        }
Пример #4
0
        public void JSON_SerializeRow_ComplexTypedRow_WithSchema()
        {
            var row1 =  new PersonWithNesting{
                                    ID = "A1",
                                    FirstName = "Joseph",
                                    LastName = "Mc'Cloud",
                                    DOB = new DateTime(1953, 12, 10),
                                    YearsInSpace = 12,
                                    LatestHistory = new HistoryItem{ ID = "111", StartDate = DateTime.Now, Description="Chaplin" },
                                    History1  = new List<HistoryItem>
                                    {
                                      new HistoryItem{ ID = "789211", StartDate = DateTime.Now, Description="Chaplin with us" },
                                      new HistoryItem{ ID = "234234", StartDate = DateTime.Now, Description="Chaplin with you" }
                                    },
                                    History2  = new HistoryItem[2]
                                   };

            var tbl1 = new Rowset(row1.Schema);
            tbl1.Add(row1);

            var json = tbl1.ToJSON( new NFX.Serialization.JSON.JSONWritingOptions
                                   {
                                     RowsetMetadata = true,
                                      SpaceSymbols = true,
                                       IndentWidth = 2,
                                        MemberLineBreak = true,
                                         ObjectLineBreak = true,
                                          RowsAsMap = true,
                                           Purpose = JSONSerializationPurpose.Marshalling
                                   });//AS MAP

            Console.WriteLine(json);

            var tbl2 = json.JSONToDynamic();

            var row2 = tbl2.Rows[0];

            Assert.AreEqual("A1",      row2.ID);
            Assert.AreEqual("Joseph",  row2.FirstName);
            Assert.AreEqual("Mc'Cloud",row2.LastName);
            Assert.AreEqual("111",     row2.LatestHistory.ID);
            Assert.AreEqual(2,         row2.History1.Count);
            Assert.AreEqual("234234",  row2.History1[1].ID);
        }
Пример #5
0
        /// <summary>
        /// Reads data from reader into rowset. the reader is NOT disposed
        /// </summary>
        public static Rowset PopulateRowset(MySQLCRUDQueryExecutionContext context, MySqlDataReader reader, string target, Query query, QuerySource qSource, bool oneRow)
        {
            Schema.FieldDef[] toLoad;
              Schema schema = GetSchemaForQuery(target, query, reader, qSource, out toLoad);
              var store= context.DataStore;

              var result = new Rowset(schema);
              while(reader.Read())
              {
                var row = PopulateRow(context, query.ResultRowType, schema, toLoad, reader);

                result.Add( row );
                if (oneRow) break;
              }

              return result;
        }
Пример #6
0
    /// <summary>
    /// Converts ErlCRUD response to CLR CRUD rowset
    /// </summary>
    /// <remarks>
    /// An Example data packet is field defs as speced in schema:
    /// "tca_jaba": has two field in PK
    /// [
    ///    {tca_jaba, {1234, tav}, "User is cool", true},
    ///    {tca_jaba, {2344, zap}, "A bird wants to drink", false}, 
    ///    {tca_jaba, {8944, tav}, "Have you seen this?", false} 
    /// ]
    /// 
    /// "aaa": has one field in PK - notice no tuple in key
    /// [
    ///    {aaa, 1234, tav, "User is cool", true},
    ///    {aaa, 2344, zap, "A bird wants to drink", false}, 
    ///    {aaa, 8944, tav, "Have you seen this?", false} 
    /// ]
    /// </remarks>
    public RowsetBase ErlCRUDResponseToRowset(string schemaName, ErlList erlData)
    {
      var crudSchema = GetCRUDSchemaForName(schemaName);
      var result = new Rowset(crudSchema);

      foreach(var elm in erlData)
      {
        var tuple = elm as ErlTuple;
        if (tuple==null)
          throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"ErlCRUDResponseToRowset(list element is not tuple)");

        var row = ErlTupleToRow(schemaName, tuple, crudSchema);
        result.Add( row );
      }

      return result;
    }
            public RowsetBase Execute(ICRUDQueryExecutionContext context, Query query, bool oneRow = false)
            {
                var ctx = (MySQLCRUDQueryExecutionContext)context;
                var target = ctx.DataStore.TargetName;

                Rowset result = null;

                using (var cmd = ctx.Connection.CreateCommand())
                {
                    
                    cmd.CommandText =  m_Source.StatementSource;
                   
                    PopulateParameters(cmd, query);
                              
                   

                    cmd.Transaction = ctx.Transaction;

                    MySqlDataReader reader = null;

                    try
                    {
                        reader = oneRow ? cmd.ExecuteReader(CommandBehavior.SingleRow) : cmd.ExecuteReader();
                        GeneratorUtils.LogCommand(ctx.DataStore.LogLevel, "queryhandler-ok", cmd, null);
                    }
                    catch(Exception error)
                    {
                        GeneratorUtils.LogCommand(ctx.DataStore.LogLevel, "queryhandler-error", cmd, error);
                        throw;
                    }


                    using (reader)
                    {
                      Schema.FieldDef[] toLoad;
                      Schema schema = getSchema(target, query, reader, out toLoad);

                      result = new Rowset(schema);
                      while(reader.Read())
                      {
                        var row = Row.MakeRow(schema, query.ResultRowType);
                        
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var fdef = toLoad[i];
                            if (fdef==null) continue;

                            var val = reader.GetValue(i);
                            if (fdef.NonNullableType==typeof(bool))
                                row[fdef.Order] = val.AsNullableBool();
                            else
                                row[fdef.Order] = val;
                        }

                        result.Add( row );
                        if (oneRow) break;
                      }
                    }//using reader

                }//using command

               return result;
            }
Пример #8
0
    /// <summary>
    /// Converts ErlCRUD response to CLR CRUD rowset
    /// </summary>
    /// <remarks>
    /// An Example data packet is field defs as speced in schema:
    /// "tca_jaba": has two field in PK
    /// [
    ///    {tca_jaba, {1234, tav}, "User is cool", true},
    ///    {tca_jaba, {2344, zap}, "A bird wants to drink", false}, 
    ///    {tca_jaba, {8944, tav}, "Have you seen this?", false} 
    /// ]
    /// 
    /// "aaa": has one field in PK - notice no tuple in key
    /// [
    ///    {aaa, 1234, tav, "User is cool", true},
    ///    {aaa, 2344, zap, "A bird wants to drink", false}, 
    ///    {aaa, 8944, tav, "Have you seen this?", false} 
    /// ]
    /// </remarks>
    public RowsetBase ErlCRUDResponseToRowset(string schemaName, ErlList erlData, Type tRow = null)
    {
      var crudSchema = GetCRUDSchemaForName(schemaName);
      var tSchema    = tRow == null
        ? crudSchema
        : Schema.GetForTypedRow(tRow);

      var result = new Rowset(tSchema);

      foreach(var elm in erlData)
      {
        var tuple = elm as ErlTuple;
        if (tuple==null)
          throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESP_PROTOCOL_ERROR+"ErlCRUDResponseToRowset(list element is not tuple)");

        var row = ErlTupleToRow(schemaName, tuple, crudSchema);
        if (tRow != null)
        {
          var trow = Row.MakeRow(tSchema, tRow);
          row.CopyFields(trow);
          row = trow;
        }
        result.Add( row );
      }

      return result;
    }