private int GetAfterSelectInsertPoint(int prependCount, SqlString sql)
 {
     if (sql.StartsWithCaseInsensitive("select distinct"))
     {
         return prependCount + 15;
     }
     else if (sql.StartsWithCaseInsensitive("select"))
     {
         return prependCount + 6;
     }
     throw new NotSupportedException("The query should start with 'SELECT' or 'SELECT DISTINCT'");
 }
Example #2
0
        /// <summary>
        /// Return customized limit string (for paging queries)
        /// For performance reason the limit string is ommitted when querying the first page.
        /// </summary>
        /// <param name="querySqlString"></param>
        /// <param name="offset"></param>
        /// <param name="last"></param>
        /// <returns></returns>
        public override NHibernate.SqlCommand.SqlString GetLimitString(NHibernate.SqlCommand.SqlString querySqlString, int offset, int last)
        {
            if (!querySqlString.StartsWithCaseInsensitive("select "))
            {
                throw new ArgumentException("querySqlString should start with select", "querySqlString");
            }
            SqlString sqlString = querySqlString.Substring(6);
            string    orderSql  = querySqlString.SubstringStartingWithLast("order by").ToString();

            if (orderSql.Length != 0)
            {
                sqlString = sqlString.Substring(0, (sqlString.Length - orderSql.Length) - 1);
            }
            SqlStringBuilder builder = new SqlStringBuilder();
            int num = offset + 1;

            builder.Add("SELECT TOP ").Add(last.ToString()).Add(" ").Add(sqlString);
            if (offset > 0)
            {
                builder.Add(" WITH FIRSTROW ").Add(num.ToString());
            }
            if (orderSql.Length > 0)
            {
                builder.Add(" ").Add(orderSql);
            }
            return(builder.ToSqlString());
        }
        private int GetAfterSelectInsertPoint(SqlString sql)
        {
            const string criteriaComment = "/* criteria query */ ";

            if (sql.StartsWithCaseInsensitive(criteriaComment))
                return GetAfterSelectInsertPoint(criteriaComment.Length, sql.Replace(criteriaComment, String.Empty));
            else
                return GetAfterSelectInsertPoint(0, sql);
        }
Example #4
0
 public JoinSequence AddCondition(SqlString condition)
 {
     if (condition.Trim().Length != 0)
     {
         if (!condition.StartsWithCaseInsensitive(" and "))
             conditions.Add(" and ");
         conditions.Add(condition);
     }
     return this;
 }
		private static int GetAfterSelectInsertPoint(SqlString sql)
		{
			string[] arrSelectStrings = {"select distinct", "select all", "select"};
			for (int i = 0; i != arrSelectStrings.Length; ++i)
			{
				string strSelect = arrSelectStrings[i];
				if (sql.StartsWithCaseInsensitive(strSelect))
				{
					return strSelect.Length;
				}
			}
			return 0;
		}
		protected bool AddCondition(SqlStringBuilder buffer, SqlString on)
		{
			if (StringHelper.IsNotEmpty(on))
			{
				if (!on.StartsWithCaseInsensitive(" and"))
				{
					buffer.Add(" and ");
				}
				buffer.Add(on);
				return true;
			}
			else
			{
				return false;
			}
		}
