public void NotSoSimpleWithDelimiters()
		{
			string s = "The lazy... I don't know ";
			StringTokenizer st = new StringTokenizer(s, " .", true);
			IEnumerator enumerator = st.GetEnumerator();
			enumerator.MoveNext();
			Assert.AreEqual("The", (string) enumerator.Current, "Can't get first token");
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get first space");
			enumerator.MoveNext();
			Assert.AreEqual("lazy", (string) enumerator.Current, "Can't get second token");
			enumerator.MoveNext();
			Assert.AreEqual(".", (string) enumerator.Current, "Can't get first dot");
			enumerator.MoveNext();
			Assert.AreEqual(".", (string) enumerator.Current, "Can't get second dot");
			enumerator.MoveNext();
			Assert.AreEqual(".", (string) enumerator.Current, "Can't get third dot");
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get second space");
			enumerator.MoveNext();
			Assert.AreEqual("I", (string) enumerator.Current, "Can't get third token");
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get third space");
			enumerator.MoveNext();
			Assert.AreEqual("don't", (string) enumerator.Current, "Can't get fourth token");
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get fourth space");
			enumerator.MoveNext();
			Assert.AreEqual("know", (string) enumerator.Current, "Can't get fifth token");
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get last space");
			Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens");
		}
 protected static void BindIndex(XmlAttribute indexAttribute, Table table, Column column)
 {
     if (indexAttribute != null && table != null)
     {
         StringTokenizer tokens = new StringTokenizer(indexAttribute.Value, ", ");
         foreach (string token in tokens)
             table.GetOrCreateIndex(token).AddColumn(column);
     }
 }
		public static void Parse(IParser p, string text, string seperators, QueryTranslator q)
		{
			StringTokenizer tokens = new StringTokenizer(text, seperators, true);
			p.Start(q);
			foreach (string token in tokens)
			{
				p.Token(token, q);
			}
			p.End(q);
		}
		public void SimpleStringWithoutDelimiters()
		{
			string s = "Hello world!";
			StringTokenizer st = new StringTokenizer(s);
			IEnumerator enumerator = st.GetEnumerator();
			enumerator.MoveNext();
			Assert.AreEqual("Hello", (string) enumerator.Current, "Can't get first token");
			enumerator.MoveNext();
			Assert.AreEqual("world!", (string) enumerator.Current, "Can't get second token");
			Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens");
		}
