Skip to content

Commit d5e1bc9

Browse files
author
Sergey Koshcheyev
committed
More persister refactorings. As a byproduct, internals were modified to support components with insert/update="false" and using formulas for individual columns of a multi-column type, but the functionality is incomplete and untested.
SVN: trunk@1978
1 parent dd902b4 commit d5e1bc9

17 files changed

+746
-693
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
2-
using System.Data;
3-
using System.Text;
42

5-
using NHibernate.Engine;
6-
using NExpression = NHibernate.Expression;
3+
using NHibernate.DomainModel;
4+
using NHibernate.Expression;
75
using NHibernate.SqlCommand;
8-
using NHibernate.Type;
6+
using NHibernate.SqlTypes;
97

10-
using NHibernate.DomainModel;
118
using NUnit.Framework;
129

10+
using NExpression = NHibernate.Expression;
11+
1312
namespace NHibernate.Test.ExpressionTest
1413
{
1514
/// <summary>
@@ -19,27 +18,26 @@ namespace NHibernate.Test.ExpressionTest
1918
public class BetweenExpressionFixture : BaseExpressionFixture
2019
{
2120
[Test]
22-
public void BetweenSqlStringTest()
21+
public void BetweenSqlStringTest()
2322
{
2423
ISession session = factory.OpenSession();
25-
26-
NExpression.ICriterion betweenExpression = NExpression.Expression.Between("Count", 5, 10);
27-
SqlString sqlString = betweenExpression.ToSqlString(factoryImpl, typeof(Simple), "simple_alias", BaseExpressionFixture.EmptyAliasClasses );
2824

25+
ICriterion betweenExpression = Expression.Expression.Between( "Count", 5, 10 );
26+
SqlString sqlString = betweenExpression.ToSqlString( factoryImpl, typeof( Simple ), "simple_alias", BaseExpressionFixture.EmptyAliasClasses );
2927

3028
string expectedSql = "simple_alias.count_ between :simple_alias.count__lo and :simple_alias.count__hi";
3129
Parameter[] expectedParams = new Parameter[2];
3230

33-
Parameter firstBetweenParam = new Parameter( "count__lo", "simple_alias", new SqlTypes.Int32SqlType() );
34-
expectedParams[0] = firstBetweenParam;
35-
36-
Parameter secondBetweenParam = new Parameter( "count__hi", "simple_alias", new SqlTypes.Int32SqlType() );
37-
expectedParams[1] = secondBetweenParam;
31+
Parameter firstBetweenParam = new Parameter( "count__lo", "simple_alias", new Int32SqlType() );
32+
expectedParams[ 0 ] = firstBetweenParam;
33+
34+
Parameter secondBetweenParam = new Parameter( "count__hi", "simple_alias", new Int32SqlType() );
35+
expectedParams[ 1 ] = secondBetweenParam;
3836

39-
CompareSqlStrings(sqlString, expectedSql, expectedParams);
37+
CompareSqlStrings( sqlString, expectedSql, expectedParams );
4038

4139
session.Close();
4240

4341
}
4442
}
45-
}
43+
}

