Example #1
0
 public void ReplaceBrackets_IgnoresBracketsFollowedWithLettersOrNumbers()
 {
     Assert.Equal("[b]a", BracketLocator.ReplaceBrackets("[b]a", PostgresDialect.Instance));
     Assert.Equal("xyz [e]b", BracketLocator.ReplaceBrackets("xyz [e]b", PostgresDialect.Instance));
     Assert.Equal("[f]0 abc", BracketLocator.ReplaceBrackets("[f]0 abc", PostgresDialect.Instance));
     Assert.Equal("u [x]k w [j]9 [r]_", BracketLocator.ReplaceBrackets("u [x]k w [j]9 [r]_", PostgresDialect.Instance));
 }
Example #2
0
 public void ReplaceBrackets_IgnoresEmptyBrackets()
 {
     Assert.Equal("[]", BracketLocator.ReplaceBrackets("[]", PostgresDialect.Instance));
     Assert.Equal("xyz []", BracketLocator.ReplaceBrackets("xyz []", PostgresDialect.Instance));
     Assert.Equal("[] abc", BracketLocator.ReplaceBrackets("[] abc", PostgresDialect.Instance));
     Assert.Equal("u [] w [] []", BracketLocator.ReplaceBrackets("u [] w [] []", PostgresDialect.Instance));
 }
Example #3
0
 public void ReplaceBrackets_IgnoresBracketsPrecededWithLettersOrNumbers()
 {
     Assert.Equal("a[b]", BracketLocator.ReplaceBrackets("a[b]", PostgresDialect.Instance));
     Assert.Equal("xyz b[e]", BracketLocator.ReplaceBrackets("xyz b[e]", PostgresDialect.Instance));
     Assert.Equal("0[f] abc", BracketLocator.ReplaceBrackets("0[f] abc", PostgresDialect.Instance));
     Assert.Equal("u k[x] w 9[j] _[r]", BracketLocator.ReplaceBrackets("u k[x] w 9[j] _[r]", PostgresDialect.Instance));
 }
Example #4
0
        public void ReplaceBracketContents_KeepsUnchangedContents()
        {
            int calls  = 0;
            var result = BracketLocator.ReplaceBracketContents("[a][b][cd]", '!', s =>
            {
                calls++;
                return(s);
            });

            Assert.Equal("[a][b][cd]", result);
            Assert.Equal(3, calls);
        }
Example #5
0
        public void ReplaceBracketContents_IgnoresEmptyBrackets()
        {
            bool isCalled = false;
            var  result   = BracketLocator.ReplaceBracketContents("[]", '!', s =>
            {
                isCalled = true;
                return(null);
            });

            Assert.Equal("[]", result);
            Assert.False(isCalled);
        }
Example #6
0
        public void ReplaceBracketContents_IgnoresBracketsInsideQuotes()
        {
            bool isCalled = false;
            var  result   = BracketLocator.ReplaceBracketContents("'[a]'bc'd[e]'", '!', s =>
            {
                isCalled = true;
                return(null);
            });

            Assert.Equal("'[a]'bc'd[e]'", result);
            Assert.False(isCalled);
        }
Example #7
0
        public void ReplaceBracketContents_HandlesSimpleBrackets()
        {
            int calls  = 0;
            var result = BracketLocator.ReplaceBracketContents("[a]", '!', s =>
            {
                Assert.Equal("a", s);
                calls++;
                return("bcd");
            });

            Assert.Equal("[bcd]", result);
            Assert.Equal(1, calls);
        }
 public XamlDebuggerXmlReader(XamlReader underlyingReader, IXamlLineInfo xamlLineInfo, TextReader textReader)
 {
     this.underlyingReader = new XamlValidatingReader(underlyingReader, xamlLineInfo);
     this.xsc = underlyingReader.SchemaContext;
     this.attachingXamlTypeName = new XamlType(attachingTypeName, this.xsc);
     this.records            = new Stack <System.Activities.Debugger.XamlNode>();
     this.lineInfoStateStack = new Stack <System.Activities.Debugger.XamlNode>();
     this.state       = InReaderState.Instance;
     this.hasLineInfo = (xamlLineInfo != null) && xamlLineInfo.HasLineInfo;
     if (((xamlLineInfo != null) && xamlLineInfo.HasLineInfo) && (textReader != null))
     {
         this.bracketLocator = new BracketLocator(textReader);
     }
 }