Example #5
0
        private static string Format(string sql)
        {
            if (sql.IndexOf("\"") > 0 || sql.IndexOf("'") > 0)
            {
                return sql;
            }

            string formatted;

            if (sql.ToLower(System.Globalization.CultureInfo.InvariantCulture).StartsWith("create table"))
            {
                StringBuilder result = new StringBuilder(60);
                StringTokenizer tokens = new StringTokenizer(sql, "(,)", true);

                int depth = 0;

                foreach (string tok in tokens)
                {
                    if (StringHelper.ClosedParen.Equals(tok))
                    {
                        depth--;
                        if (depth == 0)
                        {
                            result.AppendLine();
                        }
                    }
                    result.Append(tok);
                    if (StringHelper.Comma.Equals(tok) && depth == 1)
                    {
                        result.AppendLine().Append(' ');
                    }
                    if (StringHelper.OpenParen.Equals(tok))
                    {
                        depth++;
                        if (depth == 1)
                        {
                            result.AppendLine().Append(' ');
                        }
                    }
                }

                formatted = result.ToString();
            }
            else
            {
                formatted = sql;
            }

            return formatted;
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="property"></param>
		/// <param name="delim"></param>
		/// <param name="properties"></param>
		/// <returns></returns>
		public static IDictionary ToDictionary( string property, string delim, IDictionary properties )
		{
			IDictionary map = new Hashtable();
			string propValue = ( string ) properties[ property ];
			if( propValue != null )
			{
				StringTokenizer tokens = new StringTokenizer( propValue, delim, false );
				IEnumerator en = tokens.GetEnumerator();
				while( en.MoveNext() )
				{
					string key = ( string ) en.Current;

					string value = en.MoveNext() ? ( string ) en.Current : String.Empty;
					map[ key ] = value;
				}
			}
			return map;
		}
		public static IDictionary<string, string> ToDictionary(string property, string delim, IDictionary<string, string> properties)
		{
			IDictionary<string, string> map = new Dictionary<string, string>();
			
			if (properties.ContainsKey(property))
			{
				string propValue = properties[property];
				StringTokenizer tokens = new StringTokenizer(propValue, delim, false);
				IEnumerator<string> en = tokens.GetEnumerator();
				while (en.MoveNext())
				{
					string key = en.Current;

					string value = en.MoveNext() ? en.Current : String.Empty;
					map[key] = value;
				}
			}
			return map;
		}
			public StringTokenizerEnumerator( StringTokenizer stok )
			{
				_stokenizer = stok;
			}
 protected static void BindUniqueKey(XmlAttribute uniqueKeyAttribute, Table table, Column column)
 {
     if (uniqueKeyAttribute != null && table != null)
     {
         StringTokenizer tokens = new StringTokenizer(uniqueKeyAttribute.Value, ", ");
         foreach (string token in tokens)
             table.GetOrCreateUniqueKey(token).AddColumn(column);
     }
 }
		/// <summary>
		/// Takes the where condition provided in the mapping attribute and iterpolates the alias.
		/// </summary>
		/// <param name="whereSql">The "where" sql statement from the mapping attribute.</param>
		/// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with rendering the Sql.</param>
		/// <returns>
		/// A well formed "where" sql statement for the <see cref="Dialect.Dialect"/>.
		/// </returns>
		public static string RenderWhereStringTemplate( string whereSql, Dialect.Dialect dialect )
		{
			//TODO: make this a bit nicer.

			StringTokenizer tokens = new StringTokenizer( whereSql, delimiters, true );

			StringBuilder result = new StringBuilder( whereSql.Length + 10 );

			bool quoted = false;
			bool afterFrom = false;
			bool afterFromTable = false;

			IEnumerator tokensEnum = tokens.GetEnumerator();
			bool hasMore = tokensEnum.MoveNext();
			string nextToken = hasMore ? ( string ) tokensEnum.Current : null;

			while( hasMore )
			{
				string token = nextToken;
				hasMore = tokensEnum.MoveNext();
				nextToken = hasMore ? ( string ) tokensEnum.Current : null;

				// the begin or end "'" has been found
				if( "'".Equals( token ) )
				{
					quoted = !quoted;
				}

				if( quoted || char.IsWhiteSpace( token[ 0 ] ) )
				{
					result.Append( token );
				}
				else
				{
					bool isIdentifier = token[ 0 ] == '`' || ( // allow any identifier quoted with backtick
						Char.IsLetter( token[ 0 ] ) && // only recognizes identifiers beginning with a letter
							!Keywords.Contains( token.ToLower( System.Globalization.CultureInfo.InvariantCulture ) ) &&
							token.IndexOf( '.' ) < 0
						);

					if( afterFrom )
					{
						result.Append( token );
						afterFrom = false;
						afterFromTable = true;
					}
					else if( afterFromTable )
					{
						afterFromTable = false;
						result.Append( token );
					}
					else if( isIdentifier && ( nextToken == null || !nextToken.Equals( "(" ) ) ) // not a function call
					{
						result.Append( Placeholder )
							.Append( StringHelper.Dot )
							.Append( Quote( token, dialect ) );

					}
					else
					{
						if( "from".Equals( token.ToLower( System.Globalization.CultureInfo.InvariantCulture ) ) )
						{
							afterFrom = true;
						}
						result.Append( token );
					}
				}
			}

			return result.ToString();

		}
Example #11
0
 public StringTokenizerEnumerator(StringTokenizer stok)
 {
     _stokenizer = stok;
 }
Example #12
0
        /// <summary>
        /// Splits the String using the StringTokenizer.
        /// </summary>
        /// <param name="separators">separators for the tokens of the list</param>
        /// <param name="list">the string that will be broken into tokens</param>
        /// <param name="include">true to include the separators in the tokens.</param>
        /// <returns></returns>
        /// <remarks>
        /// This is more powerful than Split because you have the option of including or
        /// not including the separators in the tokens.
        /// </remarks>
        public static string[] Split(string separators, string list, bool include)
        {
            var tokens = new StringTokenizer(list, separators, include);

            return(tokens.ToArray());
        }
Example #13
0
		protected SqlString ExpandDynamicFilterParameters(SqlString sqlString, ICollection<IParameterSpecification> parameterSpecs, ISessionImplementor session)
		{
			var enabledFilters = session.EnabledFilters;
			if (enabledFilters.Count == 0 || sqlString.ToString().IndexOf(ParserHelper.HqlVariablePrefix) < 0)
			{
				return sqlString;
			}

			Dialect.Dialect dialect = session.Factory.Dialect;
			string symbols = ParserHelper.HqlSeparators + dialect.OpenQuote + dialect.CloseQuote;

			var originSql = sqlString.Compact();
			var result = new SqlStringBuilder();
			foreach (var sqlPart in originSql.Parts)
			{
				var parameter = sqlPart as Parameter;
				if (parameter != null)
				{
					result.Add(parameter);
					continue;
				}

				var sqlFragment = sqlPart.ToString();
				var tokens = new StringTokenizer(sqlFragment, symbols, true);

				foreach (string token in tokens)
				{
					if (token.StartsWith(ParserHelper.HqlVariablePrefix))
					{
						string filterParameterName = token.Substring(1);
						string[] parts = StringHelper.ParseFilterParameterName(filterParameterName);
						string filterName = parts[0];
						string parameterName = parts[1];
						var filter = (FilterImpl)enabledFilters[filterName];

						object value = filter.GetParameter(parameterName);
						IType type = filter.FilterDefinition.GetParameterType(parameterName);
						int parameterColumnSpan = type.GetColumnSpan(session.Factory);
						var collectionValue = value as ICollection;
						int? collectionSpan = null;

						// Add query chunk
						string typeBindFragment = string.Join(", ", Enumerable.Repeat("?", parameterColumnSpan).ToArray());
						string bindFragment;
						if (collectionValue != null && !type.ReturnedClass.IsArray)
						{
							collectionSpan = collectionValue.Count;
							bindFragment = string.Join(", ", Enumerable.Repeat(typeBindFragment, collectionValue.Count).ToArray());
						}
						else
						{
							bindFragment = typeBindFragment;
						}

						// dynamic-filter parameter tracking
						var filterParameterFragment = SqlString.Parse(bindFragment);
						var dynamicFilterParameterSpecification = new DynamicFilterParameterSpecification(filterName, parameterName, type, collectionSpan);
						var parameters = filterParameterFragment.GetParameters().ToArray();
						var sqlParameterPos = 0;
						var paramTrackers = dynamicFilterParameterSpecification.GetIdsForBackTrack(session.Factory);
						foreach (var paramTracker in paramTrackers)
						{
							parameters[sqlParameterPos++].BackTrack = paramTracker;
						}

						parameterSpecs.Add(dynamicFilterParameterSpecification);
						result.Add(filterParameterFragment);
					}
					else
					{
						result.Add(token);
					}
				}
			}
			return result.ToSqlString().Compact();
		}
        private string Replace(string message, object entity)
        {
            var tokens = new StringTokenizer(message, "#${}", true);
            var buf = new StringBuilder(100);
            var escaped = false;
            var el = false;
            var isMember = false;

            IEnumerator ie = tokens.GetEnumerator();

            while (ie.MoveNext())
            {
                string token = (string) ie.Current;

                if (!escaped && "#".Equals(token))
                {
                    el = true;
                }

                if(!el && ("$".Equals(token)))
                {
                    isMember = true;
                }
                else if("}".Equals(token) && isMember)
                {
                    isMember = false;
                }
                if (!el && "{".Equals(token))
                {
                    escaped = true;
                }
                else if (escaped && "}".Equals(token))
                {
                    escaped = false;
                }
                else if (!escaped)
                {
                    if ("{".Equals(token))
                    {
                        el = false;
                    }

                    if(!"$".Equals(token)) buf.Append(token);
                }
                else if(!isMember)
                {
                    object variable;
                    if (attributeParameters.TryGetValue(token.ToLowerInvariant(), out variable))
                    {
                        buf.Append(variable);
                    }
                    else
                    {
                        string _string = null;
                        try
                        {
                            _string = messageBundle != null ? messageBundle.GetString(token, culture) : null;
                        }
                        catch (MissingManifestResourceException)
                        {
                            //give a second chance with the default resource bundle
                        }
                        if (_string == null)
                        {
                            _string = defaultMessageBundle.GetString(token, culture);
                            // in this case we don't catch the MissingManifestResourceException because
                            // we are sure that we DefaultValidatorMessages.resx is an embedded resource
                        }
                        if (_string == null)
                        {
                            buf.Append('{').Append(token).Append('}');
                        }
                        else
                        {
                            buf.Append(Replace(_string,entity));
                        }
                    }
                }
                else
                {
                    ReplaceValue(buf, entity, token);
                }
            }
            return buf.ToString();
        }
Example #15
0
		public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialect.Dialect dialect,
		                                                 SQLFunctionRegistry functionRegistry)
		{
			//TODO: make this a bit nicer
			string symbols = new StringBuilder()
				.Append("=><!+-*/()',|&`")
				.Append(ParserHelper.Whitespace)
				.Append(dialect.OpenQuote)
				.Append(dialect.CloseQuote)
				.ToString();
			StringTokenizer tokens = new StringTokenizer(sqlOrderByString, symbols, true);

			StringBuilder result = new StringBuilder();
			bool quoted = false;
			bool quotedIdentifier = false;

			IEnumerator<string> tokensEnum = tokens.GetEnumerator();
			bool hasMore = tokensEnum.MoveNext();
			string nextToken = hasMore ? tokensEnum.Current : null;
			while (hasMore)
			{
				string token = nextToken;
				string lcToken = token.ToLowerInvariant();
				hasMore = tokensEnum.MoveNext();
				nextToken = hasMore ? tokensEnum.Current : null;

				bool isQuoteCharacter = false;

				if (!quotedIdentifier && "'".Equals(token))
				{
					quoted = !quoted;
					isQuoteCharacter = true;
				}

				if (!quoted)
				{
					bool isOpenQuote;
					if ("`".Equals(token))
					{
						isOpenQuote = !quotedIdentifier;
						token = lcToken = isOpenQuote ?
						                  dialect.OpenQuote.ToString() :
						                  dialect.CloseQuote.ToString();
						quotedIdentifier = isOpenQuote;
						isQuoteCharacter = true;
					}
					else if (!quotedIdentifier && (dialect.OpenQuote == token[0]))
					{
						isOpenQuote = true;
						quotedIdentifier = true;
						isQuoteCharacter = true;
					}
					else if (quotedIdentifier && (dialect.CloseQuote == token[0]))
					{
						quotedIdentifier = false;
						isQuoteCharacter = true;
						isOpenQuote = false;
					}
					else
					{
						isOpenQuote = false;
					}

					if (isOpenQuote)
					{
						result.Append(Placeholder).Append('.');
					}
				}

				bool quotedOrWhitespace = quoted ||
				                          quotedIdentifier ||
				                          isQuoteCharacter ||
				                          char.IsWhiteSpace(token[0]);

				if (quotedOrWhitespace)
				{
					result.Append(token);
				}
				else if (
					IsIdentifier(token, dialect) &&
					!IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry)
					)
				{
					result.Append(Placeholder)
						.Append('.')
						.Append(token);
				}
				else
				{
					result.Append(token);
				}
			}
			return result.ToString();
		}
		private ICriteriaInfoProvider GetPathInfo(string path)
		{
			StringTokenizer tokens = new StringTokenizer(path, ".", false);
			string componentPath = string.Empty;

			// start with the 'rootProvider'
			ICriteriaInfoProvider provider;
			if (nameCriteriaInfoMap.TryGetValue(rootEntityName, out provider) == false)
				throw new ArgumentException("Could not find ICriteriaInfoProvider for: " + path);


			foreach (string token in tokens)
			{
				componentPath += token;
				logger.DebugFormat("searching for {0}", componentPath);
				IType type = provider.GetType(componentPath);
				if (type.IsAssociationType)
				{
					// CollectionTypes are always also AssociationTypes - but there's not always an associated entity...
					IAssociationType atype = (IAssociationType)type;

					CollectionType ctype = type.IsCollectionType ? (CollectionType)type : null;
					IType elementType = (ctype != null) ? ctype.GetElementType(sessionFactory) : null;
					// is the association a collection of components or value-types? (i.e a colloction of valued types?)
					if (ctype != null && elementType.IsComponentType)
					{
						provider = new ComponentCollectionCriteriaInfoProvider(helper.GetCollectionPersister(ctype.Role));
					}
					else if (ctype != null && !elementType.IsEntityType)
					{
						provider = new ScalarCollectionCriteriaInfoProvider(helper, ctype.Role);
					}
					else
					{
						provider = new EntityCriteriaInfoProvider((NHibernate_Persister_Entity.IQueryable)sessionFactory.GetEntityPersister(
																				   atype.GetAssociatedEntityName(
																					   sessionFactory)
																				   ));
					}

					componentPath = string.Empty;
				}
				else if (type.IsComponentType)
				{
					componentPath += '.';
				}
				else
				{
					throw new QueryException("not an association: " + componentPath);
				}
			}

			logger.DebugFormat("returning entity name={0} for path={1} class={2}",
				provider.Name, path, provider.GetType().Name);
			return provider;
		}
        private IActor ResolveArgument(IActor resolvedActor, String expression, int index)
        {
            String argument = null;

            String whiteSpace = "                                                                                      ".Substring(0, index);

            String[] parameters = null;

            int argumentEndIndex = expression.IndexOf("->", index);
            if (argumentEndIndex == -1)
            {
                argumentEndIndex = expression.Length;
            }

            int parametersStartIndex = expression.IndexOf("(", index);
            int parametersEndIndex = -1;
            if ((parametersStartIndex != -1) && (parametersStartIndex < argumentEndIndex))
            {
                argument = expression.Substring(index, (parametersStartIndex) - (index)).Trim();
                parametersEndIndex = expression.IndexOf(")", parametersStartIndex + 1);

                if (parametersEndIndex > argumentEndIndex)
                {
                    throw new SystemException("can't resolve assigner expression : couldn't find closing bracket for bracket on index '" + parametersStartIndex + "' in expression '" + expression + "'");
                }

                // the next exception happens when a parameter contains a right bracket.
                String shouldBewhiteSpace = expression.Substring(parametersEndIndex + 1, (argumentEndIndex) - (parametersEndIndex + 1));
                if (!"".Equals(shouldBewhiteSpace.Trim()))
                {
                    throw new SystemException("can't resolve assigner expression : only whitespace allowed between closing bracket of the parameterlist of an argument and the end of the argument : closing bracket position '" + parametersEndIndex + "' in expression '" + expression + "'");
                }

                String parametersText = expression.Substring(parametersStartIndex + 1, (parametersEndIndex) - (parametersStartIndex + 1));
                ArrayList parameterList = new ArrayList();

                StringTokenizer tokenizer = new StringTokenizer(parametersText, ",");
                IEnumerator tokenEnum = tokenizer.GetEnumerator();
                while (tokenEnum.MoveNext())
                {
                    parameterList.Add(tokenEnum.Current.ToString().Trim());
                }

                if (parameterList.Count > 0)
                {
                    parameters = new String[parameterList.Count];
                    parameters = (String[])parameterList.ToArray(typeof(String));
                }
                else
                {
                    parameters = new String[0];
                }
            }
            else
            {
                argument = expression.Substring(index, (argumentEndIndex) - (index)).Trim();
                parameters = new String[0];
            }

            if ("".Equals(argument))
            {
                throw new SystemException("can't resolve assigner expression : can't resolve empty argument on index '" + index + "' for expression '" + expression + "'");
            }

            String methodName = "ResolveArgument" + argument.Substring(0, 1).ToUpper() + argument.Substring(1);
            try
            {
                MethodInfo method = this.GetType().GetMethod(methodName, (Type[])RESOLVE_METHOD_ARGUMENT_TYPES);
                Object[] args = new Object[] { resolvedActor, parameters };

                resolvedActor = (IActor)method.Invoke(this, (Object[])args);
            }
            catch (Exception t)
            {
                throw new SystemException("can't resolve assigner expression : couldn't resolve argument '" + argument + "' : " + t.Message, t);
            }

            if (argumentEndIndex != expression.Length)
            {
                if (argumentEndIndex < expression.Length)
                {
                    argumentEndIndex = expression.IndexOf("->", argumentEndIndex) + 2;
                }
                resolvedActor = ResolveArgument(resolvedActor, expression, argumentEndIndex);
            }

            return resolvedActor;
        }
