From: Kevin W. <kev...@us...> - 2004-12-31 22:26:16
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/SqlCommand In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9366 Modified Files: SqlUpdateBuilder.cs Template.cs Log Message: fixing xml comments and letting ReSharper do reformatting Index: SqlUpdateBuilder.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/SqlCommand/SqlUpdateBuilder.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** SqlUpdateBuilder.cs 22 Nov 2004 03:56:08 -0000 1.4 --- SqlUpdateBuilder.cs 31 Dec 2004 22:25:58 -0000 1.5 *************** *** 1,12 **** - using System; using System.Collections; ! using System.Text; ! ! using NHibernate.Connection; ! using NHibernate.Dialect; using NHibernate.Engine; - using NHibernate.Util; using NHibernate.Type; ! namespace NHibernate.SqlCommand --- 1,7 ---- using System.Collections; ! using log4net; using NHibernate.Engine; using NHibernate.Type; ! using NHibernate.Util; namespace NHibernate.SqlCommand *************** *** 15,37 **** /// A class that builds an <c>UPDATE</c> sql statement. /// </summary> ! public class SqlUpdateBuilder: SqlBaseBuilder, ISqlStringBuilder { ! private static readonly log4net.ILog log = log4net.LogManager.GetLogger( typeof(SqlUpdateBuilder) ); ! ! string tableName; ! IList columnNames = new ArrayList(); // name of the column ! IList columnValues = new ArrayList(); //string or a Parameter ! int identityFragmentIndex = -1; ! int versionFragmentIndex = -1; ! IList whereStrings = new ArrayList(); ! public SqlUpdateBuilder(ISessionFactoryImplementor factory) : base(factory) { } ! public SqlUpdateBuilder SetTableName(string tableName) { this.tableName = tableName; --- 10,41 ---- /// A class that builds an <c>UPDATE</c> sql statement. /// </summary> ! public class SqlUpdateBuilder : SqlBaseBuilder, ISqlStringBuilder { ! private static readonly ILog log = LogManager.GetLogger( typeof( SqlUpdateBuilder ) ); ! private string tableName; ! private IList columnNames = new ArrayList(); // name of the column ! private IList columnValues = new ArrayList(); //string or a Parameter ! private int identityFragmentIndex = -1; // not used !?! ! private int versionFragmentIndex = -1; // not used !?! ! private IList whereStrings = new ArrayList(); ! ! /// <summary> ! /// ! /// </summary> ! /// <param name="factory"></param> ! public SqlUpdateBuilder( ISessionFactoryImplementor factory ) : base( factory ) { } ! /// <summary> ! /// ! /// </summary> ! /// <param name="tableName"></param> ! /// <returns></returns> ! public SqlUpdateBuilder SetTableName( string tableName ) { this.tableName = tableName; *************** *** 39,43 **** } - /// <summary> --- 43,46 ---- *************** *** 48,54 **** /// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumn(string columnName, object val, ILiteralType literalType) { ! return AddColumn(columnName, literalType.ObjectToSQLString(val)); } --- 51,57 ---- /// <param name="literalType">The NHibernateType to use to convert the value to a sql string.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumn( string columnName, object val, ILiteralType literalType ) { ! return AddColumn( columnName, literalType.ObjectToSQLString( val ) ); } *************** *** 60,69 **** /// <param name="val">A valid sql string to set as the value of the column.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumn(string columnName, string val) { ! ! columnNames.Add(columnName); ! columnValues.Add(val); ! return this; } --- 63,71 ---- /// <param name="val">A valid sql string to set as the value of the column.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumn( string columnName, string val ) { ! columnNames.Add( columnName ); ! columnValues.Add( val ); ! return this; } *************** *** 75,86 **** /// <param name="val">A valid sql string to set as the value of the column.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumns(string[] columnName, string val) { ! ! for(int i = 0; i < columnName.Length; i++) { ! columnNames.Add(columnName[i]); ! columnValues.Add(val); } ! return this; } --- 77,88 ---- /// <param name="val">A valid sql string to set as the value of the column.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumns( string[ ] columnName, string val ) { ! for( int i = 0; i < columnName.Length; i++ ) ! { ! columnNames.Add( columnName[ i ] ); ! columnValues.Add( val ); } ! return this; } *************** *** 92,108 **** /// <param name="propertyType">The IType of the property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumns(string[] columnNames, IType propertyType) { ! Parameter[] parameters = Parameter.GenerateParameters( Factory, columnNames, propertyType ); ! for(int i = 0; i < columnNames.Length; i++) { ! this.columnNames.Add(columnNames[i]); ! columnValues.Add(parameters[i]); } ! return this; } ! /// <summary> /// Sets the IdentityColumn for the <c>UPDATE</c> sql to use. --- 94,110 ---- /// <param name="propertyType">The IType of the property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder AddColumns( string[ ] columnNames, IType propertyType ) { ! Parameter[ ] parameters = Parameter.GenerateParameters( Factory, columnNames, propertyType ); ! for( int i = 0; i < columnNames.Length; i++ ) { ! this.columnNames.Add( columnNames[ i ] ); ! columnValues.Add( parameters[ i ] ); } ! return this; } ! /// <summary> /// Sets the IdentityColumn for the <c>UPDATE</c> sql to use. *************** *** 111,119 **** /// <param name="identityType">The IType of the Identity Property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder SetIdentityColumn(string[] columnNames, IType identityType) { ! Parameter[] parameters = Parameter.GenerateParameters( Factory, columnNames, identityType ); ! identityFragmentIndex = whereStrings.Add(ToWhereString(columnNames, parameters)); return this; --- 113,121 ---- /// <param name="identityType">The IType of the Identity Property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder SetIdentityColumn( string[ ] columnNames, IType identityType ) { ! Parameter[ ] parameters = Parameter.GenerateParameters( Factory, columnNames, identityType ); ! identityFragmentIndex = whereStrings.Add( ToWhereString( columnNames, parameters ) ); return this; *************** *** 126,134 **** /// <param name="versionType">The IVersionType of the Version Property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder SetVersionColumn(string[] columnNames, IVersionType versionType) { ! Parameter[] parameters = Parameter.GenerateParameters( Factory, columnNames, versionType ); ! versionFragmentIndex = whereStrings.Add(ToWhereString(columnNames, parameters)); return this; --- 128,136 ---- /// <param name="versionType">The IVersionType of the Version Property.</param> /// <returns>The SqlUpdateBuilder.</returns> ! public SqlUpdateBuilder SetVersionColumn( string[ ] columnNames, IVersionType versionType ) { ! Parameter[ ] parameters = Parameter.GenerateParameters( Factory, columnNames, versionType ); ! versionFragmentIndex = whereStrings.Add( ToWhereString( columnNames, parameters ) ); return this; *************** *** 142,149 **** /// <param name="op">The operator to put between the column name and value.</param> /// <returns>The SqlUpdateBuilder</returns> ! public SqlUpdateBuilder AddWhereFragment(string[] columnNames, IType type, string op) { ! Parameter[] parameters = Parameter.GenerateParameters( Factory, columnNames, type ); ! whereStrings.Add(ToWhereString(columnNames, parameters, op)); return this; --- 144,151 ---- /// <param name="op">The operator to put between the column name and value.</param> /// <returns>The SqlUpdateBuilder</returns> ! public SqlUpdateBuilder AddWhereFragment( string[ ] columnNames, IType type, string op ) { ! Parameter[ ] parameters = Parameter.GenerateParameters( Factory, columnNames, type ); ! whereStrings.Add( ToWhereString( columnNames, parameters, op ) ); return this; *************** *** 155,168 **** /// <param name="whereString">A well formed sql string with no parameters.</param> /// <returns>The SqlUpdateBuilder</returns> ! public SqlUpdateBuilder AddWhereFragment(string whereString) { ! whereStrings.Add( new SqlString(whereString) ); return this; } - #region ISqlStringBuilder Members ! public SqlString ToSqlString() { // 3 = "UPDATE", tableName, "SET" --- 157,170 ---- /// <param name="whereString">A well formed sql string with no parameters.</param> /// <returns>The SqlUpdateBuilder</returns> ! public SqlUpdateBuilder AddWhereFragment( string whereString ) { ! whereStrings.Add( new SqlString( whereString ) ); return this; } #region ISqlStringBuilder Members ! /// <summary></summary> ! public SqlString ToSqlString() { // 3 = "UPDATE", tableName, "SET" *************** *** 171,177 **** // will have a comma for all but the first column, and then for each column // will have a name, " = ", value so mulitply by 3 ! if( columnNames.Count > 0 ) { ! initialCapacity += (columnNames.Count - 1) + (columnNames.Count * 3); } // 1 = "WHERE" --- 173,179 ---- // will have a comma for all but the first column, and then for each column // will have a name, " = ", value so mulitply by 3 ! if( columnNames.Count > 0 ) { ! initialCapacity += ( columnNames.Count - 1 ) + ( columnNames.Count*3 ); } // 1 = "WHERE" *************** *** 179,188 **** // the "AND" before all but the first whereString ! if( whereStrings.Count > 0 ) { ! initialCapacity += (whereStrings.Count -1); ! for( int i=0; i <whereStrings.Count; i++ ) { ! initialCapacity += ((SqlString)whereStrings[i]).Count; } } --- 181,190 ---- // the "AND" before all but the first whereString ! if( whereStrings.Count > 0 ) { ! initialCapacity += ( whereStrings.Count - 1 ); ! for( int i = 0; i < whereStrings.Count; i++ ) { ! initialCapacity += ( ( SqlString ) whereStrings[ i ] ).Count; } } *************** *** 193,248 **** bool andNeeded = false; - - sqlBuilder.Add("UPDATE ") - .Add(tableName) - .Add(" SET "); ! for(int i = 0; i < columnNames.Count; i++) { ! if(commaNeeded) sqlBuilder.Add(StringHelper.CommaSpace); commaNeeded = true; ! string columnName = (string)columnNames[i]; ! object columnValue = columnValues[i]; - sqlBuilder.Add(columnName) - .Add(" = "); - Parameter param = columnValue as Parameter; ! if(param!=null) { ! sqlBuilder.Add(param); } ! else { ! sqlBuilder.Add((string)columnValue); } } - - sqlBuilder.Add(" WHERE "); ! foreach(SqlString whereString in whereStrings) { ! if(andNeeded) sqlBuilder.Add(" AND "); andNeeded = true; ! sqlBuilder.Add(whereString, null, null, null, false); } ! if(log.IsDebugEnabled) { ! if( initialCapacity < sqlBuilder.Count ) { ! log.Debug( "The initial capacity was set too low at: " + initialCapacity + " for the UpdateSqlBuilder " + ! "that needed a capacity of: " + sqlBuilder.Count + " for the table " + tableName ); } ! else if( initialCapacity > 16 && ((float)initialCapacity/sqlBuilder.Count) > 1.2 ) { log.Debug( "The initial capacity was set too high at: " + initialCapacity + " for the UpdateSqlBuilder " + ! "that needed a capacity of: " + sqlBuilder.Count + " for the table " + tableName); } } --- 195,256 ---- bool andNeeded = false; ! sqlBuilder.Add( "UPDATE " ) ! .Add( tableName ) ! .Add( " SET " ); ! ! for( int i = 0; i < columnNames.Count; i++ ) { ! if( commaNeeded ) ! { ! sqlBuilder.Add( StringHelper.CommaSpace ); ! } commaNeeded = true; ! string columnName = ( string ) columnNames[ i ]; ! object columnValue = columnValues[ i ]; ! ! sqlBuilder.Add( columnName ) ! .Add( " = " ); Parameter param = columnValue as Parameter; ! if( param != null ) { ! sqlBuilder.Add( param ); } ! else { ! sqlBuilder.Add( ( string ) columnValue ); } } ! sqlBuilder.Add( " WHERE " ); ! ! foreach( SqlString whereString in whereStrings ) { ! if( andNeeded ) ! { ! sqlBuilder.Add( " AND " ); ! } andNeeded = true; ! sqlBuilder.Add( whereString, null, null, null, false ); } ! if( log.IsDebugEnabled ) { ! if( initialCapacity < sqlBuilder.Count ) { ! log.Debug( "The initial capacity was set too low at: " + initialCapacity + " for the UpdateSqlBuilder " + ! "that needed a capacity of: " + sqlBuilder.Count + " for the table " + tableName ); } ! else if( initialCapacity > 16 && ( ( float ) initialCapacity/sqlBuilder.Count ) > 1.2 ) { log.Debug( "The initial capacity was set too high at: " + initialCapacity + " for the UpdateSqlBuilder " + ! "that needed a capacity of: " + sqlBuilder.Count + " for the table " + tableName ); } } *************** *** 253,255 **** #endregion } ! } --- 261,263 ---- #endregion } ! } \ No newline at end of file Index: Template.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/SqlCommand/Template.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Template.cs 4 Dec 2004 22:41:30 -0000 1.10 --- Template.cs 31 Dec 2004 22:25:58 -0000 1.11 *************** *** 2,26 **** using System.Collections; using System.Text; - using Iesi.Collections; - - using NHibernate.Dialect; using NHibernate.Util; ! namespace NHibernate.SqlCommand { - /// <summary> /// Summary description for Template. /// </summary> ! public sealed class Template { public const string PlaceHolder = "$PlaceHolder"; ! private static ISet Keywords = new HashedSet(); ! private static ArrayList delimiterList = new ArrayList(13); private static string delimiters = null; ! static Template() { Keywords.Add( "and" ); --- 2,24 ---- using System.Collections; using System.Text; using Iesi.Collections; using NHibernate.Util; ! namespace NHibernate.SqlCommand { /// <summary> /// Summary description for Template. /// </summary> ! public sealed class Template { + /// <summary></summary> public const string PlaceHolder = "$PlaceHolder"; ! private static ISet Keywords = new HashedSet(); ! private static ArrayList delimiterList = new ArrayList( 13 ); private static string delimiters = null; ! /// <summary></summary> ! static Template() { Keywords.Add( "and" ); *************** *** 40,65 **** Keywords.Add( "by" ); ! delimiterList.Add(" "); ! delimiterList.Add("="); ! delimiterList.Add(">"); ! delimiterList.Add("<"); ! delimiterList.Add("!"); ! delimiterList.Add("+"); ! delimiterList.Add("-"); ! delimiterList.Add("*"); ! delimiterList.Add("/"); ! delimiterList.Add("("); ! delimiterList.Add(")"); ! delimiterList.Add("'"); ! delimiterList.Add(","); ! foreach(string delim in delimiterList) { ! delimiters += delim; } } ! private Template() { } --- 38,63 ---- Keywords.Add( "by" ); ! delimiterList.Add( " " ); ! delimiterList.Add( "=" ); ! delimiterList.Add( ">" ); ! delimiterList.Add( "<" ); ! delimiterList.Add( "!" ); ! delimiterList.Add( "+" ); ! delimiterList.Add( "-" ); ! delimiterList.Add( "*" ); ! delimiterList.Add( "/" ); ! delimiterList.Add( "(" ); ! delimiterList.Add( ")" ); ! delimiterList.Add( "'" ); ! delimiterList.Add( "," ); ! foreach( string delim in delimiterList ) { ! delimiters += delim; } } ! private Template() { } *************** *** 69,73 **** /// </summary> /// <param name="keyword">The Keyword to add.</param> ! public static void AddKeyword(string keyword) { Keywords.Add( keyword ); --- 67,71 ---- /// </summary> /// <param name="keyword">The Keyword to add.</param> ! public static void AddKeyword( string keyword ) { Keywords.Add( keyword ); *************** *** 78,90 **** /// </summary> /// <param name="delimiter">The delimiter to add.</param> ! public static void AddDelimiter(string delimiter) { ! if ( !delimiterList.Contains(delimiter) ) { ! delimiterList.Add(delimiter); ! delimiters += delimiter; } } ! /// <summary> /// Takes the where condition provided in the mapping attribute and iterpolates the alias. --- 76,88 ---- /// </summary> /// <param name="delimiter">The delimiter to add.</param> ! public static void AddDelimiter( string delimiter ) { ! if( !delimiterList.Contains( delimiter ) ) { ! delimiterList.Add( delimiter ); ! delimiters += delimiter; } } ! /// <summary> /// Takes the where condition provided in the mapping attribute and iterpolates the alias. *************** *** 93,103 **** /// <param name="dialect"></param> /// <returns></returns> ! public static string RenderWhereStringTemplate(string sqlWhereString, Dialect.Dialect dialect) { //TODO: make this a bit nicer. ! ! StringTokenizer tokens = new StringTokenizer(sqlWhereString, delimiters, true); ! ! StringBuilder result = new StringBuilder(sqlWhereString.Length + 10); bool quoted = false; --- 91,101 ---- /// <param name="dialect"></param> /// <returns></returns> ! public static string RenderWhereStringTemplate( string sqlWhereString, Dialect.Dialect dialect ) { //TODO: make this a bit nicer. ! ! StringTokenizer tokens = new StringTokenizer( sqlWhereString, delimiters, true ); ! ! StringBuilder result = new StringBuilder( sqlWhereString.Length + 10 ); bool quoted = false; *************** *** 107,159 **** 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() ) && ! 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() ) ) afterFrom = true; ! result.Append(token); } ! } } ! return result.ToString(); --- 105,163 ---- 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() ) && ! 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() ) ) ! { ! afterFrom = true; ! } ! result.Append( token ); } ! } } ! return result.ToString(); *************** *** 166,187 **** /// <param name="dialect"></param> /// <returns></returns> ! public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialect.Dialect dialect) { //TODO: make this a bit nicer ! StringTokenizer tokens = new StringTokenizer(sqlOrderByString, ",", false); ! StringBuilder result = new StringBuilder(sqlOrderByString.Length + 2); ! bool commaNeeded = false; IEnumerator tokenEnum = tokens.GetEnumerator(); ! while( tokenEnum.MoveNext() ) { ! string column = Quote(((string)tokenEnum.Current).Trim(), dialect); ! if (commaNeeded) result.Append(StringHelper.CommaSpace); commaNeeded = true; ! result.Append(PlaceHolder) ! .Append(StringHelper.Dot) ! .Append(column); } --- 170,194 ---- /// <param name="dialect"></param> /// <returns></returns> ! public static string RenderOrderByStringTemplate( string sqlOrderByString, Dialect.Dialect dialect ) { //TODO: make this a bit nicer ! StringTokenizer tokens = new StringTokenizer( sqlOrderByString, ",", false ); ! StringBuilder result = new StringBuilder( sqlOrderByString.Length + 2 ); ! bool commaNeeded = false; IEnumerator tokenEnum = tokens.GetEnumerator(); ! while( tokenEnum.MoveNext() ) { ! string column = Quote( ( ( string ) tokenEnum.Current ).Trim(), dialect ); ! if( commaNeeded ) ! { ! result.Append( StringHelper.CommaSpace ); ! } commaNeeded = true; ! result.Append( PlaceHolder ) ! .Append( StringHelper.Dot ) ! .Append( column ); } *************** *** 189,202 **** } ! private static string Quote(string column, Dialect.Dialect dialect) { ! ! if ( column[0] == '`' ) { ! if (column[column.Length -1] != '`') ! throw new ArgumentException("missing ` in column " + column); ! return dialect.QuoteForAliasName(column.Substring(1, column.Length - 2)); } ! else { return column; --- 196,210 ---- } ! private static string Quote( string column, Dialect.Dialect dialect ) { ! if( column[ 0 ] == '`' ) { ! if( column[ column.Length - 1 ] != '`' ) ! { ! throw new ArgumentException( "missing ` in column " + column ); ! } ! return dialect.QuoteForAliasName( column.Substring( 1, column.Length - 2 ) ); } ! else { return column; *************** *** 205,207 **** } ! } --- 213,215 ---- } ! } \ No newline at end of file |