public Dictionary <string, string> GetColumnTableMapping(
            Dictionary <Tuple <string, string>, Dictionary <string, NodeColumns> > columnsOfNodeTables)
        {
            if (_columnTableAliasMapping == null)
            {
                _columnTableAliasMapping = new Dictionary <string, string>(StringComparer.CurrentCultureIgnoreCase);
                var duplicateColumns = new HashSet <string>();
                foreach (var kvp in NodeTableDictionary)
                {
                    var nodeTable = kvp.Value as WNamedTableReference;
                    if (nodeTable != null)
                    {
                        var nodeTableObjectName = nodeTable.TableObjectName;
                        var nodeTableTuple      = WNamedTableReference.SchemaNameToTuple(nodeTableObjectName);
                        foreach (
                            var property in
                            columnsOfNodeTables[nodeTableTuple].Where(e => e.Value.Role != WNodeTableColumnRole.Edge)
                            .Select(e => e.Key))
                        {
                            if (!_columnTableAliasMapping.ContainsKey(property.ToLower()))
                            {
                                _columnTableAliasMapping[property.ToLower()] = kvp.Key;
                            }
                            else
                            {
                                duplicateColumns.Add(property.ToLower());
                            }
                        }
                    }
                }

                foreach (var kvp in EdgeDictionary)
                {
                    var tuple = kvp.Value;
                    var sourceTableObjectName = tuple.Item1;
                    var edgeColumnReference   = tuple.Item2;
                    var soureNodeTableTuple   =
                        WNamedTableReference.SchemaNameToTuple(sourceTableObjectName);
                    var edgeProperties =
                        columnsOfNodeTables[soureNodeTableTuple][
                            edgeColumnReference.MultiPartIdentifier.Identifiers.Last().Value.ToLower()];
                    foreach (var attribute in edgeProperties.ColumnAttributes)
                    {
                        if (!_columnTableAliasMapping.ContainsKey(attribute.ToLower()))
                        {
                            _columnTableAliasMapping[attribute.ToLower()] = kvp.Key;
                        }
                        else
                        {
                            duplicateColumns.Add(attribute.ToLower());
                        }
                    }
                }
                foreach (var col in duplicateColumns)
                {
                    _columnTableAliasMapping.Remove(col);
                }
            }
            return(_columnTableAliasMapping);
        }
        public override void Visit(WNamedTableReference node)
        {
            if (_nodeTables != null &&
                !_nodeTables.Contains(WNamedTableReference.SchemaNameToTuple(node.TableObjectName)))
            {
                //throw new GraphViewException("Invalid Table " + node);
                return;
            }
            var name = node.Alias ?? node.TableObjectName.BaseIdentifier;

            _results.AddNodeTable(name.Value, node);
        }