Example #18
0
		private void DoPathExpression(string token, QueryTranslator q)
		{
			Preprocess(token, q);

			StringTokenizer tokens = new StringTokenizer(token, ".", true);
			pathExpressionParser.Start(q);
			foreach (string tok in tokens)
			{
				pathExpressionParser.Token(tok, q);
			}
			pathExpressionParser.End(q);

			if (pathExpressionParser.IsCollectionValued)
			{
				OpenExpression(q, string.Empty);
				AppendToken(q, pathExpressionParser.GetCollectionSubquery(q.EnabledFilters));
				CloseExpression(q, string.Empty);
				// this is ugly here, but needed because its a subquery
				q.AddQuerySpaces(q.GetCollectionPersister(pathExpressionParser.CollectionRole).CollectionSpaces);
			}
			else
			{
				if (pathExpressionParser.IsExpectingCollectionIndex)
				{
					expectingIndex++;
				}
				else
				{
					AddJoin(pathExpressionParser.WhereJoin, q);
					AppendToken(q, pathExpressionParser.WhereColumn);
				}
			}
		}
		public void OnlyDelimitersWithoutDelimiters()
		{
			string s = " .,,,";
			StringTokenizer st = new StringTokenizer(s, " ,.", false);
			IEnumerator enumerator = st.GetEnumerator();
			Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens");
		}
		/// <summary>
		/// Splits the String using the StringTokenizer.  
		/// </summary>
		/// <param name="separators">separators for the tokens of the list</param>
		/// <param name="list">the string that will be broken into tokens</param>
		/// <param name="include">true to include the separators in the tokens.</param>
		/// <returns></returns>
		/// <remarks>
		/// This is more powerful than Split because you have the option of including or 
		/// not including the separators in the tokens.
		/// </remarks>
		public static string[] Split(string separators, string list, bool include)
		{
			var tokens = new StringTokenizer(list, separators, include);
			return tokens.ToArray();
		}
		public void OnlyDelimitersWithDelimiters()
		{
			string s = " .,,,";
			StringTokenizer st = new StringTokenizer(s, " ,.", true);
			IEnumerator enumerator = st.GetEnumerator();
			enumerator.MoveNext();
			Assert.AreEqual(" ", (string) enumerator.Current, "Can't get first delimiter");
			enumerator.MoveNext();
			Assert.AreEqual(".", (string) enumerator.Current, "Can't get second delimiter");
			enumerator.MoveNext();
			Assert.AreEqual(",", (string) enumerator.Current, "Can't get third delimiter");
			enumerator.MoveNext();
			Assert.AreEqual(",", (string) enumerator.Current, "Can't get fourth delimiter");
			enumerator.MoveNext();
			Assert.AreEqual(",", (string) enumerator.Current, "Can't get fifth delimiter");
			Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens");
		}
		public void ProcessFilters(SqlString sql, ISessionImplementor session)
		{
			filteredParameterValues = new List<object>();
			filteredParameterTypes = new List<IType>();
			filteredParameterLocations = new List<int>();

			if (session.EnabledFilters.Count == 0 || sql.ToString().IndexOf(ParserHelper.HqlVariablePrefix) < 0)
			{
				processedSQL = sql.Copy();
				return;
			}

			Dialect.Dialect dialect = session.Factory.Dialect;
			string symbols = ParserHelper.HqlSeparators + dialect.OpenQuote + dialect.CloseQuote;

			var result = new SqlStringBuilder();

			int originalParameterIndex = 0; // keep track of the positional parameter
			int newParameterIndex = 0;
			_adjustedParameterLocations = new Dictionary<int, int>();

			foreach (var part in sql.Parts)
			{
				if (part is Parameter)
				{
					result.AddParameter();

					// (?) can be a position parameter or a named parameter (already substituted by (?),
					// but only the positional parameters are available at this point. Adding them in the
					// order of appearance is best that can be done at this point of time, but if they
					// are mixed with named parameters, the order is still wrong, because values and
					// types for the named parameters are added later to the end of the list.
					// see test fixture NH-1098

					_adjustedParameterLocations[originalParameterIndex] = newParameterIndex;
					originalParameterIndex++;
					newParameterIndex++;
					continue;
				}

				var tokenizer = new StringTokenizer((string) part, symbols, true);

				foreach (var token in tokenizer)
				{
					if (token.StartsWith(ParserHelper.HqlVariablePrefix))
					{
						string filterParameterName = token.Substring(1);
						object value = session.GetFilterParameterValue(filterParameterName);
						IType type = session.GetFilterParameterType(filterParameterName);

						// If the value is not a value of the type but a collection of values...
						if (value != null && !type.ReturnedClass.IsAssignableFrom(value.GetType()) && // Added to fix NH-882
						    typeof (ICollection).IsAssignableFrom(value.GetType()))
						{
							var coll = (ICollection) value;
							int i = 0;
							foreach (var elementValue in coll)
							{
								i++;
								int span = type.GetColumnSpan(session.Factory);
								if (span > 0)
								{
									result.AddParameter();
									filteredParameterTypes.Add(type);
									filteredParameterValues.Add(elementValue);
									filteredParameterLocations.Add(newParameterIndex);
									newParameterIndex++;
									if (i < coll.Count)
									{
										result.Add(", ");
									}
								}
							}
						}
						else
						{
							int span = type.GetColumnSpan(session.Factory);
							if (span > 0)
							{
								result.AddParameter();
								filteredParameterTypes.Add(type);
								filteredParameterValues.Add(value);
								filteredParameterLocations.Add(newParameterIndex);
								newParameterIndex++;
							}
						}
					}
					else
					{
						result.Add(token);
					}
				}
			}

			processedSQL = result.ToSqlString();
		}
