Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save casted value #133

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Orient.Nunit.Test/Query/SqlGenerateInsertQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void ShouldGenerateInsertContentQuery()
string query =
"INSERT INTO TestClass " +
"CONTENT " + TestConstants.CreateJson;

Assert.AreEqual(generatedQuery, query);
}

[Test]
Expand All @@ -131,6 +133,8 @@ public void ShouldGenerateInsertIntoContentQuery()
string query =
"INSERT INTO TestClass " +
"CONTENT " + TestConstants.CreateJson;

Assert.AreEqual(generatedQuery, query);
}

[Test]
Expand All @@ -146,6 +150,8 @@ public void ShouldGenerateInsertIntoContentClusterQuery()
"INSERT INTO TestClass " +
"CLUSTER TestCluster " +
"CONTENT " + TestConstants.CreateJson;

Assert.AreEqual(generatedQuery, query);
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions src/Orient.Nunit.Test/Query/SqlGenerateUpdateQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ public void ShouldAtemptGenerateUpdateContentAndSetQuery_ThrowsException()
OException ex = Assert.Throws<OException>(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();
}
Expand Down
9 changes: 7 additions & 2 deletions src/Orient.Nunit.Test/Query/TestTransactions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,14 @@ public void TestCreateVerticesAndEdge()
var createdVertices = database.Select().From("TestVertexClass").ToList<OVertex>();
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());
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Orient.Nunit.Test/TestVertexClass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Orient.Nunit.Test
using Orient.Client;

namespace Orient.Nunit.Test
{
public class TestVertexClass
{
Expand Down
65 changes: 50 additions & 15 deletions src/OrientDB-Net.binary.Innov8tive/API/Types/ODocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public T GetField<T>(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
Expand Down Expand Up @@ -116,7 +118,7 @@ public T GetField<T>(string fieldPath)
}
}

return value;
return SaveAndReturnValue<T>(fieldPath, value);
}

if (type.Name == "HashSet`1")
Expand Down Expand Up @@ -144,43 +146,49 @@ public T GetField<T>(string fieldPath)
addMethod.Invoke(value, new object[] { enumerator.Current });
}
}
return value;

return SaveAndReturnValue<T>(fieldPath, value);
}
else if (type == typeof(DateTime))
else if (type == typeof(DateTime) || type == typeof(Nullable<DateTime>))
{
if (fieldValue != null && (fieldValue.GetType() == typeof(DateTime) || fieldValue.GetType() == typeof(Nullable<DateTime>)))
return SaveAndReturnValue<T>(fieldPath, fieldValue);
DateTime parsedValue;
if (DateTime.TryParse((string)fieldValue, out parsedValue))
{
return (T)(object)parsedValue;
return SaveAndReturnValue<T>(fieldPath, parsedValue);
}
}
else if (type == typeof(TimeSpan) || type == typeof(Nullable<TimeSpan>))
{
if (fieldValue != null && (fieldValue.GetType() == typeof(TimeSpan) || fieldValue.GetType() == typeof(Nullable<TimeSpan>)))
return (T)fieldValue;
return SaveAndReturnValue<T>(fieldPath, fieldValue);
TimeSpan parsedValue;
if (TimeSpan.TryParse((string)fieldValue, out parsedValue))
{
return (T)(object)parsedValue;
return SaveAndReturnValue<T>(fieldPath, parsedValue);
}
}
else if (type == typeof(Guid))
{
Guid parsedValue;
if (Guid.TryParse((string)fieldValue, out parsedValue))
{
return (T)(object)parsedValue;
return SaveAndReturnValue<T>(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<T>(fieldPath, gValue);
}
}
return SaveAndReturnValue<T>(fieldPath, fieldValue);

return (T)fieldValue;
}
if (fieldPath.Contains("."))
{
Expand All @@ -194,8 +202,35 @@ public T GetField<T>(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<T>(fieldPath, result);
else
return result;
}

private T SaveAndReturnValue<T>(string fieldPath, Object fieldValue)
{
// We don't save if the value is null
if (fieldValue == null)
return (T)(object)null;

T castedValue = ConvertValue<T>(fieldValue);
SetField<T>(fieldPath, castedValue);
return castedValue;
}

private T ConvertValue<T>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ private string GenerateUpdateQuery()
query += string.Join(" ", Q.Update, _compiler.OrderedValue(Q.Class, Q.Cluster, Q.Record));

var usedKeywords = new List<bool>();
var exclusiveKeywords = new List<String> { Q.Set, Q.Add, Q.Remove, Q.Content, Q.Merge };
var exclusiveKeywords = new List<String> { Q.Add, Q.Remove, Q.Content, Q.Merge };
exclusiveKeywords.ForEach(keyword => usedKeywords.Add(_compiler.HasKey(keyword)));

if (usedKeywords.ExceedsThreshold(1))
Expand Down