Exemple #1
0
        private void InsertReplace(DmlInsertReplaceStatement node)
        {
            var table   = node.Table;
            var collist = node.ColumnNameList;
            var query   = node.Select;
            var rows    = node.RowList;

            TableAsTableFactor(table);

            var tableName = table.IdTextUpUnescape;

            VisitChild(2, false, false, collist);
            if (query != null)
            {
                query.Accept(this);
                return;
            }

            foreach (var row in rows)
            {
                VisitChild(2, false, false, row);
            }

            var colVals      = EnsureColumnValueByTable(tableName);
            var colValsIndex = EnsureColumnValueIndexByTable(tableName);

            if (collist == null)
            {
                return;
            }

            for (var i = 0; i < collist.Count; ++i)
            {
                var colName = collist[i].IdTextUpUnescape;
                if (IsRuledColumn(tableName, colName))
                {
                    var valueList = EnsureColumnValueList(colVals, colName);
                    var valMap    = EnsureColumnValueIndexObjMap(colValsIndex, colName);
                    foreach (var row_1 in rows)
                    {
                        var expr  = row_1.RowExprList[i];
                        var value = expr == null ? null : expr.Evaluation(evaluationParameter);
                        if (value != ExpressionConstants.Unevaluatable)
                        {
                            valueList.Add(value);
                            AddIntoColumnValueIndex(valMap, value, row_1, node);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static void DispatchInsertReplace(RouteResultsetNode[] rn,
                                                  DmlInsertReplaceStatement stmt,
                                                  IList <string> ruleColumns,
                                                  IDictionary <int, IList <object[]> > dataNodeMap,
                                                  TableConfig matchedTable,
                                                  string originalSql,
                                                  PartitionKeyVisitor visitor)
        {
            if (stmt.Select != null)
            {
                DispatchWhereBasedStmt(rn, stmt, ruleColumns, dataNodeMap, matchedTable, originalSql, visitor);
                return;
            }

            var colsIndex = visitor.GetColumnIndex(stmt.Table.IdTextUpUnescape);

            if (colsIndex == null || colsIndex.IsEmpty())
            {
                throw new ArgumentException("columns index is empty: " + originalSql);
            }

            var colsIndexList = new List <ColumnValueType>(ruleColumns.Count);

            for (int i = 0, len = ruleColumns.Count; i < len; ++i)
            {
                colsIndexList.Add(colsIndex[ruleColumns[i]]);
            }

            var dataNodeId = -1;

            foreach (var en in dataNodeMap)
            {
                var tuples         = en.Value;
                var replaceRowList = new HashSet <RowExpression>();
                foreach (var tuple in tuples)
                {
                    ICollection <Pair <IExpression, IAstNode> > tupleExprs = null;
                    for (var i1 = 0; i1 < tuple.Length; ++i1)
                    {
                        var valueMap = colsIndexList[i1];
                        var value    = tuple[i1];
                        var set      = GetExpressionSet(valueMap, value);
                        tupleExprs = CollectionUtil.IntersectSet(tupleExprs, set);
                    }

                    if (tupleExprs == null || tupleExprs.IsEmpty())
                    {
                        throw new ArgumentException(
                                  string.Format("route: empty expression list for insertReplace stmt: {0}", originalSql));
                    }

                    foreach (var p in tupleExprs)
                    {
                        if (p.Value == stmt && p.Key is RowExpression)
                        {
                            replaceRowList.Add((RowExpression)p.Key);
                        }
                    }
                }

                stmt.ReplaceRowList = new List <RowExpression>(replaceRowList);
                var sql = GenSql(stmt, originalSql);
                stmt.ClearReplaceRowList();

                var dataNodeName = matchedTable.DataNodes[en.Key];
                rn[++dataNodeId] = new RouteResultsetNode(dataNodeName, sql);
            }
        }