Example #23
0
        private void AddColumnsFromList(HbmId idSchema, SimpleValue id)
        {
            int count = 0;

            foreach (HbmColumn columnSchema in idSchema.column ?? new HbmColumn[0])
            {
                Column column = CreateColumn(columnSchema, id, count++);
                column.Name = mappings.NamingStrategy.ColumnName(columnSchema.name);

                if (id.Table != null)
                    id.Table.AddColumn(column);
                //table=null -> an association, fill it in later

                id.AddColumn(column);

                if (columnSchema.index != null && id.Table != null)
                {
                    StringTokenizer tokens = new StringTokenizer(columnSchema.index, ", ");
                    foreach (string token in tokens)
                        id.Table.GetOrCreateIndex(token).AddColumn(column);
                }

                if (columnSchema.uniquekey != null && id.Table != null)
                {
                    StringTokenizer tokens = new StringTokenizer(columnSchema.uniquekey, ", ");
                    foreach (string token in tokens)
                        id.Table.GetOrCreateUniqueKey(token).AddColumn(column);
                }
            }
        }
Example #24
0
        /// <summary>
        /// Format an SQL statement using simple rules
        /// </summary>
        /// <param name="sql">The string containing the sql to format.</param>
        /// <returns>A string that contains formatted sql.</returns>
        /// <remarks>
        /// The simple rules to used when formatting are:
        /// <list type="number">
        ///		<item>
        ///			<description>Insert a newline after each comma</description>
        ///		</item>
        ///		<item>
        ///			<description>Indent three spaces after each inserted newline</description>
        ///		</item>
        ///		<item>
        ///			<description>
        ///			If the statement contains single/double quotes return unchanged because
        ///			it is too complex and could be broken by simple formatting.
        ///			</description>
        ///		</item>
        /// </list>
        /// </remarks>
        private static string Format(string sql)
        {
            if (sql.IndexOf("\"") > 0 || sql.IndexOf("'") > 0)
            {
                return sql;
            }

            string formatted;

            if (StringHelper.StartsWithCaseInsensitive(sql, "create table"))
            {
                StringBuilder result = new StringBuilder(60);
                StringTokenizer tokens = new StringTokenizer(sql, "(,)", true);

                int depth = 0;

                foreach (string tok in tokens)
                {
                    if (StringHelper.ClosedParen.Equals(tok))
                    {
                        depth--;
                        if (depth == 0)
                        {
                            result.Append("\n");
                        }
                    }
                    result.Append(tok);
                    if (StringHelper.Comma.Equals(tok) && depth == 1)
                    {
                        result.Append("\n  ");
                    }
                    if (StringHelper.OpenParen.Equals(tok))
                    {
                        depth++;
                        if (depth == 1)
                        {
                            result.Append("\n  ");
                        }
                    }
                }

                formatted = result.ToString();
            }
            else
            {
                formatted = sql;
            }

            return formatted;
        }
