Ejemplo n.º 1
0
        public void TestStruct()
        {
            IIonValue nestedStruct = factory.NewEmptyStruct();

            nestedStruct.SetField("nestedString", factory.NewString("Ion"));
            nestedStruct.SetField("nestedInt", factory.NewInt(123));
            value.SetField("value", nestedStruct);
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":{\"nestedString\":\"Ion\",\"nestedInt\":123}}", this.sw.ToString());
        }
        public void Execute_InsertDocumentWithMultipleFields_DocumentIsInserted()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = ValueFactory.NewEmptyStruct();

            ionStruct.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));
            ionStruct.SetField(Constants.SecondColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            // When.
            var query = $"INSERT  INTO {Constants.TableName} ?";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, ionStruct);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, count);

            // Then.
            var       searchQuery = $@"SELECT {Constants.ColumnName}, {Constants.SecondColumnName} FROM {Constants.TableName} 
                               WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}' AND  {Constants.SecondColumnName} = '{Constants.SingleDocumentValue}'";
            IIonValue value       = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery);

                IIonValue value = null;
                foreach (var row in result)
                {
                    value = row;
                }
                return(value);
            });

            var ionReader = IonReaderBuilder.Build(value);

            ionReader.MoveNext();
            ionReader.StepIn();
            ionReader.MoveNext();
            Assert.AreEqual(Constants.ColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
            ionReader.MoveNext();
            Assert.AreEqual(Constants.SecondColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
        }
        public async Task ExecuteAsync_InsertDocumentWithMultipleFields_DocumentIsInserted()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = GetSingleValueIonStruct(IonString);

            ionStruct.SetField(Constants.SecondColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            // When.
            var insertQuery = $"INSERT INTO {Constants.TableName} ?";

            Assert.AreEqual(1, await ExecuteWithParamAndReturnRowCount(insertQuery, ionStruct));

            // Then.
            var       searchQuery = $@"SELECT {Constants.ColumnName}, {Constants.SecondColumnName} FROM {Constants.TableName} 
                               WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}' 
                               AND  {Constants.SecondColumnName} = '{Constants.SingleDocumentValue}'";
            IIonValue value       = await ExecuteAndReturnIonValue(searchQuery);

            var ionReader = IonReaderBuilder.Build(value);

            ionReader.MoveNext();
            ionReader.StepIn();
            ionReader.MoveNext();
            Assert.AreEqual(Constants.ColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
            ionReader.MoveNext();
            Assert.AreEqual(Constants.SecondColumnName, ionReader.CurrentFieldName);
            Assert.AreEqual(Constants.SingleDocumentValue, ionReader.StringValue());
        }
        public void Execute_InsertMultipleDocuments_DocumentsInserted()
        {
            IIonValue ionString1 = ValueFactory.NewString(Constants.MultipleDocumentValue1);
            IIonValue ionString2 = ValueFactory.NewString(Constants.MultipleDocumentValue2);

            // Given.
            // Create Ion structs to insert.
            IIonValue ionStruct1 = ValueFactory.NewEmptyStruct();

            ionStruct1.SetField(Constants.ColumnName, ionString1);

            IIonValue ionStruct2 = ValueFactory.NewEmptyStruct();

            ionStruct2.SetField(Constants.ColumnName, ionString2);

            List <IIonValue> parameters = new List <IIonValue>()
            {
                ionStruct1, ionStruct2
            };

            // When.
            var query = $"INSERT INTO {Constants.TableName} <<?,?>>";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, parameters);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(2, count);

            // Then.
            var searchQuery = $@"SELECT VALUE {Constants.ColumnName} FROM {Constants.TableName}
                                 WHERE {Constants.ColumnName} IN (?,?)";
            var values      = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery, ionString1, ionString2);

                var values = new List <String>();
                foreach (var row in result)
                {
                    values.Add(row.StringValue);
                }
                return(values);
            });

            Assert.IsTrue(values.Contains(Constants.MultipleDocumentValue1));
            Assert.IsTrue(values.Contains(Constants.MultipleDocumentValue2));
        }
Ejemplo n.º 5
0
        internal IIonValue SerializeObject(object obj, IIonValue ionValue)
        {
            // Get object attributes
            IList <PropertyInfo> properties = new List <PropertyInfo>(obj.GetType().GetProperties());

            foreach (var property in properties)
            {
                // If object attribute is another object
                if (Type.GetTypeCode(property.GetValue(obj, null).GetType()) == TypeCode.Object)
                {
                    IIonValue emptyIonStruct  = factory.NewEmptyStruct();
                    IIonValue filledIonStruct = SerializeObject(property.GetValue(obj, null), emptyIonStruct);
                    ionValue.SetField(property.Name, filledIonStruct);
                }
                else
                {
                    IIonValue propertyValue = SerializingMappers(property.GetValue(obj, null));
                    ionValue.SetField(property.Name, propertyValue);
                }
            }
            return(ionValue);
        }