Example #9
0
        public static string Replace(string expression)
        {
            if (expression == null || expression.IndexOf('^') < 0)
            {
                return(expression);
            }

            return(BracketLocator.ReplaceBracketContents(expression, '^', contents =>
            {
                var idx = contents.IndexOf('^');
                if (idx < 0)
                {
                    return contents;
                }

                string connectionKey = null;

                if (idx != 0)
                {
                    connectionKey = contents.Substring(0, idx);
                }

                string databaseName;

                if (!connectionKey.IsEmptyOrNull())
                {
                    databaseName = SqlConnections.GetDatabaseName(connectionKey);
                    if (!string.IsNullOrEmpty(databaseName))
                    {
                        return databaseName;
                    }
                }

                if (idx < contents.Length - 1)
                {
                    return contents.Substring(idx + 1);
                }

                return contents;
            }));
        }
Example #10
0
        public bool ActivateFor(IRow row)
        {
            var attrs = row.GetType().GetCustomAttributes <UpdatableExtensionAttribute>();

            if (attrs == null || !attrs.Any())
            {
                return(false);
            }

            var sourceByExpression = row.GetFields().ToLookup(x =>
                                                              BracketLocator.ReplaceBrackets(x.Expression.TrimToEmpty(), BracketRemoverDialect.Instance));

            infoList = attrs.Select(attr =>
            {
                var info = new RelationInfo
                {
                    Attr = attr
                };

                var rowType = attr.RowType;
                if (rowType.IsAbstract ||
                    !typeof(IRow).IsAssignableFrom(rowType) ||
                    rowType.IsInterface)
                {
                    throw new ArgumentException(string.Format(
                                                    "Row type '{1}' has an ExtensionRelation attribute " +
                                                    "but its specified extension row type '{0}' is not a valid row class!",
                                                    rowType.FullName,
                                                    row.GetType().FullName));
                }

                info.RowFactory = () => (IRow)Activator.CreateInstance(rowType);

                var thisKey = attr.ThisKey;
                if (string.IsNullOrEmpty(thisKey))
                {
                    if (!(row is IIdRow))
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{0}' has an ExtensionRelation attribute " +
                                                        "but its ThisKey is not specified!",
                                                        row.GetType().FullName));
                    }

                    info.ThisKeyField = row.IdField;
                }
                else
                {
                    info.ThisKeyField = row.FindFieldByPropertyName(attr.ThisKey) ??
                                        row.FindField(attr.ThisKey);
                    if (info.ThisKeyField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified for an ExtensionRelation attribute",
                                                                  attr.ThisKey,
                                                                  row.GetType().FullName));
                    }
                }

                var ext = info.RowFactory();

                var otherKey = attr.OtherKey;
                if (string.IsNullOrEmpty(otherKey))
                {
                    info.OtherKeyField = ext.FindField(info.ThisKeyField.Name);

                    if (info.OtherKeyField is null && ext is IIdRow)
                    {
                        info.OtherKeyField = row.IdField;
                    }

                    if (info.OtherKeyField is null)
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{1}' has an ExtensionRelation attribute " +
                                                        "but its OtherKey is not specified!",
                                                        row.GetType().FullName));
                    }
                }
                else
                {
                    info.OtherKeyField = ext.FindFieldByPropertyName(attr.OtherKey) ?? ext.FindField(attr.OtherKey);
                    if (info.OtherKeyField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified for an ExtensionRelation attribute on '{2}'",
                                                                  attr.OtherKey,
                                                                  ext.GetType().FullName,
                                                                  row.GetType().FullName));
                    }
                }

                if (!string.IsNullOrEmpty(attr.FilterField))
                {
                    info.FilterField = ext.FindFieldByPropertyName(attr.FilterField) ?? ext.FindField(attr.FilterField);
                    if (info.FilterField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified as FilterField for an ExtensionRelation attribute on '{2}'",
                                                                  attr.OtherKey,
                                                                  ext.GetType().FullName,
                                                                  row.GetType().FullName));
                    }

                    info.FilterValue = info.FilterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture);
                }

                if (!string.IsNullOrEmpty(attr.PresenceField))
                {
                    info.PresenceField = row.FindFieldByPropertyName(attr.PresenceField) ?? row.FindField(attr.PresenceField);
                    if (info.PresenceField is null)
                    {
                        throw new ArgumentException(string.Format("Field '{0}' doesn't exist in row of type '{1}'." +
                                                                  "This field is specified as PresenceField as an ExtensionRelation attribute.",
                                                                  attr.PresenceField,
                                                                  row.GetType().FullName));
                    }

                    info.PresenceValue = attr.PresenceValue;
                }

                var extFields   = ext.GetFields();
                var alias       = attr.Alias;
                var aliasPrefix = attr.Alias + "_";

                var joinByKey = new HashSet <string>(extFields.Joins.Keys, StringComparer.OrdinalIgnoreCase);

                string mapAlias(string x)
                {
                    if (x == "t0" || x == "T0")
                    {
                        return(alias);
                    }

                    if (!joinByKey.Contains(x))
                    {
                        return(x);
                    }

                    return(aliasPrefix + x);
                }

                string mapExpression(string x)
                {
                    if (x == null)
                    {
                        return(null);
                    }

                    return(JoinAliasLocator.ReplaceAliases(x, mapAlias));
                }

                info.Mappings = new List <Tuple <Field, Field> >();
                foreach (var field in extFields)
                {
                    if (ReferenceEquals(info.OtherKeyField, field))
                    {
                        continue;
                    }

                    if (ReferenceEquals(info.FilterField, field))
                    {
                        continue;
                    }

                    var expression = field.Expression.TrimToEmpty();

                    if (string.IsNullOrEmpty(expression))
                    {
                        continue;
                    }

                    expression = mapExpression(expression);
                    expression = BracketLocator.ReplaceBrackets(expression,
                                                                BracketRemoverDialect.Instance);

                    var match = sourceByExpression[expression].FirstOrDefault();
                    if (match is null)
                    {
                        continue;
                    }

                    if (match.IsTableField())
                    {
                        continue;
                    }

                    if (ReferenceEquals(info.ThisKeyField, match))
                    {
                        continue;
                    }

                    if (field.GetType() != match.GetType())
                    {
                        throw new ArgumentException(string.Format(
                                                        "Row type '{0}' has an ExtensionRelation attribute to '{1}'." +
                                                        "Their '{2}' and '{3}' fields are matched but they have different types ({4} and {5})!",
                                                        row.GetType().FullName,
                                                        ext.GetType().FullName,
                                                        field.PropertyName ?? field.Name,
                                                        match.PropertyName ?? match.Name,
                                                        field.GetType().Name,
                                                        match.GetType().Name));
                    }

                    info.Mappings.Add(new Tuple <Field, Field>(match, field));
                }

                if (info.Mappings.Count == 0)
                {
                    throw new ArgumentException(string.Format(
                                                    "Row type '{0}' has an ExtensionRelation attribute " +
                                                    "but no view fields could be matched to extension row '{1}'!",
                                                    row.GetType().FullName,
                                                    ext.GetType().FullName));
                }

                return(info);
            }).ToList();

            return(true);
        }
