private static void WriteSqlQuery(IEnumerable<SqlQuery> sqlQueries, IAssociativeXmlObject parentAssociativeXmlObject, Type connectionType, string connectionString, bool getSchemaOnly) { ArrayConstruct arrayConstruct; ObjectConstruct objectConstruct; PropertyConstruct propertyConstructA, propertyConstructB; Tokenizer tokenizer; IEnumerable<IDictionary<string, object>> records; string commandText; int count = 0; if ((object)sqlQueries == null) throw new ArgumentNullException(nameof(sqlQueries)); if ((object)parentAssociativeXmlObject == null) throw new ArgumentNullException(nameof(parentAssociativeXmlObject)); if ((object)connectionType == null) throw new ArgumentNullException(nameof(connectionType)); if ((object)connectionString == null) throw new ArgumentNullException(nameof(connectionString)); if (SolderLegacyInstanceAccessor.DataTypeFascadeLegacyInstance.IsWhiteSpace(connectionString)) throw new ArgumentOutOfRangeException(nameof(connectionString)); tokenizer = new Tokenizer(true); foreach (SqlQuery sqlQuery in sqlQueries.OrderBy(c => c.Key).ThenBy(c => c.Order)) { arrayConstruct = new ArrayConstruct(); arrayConstruct.Name = sqlQuery.Key; parentAssociativeXmlObject.Items.Add(arrayConstruct); commandText = tokenizer.ExpandTokens(sqlQuery.Text, new DynamicWildcardTokenReplacementStrategy(new object[] { parentAssociativeXmlObject })); records = DatazoidLegacyInstanceAccessor.AdoNetBufferingLegacyInstance.ExecuteRecords(getSchemaOnly, connectionType, connectionString, false, IsolationLevel.Unspecified, sqlQuery.Type, commandText, null); propertyConstructA = new PropertyConstruct(); propertyConstructA.Name = "RowCount"; arrayConstruct.Items.Add(propertyConstructA); if ((object)records != null) { foreach (IDictionary<string, object> record in records) { objectConstruct = new ObjectConstruct(); arrayConstruct.Items.Add(objectConstruct); if ((object)record != null) { foreach (KeyValuePair<string, object> keyValuePair in record) { propertyConstructB = new PropertyConstruct(); propertyConstructB.Name = keyValuePair.Key; propertyConstructB.RawValue = keyValuePair.Value; objectConstruct.Items.Add(propertyConstructB); } } // correlated WriteSqlQuery(sqlQuery.SubQueries, objectConstruct, connectionType, connectionString, getSchemaOnly); count++; } propertyConstructA.RawValue = count; } } }
public void ShouldExpandTokensLooseMatchingTest() { Tokenizer tokenizer; MockFactory mockFactory; IDictionary<string, ITokenReplacementStrategy> mockTokenReplacementStrategies; ITokenReplacementStrategy mockTokenReplacementStrategy; ITokenReplacementStrategy _unusedTokenReplacementStrategy = null; string _unusedString = null; string[] _unusedStrings = null; string tokenizedValue; string expandedValue; string expectedValue; mockFactory = new MockFactory(); mockTokenReplacementStrategies = mockFactory.CreateInstance<IDictionary<string, ITokenReplacementStrategy>>(); mockTokenReplacementStrategy = mockFactory.CreateInstance<ITokenReplacementStrategy>(); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myValueSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).WillReturn("testValue"); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myFunctionSemanticToken0"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(new string[] { })).WillReturn("testValue"); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myFunctionSemanticToken1"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(new string[] { "a", })).WillReturn("testValue"); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myFunctionSemanticToken2"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(new string[] { "a", "b" })).WillReturn("testValue"); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myUnkSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", null), Return.Value(false)); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("myErrSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).Will(Throw.Exception(new Exception())); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("a"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).WillReturn(string.Empty); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("b"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).WillReturn(string.Empty); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("c"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).WillReturn(string.Empty); Expect.On(mockTokenReplacementStrategies).One.Method(x => x.TryGetValue(_unusedString, out _unusedTokenReplacementStrategy)).With(new EqualMatcher("d"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), new AlwaysMatcher(true, string.Empty))).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true)); Expect.On(mockTokenReplacementStrategy).One.Method(x => x.Evaluate(_unusedStrings)).With(new EqualMatcher(null)).Will(Throw.Exception(new Exception())); tokenizer = new Tokenizer(mockTokenReplacementStrategies, false); tokenizedValue = string.Empty; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = string.Empty; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...{myNoSemanticToken}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...{myNoSemanticToken}..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myValueSemanticToken}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...testValue..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myFunctionSemanticToken0()}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...testValue..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myFunctionSemanticToken1(`a`)}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...testValue..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myFunctionSemanticToken2(`a`, `b`)}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...testValue..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myUnkSemanticToken}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...${myUnkSemanticToken}..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${myErrSemanticToken}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "...${myErrSemanticToken}..."; Assert.AreEqual(expectedValue, expandedValue); tokenizedValue = "...${a}...${c}...${b}...${d}..."; expandedValue = tokenizer.ExpandTokens(tokenizedValue); expectedValue = "............${d}..."; Assert.AreEqual(expectedValue, expandedValue); Assert.IsNotNull(tokenizer.OrderedPreviousExpansionTokens); Assert.AreEqual("a,b,c,d", string.Join(",", tokenizer.OrderedPreviousExpansionTokens)); mockFactory.VerifyAllExpectationsHaveBeenMet(); }