Exemple #3
0
        //public virtual WSchemaObjectFunctionTableReference ToSchemaObjectFunction(string nodeAlias, GraphMetaData metaData)
        //{
        //    var edgeIdentifiers = EdgeColumn.MultiPartIdentifier.Identifiers;
        //    var edgeColIdentifier = edgeIdentifiers.Last();
        //    var edgeColName = edgeColIdentifier.Value;

        //    HashSet<string> nodeSet;
        //    if (!metaData.NodeViewMapping.TryGetValue(
        //        WNamedTableReference.SchemaNameToTuple(SourceNode.NodeTableObjectName), out nodeSet))
        //        nodeSet = null;
        //    NodeColumns columnInfo = metaData.ColumnsOfNodeTables[WNamedTableReference.SchemaNameToTuple(BindNodeTableObjName)][
        //        edgeColIdentifier.Value];
        //    EdgeInfo edgeInfo = columnInfo.EdgeInfo;
        //    List<Tuple<string, string>> edgeTuples = edgeInfo.EdgeColumns;
        //    var parameters = ConstructEdgeTvfParameters(nodeAlias, "", nodeSet, edgeTuples);

        //    //var isRevEdge = columnInfo.IsReversedEdge;
        //    //string refTableName = null, originalEdgeName = null;
        //    //if (isRevEdge)
        //    //{
        //    //    var index = edgeColName.IndexOf('_');
        //    //    refTableName = edgeColName.Substring(0, index);
        //    //    originalEdgeName = edgeColName.Substring(index + 1,
        //    //        edgeColName.Length - "Reversed".Length - index - 1);
        //    //}
        //    //var decoderSchemaName = isRevEdge ? columnInfo.RefTableSchema : BindNodeTableObjName.SchemaIdentifier.Value;
        //    //var decoderTableName = isRevEdge ? refTableName : BindNodeTableObjName.BaseIdentifier.Value;
        //    //var decoderEdgeName = isRevEdge ? originalEdgeName : EdgeColumn.MultiPartIdentifier.Identifiers.Last().Value;
        //    //var decoderStr = decoderSchemaName + "_" + decoderTableName + "_" + decoderEdgeName + "_Decoder";

        //    var decoderFunction = new Identifier
        //    {
        //        Value = edgeInfo.EdgeUdfPrefix + "_Decoder",
        //    };
        //    return new WSchemaObjectFunctionTableReference
        //    {
        //        SchemaObject = new WSchemaObjectName(
        //            new Identifier { Value = "dbo" },
        //            decoderFunction),
        //        Parameters = parameters,
        //        Alias = new Identifier
        //        {
        //            Value = EdgeAlias,
        //        }
        //    };
        //}

        /// <summary>
        /// Converts the edge to the table-valued function
        /// </summary>
        /// <param name="nodeAlias">Source node alias</param>
        /// <param name="dumbNode">Dumb node parameter alias</param>
        /// <param name="metaData">Meta data</param>
        /// <returns>A syntax tree node representing the table-valued function</returns>
        public virtual WSchemaObjectFunctionTableReference ToSchemaObjectFunction(string nodeAlias, string dumbNode, GraphMetaData metaData)
        {
            var edgeIdentifiers    = EdgeColumn.MultiPartIdentifier.Identifiers;
            var edgeColIdentifier  = edgeIdentifiers.Last();
            var edgeColName        = edgeColIdentifier.Value;
            var bindNodeTableTuple = WNamedTableReference.SchemaNameToTuple(BindNodeTableObjName);

            HashSet <string> nodeSet;

            if (!metaData.NodeViewMapping.TryGetValue(
                    WNamedTableReference.SchemaNameToTuple(SourceNode.NodeTableObjectName), out nodeSet))
            {
                nodeSet = null;
            }

            if (IsEdgeView && IsReversedEdge)
            {
                var sinkNodeTableTuple = WNamedTableReference.SchemaNameToTuple(SinkNode.NodeTableObjectName);
                var index = edgeColName.IndexOf(sinkNodeTableTuple.Item2, StringComparison.OrdinalIgnoreCase);
                index += sinkNodeTableTuple.Item2.Length;
                var originalEdgeViewName = edgeColName.Substring(index + 1,
                                                                 edgeColName.Length - "Reversed".Length - index - 1);
                edgeColName = bindNodeTableTuple.Item2 + "_" + originalEdgeViewName + "Reversed";
            }

            NodeColumns columnInfo =
                metaData.ColumnsOfNodeTables[WNamedTableReference.SchemaNameToTuple(BindNodeTableObjName)][
                    edgeColName];
            EdgeInfo edgeInfo = columnInfo.EdgeInfo;
            List <Tuple <string, string> > edgeTuples = edgeInfo.EdgeColumns;

            var parameters = ConstructEdgeTvfParameters(nodeAlias, dumbNode, nodeSet, edgeTuples);

            var decoderFunction = new Identifier
            {
                Value = edgeInfo.EdgeUdfPrefix + "_Decoder",
            };

            return(new WSchemaObjectFunctionTableReference
            {
                SchemaObject = new WSchemaObjectName(
                    new Identifier {
                    Value = "dbo"
                },
                    decoderFunction),
                Parameters = parameters,
                Alias = new Identifier
                {
                    Value = EdgeAlias,
                }
            });
        }