Example #11
0
 public void ReplaceBrackets_ReplacesIdentifiersWithSpaces()
 {
     Assert.Equal("\"Order Details\"", BracketLocator.ReplaceBrackets("[Order Details]", PostgresDialect.Instance));
     Assert.Equal("SELECT c.\"Some Field\" from Customers c",
                  BracketLocator.ReplaceBrackets("SELECT c.[Some Field] from Customers c", PostgresDialect.Instance));
 }
Example #12
0
 public void ReplaceBrackets_ReplacesSimpleIdentifiers()
 {
     Assert.Equal("\"a\"", BracketLocator.ReplaceBrackets("[a]", PostgresDialect.Instance));
     Assert.Equal("x.\"y\" z.d", BracketLocator.ReplaceBrackets("x.[y] z.d", PostgresDialect.Instance));
 }
Example #13
0
 public void ReplaceBrackets_IgnoresBracketsWithNumericContents()
 {
     Assert.Equal("[0]", BracketLocator.ReplaceBrackets("[0]", PostgresDialect.Instance));
     Assert.Equal("[77][89]", BracketLocator.ReplaceBrackets("[77][89]", PostgresDialect.Instance));
 }
Example #14
0
 public void ReplaceBrackets_IgnoresBracketsInStrings()
 {
     Assert.Equal("'[b]'", BracketLocator.ReplaceBrackets("'[b]'", PostgresDialect.Instance));
     Assert.Equal("'sfg [c] ab [x]'", BracketLocator.ReplaceBrackets("'sfg [c] ab [x]'", PostgresDialect.Instance));
 }