/// <summary> /// convert statements in annotation to according graph data modification statements /// </summary> /// <param name="script">SQL script</param> /// <param name="annotations">stores information of data modification statements</param> /// <returns>Converted script</returns> private void ConvertToGraphModificationStatement(IList <WSqlStatement> statements) { for (var j = 0; j < statements.Count; ++j) { var stmt = statements[j]; if (_nodeCount >= _annotations.Count) { break; } if (stmt is WSelectQueryBlock && stmt.FirstTokenIndex == _annotations[_nodeCount].Position) { var newStmt = stmt as WSelectQueryBlock; var annotation = _annotations[_nodeCount] as DeleteEdgeAnnotation; if (annotation != null) { newStmt.MatchClause = new WMatchClause { Paths = new List <WMatchPath> { annotation.Path } } } ; statements[j] = new WDeleteEdgeSpecification(newStmt); _nodeCount++; } if (!(stmt is WInsertSpecification) && !(stmt is WDeleteSpecification)) { continue; } if (stmt.LastTokenIndex < _annotations[_nodeCount].Position) { continue; } if (_annotations[_nodeCount] is InsertNodeAnnotation) { statements[j] = new WInsertNodeSpecification(stmt as WInsertSpecification); } else if (_annotations[_nodeCount] is InsertEdgeAnnotation) { var annotation = _annotations[_nodeCount] as InsertEdgeAnnotation; statements[j] = new WInsertEdgeSpecification(stmt as WInsertSpecification) { EdgeColumn = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(annotation.EdgeColumn) }, }; } else if (_annotations[_nodeCount] is DeleteNodeAnnotation) { statements[j] = new WDeleteNodeSpecification(stmt as WDeleteSpecification); } _nodeCount++; } } }
public virtual void Visit(WDeleteNodeSpecification node) { node.AcceptChildren(this); }
private void TranslateNodeDelete(WDeleteNodeSpecification node, List<WSqlStatement> res) { var table = node.Target as WNamedTableReference; if (table == null) throw new GraphViewException("Target of DELETE NODE statement should be a named table reference."); WBooleanExpression cond = new WBooleanComparisonExpression { ComparisonType = BooleanComparisonType.Equals, FirstExpr = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(new Identifier { Value = "InDegree" }) }, SecondExpr = new WValueExpression { Value = "0" } }; var tableSchema = table.TableObjectName.SchemaIdentifier == null ? "dbo" : table.TableObjectName.SchemaIdentifier.Value; var tableName = table.TableObjectName.BaseIdentifier.Value; using (var command = Tx.Connection.CreateCommand()) { command.Transaction = Tx; command.CommandText = string.Format( @"SELECT [TableSchema], [TableName], [ColumnName] FROM [{3}] WHERE TableSchema = '{0}' and [TableName]='{1}' and ColumnRole={2}", tableSchema, tableName, 1, GraphViewConnection.MetadataTables[1]); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var col = reader["ColumnName"].ToString(); cond = WBooleanBinaryExpression.Conjunction(cond, new WBooleanComparisonExpression { ComparisonType = BooleanComparisonType.Equals, FirstExpr = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(new Identifier { Value = col + "OutDegree" }) }, SecondExpr = new WValueExpression { Value = "0" } }); } } } WWhereClause checkDeleteNode = new WWhereClause { SearchCondition = new WBooleanNotExpression { Expression = new WBooleanParenthesisExpression { Expression = cond } } }; if (node.WhereClause == null || node.WhereClause.SearchCondition == null) { node.WhereClause = new WWhereClause { SearchCondition = cond }; } else { checkDeleteNode = new WWhereClause { SearchCondition = new WBooleanBinaryExpression { BooleanExpressionType = BooleanBinaryExpressionType.And, FirstExpr = node.WhereClause.SearchCondition, SecondExpr = checkDeleteNode.SearchCondition } }; node.WhereClause = new WWhereClause { SearchCondition = new WBooleanBinaryExpression { BooleanExpressionType = BooleanBinaryExpressionType.And, FirstExpr = node.WhereClause.SearchCondition, SecondExpr = cond } }; } using (var command = Tx.Connection.CreateCommand()) { command.Transaction = Tx; command.CommandText = string.Format("SELECT InDegree FROM {0}.{1} {2}", tableSchema, tableName, checkDeleteNode.ToString()); using (var reader = command.ExecuteReader()) { while (reader.Read()) { throw new GraphViewException( string.Format( "Node(s) of node table {0} being deleted still has/have ingoing or outdoing edge(s)", tableName)); } } } res.Add(new WDeleteSpecification { FromClause = node.FromClause, Target = node.Target, TopRowFilter = node.TopRowFilter, WhereClause = node.WhereClause, }); }
/// <summary> /// convert statements in annotation to according graph data modification statements /// </summary> /// <param name="script">SQL script</param> /// <param name="annotations">stores information of data modification statements</param> /// <returns>Converted script</returns> private void ConvertToGraphModificationStatement(IList<WSqlStatement> statements) { for (var j = 0; j < statements.Count; ++j) { var stmt = statements[j]; if (_nodeCount >= _annotations.Count) break; if (stmt is WSelectStatement && stmt.FirstTokenIndex == _annotations[_nodeCount].Position) { var newStmt = (stmt as WSelectStatement).QueryExpr as WSelectQueryBlock; var annotation = _annotations[_nodeCount] as DeleteEdgeAnnotation; if (annotation != null) newStmt.MatchClause = new WMatchClause { Paths = new List<WMatchPath> { annotation.Path } }; statements[j] = new WDeleteEdgeSpecification(newStmt); _nodeCount++; } if (!(stmt is WInsertSpecification) && !(stmt is WDeleteSpecification)) continue; if (stmt.LastTokenIndex < _annotations[_nodeCount].Position) { continue; } if (_annotations[_nodeCount] is InsertNodeAnnotation) { statements[j] = new WInsertNodeSpecification(stmt as WInsertSpecification); } else if (_annotations[_nodeCount] is InsertEdgeAnnotation) { var annotation = _annotations[_nodeCount] as InsertEdgeAnnotation; statements[j] = new WInsertEdgeSpecification(stmt as WInsertSpecification) { EdgeColumn = new WColumnReferenceExpression { MultiPartIdentifier = new WMultiPartIdentifier(annotation.EdgeColumn) }, }; } else if (_annotations[_nodeCount] is DeleteNodeAnnotation) { statements[j] = new WDeleteNodeSpecification(stmt as WDeleteSpecification); } _nodeCount++; } }