Example #25
0
		private IJoinable GetPathJoinable(string path)
		{
			IJoinable last = (IJoinable)Factory.GetEntityPersister(rootEntityName);
			IPropertyMapping lastEntity = (IPropertyMapping)last;

			string componentPath = "";

			StringTokenizer tokens = new StringTokenizer(path, ".", false);
			foreach (string token in tokens)
			{
				componentPath += token;
				IType type = lastEntity.ToType(componentPath);
				if (type.IsAssociationType)
				{
					IAssociationType atype = (IAssociationType)type;
					last = atype.GetAssociatedJoinable(Factory);
					lastEntity = (IPropertyMapping)Factory.GetEntityPersister(atype.GetAssociatedEntityName(Factory));
					componentPath = "";
				}
				else if (type.IsComponentType)
				{
					componentPath += '.';
				}
				else
				{
					throw new QueryException("not an association: " + componentPath);
				}
			}
			return last;
		}
		private Persister.Entity.IJoinable GetPathJoinable(string path)
		{
			NHibernate_Persister_Entity.IJoinable last = (NHibernate_Persister_Entity.IJoinable)Factory.GetEntityPersister(rootEntityName);
			NHibernate_Persister_Entity.IPropertyMapping lastEntity = (NHibernate_Persister_Entity.IPropertyMapping)last;

			string componentPath = "";

			StringTokenizer tokens = new StringTokenizer(path, ".", false);
			foreach (string token in tokens)
			{
				componentPath += token;
				IType type = lastEntity.ToType(componentPath);
				if (type.IsAssociationType)
				{
					if(type.IsCollectionType)
					{
						// ignore joinables for composite collections
						var collectionType = (CollectionType)type;
						var persister = Factory.GetCollectionPersister(collectionType.Role);
						if(persister.ElementType.IsEntityType==false)
							return null;
					}
					IAssociationType atype = (IAssociationType)type;
					
					last = atype.GetAssociatedJoinable(Factory);
					lastEntity = (NHibernate_Persister_Entity.IPropertyMapping)Factory.GetEntityPersister(atype.GetAssociatedEntityName(Factory));
					componentPath = "";
				}
				else if (type.IsComponentType)
				{
					componentPath += '.';
				}
				else
				{
					throw new QueryException("not an association: " + componentPath);
				}
			}
			return last;
		}
