Skip to content

Commit e3ff1d1

Browse files
committed
Merging 4905-4908 revisions from 2.1 branch
SVN: trunk@4909
1 parent 24649ae commit e3ff1d1

File tree

15 files changed

+664
-9
lines changed

15 files changed

+664
-9
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using NHibernate;
3+
using NHibernate.Engine;
4+
using NHibernate.Mapping;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH2055
7+
{
8+
public class AuxType : AbstractAuxiliaryDatabaseObject
9+
{
10+
11+
override public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
12+
{
13+
return "select '" + Parameters["scriptParameter"] + "'";
14+
}
15+
16+
override public string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
17+
{
18+
return "select 'drop script'";
19+
}
20+
21+
}
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using NUnit.Framework;
5+
using NHibernate.Tool.hbm2ddl;
6+
using System.Text;
7+
using NHibernate.Cfg;
8+
9+
namespace NHibernate.Test.NHSpecificTest.NH2055
10+
{
11+
[TestFixture]
12+
public class Fixture : BugTestCase
13+
{
14+
protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
15+
{
16+
return (dialect is Dialect.MsSql2000Dialect);
17+
}
18+
19+
protected override void Configure(Configuration configuration)
20+
{
21+
base.Configure(configuration);
22+
cfg = configuration;
23+
}
24+
25+
[Test]
26+
public void CanCreateAndDropSchema()
27+
{
28+
using(var s = sessions.OpenSession())
29+
{
30+
using(var cmd = s.Connection.CreateCommand())
31+
{
32+
cmd.CommandType = CommandType.StoredProcedure;
33+
34+
cmd.CommandText = "test_proc1";
35+
36+
Assert.AreEqual(1, cmd.ExecuteScalar());
37+
38+
cmd.CommandText = "test_proc2";
39+
40+
Assert.AreEqual(2, cmd.ExecuteScalar());
41+
}
42+
}
43+
}
44+
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH2055">
5+
6+
<database-object>
7+
<create>
8+
CREATE PROC test_proc1
9+
AS
10+
SELECT 1
11+
GO
12+
CREATE PROC test_proc2
13+
AS
14+
SELECT 2
15+
GO
16+
</create>
17+
<drop>
18+
if (object_id('test_proc1') is not null )
19+
DROP PROC test_proc1
20+
GO
21+
if (object_id('test_proc2') is not null )
22+
DROP PROC test_proc2
23+
GO
24+
</drop>
25+
</database-object>
26+
27+
</hibernate-mapping>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Transactions;
5+
using NHibernate.Impl;
6+
using NUnit.Framework;
7+
using NHibernate.Criterion;
8+
9+
namespace NHibernate.Test.NHSpecificTest.NH2057
10+
{
11+
[TestFixture]
12+
public class Fixture : BugTestCase
13+
{
14+
[Test]
15+
public void WillCloseWhenUsingDTC()
16+
{
17+
SessionImpl s;
18+
using (var tx = new TransactionScope())
19+
{
20+
using (s = (SessionImpl)OpenSession())
21+
{
22+
s.Get<Person>(1);
23+
}
24+
//not closed because the tx is opened yet
25+
Assert.False(s.IsClosed);
26+
tx.Complete();
27+
}
28+
Assert.True(s.IsClosed);
29+
}
30+
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH2057">
5+
6+
<class name="Person">
7+
<id name="Id">
8+
<generator class="native" />
9+
</id>
10+
<property name="Name"/>
11+
</class>
12+
13+
</hibernate-mapping>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Xml.Serialization;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
using NHibernate;
8+
using NHibernate.Classic;
9+
10+
namespace NHibernate.Test.NHSpecificTest.NH2057
11+
{
12+
13+
public class Person
14+
{
15+
public virtual int Id { get; set; }
16+
public virtual string Name { get; set; }
17+
}
18+
19+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@
658658
<Compile Include="NHSpecificTest\NH1938\Model.cs" />
659659
<Compile Include="NHSpecificTest\NH1939\AuxType.cs" />
660660
<Compile Include="NHSpecificTest\NH1939\Fixture.cs" />
661+
<Compile Include="NHSpecificTest\NH2055\AuxType.cs" />
662+
<Compile Include="NHSpecificTest\NH2055\Fixture.cs" />
663+
<Compile Include="NHSpecificTest\NH2057\Fixture.cs" />
664+
<Compile Include="NHSpecificTest\NH2057\Model.cs" />
661665
<Compile Include="NHSpecificTest\NH1941\Fixture.cs" />
662666
<Compile Include="NHSpecificTest\NH1941\Model.cs" />
663667
<Compile Include="NHSpecificTest\NH1941\SexEnumStringType.cs" />
@@ -2096,6 +2100,8 @@
20962100
<Content Include="DynamicEntity\package.html" />
20972101
<EmbeddedResource Include="NHSpecificTest\NH2030\Mappings.hbm.xml" />
20982102
<EmbeddedResource Include="Linq\Mappings\Patient.hbm.xml" />
2103+
<EmbeddedResource Include="NHSpecificTest\NH2055\Mappings.hbm.xml" />
2104+
<EmbeddedResource Include="NHSpecificTest\NH2057\Mappings.hbm.xml" />
20992105
<EmbeddedResource Include="NHSpecificTest\NH2011\Mappings.hbm.xml" />
21002106
<EmbeddedResource Include="Linq\Mappings\Animal.hbm.xml" />
21012107
<EmbeddedResource Include="Linq\Mappings\AnotherEntity.hbm.xml" />

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ protected void Prepare(IDbCommand cmd)
9797
{
9898
try
9999
{
100-
LogCommand(cmd);
101100
IDbConnection sessionConnection = connectionManager.GetConnection();
102101

103102
if (cmd.Connection != null)
@@ -201,7 +200,8 @@ private void InvalidateBatchCommand()
201200
public int ExecuteNonQuery(IDbCommand cmd)
202201
{
203202
CheckReaders();
204-
Prepare(cmd);
203+
LogCommand(cmd);
204+
Prepare(cmd);
205205
Stopwatch duration = null;
206206
if(log.IsDebugEnabled)
207207
duration = Stopwatch.StartNew();
@@ -225,7 +225,8 @@ public int ExecuteNonQuery(IDbCommand cmd)
225225
public IDataReader ExecuteReader(IDbCommand cmd)
226226
{
227227
CheckReaders();
228-
Prepare(cmd);
228+
LogCommand(cmd);
229+
Prepare(cmd);
229230
Stopwatch duration = null;
230231
if (log.IsDebugEnabled)
231232
duration = Stopwatch.StartNew();

src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.Data;
55
using System.Reflection;
6+
using System.Text;
7+
using NHibernate.AdoNet.Util;
68

79
namespace NHibernate.AdoNet
810
{
@@ -18,20 +20,40 @@ public class OracleDataClientBatchingBatcher : AbstractBatcher
1820
private IDbCommand currentBatch;
1921
private IDictionary<string, List<object>> parameterValueListHashTable;
2022
private IDictionary<string, bool> parameterIsAllNullsHashTable;
23+
private StringBuilder currentBatchCommandsLog;
2124

2225

2326
public OracleDataClientBatchingBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
2427
: base(connectionManager, interceptor)
2528
{
2629
batchSize = Factory.Settings.AdoBatchSize;
27-
}
30+
//we always create this, because we need to deal with a scenario in which
31+
//the user change the logging configuration at runtime. Trying to put this
32+
//behind an if(log.IsDebugEnabled) will cause a null reference exception
33+
//at that point.
34+
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
35+
}
2836

2937
public override void AddToBatch(IExpectation expectation)
3038
{
3139
bool firstOnBatch = true;
3240
totalExpectedRowsAffected += expectation.ExpectedRowCount;
33-
log.Info("Adding to batch");
34-
LogCommand(CurrentCommand);
41+
string lineWithParameters = null;
42+
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
43+
if (sqlStatementLogger.IsDebugEnabled || log.IsDebugEnabled)
44+
{
45+
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(CurrentCommand);
46+
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
47+
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
48+
currentBatchCommandsLog.Append("command ")
49+
.Append(countOfCommands)
50+
.Append(":")
51+
.AppendLine(lineWithParameters);
52+
}
53+
if (log.IsDebugEnabled)
54+
{
55+
log.Debug("Adding to batch:" + lineWithParameters);
56+
}
3557

3658
if (currentBatch == null)
3759
{
@@ -87,6 +109,12 @@ protected override void DoExecuteBatch(IDbCommand ps)
87109
CheckReaders();
88110
Prepare(currentBatch);
89111

112+
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
113+
{
114+
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
115+
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
116+
}
117+
90118
foreach (IDataParameter currentParameter in currentBatch.Parameters)
91119
{
92120
List<object> parameterValueArray = parameterValueListHashTable[currentParameter.ParameterName];

src/NHibernate/Dialect/Dialect.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,5 +2105,17 @@ public virtual System.Type IdentityStyleIdentifierGeneratorClass
21052105
}
21062106
}
21072107
#endregion
2108+
2109+
/// <summary>
2110+
/// Supports splitting batches using GO T-SQL command
2111+
/// </summary>
2112+
/// <remarks>
2113+
/// Batches http://msdn.microsoft.com/en-us/library/ms175502.aspx
2114+
/// </remarks>
2115+
public virtual bool SupportsSqlBatches
2116+
{
2117+
get { return false; }
2118+
}
2119+
21082120
}
21092121
}

src/NHibernate/Dialect/MsSql2000Dialect.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,13 @@ public override bool SupportsUnionAll
495495
{
496496
get { return true; }
497497
}
498+
499+
public override bool SupportsSqlBatches
500+
{
501+
get
502+
{
503+
return true;
504+
}
505+
}
498506
}
499507
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Exceptions
4+
{
5+
public class SqlParseException : Exception
6+
{
7+
8+
public SqlParseException(string Message) : base(Message)
9+
{
10+
}
11+
12+
}
13+
}

src/NHibernate/NHibernate.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@
607607
<Compile Include="Event\IPreDatabaseOperationEventArgs.cs" />
608608
<Compile Include="Exceptions\AdoExceptionContextInfo.cs" />
609609
<Compile Include="Exceptions\ReflectionBasedSqlStateExtracter.cs" />
610+
<Compile Include="Exceptions\SqlParseException.cs" />
610611
<Compile Include="Exceptions\SqlStateExtracter.cs" />
611612
<Compile Include="Exceptions\TemplatedViolatedConstraintNameExtracter.cs" />
612613
<Compile Include="Hql\Ast\ANTLR\AstPolymorphicProcessor.cs" />
@@ -800,6 +801,7 @@
800801
<Compile Include="Proxy\AbstractProxyFactory.cs" />
801802
<Compile Include="SqlCommand\InsertSelect.cs" />
802803
<Compile Include="Tool\hbm2ddl\SchemaMetadataUpdater.cs" />
804+
<Compile Include="Tool\hbm2ddl\ScriptSplitter.cs" />
803805
<Compile Include="Transaction\AdoNetWithDistrubtedTransactionFactory.cs" />
804806
<Compile Include="Transform\ToListResultTransformer.cs" />
805807
<Compile Include="Type\DbTimestampType.cs" />

src/NHibernate/Tool/hbm2ddl/SchemaExport.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ private void Execute(Action<string> scriptAction, bool export, bool throwOnError
127127
}
128128
if (export)
129129
{
130-
statement.CommandText = sql;
131-
statement.CommandType = CommandType.Text;
132-
statement.ExecuteNonQuery();
130+
ExecuteSql(statement, sql);
133131
}
134132
}
135133
catch (Exception e)
@@ -143,6 +141,29 @@ private void Execute(Action<string> scriptAction, bool export, bool throwOnError
143141
}
144142
}
145143

144+
private void ExecuteSql(IDbCommand cmd, string sql)
145+
{
146+
if (dialect.SupportsSqlBatches)
147+
{
148+
var objFactory = Environment.BytecodeProvider.ObjectsFactory;
149+
ScriptSplitter splitter = (ScriptSplitter)objFactory.CreateInstance(typeof(ScriptSplitter), sql);
150+
151+
foreach (string stmt in splitter)
152+
{
153+
log.DebugFormat("SQL Batch: {0}", stmt);
154+
cmd.CommandText = stmt;
155+
cmd.CommandType = CommandType.Text;
156+
cmd.ExecuteNonQuery();
157+
}
158+
}
159+
else
160+
{
161+
cmd.CommandText = sql;
162+
cmd.CommandType = CommandType.Text;
163+
cmd.ExecuteNonQuery();
164+
}
165+
}
166+
146167
/// <summary>
147168
/// Executes the Export of the Schema in the given connection
148169
/// </summary>

0 commit comments

Comments
 (0)