Exemple #4
0
        /// <summary>
        /// Converts the edge to the table-valued function
        /// </summary>
        /// <param name="nodeAlias">Source node alias</param>
        /// <param name="metaData">Meta data</param>
        /// <returns>A syntax tree node representing the table-valued function</returns>
        public virtual WSchemaObjectFunctionTableReference ToSchemaObjectFunction(string nodeAlias, GraphMetaData metaData)
        {
            var edgeIdentifiers   = EdgeColumn.MultiPartIdentifier.Identifiers;
            var edgeColIdentifier = edgeIdentifiers.Last();

            HashSet <string> nodeSet;

            if (!metaData.NodeViewMapping.TryGetValue(
                    WNamedTableReference.SchemaNameToTuple(SourceNode.NodeTableObjectName), out nodeSet))
            {
                nodeSet = null;
            }
            EdgeInfo edgeInfo =
                metaData.ColumnsOfNodeTables[WNamedTableReference.SchemaNameToTuple(BindNodeTableObjName)][
                    edgeColIdentifier.Value].EdgeInfo;
            List <Tuple <string, string> > edgeTuples = edgeInfo.EdgeColumns;
            var parameters = ConstructEdgeTvfParameters(nodeAlias, nodeSet, edgeTuples);

            var decoderFunction = new Identifier
            {
                Value = BindNodeTableObjName.SchemaIdentifier.Value + '_' +
                        BindNodeTableObjName.BaseIdentifier.Value + '_' +
                        EdgeColumn.MultiPartIdentifier.Identifiers.Last().Value + '_' +
                        "Decoder"
            };

            return(new WSchemaObjectFunctionTableReference
            {
                SchemaObject = new WSchemaObjectName(
                    new Identifier {
                    Value = "dbo"
                },
                    decoderFunction),
                Parameters = parameters,
                Alias = new Identifier
                {
                    Value = EdgeAlias,
                }
            });
        }