Example #27
0
		private string GetPathEntityName(string path)
		{
			IQueryable persister = (IQueryable)sessionFactory.GetEntityPersister(rootEntityName);
			StringTokenizer tokens = new StringTokenizer(path, ".", false);
			string componentPath = "";
			foreach (string token in tokens)
			{
				componentPath += token;
				IType type = persister.ToType(componentPath);
				if (type.IsAssociationType)
				{
					IAssociationType atype = (IAssociationType)type;
					persister = (IQueryable)sessionFactory.GetEntityPersister(atype.GetAssociatedEntityName(sessionFactory));
					componentPath = "";
				}
				else if (type.IsComponentType)
				{
					componentPath += '.';
				}
				else
				{
					throw new QueryException("not an association: " + componentPath);
				}
			}
			return persister.EntityName;
		}
Example #28
0
		public static string RenderWhereStringTemplate(string sqlWhereString, string placeholder, Dialect.Dialect dialect,
		                                               SQLFunctionRegistry functionRegistry)
		{
			//TODO: make this a bit nicer
			string symbols = new StringBuilder()
				.Append("=><!+-*/()',|&`")
				.Append(ParserHelper.Whitespace)
				.Append(dialect.OpenQuote)
				.Append(dialect.CloseQuote)
				.ToString();
			StringTokenizer tokens = new StringTokenizer(sqlWhereString, symbols, true);

			StringBuilder result = new StringBuilder();
			bool quoted = false;
			bool quotedIdentifier = false;
			bool beforeTable = false;
			bool inFromClause = false;
			bool afterFromTable = false;

			IEnumerator tokensEnum = tokens.GetEnumerator();
			bool hasMore = tokensEnum.MoveNext();
			string nextToken = hasMore ? (string) tokensEnum.Current : null;
			while (hasMore)
			{
				string token = nextToken;
				string lcToken = token.ToLower(CultureInfo.InvariantCulture);
				hasMore = tokensEnum.MoveNext();
				nextToken = hasMore ? (string) tokensEnum.Current : null;

				bool isQuoteCharacter = false;

				if (!quotedIdentifier && "'".Equals(token))
				{
					quoted = !quoted;
					isQuoteCharacter = true;
				}

				if (!quoted)
				{
					bool isOpenQuote;
					if ("`".Equals(token))
					{
						isOpenQuote = !quotedIdentifier;
						token = lcToken = isOpenQuote ?
						                  dialect.OpenQuote.ToString() :
						                  dialect.CloseQuote.ToString();
						quotedIdentifier = isOpenQuote;
						isQuoteCharacter = true;
					}
					else if (!quotedIdentifier && (dialect.OpenQuote == token[0]))
					{
						isOpenQuote = true;
						quotedIdentifier = true;
						isQuoteCharacter = true;
					}
					else if (quotedIdentifier && (dialect.CloseQuote == token[0]))
					{
						quotedIdentifier = false;
						isQuoteCharacter = true;
						isOpenQuote = false;
					}
					else
					{
						isOpenQuote = false;
					}

					if (isOpenQuote)
					{
						result.Append(placeholder).Append('.');
					}
				}

				bool quotedOrWhitespace = quoted ||
				                          quotedIdentifier ||
				                          isQuoteCharacter ||
				                          char.IsWhiteSpace(token[0]);

				if (quotedOrWhitespace)
				{
					result.Append(token);
				}
				else if (beforeTable)
				{
					result.Append(token);
					beforeTable = false;
					afterFromTable = true;
				}
				else if (afterFromTable)
				{
					if (!"as".Equals(lcToken))
						afterFromTable = false;
					result.Append(token);
				}
				else if (IsNamedParameter(token))
				{
					result.Append(token);
				}
				else if (
					IsIdentifier(token, dialect) &&
					!IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry)
					)
				{
					result.Append(placeholder)
						.Append('.')
						.Append(token);
				}
				else
				{
					if (BeforeTableKeywords.Contains(lcToken))
					{
						beforeTable = true;
						inFromClause = true;
					}
					else if (inFromClause && ",".Equals(lcToken))
					{
						beforeTable = true;
					}
					result.Append(token);
				}

				if ( //Yuck:
					inFromClause &&
					Keywords.Contains(lcToken) && //"as" is not in Keywords
					!BeforeTableKeywords.Contains(lcToken)
					)
				{
					inFromClause = false;
				}
			}
			return result.ToString();
		}
