Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Sql
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6953/NHibernate/Sql
Modified Files:
QuerySelect.cs
Log Message:
Refactoring of converting Hql to SqlString.
Index: QuerySelect.cs
===================================================================
RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Sql/QuerySelect.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** QuerySelect.cs 16 Aug 2004 05:18:52 -0000 1.7
--- QuerySelect.cs 18 Aug 2004 20:47:52 -0000 1.8
***************
*** 16,19 ****
--- 16,20 ----
private StringBuilder select = new StringBuilder();
private StringBuilder where = new StringBuilder();
+ private SqlCommand.SqlStringBuilder whereBuilder = new SqlCommand.SqlStringBuilder();
private StringBuilder groupBy = new StringBuilder();
private StringBuilder orderBy = new StringBuilder();
***************
*** 86,90 ****
{
//if ( conjunctiveWhere.length()>0 ) conjunctiveWhere.append(" and ");
! AppendTokens(where, tokens);
}
--- 87,92 ----
{
//if ( conjunctiveWhere.length()>0 ) conjunctiveWhere.append(" and ");
! AppendTokens(whereBuilder, tokens);
! //AppendTokens(where, tokens);
}
***************
*** 113,116 ****
--- 115,119 ----
}
+ [Obsolete("Should be using ToQuerySqlString instead")]
public string ToQueryString()
{
***************
*** 155,158 ****
--- 158,202 ----
}
+ public SqlCommand.SqlString ToQuerySqlString()
+ {
+ SqlCommand.SqlStringBuilder builder = new SqlCommand.SqlStringBuilder();
+
+ builder.Add("select ");
+
+ if (distinct) builder.Add("distinct ");
+
+ SqlCommand.SqlString from = joins.ToFromFragmentString;
+ if ( from.StartsWith(",") )
+ {
+ from = from.Substring(1);
+ }
+ else if ( from.StartsWith(" inner join") )
+ {
+ from = from.Substring(11);
+ }
+
+ builder.Add(select.ToString())
+ .Add(" from")
+ .Add( from );
+
+ SqlCommand.SqlString part1 = joins.ToWhereFragmentString.Trim();
+ SqlCommand.SqlString part2 = whereBuilder.ToSqlString().Trim();
+ bool hasPart1 = part1.SqlParts.Length > 0;
+ bool hasPart2 = part2.SqlParts.Length > 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.Length > 0 ) builder.Add(" group by ").Add( groupBy.ToString() );
+ if ( having.Length > 0 ) builder.Add(" having ").Add( having.ToString() );
+ if ( orderBy.Length > 0 ) builder.Add(" order by ").Add( orderBy.ToString() );
+ return builder.ToSqlString();
+ }
+
private void AppendTokens(StringBuilder buf, ICollection iter)
{
***************
*** 160,163 ****
--- 204,208 ----
bool lastQuoted = false;
+ int debugIndex = 0;
foreach(string token in iter)
{
***************
*** 173,176 ****
--- 218,279 ----
buf.Append(token);
lastQuoted = token.EndsWith("'");
+ debugIndex++;
+ }
+ }
+
+ private void AppendTokens(SqlCommand.SqlStringBuilder builder, ICollection iter)
+ {
+ bool lastSpaceable = true;
+ bool lastQuoted = false;
+
+ int debugIndex = 0;
+ foreach(object token in iter)
+ {
+ string tokenString = token as string;
+ SqlCommand.SqlString tokenSqlString = token as SqlCommand.SqlString;
+
+ bool spaceable = !dontSpace.Contains(token);
+ bool quoted = false;
+
+ //TODO: seems HACKish to cast between String and SqlString
+ if(tokenString!=null)
+ {
+ quoted = tokenString.StartsWith("'");
+ }
+ else
+ {
+ quoted = tokenSqlString.StartsWith("'");
+ }
+
+ if (spaceable && lastSpaceable)
+ {
+ if (!quoted || !lastQuoted) builder.Add(" ");
+ }
+
+ lastSpaceable = spaceable;
+
+ if( token.Equals(StringHelper.SqlParameter) )
+ {
+ SqlCommand.Parameter param = new SqlCommand.Parameter();
+ param.Name = "placholder";
+ builder.Add(param);
+ }
+ else
+ {
+ // not sure if we have a string or a SqlString here and token is a
+ // reference to an object - so let the builder figure out what the
+ // actual object is
+ builder.AddObject(token);
+ }
+ debugIndex++;
+
+ if( tokenString!=null)
+ {
+ lastQuoted = tokenString.EndsWith("'");
+ }
+ else
+ {
+ tokenSqlString.EndsWith("'");
+ }
}
}
|