Ejemplo n.º 6
0
        private void BuildChildren(IIonValue parent, object instance)
        {
            PropertyInfo[] propertyInfo = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var info in propertyInfo)
            {
                var value = GetPropertyValue(info, instance);
                if (value != null)
                {
                    parent.SetField(info.Name, value);
                }
            }
        }
Ejemplo n.º 7
0
        private void BuildDictionary(IIonValue dict, IDictionary instance)
        {
            Type[] typeParameters = instance.GetType().GetGenericArguments();
            var    keyType        = typeParameters[0];
            var    valueType      = typeParameters[1];

            foreach (var key in instance.Keys)
            {
                var       handler = FieldHandlers.GetHandler(valueType);
                var       item    = instance[key];
                IIonValue value   = handler.ConvertFrom(item);
                dict.SetField(key.ToString(), value);
            }
        }
        public void Execute_QueryTableEnclosedInQuotes_ReturnsResult()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = ValueFactory.NewEmptyStruct();

            ionStruct.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            var query = $"INSERT INTO {Constants.TableName} ?";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, ionStruct);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, count);

            // When.
            var searchQuery = $@"SELECT VALUE {Constants.ColumnName} FROM ""{Constants.TableName}""
                                 WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}'";
            var value       = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery);

                var value = "";
                foreach (var row in result)
                {
                    value = row.StringValue;
                }
                return(value);
            });

            // Then.
            Assert.AreEqual(Constants.SingleDocumentValue, value);
        }
        public async Task Execute_QuerySingleField_ReturnsSingleField()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = ValueFactory.NewEmptyStruct();

            ionStruct.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            var query = $"INSERT INTO {Constants.TableName} ?";
            var count = await qldbDriver.Execute(async txn =>
            {
                var result = await txn.Execute(query, ionStruct);

                var count = 0;
                await foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, count);

            // When.
            var searchQuery = $@"SELECT VALUE {Constants.ColumnName} FROM {Constants.TableName}
                                 WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}'";
            var value       = await qldbDriver.Execute(async txn =>
            {
                var result = await txn.Execute(searchQuery);

                var value = "";
                await foreach (var row in result)
                {
                    value = row.StringValue;
                }
                return(value);
            });

            // Then.
            Assert.AreEqual(Constants.SingleDocumentValue, value);
        }
        public void Execute_DeleteAllDocuments_DocumentsAreDeleted()
        {
            // Given.
            // Create Ion structs to insert.
            IIonValue ionStruct1 = ValueFactory.NewEmptyStruct();

            ionStruct1.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.MultipleDocumentValue1));

            IIonValue ionStruct2 = ValueFactory.NewEmptyStruct();

            ionStruct2.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.MultipleDocumentValue2));

            List <IIonValue> parameters = new List <IIonValue>()
            {
                ionStruct1, ionStruct2
            };

            var query = $"INSERT INTO {Constants.TableName} <<?,?>>";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, parameters);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(2, count);

            // When.
            var deleteQuery = $"DELETE FROM { Constants.TableName}";
            var deleteCount = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(deleteQuery);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(2, deleteCount);

            // Then.
            var searchQuery = $"SELECT COUNT(*) FROM {Constants.TableName}";
            var searchCount = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery);

                int count = -1;
                foreach (var row in result)
                {
                    // This gives:
                    // {
                    //    _1: 1
                    // }
                    IIonValue ionValue = row.GetField("_1");
                    count = ((IIonInt)ionValue).IntValue;
                }
                return(count);
            });

            Assert.AreEqual(0, searchCount);
        }
        public void Execute_DeleteSingleDocument_DocumentIsDeleted()
        {
            // Given.
            // Create Ion struct to insert.
            IIonValue ionStruct = ValueFactory.NewEmptyStruct();

            ionStruct.SetField(Constants.ColumnName, ValueFactory.NewString(Constants.SingleDocumentValue));

            var query = $"INSERT INTO {Constants.TableName} ?";
            var count = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(query, ionStruct);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, count);

            // When.
            var deleteQuery  = $@"DELETE FROM { Constants.TableName}
                                 WHERE {Constants.ColumnName} = '{Constants.SingleDocumentValue}'";
            var deletedCount = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(deleteQuery);

                var count = 0;
                foreach (var row in result)
                {
                    count++;
                }
                return(count);
            });

            Assert.AreEqual(1, deletedCount);

            // Then.
            var searchQuery = $"SELECT COUNT(*) FROM {Constants.TableName}";
            var searchCount = qldbDriver.Execute(txn =>
            {
                var result = txn.Execute(searchQuery);

                int count = -1;
                foreach (var row in result)
                {
                    // This gives:
                    // {
                    //    _1: 1
                    // }
                    IIonValue ionValue = row.GetField("_1");
                    count = ((IIonInt)ionValue).IntValue;
                }
                return(count);
            });

            Assert.AreEqual(0, searchCount);
        }
Ejemplo n.º 12
0
        public void TestGenericNull()
        {
            value.SetField("value", factory.NewNull());
            var reader = IonReaderBuilder.Build(value);

            jsonWriter.WriteValues(reader);
            Assert.AreEqual("{\"value\":null}", this.sw.ToString());
        }