Exemple #5
0
        /// <summary>
        /// Constructs parameters for the edge table-valued function when translation the MATCH clause
        /// </summary>
        /// <param name="nodeAlias">Source node alias</param>
        /// <param name="dumbNode">Dumb node parameter alias</param>
        /// <param name="nodeTableNameSet">Node table names mapping to the source node of the edge.
        /// If null, the source node is mapped to a physical table in the syntax tree. Otherewise,
        /// the source node is mapped to a node view</param>
        /// <param name="edgeNameTuples">A tuple (Node table name, Edge column name) mapping to the edge.
        /// If null, the edge is mapped to an edge column in a physical node table. Ohterwise,
        /// the edge is mapped to an edge view</param>
        /// <returns>Parameters in the table-valued function</returns>
        protected List <WScalarExpression> ConstructEdgeTvfParameters(string nodeAlias, string dumbNode,
                                                                      HashSet <string> nodeTableNameSet = null, List <Tuple <string, string> > edgeNameTuples = null)
        {
            var        edgeIdentifiers   = EdgeColumn.MultiPartIdentifier.Identifiers;
            var        edgeColIdentifier = edgeIdentifiers.Last();
            Identifier srcNodeIdentifier = new Identifier {
                Value = nodeAlias
            };
            List <WScalarExpression> parameters = new List <WScalarExpression>();

            // The source is a physical node
            if (nodeTableNameSet == null)
            {
                // The edge is a physical edge
                if (edgeNameTuples == null)
                {
                    parameters.Add(new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(srcNodeIdentifier, edgeColIdentifier)
                    });
                    parameters.Add(new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(srcNodeIdentifier,
                                                     new Identifier {
                            Value = edgeColIdentifier.Value + "DeleteCol"
                        })
                    });
                }
                // The edge is an edge view
                else
                {
                    var sourceTableName = WNamedTableReference.SchemaNameToTuple(SourceNode.NodeTableObjectName).Item2;
                    foreach (var column in edgeNameTuples)
                    {
                        if (sourceTableName.Equals(column.Item1, StringComparison.OrdinalIgnoreCase))
                        {
                            Identifier includedEdgeColumnIdentifier = new Identifier {
                                Value = column.Item2
                            };
                            parameters.Add(new WColumnReferenceExpression
                            {
                                MultiPartIdentifier =
                                    new WMultiPartIdentifier(srcNodeIdentifier, includedEdgeColumnIdentifier)
                            });
                            parameters.Add(new WColumnReferenceExpression
                            {
                                MultiPartIdentifier =
                                    new WMultiPartIdentifier(srcNodeIdentifier,
                                                             new Identifier {
                                    Value = includedEdgeColumnIdentifier.Value + "DeleteCol"
                                })
                            });
                        }
                        else
                        {
                            parameters.Add(new WValueExpression {
                                Value = "null"
                            });
                            parameters.Add(new WValueExpression {
                                Value = "null"
                            });
                        }
                    }
                }
            }
            // The source is a node view
            else
            {
                // The edge is a physical edge
                if (edgeNameTuples == null)
                {
                    string     srcTableName = BindNodeTableObjName.BaseIdentifier.Value;
                    Identifier nodeViewEdgeColIdentifier = new Identifier
                    {
                        Value = srcTableName + "_" + edgeColIdentifier.Value
                    };
                    parameters.Add(new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(srcNodeIdentifier, nodeViewEdgeColIdentifier)
                    });
                    parameters.Add(new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(srcNodeIdentifier,
                                                     new Identifier {
                            Value = nodeViewEdgeColIdentifier.Value + "DeleteCol"
                        })
                    });
                }
                // The edge is an edge view
                else
                {
                    foreach (var column in edgeNameTuples)
                    {
                        if (nodeTableNameSet.Contains(column.Item1))
                        {
                            Identifier includedEdgeColumnIdentifier = new Identifier
                            {
                                Value = column.Item1 + "_" + column.Item2
                            };
                            parameters.Add(new WColumnReferenceExpression
                            {
                                MultiPartIdentifier =
                                    new WMultiPartIdentifier(srcNodeIdentifier, includedEdgeColumnIdentifier)
                            });
                            parameters.Add(new WColumnReferenceExpression
                            {
                                MultiPartIdentifier =
                                    new WMultiPartIdentifier(srcNodeIdentifier,
                                                             new Identifier {
                                    Value = includedEdgeColumnIdentifier.Value + "DeleteCol"
                                })
                            });
                        }
                        else
                        {
                            parameters.Add(new WValueExpression {
                                Value = "null"
                            });
                            parameters.Add(new WValueExpression {
                                Value = "null"
                            });
                        }
                    }
                }
            }
            // dumbNode == NULL -> pathFunction, no dumb alias
            if (dumbNode != null)
            {
                if (string.IsNullOrEmpty(dumbNode))
                {
                    parameters.Add(new WValueExpression {
                        Value = "0"
                    });
                }
                else
                {
                    parameters.Add(new WColumnReferenceExpression
                    {
                        MultiPartIdentifier = new WMultiPartIdentifier(
                            new Identifier {
                            Value = dumbNode
                        },
                            new Identifier {
                            Value = "GlobalNodeId"
                        }
                            ),
                    });
                }
            }

            return(parameters);
        }