Example #29
0
		public static void ProcessDynamicFilterParameters(
				SqlString sqlFragment,
				IParameterContainer container,
				HqlSqlWalker walker) 
		{
			if ( walker.EnabledFilters.Count == 0
					&& ( ! HasDynamicFilterParam( sqlFragment ) )
					&& ( ! ( HasCollectionFilterParam( sqlFragment ) ) ) ) 
			{
				return;
			}

			Dialect.Dialect dialect = walker.SessionFactoryHelper.Factory.Dialect;

			string symbols = new StringBuilder().Append( ParserHelper.HqlSeparators )
					.Append( dialect.OpenQuote)
					.Append( dialect.CloseQuote)
					.ToString();

			StringTokenizer tokens = new StringTokenizer( sqlFragment.ToString(), symbols, true );
			StringBuilder result = new StringBuilder();

			foreach (string token in tokens)
			{
				if ( token.StartsWith( ParserHelper.HqlVariablePrefix ) ) 
				{
					string filterParameterName = token.Substring( 1 );
					string[] parts = StringHelper.ParseFilterParameterName( filterParameterName );
					FilterImpl filter = ( FilterImpl ) walker.EnabledFilters[parts[0]];
					Object value = filter.GetParameter( parts[1] );
					IType type = filter.FilterDefinition.GetParameterType( parts[1] );
					String typeBindFragment = StringHelper.Join(
							",",
							ArrayHelper.FillArray( "?", type.GetColumnSpan( walker.SessionFactoryHelper.Factory ) )
					);
					string bindFragment = ( value != null && value is ICollection)
							? StringHelper.Join( ",", ArrayHelper.FillArray( typeBindFragment, ( ( ICollection ) value ).Count ) )
							: typeBindFragment;
					//result.Append( bindFragment );
					result.Append(token);
					container.AddEmbeddedParameter( new DynamicFilterParameterSpecification( parts[0], parts[1], type ) );
				}
				else 
				{
					result.Append( token );
				}
			}

			container.Text = result.ToString();
		}
