public void ReturnAllElements()
        {
            var node1 = Node.CreateNode();
            var node2 = Node.CreateNode();
            var rel1  = node1.CreateRelationshipTo(node2, "like");


            var cypher = new Cypher();

            cypher.Start(s => s.Node("node1", node1.Id));
            cypher.Match(m => m.Node("node1").To("rel1", "like").Node("node2"));
            cypher.Return(r => r.AllElements());

            var row = cypher.Execute().AsEnumerable().FirstOrDefault();

            Assert.IsNotNull(row);

            var objNode1 = row.Field <object>("node1");
            var objNode2 = row.Field <object>("node2");
            var objRel1  = row.Field <object>("rel1");

            var rowNode1 = RestNodeStore.CreateNodeFromJson((JObject)objNode1);
            var rowNode2 = RestNodeStore.CreateNodeFromJson((JObject)objNode2);
            var rowRel1  = RestRelationshipStore.CreateRelationshipFromJson((JObject)objRel1);

            Assert.IsTrue(node1.Id == rowNode1.Id);
            Assert.IsTrue(node2.Id == rowNode2.Id);
            Assert.IsTrue(rel1.Id == rowRel1.Id);
        }
Exemple #2
0
        public IEnumerable <DataRow> Execute(ConnectionElement connection)
        {
            var joScript = new JObject {
                { "query", Query }
            };

            string response;

            Rest.HttpRest.Post(connection.CypherUrl, joScript.ToString(Formatting.None), out response);

            var joResponse  = JObject.Parse(response);
            var jaColumns   = (JArray)joResponse["columns"];
            var jaData      = (JArray)joResponse["data"];
            var returnTypes = GetReturnTypes;

            var dt = new DataTable();

            if (jaData == null)
            {
                return(dt.AsEnumerable());
            }

            var initColumns = true;

            foreach (JArray jRow in jaData)
            {
                var colIndex = 0;
                var row      = new List <object>();

                foreach (var jCol in jRow)
                {
                    if (initColumns)
                    {
                        // NOTE: DataTable does NOT support nullable data types
                        dt.Columns.Add(jaColumns[colIndex].ToString(), colIndex < returnTypes.Count ? returnTypes[colIndex] : typeof(object));
                    }

                    if (colIndex >= returnTypes.Count)
                    {
                        row.Add(jCol.Value <object>());
                    }
                    else if (returnTypes[colIndex] == typeof(Node))
                    {
                        row.Add(jCol.Type == JTokenType.Null ? null : RestNodeStore.CreateNodeFromJson((JObject)jCol));
                    }
                    else if (returnTypes[colIndex] == typeof(Relationship))
                    {
                        row.Add(jCol.Type == JTokenType.Null ? null : RestRelationshipStore.CreateRelationshipFromJson((JObject)jCol));
                    }
                    else if (returnTypes[colIndex] == typeof(Path))
                    {
                        row.Add(jCol.Type == JTokenType.Null ? null : Path.ParseJson((JArray)jCol));
                    }

                    // Handle the Null case for all Types
                    else if (jCol.Type == JTokenType.Null)
                    {
                        row.Add(DBNull.Value);
                    }

                    // Explicitly cast to correct data type
                    else if (returnTypes[colIndex] == typeof(DateTime))
                    {
                        row.Add(jCol.Value <DateTime>());
                    }
                    else if (returnTypes[colIndex] == typeof(double))
                    {
                        row.Add(jCol.Value <double>());
                    }
                    else if (returnTypes[colIndex] == typeof(int))
                    {
                        row.Add(jCol.Value <int>());
                    }
                    else if (returnTypes[colIndex] == typeof(long))
                    {
                        row.Add(jCol.Value <long>());
                    }
                    else if (returnTypes[colIndex] == typeof(string))
                    {
                        row.Add(jCol.Value <string>());
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("Return Type of {0} is not supported", returnTypes[colIndex].Name));
                    }

                    colIndex++;
                }

                initColumns = false;
                var dtRow = dt.NewRow();
                dtRow.ItemArray = row.ToArray();
                dt.Rows.Add(dtRow);
            }

            return(dt.AsEnumerable());
        }
Exemple #3
0
        public static DataTable GetTable(ConnectionElement connection, GremlinScript script)
        {
            string response;
            var    status = Rest.HttpRest.Post(connection.GremlinUrl, script.GetScript(), out response);

            var joResponse = JObject.Parse(response);
            var jaColumns  = (JArray)joResponse["columns"];
            var jaData     = (JArray)joResponse["data"];

            var dt = new DataTable();

            var initColumns = true;
            var colIndex    = 0;

            foreach (JArray jRow in jaData)
            {
                var row = new List <object>();
                foreach (var jCol in jRow)
                {
                    switch (jCol.Type)
                    {
                    case JTokenType.String:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Object:
                        if (jCol["self"] == null)
                        {
                            row.Add(jCol.ToString());

                            if (initColumns)
                            {
                                dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                                colIndex++;
                            }
                        }
                        else
                        {
                            string   self      = jCol["self"].ToString();
                            string[] selfArray = self.Split('/');
                            if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node")
                            {
                                row.Add(RestNodeStore.CreateNodeFromJson((JObject)jCol));

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(Node));
                                    colIndex++;
                                }
                            }
                            else if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "relationship")
                            {
                                row.Add(RestRelationshipStore.CreateRelationshipFromJson((JObject)jCol));

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(Relationship));
                                    colIndex++;
                                }
                            }
                            else
                            {
                                // Not a Node or Relationship - return as string
                                row.Add(jCol.ToString());

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                                    colIndex++;
                                }
                            }
                        }
                        break;

                    case JTokenType.Integer:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(int));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Float:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(float));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Date:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(DateTime));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Boolean:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(bool));
                            colIndex++;
                        }
                        break;

                    default:
                        row.Add(jCol.ToString());

                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                            colIndex++;
                        }
                        break;
                    }
                }

                initColumns = false;
                var dtRow = dt.NewRow();
                dtRow.ItemArray = row.ToArray();
                dt.Rows.Add(dtRow);
            }

            return(dt);
        }