Exemple #6
0
        /// <summary>
        /// Converts the edge to the table-valued function
        /// </summary>
        /// <param name="nodeAlias">Source node alias</param>
        /// <param name="dumbNode"></param>
        /// <param name="metaData">Meta data</param>
        /// <returns>A syntax tree node representing the table-valued function</returns>
        public override WSchemaObjectFunctionTableReference ToSchemaObjectFunction(string nodeAlias, string dumbNode, GraphMetaData metaData)
        {
            var edgeIdentifiers   = EdgeColumn.MultiPartIdentifier.Identifiers;
            var edgeColIdentifier = edgeIdentifiers.Last();
            HashSet <string> nodeSet;

            if (!metaData.NodeViewMapping.TryGetValue(
                    WNamedTableReference.SchemaNameToTuple(SourceNode.NodeTableObjectName), out nodeSet))
            {
                nodeSet = null;
            }
            var sourceNodeColumns =
                metaData.ColumnsOfNodeTables[WNamedTableReference.SchemaNameToTuple(BindNodeTableObjName)];
            var edgeInfo = sourceNodeColumns[edgeColIdentifier.Value].EdgeInfo;
            List <Tuple <string, string> > edgeTuples = edgeInfo.EdgeColumns;
            var parameters = ConstructEdgeTvfParameters(nodeAlias, null, nodeSet, edgeTuples);

            Identifier decoderFunction;

            if (ReferencePathInfo)
            {
                decoderFunction = new Identifier
                {
                    Value = BindNodeTableObjName.SchemaIdentifier.Value + '_' +
                            BindNodeTableObjName.BaseIdentifier.Value + '_' +
                            EdgeColumn.MultiPartIdentifier.Identifiers.Last().Value + '_' +
                            "bfsPathWithMessage"
                };
                // Node view
                if (nodeSet != null)
                {
                    parameters.Insert(0, new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(new Identifier()
                        {
                            Value = SourceNode.RefAlias
                        },
                                                     new Identifier()
                        {
                            Value = "_NodeId"
                        })
                    });
                    parameters.Insert(0, new WColumnReferenceExpression
                    {
                        MultiPartIdentifier =
                            new WMultiPartIdentifier(new Identifier()
                        {
                            Value = SourceNode.RefAlias
                        },
                                                     new Identifier()
                        {
                            Value = "_NodeType"
                        })
                    });
                }
                else
                {
                    string nodeIdName =
                        sourceNodeColumns.FirstOrDefault(e => e.Value.Role == WNodeTableColumnRole.NodeId).Key;
                    if (string.IsNullOrEmpty(nodeIdName))
                    {
                        parameters.Insert(0, new WValueExpression {
                            Value = "null"
                        });
                    }
                    else
                    {
                        parameters.Insert(0, new WColumnReferenceExpression
                        {
                            MultiPartIdentifier =
                                new WMultiPartIdentifier(new Identifier()
                            {
                                Value = SourceNode.RefAlias
                            },
                                                         new Identifier()
                            {
                                Value = nodeIdName
                            })
                        });
                    }
                    parameters.Insert(0,
                                      new WValueExpression {
                        Value = BindNodeTableObjName.BaseIdentifier.Value, SingleQuoted = true
                    });
                }
            }
            else
            {
                decoderFunction = new Identifier
                {
                    Value = BindNodeTableObjName.SchemaIdentifier.Value + '_' +
                            BindNodeTableObjName.BaseIdentifier.Value + '_' +
                            EdgeColumn.MultiPartIdentifier.Identifiers.Last().Value + '_' +
                            "bfsPath"
                };
            }
            parameters.Insert(0, new WValueExpression {
                Value = MaxLength.ToString()
            });
            parameters.Insert(0, new WValueExpression {
                Value = MinLength.ToString()
            });
            parameters.Insert(0,
                              new WColumnReferenceExpression
            {
                MultiPartIdentifier =
                    new WMultiPartIdentifier(new[] { new Identifier {
                                                         Value = nodeAlias
                                                     }, new Identifier {
                                                         Value = "GlobalNodeId"
                                                     }, })
            });
            var attributes = edgeInfo.ColumnAttributes;

            if (AttributeValueDict == null)
            {
                WValueExpression nullExpression = new WValueExpression {
                    Value = "null"
                };
                for (int i = 0; i < attributes.Count; i++)
                {
                    parameters.Add(nullExpression);
                }
            }
            else
            {
                foreach (var attribute in attributes)
                {
                    string value;
                    var    valueExpression = new WValueExpression
                    {
                        Value = AttributeValueDict.TryGetValue(attribute, out value) ? value : "null"
                    };

                    parameters.Add(valueExpression);
                }
            }
            return(new WSchemaObjectFunctionTableReference
            {
                SchemaObject = new WSchemaObjectName(
                    new Identifier {
                    Value = "dbo"
                },
                    decoderFunction),
                Parameters = parameters,
                Alias = new Identifier
                {
                    Value = EdgeAlias,
                }
            });
        }