diff --git a/src/Orient.Nunit.Test/Query/SqlGenerateInsertQueryTests.cs b/src/Orient.Nunit.Test/Query/SqlGenerateInsertQueryTests.cs index 3dccc487..871ea33d 100644 --- a/src/Orient.Nunit.Test/Query/SqlGenerateInsertQueryTests.cs +++ b/src/Orient.Nunit.Test/Query/SqlGenerateInsertQueryTests.cs @@ -118,6 +118,8 @@ public void ShouldGenerateInsertContentQuery() string query = "INSERT INTO TestClass " + "CONTENT " + TestConstants.CreateJson; + + Assert.AreEqual(generatedQuery, query); } [Test] @@ -131,6 +133,8 @@ public void ShouldGenerateInsertIntoContentQuery() string query = "INSERT INTO TestClass " + "CONTENT " + TestConstants.CreateJson; + + Assert.AreEqual(generatedQuery, query); } [Test] @@ -146,6 +150,8 @@ public void ShouldGenerateInsertIntoContentClusterQuery() "INSERT INTO TestClass " + "CLUSTER TestCluster " + "CONTENT " + TestConstants.CreateJson; + + Assert.AreEqual(generatedQuery, query); } [Test] diff --git a/src/Orient.Nunit.Test/Query/SqlGenerateUpdateQueryTests.cs b/src/Orient.Nunit.Test/Query/SqlGenerateUpdateQueryTests.cs index c151cc8b..200685f7 100644 --- a/src/Orient.Nunit.Test/Query/SqlGenerateUpdateQueryTests.cs +++ b/src/Orient.Nunit.Test/Query/SqlGenerateUpdateQueryTests.cs @@ -321,14 +321,14 @@ public void ShouldAtemptGenerateUpdateContentAndSetQuery_ThrowsException() OException ex = Assert.Throws(new TestDelegate(GenerateInvalidKeywordCombinationUpdateStatement)); Assert.That(ex.Type, Is.EqualTo(OExceptionType.Query)); - Assert.That(ex.Message, Is.EqualTo("Only one Keyword of SET|ADD|REMOVE|CONTENT|MERGE is allowed in query")); + Assert.That(ex.Message, Is.EqualTo("Only one Keyword of ADD|REMOVE|CONTENT|MERGE is allowed in query")); } void GenerateInvalidKeywordCombinationUpdateStatement() { new OSqlUpdate() .Record(new ORID(8, 0)) - .Set("foo", "bar") + .Add("somelist", "newvalue") .Content("{\"foo\":\"bar\"}") .ToString(); } diff --git a/src/Orient.Nunit.Test/Query/TestTransactions.cs b/src/Orient.Nunit.Test/Query/TestTransactions.cs index e9152be2..67d8ee67 100644 --- a/src/Orient.Nunit.Test/Query/TestTransactions.cs +++ b/src/Orient.Nunit.Test/Query/TestTransactions.cs @@ -200,9 +200,14 @@ public void TestCreateVerticesAndEdge() var createdVertices = database.Select().From("TestVertexClass").ToList(); Assert.AreEqual(2, createdVertices.Count); - Assert.AreEqual(createdVertices[1].ORID, createdVertices[0].OutE.First()); - Assert.AreEqual(createdVertices[0].ORID, createdVertices[1].InE.First()); + var createdVertex1 = createdVertices.FirstOrDefault(x => x.ORID.RID == testVertex1.ORID.RID); + var createdVertex2 = createdVertices.FirstOrDefault(x => x.ORID.RID == testVertex2.ORID.RID); + Assert.NotNull(createdVertex1); + Assert.NotNull(createdVertex2); + + Assert.AreEqual(createdVertex2.ORID, createdVertex1.OutE.First()); + Assert.AreEqual(createdVertex1.ORID, createdVertex2.InE.First()); } } } diff --git a/src/Orient.Nunit.Test/TestVertexClass.cs b/src/Orient.Nunit.Test/TestVertexClass.cs index 8ba2445f..e4d179bd 100644 --- a/src/Orient.Nunit.Test/TestVertexClass.cs +++ b/src/Orient.Nunit.Test/TestVertexClass.cs @@ -1,4 +1,6 @@ -namespace Orient.Nunit.Test +using Orient.Client; + +namespace Orient.Nunit.Test { public class TestVertexClass { diff --git a/src/OrientDB-Net.binary.Innov8tive/API/Types/ODocument.cs b/src/OrientDB-Net.binary.Innov8tive/API/Types/ODocument.cs index c9fcd997..c6d33c46 100644 --- a/src/OrientDB-Net.binary.Innov8tive/API/Types/ODocument.cs +++ b/src/OrientDB-Net.binary.Innov8tive/API/Types/ODocument.cs @@ -82,7 +82,9 @@ public T GetField(string fieldPath) var enumerable = (fieldValue as IEnumerable).GetEnumerator(); enumerable.MoveNext(); if (enumerable.Current != null && enumerable.Current is T) + { return (T)enumerable.Current; + } } // if value is list or set type, get element type and enumerate over its elements @@ -116,7 +118,7 @@ public T GetField(string fieldPath) } } - return value; + return SaveAndReturnValue(fieldPath, value); } if (type.Name == "HashSet`1") @@ -144,24 +146,27 @@ public T GetField(string fieldPath) addMethod.Invoke(value, new object[] { enumerator.Current }); } } - return value; + + return SaveAndReturnValue(fieldPath, value); } - else if (type == typeof(DateTime)) + else if (type == typeof(DateTime) || type == typeof(Nullable)) { + if (fieldValue != null && (fieldValue.GetType() == typeof(DateTime) || fieldValue.GetType() == typeof(Nullable))) + return SaveAndReturnValue(fieldPath, fieldValue); DateTime parsedValue; if (DateTime.TryParse((string)fieldValue, out parsedValue)) { - return (T)(object)parsedValue; + return SaveAndReturnValue(fieldPath, parsedValue); } } else if (type == typeof(TimeSpan) || type == typeof(Nullable)) { if (fieldValue != null && (fieldValue.GetType() == typeof(TimeSpan) || fieldValue.GetType() == typeof(Nullable))) - return (T)fieldValue; + return SaveAndReturnValue(fieldPath, fieldValue); TimeSpan parsedValue; if (TimeSpan.TryParse((string)fieldValue, out parsedValue)) { - return (T)(object)parsedValue; + return SaveAndReturnValue(fieldPath, parsedValue); } } else if (type == typeof(Guid)) @@ -169,18 +174,21 @@ public T GetField(string fieldPath) Guid parsedValue; if (Guid.TryParse((string)fieldValue, out parsedValue)) { - return (T)(object)parsedValue; + return SaveAndReturnValue(fieldPath, parsedValue); } } - else if (type == typeof(Decimal)) + + if (fieldValue != null) { - if (fieldValue != null) - return (T)(object)Convert.ChangeType(fieldValue, typeof(T)); - else - return (T)(object)null; + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + var generic = type.GetGenericArguments().FirstOrDefault(); + var gValue = (T)(object)Convert.ChangeType(fieldValue, generic); + return SaveAndReturnValue(fieldPath, gValue); + } } + return SaveAndReturnValue(fieldPath, fieldValue); - return (T)fieldValue; } if (fieldPath.Contains(".")) { @@ -194,8 +202,35 @@ public T GetField(string fieldPath) } var result = type.GetTypeInfo().IsPrimitive || type == typeof(string) || type.IsArray ? default(T) : (T)Activator.CreateInstance(type); - SetField(fieldPath, result); - return result; + // if the key wasn't in the dictionary, then return the default, but don't add the key to the dictionary unless the type is a collection. + if (ImplementsIList(type) || type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>)) + return SaveAndReturnValue(fieldPath, result); + else + return result; + } + + private T SaveAndReturnValue(string fieldPath, Object fieldValue) + { + // We don't save if the value is null + if (fieldValue == null) + return (T)(object)null; + + T castedValue = ConvertValue(fieldValue); + SetField(fieldPath, castedValue); + return castedValue; + } + + private T ConvertValue(object value) + { + if (value.GetType() == typeof(T)) + return (T)value; + + if (typeof(T).GetInterfaces().Contains(typeof(IConvertible))) + { + var castedValue = (T)(object)Convert.ChangeType(value, typeof(T)); + return castedValue; + } + return (T)value; } private bool ImplementsIList(Type type) diff --git a/src/OrientDB-Net.binary.Innov8tive/Protocol/Query/SqlQuery.cs b/src/OrientDB-Net.binary.Innov8tive/Protocol/Query/SqlQuery.cs index a7ba43ab..e1b38dfa 100644 --- a/src/OrientDB-Net.binary.Innov8tive/Protocol/Query/SqlQuery.cs +++ b/src/OrientDB-Net.binary.Innov8tive/Protocol/Query/SqlQuery.cs @@ -852,7 +852,7 @@ private string GenerateUpdateQuery() query += string.Join(" ", Q.Update, _compiler.OrderedValue(Q.Class, Q.Cluster, Q.Record)); var usedKeywords = new List(); - var exclusiveKeywords = new List { Q.Set, Q.Add, Q.Remove, Q.Content, Q.Merge }; + var exclusiveKeywords = new List { Q.Add, Q.Remove, Q.Content, Q.Merge }; exclusiveKeywords.ForEach(keyword => usedKeywords.Add(_compiler.HasKey(keyword))); if (usedKeywords.ExceedsThreshold(1))