Example #30
0
        private Property GetRecursiveProperty(string propertyPath, IEnumerable<Property> iter)
        {
            Property property = null;

            StringTokenizer st = new StringTokenizer(propertyPath, ".", false);
            try
            {
                foreach (string element in st)
                {
                    if (property == null)
                    {
                        // we are processing the root of the prpertyPath, so we have the following
                        // considerations:
                        //		1) specifically account for identifier properties
                        //		2) specifically account for embedded composite-identifiers
                        // 		3) perform a normal property lookup
                        Property identifierProperty = IdentifierProperty;
                        if (identifierProperty != null && identifierProperty.Name.Equals(element))
                        {
                            // we have a mapped identifier property and the root of
                            // the incoming property path matched that identifier
                            // property
                            property = identifierProperty;
                        }
                        else if (identifierProperty == null && Identifier != null && typeof(Component).IsInstanceOfType(Identifier))
                        {
                            // we have an embedded composite identifier
                            try
                            {
                                identifierProperty = GetProperty(element, ((Component)Identifier).PropertyIterator);
                                if (identifierProperty != null)
                                {
                                    // the root of the incoming property path matched one
                                    // of the embedded composite identifier properties
                                    property = identifierProperty;
                                }
                            }
                            catch (MappingException)
                            {
                                // ignore it...
                            }
                        }

                        if (property == null)
                        {
                            property = GetProperty(element, iter);
                        }
                    }
                    else
                    {
                        //flat recursive algorithm
                        property = ((Component) property.Value).GetProperty(element);
                    }
                }
            }
            catch (MappingException)
            {
                throw new MappingException("property [" + propertyPath + "] not found on entity [" + EntityName + "]");
            }

            return property;
        }
		private Property GetRecursiveProperty(string propertyPath, ICollection iter)
		{
			Property property = null;

			StringTokenizer st = new StringTokenizer(propertyPath, ".", false);
			try
			{
				foreach (string element in st)
				{
					if (property == null)
					{
						property = GetProperty(element, iter);
					}
					else
					{
						//flat recursive algorithm
						property = ((Component) property.Value).GetProperty(element);
					}
				}
			}
			catch (MappingException e)
			{
				throw new MappingException(
					"property not found: " + propertyPath +
					" in entity: " + Name, e
					);
			}

			return property;
		}
Example #32
0
		/// <summary>
		/// Splits the String using the StringTokenizer.  
		/// </summary>
		/// <param name="separators">separators for the tokens of the list</param>
		/// <param name="list">the string that will be broken into tokens</param>
		/// <param name="include">true to include the separators in the tokens.</param>
		/// <returns></returns>
		/// <remarks>
		/// This is more powerful than Split because you have the option of including or 
		/// not including the separators in the tokens.
		/// </remarks>
		public static string[] Split(string separators, string list, bool include)
		{
			StringTokenizer tokens = new StringTokenizer(list, separators, include);
			ArrayList results = new ArrayList();
			foreach (string token in tokens)
			{
				results.Add(token);
			}
			return (string[])results.ToArray(typeof(string));
		}
Example #33
0
        internal static SqlString ExpandDynamicFilterParameters(SqlString sqlString, ICollection <IParameterSpecification> parameterSpecs, ISessionImplementor session)
        {
            var enabledFilters = session.EnabledFilters;

            if (enabledFilters.Count == 0 || !ParserHelper.HasHqlVariable(sqlString))
            {
                return(sqlString);
            }

            Dialect.Dialect dialect = session.Factory.Dialect;
            string          symbols = ParserHelper.HqlSeparators + dialect.OpenQuote + dialect.CloseQuote;

            var result = new SqlStringBuilder();

            foreach (var sqlPart in sqlString)
            {
                var parameter = sqlPart as Parameter;
                if (parameter != null)
                {
                    result.Add(parameter);
                    continue;
                }

                var sqlFragment = sqlPart.ToString();
                var tokens      = new StringTokenizer(sqlFragment, symbols, true);

                foreach (string token in tokens)
                {
                    if (ParserHelper.IsHqlVariable(token))
                    {
                        string   filterParameterName = token.Substring(1);
                        string[] parts         = StringHelper.ParseFilterParameterName(filterParameterName);
                        string   filterName    = parts[0];
                        string   parameterName = parts[1];
                        var      filter        = (FilterImpl)enabledFilters[filterName];

                        int?  collectionSpan      = filter.GetParameterSpan(parameterName);
                        IType type                = filter.FilterDefinition.GetParameterType(parameterName);
                        int   parameterColumnSpan = type.GetColumnSpan(session.Factory);

                        // Add query chunk
                        string typeBindFragment = string.Join(", ", Enumerable.Repeat("?", parameterColumnSpan));
                        string bindFragment;
                        if (collectionSpan.HasValue && !type.ReturnedClass.IsArray)
                        {
                            bindFragment = string.Join(", ", Enumerable.Repeat(typeBindFragment, collectionSpan.Value));
                        }
                        else
                        {
                            bindFragment = typeBindFragment;
                        }

                        // dynamic-filter parameter tracking
                        var filterParameterFragment             = SqlString.Parse(bindFragment);
                        var dynamicFilterParameterSpecification = new DynamicFilterParameterSpecification(filterName, parameterName, type, collectionSpan);
                        var parameters      = filterParameterFragment.GetParameters().ToArray();
                        var sqlParameterPos = 0;
                        var paramTrackers   = dynamicFilterParameterSpecification.GetIdsForBackTrack(session.Factory);
                        foreach (var paramTracker in paramTrackers)
                        {
                            parameters[sqlParameterPos++].BackTrack = paramTracker;
                        }

                        if (parameterSpecs.OfType <DynamicFilterParameterSpecification>().Any(p => p.FilterParameterFullName == dynamicFilterParameterSpecification.FilterParameterFullName))
                        {
                            var alreadyExistingParamSpecs = parameterSpecs.OfType <DynamicFilterParameterSpecification>().Where(p => p.FilterParameterFullName == dynamicFilterParameterSpecification.FilterParameterFullName).ToList();
                            foreach (var alreadyExistingParamSpec in alreadyExistingParamSpecs)
                            {
                                parameterSpecs.Remove(alreadyExistingParamSpec);
                            }
                        }
                        parameterSpecs.Add(dynamicFilterParameterSpecification);
                        result.Add(filterParameterFragment);
                    }
                    else
                    {
                        result.Add(token);
                    }
                }
            }
            return(result.ToSqlString());
        }