Example #7
0
        public override bool AddCondition(SqlString condition)
        {
            //TODO: this seems hackish
            if (
                afterFrom.ToString().IndexOf(condition.Trim().ToString()) < 0 &&
                afterWhere.ToString().IndexOf(condition.Trim().ToString()) < 0)
            {
                if (!condition.StartsWithCaseInsensitive(" and "))
                {
                    afterWhere.Add(" and ");
                }

                afterWhere.Add(condition);
                return true;
            }

            return false;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="fragment"></param>
        public void AddSelectFragmentString(SqlString fragment)
        {
            if (fragment.StartsWithCaseInsensitive(","))
            {
                fragment = fragment.Substring(1);
            }

            fragment = fragment.Trim();

            if (fragment.Length > 0)
            {
                if (selectBuilder.Count > 0)
                {
                    selectBuilder.Add(StringHelper.CommaSpace);
                }

                selectBuilder.Add(fragment);
            }
        }
        public override bool AddCondition(SqlString condition)
        {
            //TODO: this seems hackish
            var trimCondition = condition.Trim().ToString();

            if (trimCondition.Length > 0 &&
                afterFrom.ToString().IndexOf(trimCondition, StringComparison.Ordinal) < 0 &&
                afterWhere.ToString().IndexOf(trimCondition, StringComparison.Ordinal) < 0)
            {
                if (!condition.StartsWithCaseInsensitive(" and "))
                {
                    afterWhere.Add(" and ");
                }

                afterWhere.Add(condition);
                return(true);
            }

            return(false);
        }
Example #10
0
		private static int GetAfterSelectInsertPoint(SqlString sql)
		{
			if (sql.StartsWithCaseInsensitive("select distinct"))
			{
				return 15;
			}
			if (sql.StartsWithCaseInsensitive("select"))
			{
				return 6;
			}
			throw new NotSupportedException("The query should start with 'SELECT' or 'SELECT DISTINCT'");
		}
		public void AddWhereFragment(
				JoinFragment joinFragment,
				SqlString whereFragment,
				QueryNode query,
				FromElement fromElement,
				HqlSqlWalker hqlSqlWalker)
		{
			if (whereFragment == null)
			{
				return;
			}

			if (!fromElement.UseWhereFragment && !joinFragment.HasThetaJoins)
			{
				return;
			}

			whereFragment = whereFragment.Trim();
			if (StringHelper.IsEmpty(whereFragment.ToString()))
			{
				return;
			}

			// Forcefully remove leading ands from where fragments; the grammar will
			// handle adding them
			if (whereFragment.StartsWithCaseInsensitive("and"))
			{
				whereFragment = whereFragment.Substring(4);
			}

			log.Debug("Using unprocessed WHERE-fragment [" + whereFragment +"]");

			SqlFragment fragment = (SqlFragment) Create(HqlSqlWalker.SQL_TOKEN, whereFragment.ToString());

			fragment.SetJoinFragment(joinFragment);
			fragment.FromElement = fromElement;

			if (fromElement.IndexCollectionSelectorParamSpec != null)
			{
				fragment.AddEmbeddedParameter(fromElement.IndexCollectionSelectorParamSpec);
				fromElement.IndexCollectionSelectorParamSpec = null;
			}

			if (hqlSqlWalker.IsFilter())
			{
				//if (whereFragment.IndexOfCaseInsensitive("?") >= 0)
                if (whereFragment.ToString().IndexOf("?") >= 0)
                {
					IType collectionFilterKeyType = hqlSqlWalker.SessionFactoryHelper
							.RequireQueryableCollection(hqlSqlWalker.CollectionFilterRole)
							.KeyType;
					CollectionFilterKeyParameterSpecification paramSpec = new CollectionFilterKeyParameterSpecification(
							hqlSqlWalker.CollectionFilterRole,
							collectionFilterKeyType,
							0
					);
					fragment.AddEmbeddedParameter(paramSpec);
				}
			}

			JoinProcessor.ProcessDynamicFilterParameters(
					whereFragment,
					fragment,
					hqlSqlWalker
			);

			log.Debug("Using processed WHERE-fragment [" + fragment.Text + "]");

			// Filter conditions need to be inserted before the HQL where condition and the
			// theta join node.  This is because org.hibernate.loader.Loader binds the filter parameters first,
			// then it binds all the HQL query parameters, see org.hibernate.loader.Loader.processFilterParameters().
			if (fragment.FromElement.IsFilter || fragment.HasFilterCondition)
			{
				if (_filters == null)
				{
					// Find or create the WHERE clause
					IASTNode where = (IASTNode) query.WhereClause;
					// Create a new FILTERS node as a parent of all filters
					_filters = Create(HqlSqlWalker.FILTERS, "{filter conditions}");
					// Put the FILTERS node before the HQL condition and theta joins
					where.InsertChild(0, _filters);
				}

				// add the current fragment to the FILTERS node
				_filters.AddChild(fragment);
			}
			else
			{
				if (_thetaJoins == null)
				{
					// Find or create the WHERE clause
					IASTNode where = (IASTNode) query.WhereClause;

					// Create a new THETA_JOINS node as a parent of all filters
					_thetaJoins = Create(HqlSqlWalker.THETA_JOINS, "{theta joins}");

					// Put the THETA_JOINS node before the HQL condition, after the filters.
					if (_filters == null)
					{
						where.InsertChild(0, _thetaJoins);
					}
					else
					{
                        _filters.AddSibling(_thetaJoins);
					}
				}

				// add the current fragment to the THETA_JOINS node
				_thetaJoins.AddChild(fragment);
			}
		}
		private static int GetAfterSelectInsertPoint(SqlString text)
		{
			if (text.StartsWithCaseInsensitive("select"))
			{
				return 6;
			}

			return -1;
		}
		public void StartsWithEmptyString()
		{
			SqlString sql = new SqlString(new string[] { "", "select", " from table" });
			Assert.IsTrue(sql.StartsWithCaseInsensitive("s"));
			Assert.IsFalse(sql.StartsWithCaseInsensitive(","));
		}
		public void StartsWithWhenContainsParameters()
		{
			SqlString sql = new SqlString(" and ", "(", new SqlString("blah = ", Parameter.Placeholder), ")");
			Assert.IsTrue(sql.StartsWithCaseInsensitive(" and"));
			Assert.IsFalse(sql.StartsWithCaseInsensitive("blah"));
		}
Example #15
0
 private static bool IsSelectStatement(SqlString sqlString)
 {
     return sqlString.StartsWithCaseInsensitive("select");
 }
		private static int GetAfterSelectInsertPoint(SqlString sql)
		{
			// Assume no common table expressions with the statement.
			if (sql.StartsWithCaseInsensitive("select distinct"))
			{
				return 15;
			}
			else if (sql.StartsWithCaseInsensitive("select"))
			{
				return 6;
			}
			return 0;
		}
        public SqlString ToQuerySqlString()
        {
            var builder = new SqlStringBuilder();

            builder.Add("select ");

            if (distinct)
            {
                builder.Add("distinct ");
            }

            SqlString from = joins.ToFromFragmentString;

            if (from.StartsWithCaseInsensitive(","))
            {
                from = from.Substring(1);
            }
            else if (from.StartsWithCaseInsensitive(" inner join"))
            {
                from = from.Substring(11);
            }

            builder.Add(selectBuilder.ToSqlString())
            .Add(" from")
            .Add(from);

            SqlString part1    = joins.ToWhereFragmentString.Trim();
            SqlString part2    = whereBuilder.ToSqlString();
            bool      hasPart1 = part1.Count > 0;
            bool      hasPart2 = part2.Count > 0;

            if (hasPart1 || hasPart2)
            {
                builder.Add(" where ");
            }
            if (hasPart1)
            {
                builder.Add(part1.Substring(4));
            }
            if (hasPart2)
            {
                if (hasPart1)
                {
                    builder.Add(" and (");
                }
                builder.Add(part2);
                if (hasPart1)
                {
                    builder.Add(")");
                }
            }
            if (groupBy.Count > 0)
            {
                builder.Add(" group by ").Add(groupBy.ToSqlString());
            }
            if (having.Count > 0)
            {
                builder.Add(" having ").Add(having.ToSqlString());
            }
            if (orderBy.Count > 0)
            {
                builder.Add(" order by ").Add(orderBy.ToSqlString());
            }
            return(builder.ToSqlString());
        }
        public override SqlString GetLimitString(SqlString queryString, SqlString offset, SqlString limit)
        {
            // This does not support the Cache SQL 'DISTINCT BY (comma-list)' extensions,
            // but this extension is not supported through Hibernate anyway.
            int insertionPoint = queryString.StartsWithCaseInsensitive("select distinct") ? 15 : 6;

            return new SqlStringBuilder(queryString.Length + 8)
                .Add(queryString)
                .Insert(insertionPoint, " TOP ? ")
                .ToSqlString();
        }
Example #19
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="fragment"></param>
		public void AddSelectFragmentString(SqlString fragment)
		{
			if (fragment.StartsWithCaseInsensitive(","))
			{
				fragment = fragment.Substring(1);
			}

			fragment = fragment.Trim();

			if (fragment.Length > 0)
			{
				if (selectBuilder.Count > 0)
				{
					selectBuilder.Add(StringHelper.CommaSpace);
				}

				selectBuilder.Add(fragment);
			}
		}
Example #20
0
 private static int GetAfterSelectInsertPoint(SqlString sql)
 {
     if (sql.StartsWithCaseInsensitive("select distinct"))
     {
         return 15;
     }
     else if (sql.StartsWithCaseInsensitive("select"))
     {
         return 6;
     }
     return 0;
 }