Skip to content
Browse files

Updated to new version of Newtonsoft Json

updated CypherProperty to support Where Clause Option Default value correctly

Added Coalesce to Cypher Return

Added Order enum to CypherOrderBy

Updated ParseWhereLambda to support order of presidents with parentheses.

Corrected GremlinScriptCommands to correctly support multiple Nodes/Relationships for
inV, outV, inE, outE, out, in commands
  • Loading branch information...
1 parent 2d289bf commit eb732d40a2e1b895d297c7a6a59fd8d3c6b1ea73 unknown committed Mar 19, 2012
View
76 Neo4jRestNet/CypherPlugin/CypherOrderBy.cs
@@ -5,6 +5,11 @@ namespace Neo4jRestNet.CypherPlugin
{
public class CypherOrderBy : ICypherObject
{
+ public enum Order
+ {
+ Acending,
+ Decending
+ }
private readonly StringBuilder _sb = new StringBuilder();
private bool _isStringEmpty = true; // used becuase string could contain Distinct
@@ -13,30 +18,37 @@ public class CypherOrderBy : ICypherObject
public CypherOrderBy Node(string name, string property)
{
- _sb.AppendFormat("{0} {1}.{2}", _isStringEmpty ? string.Empty : ",", name, property);
-
- _isStringEmpty = false;
-
- return this;
+ return OrderByClause(name, property, Order.Acending, false);
}
public CypherOrderBy Node(string name, Enum property)
{
- return Node(name, property.ToString());
+ return OrderByClause(name, property.ToString(), Order.Acending, false);
}
- public CypherOrderBy Node(string name, string property, bool decending)
+ public CypherOrderBy Node(string name, Enum property, bool optional)
{
- _sb.AppendFormat("{0} {1}.{2} {3}", _isStringEmpty ? string.Empty : ",", name, property, decending ? "DESC" : string.Empty);
+ return OrderByClause(name, property.ToString(), Order.Acending, optional);
+ }
- _isStringEmpty = false;
+ public CypherOrderBy Node(string name, string property, Order order)
+ {
+ return OrderByClause(name, property, Order.Acending, false);
+ }
- return this;
+ public CypherOrderBy Node(string name, string property, Order order, bool optional)
+ {
+ return OrderByClause(name, property, Order.Acending, optional);
+ }
+
+ public CypherOrderBy Node(string name, Enum property, Order order)
+ {
+ return OrderByClause(name, property.ToString(), order, false);
}
- public CypherOrderBy Node(string name, Enum property, bool decending)
+ public CypherOrderBy Node(string name, Enum property, Order order, bool optional)
{
- return Node(name, property.ToString(), decending);
+ return OrderByClause(name, property.ToString(), order, optional);
}
#endregion
@@ -45,34 +57,50 @@ public CypherOrderBy Node(string name, Enum property, bool decending)
public CypherOrderBy Relationship(string name, string property)
{
- _sb.AppendFormat("{0} {1}.{2}", _isStringEmpty ? string.Empty : ",", name, property);
-
- _isStringEmpty = false;
-
- return this;
+ return OrderByClause(name, property, Order.Acending, false);
}
public CypherOrderBy Relationship(string name, Enum property)
{
- return Relationship(name, property.ToString());
+ return OrderByClause(name, property.ToString(), Order.Acending, false);
}
- public CypherOrderBy Relationship(string name, string property, bool decending)
+ public CypherOrderBy Relationship(string name, Enum property, bool optional)
{
- _sb.AppendFormat("{0} {1}.{2} {3}", _isStringEmpty ? string.Empty : ",", name, property, decending ? "DESC" : string.Empty);
+ return OrderByClause(name, property.ToString(), Order.Acending, optional);
+ }
- _isStringEmpty = false;
+ public CypherOrderBy Relationship(string name, string property, Order order)
+ {
+ return OrderByClause(name, property, Order.Acending, false);
+ }
- return this;
+ public CypherOrderBy Relationship(string name, string property, Order order, bool optional)
+ {
+ return OrderByClause(name, property, Order.Acending, optional);
}
- public CypherOrderBy Relationship(string name, Enum property, bool decending)
+ public CypherOrderBy Relationship(string name, Enum property, Order order)
{
- return Relationship(name, property.ToString(), decending);
+ return OrderByClause(name, property.ToString(), order, false);
+ }
+
+ public CypherOrderBy Relationship(string name, Enum property, Order order, bool optional)
+ {
+ return OrderByClause(name, property.ToString(), order, optional);
}
#endregion
+ private CypherOrderBy OrderByClause(string name, string property, Order order, bool optional)
+ {
+ _sb.AppendFormat("{0} {1}.{2}{4} {3}", _isStringEmpty ? string.Empty : ",", name, property, order == Order.Decending ? "DESC" : string.Empty, optional ? "?" : string.Empty);
+
+ _isStringEmpty = false;
+
+ return this;
+ }
+
public ICypherObject Append(string value)
{
_sb.Append(value);
View
8 Neo4jRestNet/CypherPlugin/CypherProperty.cs
@@ -14,14 +14,14 @@ public static CypherWhere Property(this CypherWhere cypherWhere, Enum name)
return Property(cypherWhere, name.ToString());
}
- public static CypherWhere Property(this CypherWhere cypherWhere, string name, bool optional)
+ public static CypherWhere Property(this CypherWhere cypherWhere, string name, bool optionalDefault)
{
- return (CypherWhere)cypherWhere.Append(".{0}{1}", name, optional ? "?" : string.Empty);
+ return (CypherWhere)cypherWhere.Append(".{0}{1}", name, optionalDefault ? "?" : "!");
}
- public static CypherWhere Property(this CypherWhere cypherWhere, Enum name, bool optional)
+ public static CypherWhere Property(this CypherWhere cypherWhere, Enum name, bool optionalDefault)
{
- return Property(cypherWhere, name.ToString(), optional);
+ return Property(cypherWhere, name.ToString(), optionalDefault);
}
public static CypherReturn Property<T>(this CypherReturn cypherReturn, string name)
View
11 Neo4jRestNet/CypherPlugin/CypherReturn.cs
@@ -131,6 +131,17 @@ public CypherReturn Count(string name, bool distinct)
return this;
}
+ public CypherReturn Coalesce<T>(string elements)
+ {
+ _sb.AppendFormat("{1} Coalesce({0})", elements, _isStringEmpty ? string.Empty : ",");
+
+ _isStringEmpty = false;
+
+ ReturnTypes.Add(typeof(T));
+
+ return this;
+ }
+
#endregion
#region Type
View
59 Neo4jRestNet/CypherPlugin/ParseWhereLambda.cs
@@ -11,6 +11,9 @@ public class ParseWhereLambda
public string Parse(Expression expression)
{
+ var leftParens = false;
+ var rightParens = false;
+
var sbFilter = new StringBuilder();
var body = expression.NodeType == ExpressionType.Lambda ? ((LambdaExpression)expression).Body : expression;
BinaryExpression be;
@@ -93,12 +96,42 @@ public string Parse(Expression expression)
case ExpressionType.AndAlso:
be = (BinaryExpression)body;
- sbFilter.Append(InvokeExpression(be.Left, "and", be.Right));
+
+ if (be.Left.NodeType == ExpressionType.OrElse)
+ {
+ // Check to see if NullOp
+ if (!string.IsNullOrWhiteSpace(Parse(((BinaryExpression)be.Left).Left)))
+ {
+ leftParens = true;
+ }
+ }
+
+ if (be.Right.NodeType == ExpressionType.OrElse)
+ {
+ rightParens = true;
+ }
+
+ sbFilter.Append(InvokeExpression(be.Left, "and", be.Right, leftParens, rightParens));
break;
case ExpressionType.OrElse:
be = (BinaryExpression)body;
- sbFilter.Append(InvokeExpression(be.Left, "or", be.Right));
+
+ if (be.Left.NodeType == ExpressionType.AndAlso)
+ {
+ // Check to see if NullOp
+ if(!string.IsNullOrWhiteSpace(Parse(((BinaryExpression)be.Left).Left)))
+ {
+ leftParens = true;
+ }
+ }
+
+ if (be.Right.NodeType == ExpressionType.AndAlso)
+ {
+ rightParens = true;
+ }
+
+ sbFilter.Append(InvokeExpression(be.Left, "or", be.Right, leftParens, rightParens));
break;
case ExpressionType.LessThan:
@@ -129,13 +162,29 @@ public string Parse(Expression expression)
return sbFilter.ToString();
}
- private string InvokeExpression(Expression leftExpression, string strOperator, Expression rightExpression)
+ private string InvokeExpression(Expression leftExpression, string strOperator, Expression rightExpression, bool leftParens = false, bool rightParens = false)
{
var sb = new StringBuilder();
- sb.Append(Parse(leftExpression));
+ if (leftParens)
+ {
+ sb.AppendFormat("({0})", Parse(leftExpression));
+ }
+ else
+ {
+ sb.Append(Parse(leftExpression));
+ }
+
sb.AppendFormat(" {0} ", strOperator);
- sb.Append(Parse(rightExpression));
+
+ if (rightParens)
+ {
+ sb.AppendFormat("({0})", Parse(rightExpression));
+ }
+ else
+ {
+ sb.Append(Parse(rightExpression));
+ }
return sb.ToString();
}
View
54 Neo4jRestNet/GremlinPlugin/GremlinScriptCommands.cs
@@ -87,7 +87,14 @@ public static GremlinScript In(this GremlinScript query, params Enum[] relations
public static GremlinScript In(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".in({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".in({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".in({0})", relationships));
}
#endregion
@@ -108,7 +115,14 @@ public static GremlinScript InE(this GremlinScript query, params Enum[] relation
public static GremlinScript InE(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".inE({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".inE({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".inE({0})", relationships));
}
#endregion
@@ -129,7 +143,14 @@ public static GremlinScript InV(this GremlinScript query, params Enum[] relation
public static GremlinScript InV(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".inV({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".inV({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".inV({0})", relationships));
}
#endregion
@@ -150,7 +171,14 @@ public static GremlinScript Out(this GremlinScript query, params Enum[] relation
public static GremlinScript Out(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".out({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".out({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".out({0})", relationships));
}
#endregion
@@ -171,7 +199,14 @@ public static GremlinScript OutE(this GremlinScript query, params Enum[] relatio
public static GremlinScript OutE(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".outE({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".outE({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".outE({0})", relationships));
}
#endregion
@@ -192,7 +227,14 @@ public static GremlinScript OutV(this GremlinScript query, params Enum[] relatio
public static GremlinScript OutV(this GremlinScript query, params string[] relationshipName)
{
- return query.Append(".outV({0})", string.Join(",", relationshipName));
+ if (relationshipName.Count() == 1)
+ {
+ return query.Append(".outV({0})", relationshipName[0]);
+ }
+
+ // Can not send more then one element via parameters
+ var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));
+ return query.Append(string.Format(".outV({0})", relationships));
}
#endregion
View
3 Neo4jRestNet/Neo4jRestNet.csproj
@@ -63,7 +63,8 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
- <Reference Include="Newtonsoft.Json">
+ <Reference Include="Newtonsoft.Json, Version=4.0.2.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
<HintPath>..\lib\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
View
BIN lib/Newtonsoft.Json.dll
Binary file not shown.

0 comments on commit eb732d4

Please sign in to comment.
Something went wrong with that request. Please try again.