예제 #1
0
        public void Rowset_Default()
        {
            var rowset = new Rowset(m_Row.Schema);

            rowset.Add(m_Row);
            rowset.Add(m_Row);

            var res = CSVWriter.Write(rowset);
            var str = m_Header + m_Data + m_Data;

            Aver.AreEqual(str, res);
        }
예제 #2
0
        public void Rowset_NoHeader()
        {
            var rowset = new Rowset(m_Row.Schema);

            rowset.Add(m_Row);
            rowset.Add(m_Row);

            var res = CSVWriter.Write(rowset, CSVWritingOptions.NoHeader);
            var str = m_Data + m_Data;

            Aver.AreEqual(str, res);
        }
예제 #3
0
        public void Rowset_Filter()
        {
            var rowset = new Rowset(m_Row.Schema);

            rowset.Add(m_Row);
            rowset.Add(m_Row);

            FieldFilterFunc filter = (r, k, fd) => fd.Name.EqualsIgnoreCase("SimpleStr") ||
                                     fd.Name.EqualsIgnoreCase("IntValue");

            var res = CSVWriter.Write(rowset, filter: filter);
            var str = m_FilteredHeader + m_FilteredData + m_FilteredData;

            Aver.AreEqual(str, res);
        }
예제 #4
0
      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"]);
      }
        public void Rowset_FromJSON_DefMissed(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 schema = (map["Schema"] as IDictionary <string, object>);
            var defs   = schema["FieldDefs"] as IList <object>;

            defs.RemoveAt(1);

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

            Aver.IsFalse(allMatched);
            var trgRow = trg[0];

            Aver.AreEqual(trgRow.Schema.FieldCount, 1);
            Aver.AreObjectsEqual(trgRow["Name"], "Henry");
        }
예제 #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, 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_RESPONSE_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);
        }
예제 #7
0
        private async Task <Rowset> readAsync(SqlConnection connection, Command command, bool isSql)
        {
            Rowset result = null;

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandType = isSql ? System.Data.CommandType.Text : System.Data.CommandType.StoredProcedure;
                cmd.CommandText = command.Text;

                bindParams(cmd, command);

                using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
                {
                    var schema = inferSchema(reader, command);
                    result            = new Rowset(schema);
                    result.LogChanges = false;
                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        var doc = Doc.MakeDoc(result.Schema);
                        populateDoc(doc, reader, command);
                        result.Add(doc);

                        if (result.Count > FETCH_LIMIT)
                        {
                            break;
                        }
                    }
                }
            }

            return(result);
        }
예제 #8
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 = 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 (val == null || val is DBNull)
                    {
                        row[fdef.Order] = null;
                        continue;
                    }

                    if (fdef.NonNullableType == typeof(bool))
                    {
                        if (store.StringBool)
                        {
                            var bval = (val is bool) ? (bool)val : val.ToString().EqualsIgnoreCase(store.StringForTrue);
                            row[fdef.Order] = bval;
                        }
                        else
                        {
                            row[fdef.Order] = val.AsNullableBool();
                        }
                    }
                    else
                    {
                        row[fdef.Order] = val;
                    }
                }

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

            return(result);
        }
예제 #9
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 Azos.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];

            Aver.AreEqual("A1", row2.ID);
            Aver.AreEqual("Joseph", row2.FirstName);
            Aver.AreEqual("Mc'Cloud", row2.LastName);
            Aver.AreEqual("111", row2.LatestHistory.ID);
            Aver.AreEqual(2, row2.History1.Count);
            Aver.AreEqual("234234", row2.History1[1].ID);
        }
예제 #10
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);
        }
예제 #11
0
        public override RowsetBase Execute(ICRUDQueryExecutionContext context, Query query, bool oneRow = false)
        {
            var ctx = (MongoDbCRUDQueryExecutionContext)context;

            Azos.Data.Access.MongoDb.Connector.Collection collection;
            var qry = MakeQuery(ctx.Database, query, Source, 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.GetForTypedDoc(typeof(TResult)));

            result.Add(rrow);
            return(result);
        }
예제 #12
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);
        }
        /// <summary>
        /// Reads data from reader into rowset. th reader is NOT disposed
        /// </summary>
        public static Rowset PopulateRowset(MySqlDataReader reader, string target, Query query, QuerySource qSource, bool oneRow)
        {
            Schema.FieldDef[] toLoad;
            Schema            schema = GetSchemaForQuery(target, query, reader, qSource, out toLoad);

            var 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;
                }
            }

            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);
        }
예제 #15
0
        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);
        }