src/NHibernate/Cfg/HbmBinder.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public static void BindSimpleValue( XmlNode node, SimpleValue model, bool isNull
545545
{
546546
Formula f = new Formula();
547547
f.FormulaString = formulaNode.InnerText;
548-
model.Formula = f;
548+
model.AddFormula( f );
549549
}
550550
else
551551
{
@@ -613,7 +613,7 @@ private static string Columns( IValue val )
613613
{
614614
StringBuilder columns = new StringBuilder();
615615
bool first = true;
616-
foreach( Column col in val.ColumnCollection )
616+
foreach( ISelectable col in val.ColumnCollection )
617617
{
618618
if( first )
619619
{
@@ -623,7 +623,7 @@ private static string Columns( IValue val )
623623
{
624624
columns.Append( ", " );
625625
}
626-
columns.Append( col.Name );
626+
columns.Append( col.Text );
627627
}
628628
return columns.ToString();
629629
}
@@ -1093,16 +1093,6 @@ public static void BindComponent( XmlNode node, Component model, System.Type ref
10931093
int i = 0;
10941094
foreach( Mapping.Property prop in model.PropertyCollection )
10951095
{
1096-
if( prop.IsFormula )
1097-
{
1098-
throw new MappingException( "properties of components may not be formulas: " + prop.Name );
1099-
}
1100-
/*
1101-
if( !prop.IsInsertable || !prop.IsUpdateable )
1102-
{
1103-
throw new MappingException( "insert=\"false\", update=\"false\" not supported for properties of components: " + prop.Name );
1104-
}
1105-
*/
11061096
names[ i ] = prop.Name;
11071097
types[ i ] = prop.Type;
11081098
cascade[ i ] = prop.CascadeStyle;

src/NHibernate/Dialect/Dialect.cs

+5
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,11 @@ public virtual string QuoteForSchemaName( string schemaName )
928928
Quote( schemaName );
929929
}
930930

931+
public virtual int MaxAliasLength
932+
{
933+
get { return 10; }
934+
}
935+
931936
/// <summary>
932937
///
933938
/// </summary>

src/NHibernate/Mapping/Column.cs

+27-15
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,39 @@ public string GetQuotedName( Dialect.Dialect d )
103103
name;
104104
}
105105

106-
/// <summary>
107-
/// Gets an Alias for the column name.
108-
/// </summary>
109-
/// <param name="d">The <see cref="Dialect.Dialect"/> that contains the rules for Aliasing.</param>
110-
/// <returns>
111-
/// A string that can be used as the alias for this Column.
112-
/// </returns>
113-
public string GetAlias( Dialect.Dialect d )
106+
/**
107+
* For any column name, generate an alias that is unique
108+
* to that column name, and also 10 characters or less
109+
* in length.
110+
*/
111+
public string GetAlias(Dialect.Dialect dialect)
114112
{
115-
if( quoted || name[0] == StringHelper.SingleQuote || char.IsDigit( name, 0 ) )
113+
string alias = name;
114+
string unique = uniqueInteger.ToString() + '_';
115+
int lastLetter = StringHelper.LastIndexOfLetter(name);
116+
if( lastLetter == -1 )
116117
{
117-
return "y" + uniqueInteger.ToString() + StringHelper.Underscore;
118+
alias = "column";
118119
}
119-
120-
if( name.Length < 11 )
120+
else if( lastLetter < name.Length-1 )
121121
{
122-
return name;
122+
alias = name.Substring(0, lastLetter+1);
123123
}
124-
else
124+
if ( alias.Length > dialect.MaxAliasLength )
125+
{
126+
alias = alias.Substring( 0, dialect.MaxAliasLength - unique.Length );
127+
}
128+
bool useRawName = name.Equals(alias) &&
129+
!quoted &&
130+
!name.ToLower(System.Globalization.CultureInfo.InvariantCulture).Equals("rowid");
131+
132+
if ( useRawName )
133+
{
134+
return alias;
135+
}
136+
else
125137
{
126-
return ( new Alias( 10, uniqueInteger.ToString() + StringHelper.Underscore ) ).ToAliasString( name, d );
138+
return alias + unique;
127139
}
128140
}
129141

src/NHibernate/Mapping/Formula.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public string GetTemplate( Dialect.Dialect dialect )
3131
return Template.RenderWhereStringTemplate( formula, dialect );
3232
}
3333

34-
/// <summary></summary>
35-
public string Alias
36-
{
37-
get { return "f" + uniqueInteger.ToString() + StringHelper.Underscore; }
38-
}
39-
4034
/// <summary></summary>
4135
public string FormulaString
4236
{
@@ -66,7 +60,7 @@ public string GetAlias(Dialect.Dialect dialect, Table table)
6660

6761
public bool IsFormula
6862
{
69-
get { return false; }
63+
get { return true; }
7064
}
7165

7266
public override string ToString()

src/NHibernate/Mapping/IValue.cs

-9
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ public interface IValue
3434
/// </summary>
3535
Table Table { get; }
3636

37-
/// <summary>
38-
/// Gets the <see cref="Formula"/> used to populate the Value.
39-
/// </summary>
40-
/// <remarks>
41-
/// If a <see cref="Formula"/> is not used then this will be
42-
/// <c>null</c>.
43-
/// </remarks>
44-
Formula Formula { get; }
45-
4637
/// <summary>
4738
/// Gets a <see cref="bool"/> indicating if this Value is unique.
4839
/// </summary>

src/NHibernate/Mapping/Property.cs

+1-15
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public bool IsUpdateable
148148
{
149149
bool[] columnUpdateability = propertyValue.ColumnUpdateability;
150150
return updateable &&
151-
!IsFormula && // temporary
152151
(
153152
// columnUpdateability.Length == 0 ||
154153
!ArrayHelper.IsAllFalse(columnUpdateability)
@@ -163,7 +162,6 @@ public bool IsInsertable
163162
{
164163
bool[] columnInsertability = propertyValue.ColumnInsertability;
165164
return insertable &&
166-
!IsFormula && // temporary
167165
(
168166
columnInsertability.Length == 0 ||
169167
!ArrayHelper.IsAllFalse( columnInsertability )
@@ -172,18 +170,6 @@ public bool IsInsertable
172170
set { insertable = value; }
173171
}
174172

175-
/// <summary></summary>
176-
public Formula Formula
177-
{
178-
get { return propertyValue.Formula; }
179-
}
180-
181-
/// <summary></summary>
182-
public bool IsFormula
183-
{
184-
get { return Formula != null; }
185-
}
186-
187173
/// <summary></summary>
188174
public bool IsNullable
189175
{
@@ -247,7 +233,7 @@ public MetaAttribute GetMetaAttribute( string name )
247233
/// <returns></returns>
248234
public bool IsValid( IMapping mapping )
249235
{
250-
return IsFormula ? ColumnSpan == 0 : Value.IsValid( mapping );
236+
return Value.IsValid( mapping );
251237
}
252238

253239
/// <summary>

0 commit comments

Comments
 (0)