public void TestException()
        {
            Assert.Throws<ArgumentNullException>(() => new ObjectIdentifier((uint[])null));
            Assert.Throws<ArgumentException>(() => new ObjectIdentifier(new uint[] {1}));
            Assert.Throws<ArgumentException>(() => new ObjectIdentifier(new uint[] {5, 8}));
            Assert.Throws<ArgumentException>(() => new ObjectIdentifier(new uint[] {1, 80}));
            Assert.Throws<ArgumentNullException>(() => new ObjectIdentifier(new Tuple<int, byte[]>(0, new byte[] { 0 }), null));
            Assert.Throws<ArgumentException>(() => new ObjectIdentifier(new Tuple<int, byte[]>(0, new byte[] { 0 }), new MemoryStream()));
// ReSharper disable RedundantCast
            Assert.Throws<ArgumentNullException>(() => new ObjectIdentifier(new uint[] {1, 3}).CompareTo((ObjectIdentifier)null));
// ReSharper restore RedundantCast
            Assert.Throws<ArgumentNullException>(() => ObjectIdentifier.Convert((uint[])null));
            Assert.Throws<ArgumentNullException>(() => ObjectIdentifier.Convert((string)null));
            var oid = new ObjectIdentifier(new uint[] { 1, 3 });
            Assert.Throws<ArgumentException>(() => oid.CompareTo(80));
            Assert.Throws<ArgumentNullException>(() => ObjectIdentifier.Create(null, 0));
            Assert.Throws<ArgumentNullException>(() => oid.AppendBytesTo(null));
        }
        public void TestException()
        {
            Assert.Throws <ArgumentNullException>(() => new ObjectIdentifier((uint[])null));
            Assert.Throws <ArgumentException>(() => new ObjectIdentifier(new uint[] { 1 }));
            Assert.Throws <ArgumentException>(() => new ObjectIdentifier(new uint[] { 5, 8 }));
            Assert.Throws <ArgumentException>(() => new ObjectIdentifier(new uint[] { 1, 80 }));
            Assert.Throws <ArgumentNullException>(() => new ObjectIdentifier(new Tuple <int, byte[]>(0, new byte[] { 0 }), null));
            Assert.Throws <ArgumentException>(() => new ObjectIdentifier(new Tuple <int, byte[]>(0, new byte[] { 0 }), new MemoryStream()));
// ReSharper disable RedundantCast
            Assert.Throws <ArgumentNullException>(() => new ObjectIdentifier(new uint[] { 1, 3 }).CompareTo((ObjectIdentifier)null));
// ReSharper restore RedundantCast
            Assert.Throws <ArgumentNullException>(() => ObjectIdentifier.Convert((uint[])null));
            Assert.Throws <ArgumentNullException>(() => ObjectIdentifier.Convert((string)null));
            var oid = new ObjectIdentifier(new uint[] { 1, 3 });

            Assert.Throws <ArgumentException>(() => oid.CompareTo(80));
            Assert.Throws <ArgumentNullException>(() => ObjectIdentifier.Create(null, 0));
            Assert.Throws <ArgumentNullException>(() => oid.AppendBytesTo(null));
        }
Пример #3
0
        public static IList <JoinInfo> GetFromClauseJoinTables(this FromClause from)
        {
            if (from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            var joins = new List <JoinInfo>();

            if (from.TableReferences.Count == 0 || from.TableReferences.First().GetType() != typeof(QualifiedJoin))
            {
                return(joins);
            }

            var joinVisitor = new JoinVisitor();

            from.Accept(joinVisitor);

            //build the list of pure tables along with the list of boolean comparisons
            foreach (var join in joinVisitor.QualifiedJoins)
            {
                var joinInfo = new JoinInfo {
                };

                var boolVisitor = new BooleanComparisonVisitor();
                join.SearchCondition.Accept(boolVisitor);
                joinInfo.Compares = new List <BooleanComparisonExpression>(boolVisitor.Statements);

                if (join.FirstTableReference.GetType() == typeof(NamedTableReference))
                {
                    joinInfo.Table1 = join.FirstTableReference as NamedTableReference;
                }
                if (join.SecondTableReference.GetType() == typeof(NamedTableReference))
                {
                    joinInfo.Table2 = join.SecondTableReference as NamedTableReference;
                }
                joins.Add(joinInfo);
            }

            //table2 should always have a table..... maybe. unless the table is actually a sub-select. Then we will ignore it
            foreach (var join in joins.Where(j => j.Table2 != null))
            {
                var table1      = join.Table1;
                var table2      = join.Table2;
                var table2Alias = table2.Alias?.Value;
                var table2Name  = new ObjectIdentifier(table2.SchemaObject.Identifiers.Select(x => x.Value));

                //we need to figure out which side of the comparison goes to which table..... PITA. yes.....
                foreach (var compare in join.Compares
                         .Where(x => x.FirstExpression is ColumnReferenceExpression && x.SecondExpression is ColumnReferenceExpression))
                {
                    //we use a loop as we need to check both the first expression and the second expression to see which table the columns belong to
                    for (int i = 0; i < 2; i++)
                    {
                        var col        = (i == 0 ? compare.FirstExpression : compare.SecondExpression) as ColumnReferenceExpression;
                        var colTblName = GetTableOrAliasName(col.MultiPartIdentifier.Identifiers);
                        if (table2Alias.StringEquals(colTblName.First()) || table2Name.CompareTo(colTblName) >= 5)
                        {
                            join.Table2JoinColumns.Add(col);
                            continue;
                        }

                        //use table1 if it was supplied in the compare. else scan the joins to find the matching table to the column
                        var tbl = table1 ?? joins.Select(x =>
                        {
                            if (CheckName(x.Table2, col))
                            {
                                return(x.Table2);
                            }
                            if (CheckName(x.Table1, col))
                            {
                                return(x.Table1);
                            }
                            return(null);
                        }).FirstOrDefault(x => x != null);

                        if (tbl != null)
                        {
                            var tblAlias = tbl.Alias?.Value;
                            var tblName  = new ObjectIdentifier(tbl.SchemaObject.Identifiers.Select(x => x.Value));

                            if (join.Table1 == null)
                            {
                                join.Table1 = tbl;
                            }
                            if (tblAlias.StringEquals(colTblName.First()) || tblName.CompareTo(colTblName) >= 5)
                            {
                                join.Table1JoinColumns.Add(col);
                            }
                        }
                    }
                }
            }

            return(joins);
        }