Skip to content

Commit b1b4fd9

Browse files
Merge pull request #435 from nrubino/master
Add SybaseASE driver for ado.net 4/4.5 connector, NH-3991 Fix Trim for Sybase ASE
2 parents fc76122 + 583bcc8 commit b1b4fd9

File tree

6 files changed

+132
-29
lines changed

6 files changed

+132
-29
lines changed

src/NHibernate/Dialect/Function/AnsiTrimEmulationFunction.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using NHibernate.Engine;
66
using NHibernate.SqlCommand;
77
using NHibernate.Type;
8-
using NHibernate.Util;
98
using System.Text.RegularExpressions;
109

1110
namespace NHibernate.Dialect.Function
@@ -41,6 +40,40 @@ public class AnsiTrimEmulationFunction : ISQLFunction, IFunctionGrammar
4140
new SQLFunctionTemplate(NHibernateUtil.String,
4241
"replace( replace( ltrim( rtrim( replace( replace( ?1, ' ', '${space}$' ), ?2, ' ' ) ) ), ' ', ?2 ), '${space}$', ' ' )");
4342

43+
private readonly ISQLFunction _leadingTrim = LeadingTrim;
44+
private readonly ISQLFunction _trailingTrim = TrailingTrim;
45+
private readonly ISQLFunction _bothTrim = BothTrim;
46+
47+
/// <summary>
48+
/// Default constructor. The target database has to support the <c>replace</c> function.
49+
/// </summary>
50+
public AnsiTrimEmulationFunction()
51+
{
52+
}
53+
54+
/// <summary>
55+
/// Constructor for supplying the name of the replace function to use.
56+
/// </summary>
57+
/// <param name="replaceFunction">The replace function.</param>
58+
public AnsiTrimEmulationFunction(string replaceFunction)
59+
{
60+
_leadingTrim =
61+
new SQLFunctionTemplate(
62+
NHibernateUtil.String,
63+
$"{replaceFunction}( {replaceFunction}( ltrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " +
64+
"'${space}$' ), ?2, ' ' ) ), ' ', ?2 ), '${space}$', ' ' )");
65+
_trailingTrim =
66+
new SQLFunctionTemplate(
67+
NHibernateUtil.String,
68+
$"{replaceFunction}( {replaceFunction}( rtrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " +
69+
"'${space}$' ), ?2, ' ' ) ), ' ', ?2 ), '${space}$', ' ' )");
70+
_bothTrim =
71+
new SQLFunctionTemplate(
72+
NHibernateUtil.String,
73+
$"{replaceFunction}( {replaceFunction}( ltrim( rtrim( {replaceFunction}( {replaceFunction}( ?1, ' ', " +
74+
"'${space}$' ), ?2, ' ' ) ) ), ' ', ?2 ), '${space}$', ' ' )");
75+
}
76+
4477
#region ISQLFunction Members
4578

4679
public IType ReturnType(IType columnType, IMapping mapping)
@@ -174,18 +207,18 @@ public SqlString Render(IList args, ISessionFactoryImplementor factory)
174207

175208
return TrailingSpaceTrim.Render(argsToUse, factory);
176209
}
177-
210+
178211
if (leading && trailing)
179212
{
180-
return BothTrim.Render(argsToUse, factory);
213+
return _bothTrim.Render(argsToUse, factory);
181214
}
182-
215+
183216
if (leading)
184217
{
185-
return LeadingTrim.Render(argsToUse, factory);
218+
return _leadingTrim.Render(argsToUse, factory);
186219
}
187-
188-
return TrailingTrim.Render(argsToUse, factory);
220+
221+
return _trailingTrim.Render(argsToUse, factory);
189222
}
190223

191224
#endregion

src/NHibernate/Dialect/SybaseASE15Dialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public SybaseASE15Dialect()
105105
RegisterFunction("square", new StandardSQLFunction("square"));
106106
RegisterFunction("str", new StandardSQLFunction("str", NHibernateUtil.String));
107107
RegisterFunction("tan", new StandardSQLFunction("tan", NHibernateUtil.Double));
108-
// TODO RegisterFunction("trim", new SQLFunctionTemplate(NHibernateUtil.String, "ltrim(rtrim(?1))"));
108+
RegisterFunction("trim", new AnsiTrimEmulationFunction("str_replace"));
109109
RegisterFunction("upper", new StandardSQLFunction("upper"));
110110
RegisterFunction("user", new NoArgSQLFunction("user", NHibernateUtil.String));
111111
RegisterFunction("year", new StandardSQLFunction("year", NHibernateUtil.Int32));
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
2-
3-
namespace NHibernate.Driver
1+
namespace NHibernate.Driver
42
{
53
/// <summary>
6-
/// This provides a driver for Sybase ASE 15 using the ADO.NET driver.
4+
/// This provides a driver for Sybase ASE 15 using the ADO.NET 2 driver.
75
/// </summary>
86
/// <remarks>
97
/// You will need the following libraries available to your application:
@@ -12,25 +10,13 @@ namespace NHibernate.Driver
1210
/// <li>sybdrvado20.dll</li>
1311
/// </ul>
1412
/// </remarks>
15-
public class SybaseAseClientDriver : ReflectionBasedDriver
13+
public class SybaseAseClientDriver : SybaseAseClientDriverBase
1614
{
17-
public SybaseAseClientDriver() : base("Sybase.AdoNet2.AseClient", "Sybase.Data.AseClient.AseConnection", "Sybase.Data.AseClient.AseCommand")
15+
/// <summary>
16+
/// Default constructor.
17+
/// </summary>
18+
public SybaseAseClientDriver() : base("Sybase.AdoNet2.AseClient")
1819
{
1920
}
20-
21-
public override string NamedPrefix
22-
{
23-
get { return "@"; }
24-
}
25-
26-
public override bool UseNamedPrefixInParameter
27-
{
28-
get { return true; }
29-
}
30-
31-
public override bool UseNamedPrefixInSql
32-
{
33-
get { return true; }
34-
}
3521
}
3622
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NHibernate.Driver
2+
{
3+
/// <summary>
4+
/// This provides a driver for Sybase ASE 15 using the ADO.NET 4 driver.
5+
/// </summary>
6+
public class SybaseAdoNet4Driver : SybaseAseClientDriverBase
7+
{
8+
/// <summary>
9+
/// Default constructor.
10+
/// </summary>
11+
public SybaseAdoNet4Driver() : base("Sybase.AdoNet4.AseClient")
12+
{
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NHibernate.Driver
2+
{
3+
/// <summary>
4+
/// This provides a driver for Sybase ASE 16 using the ADO.NET 4.5 driver.
5+
/// </summary>
6+
public class SybaseAdoNet45Driver : SybaseAseClientDriverBase
7+
{
8+
/// <summary>
9+
/// Default constructor.
10+
/// </summary>
11+
public SybaseAdoNet45Driver() : base("Sybase.AdoNet45.AseClient")
12+
{
13+
}
14+
}
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace NHibernate.Driver
2+
{
3+
/// <summary>
4+
/// This provides a driver base for Sybase ASE 15 using the ADO.NET driver. (Also known as SAP
5+
/// Adaptive Server Enterprise.)
6+
/// </summary>
7+
/// <remarks>
8+
/// ASE was formerly Sybase SQL Server, not to be confused with SQL Anywhere / ASA.
9+
/// </remarks>
10+
public abstract class SybaseAseClientDriverBase : ReflectionBasedDriver
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of <see cref="SybaseAseClientDriverBase" /> with
14+
/// type names that are loaded from the specified assembly.
15+
/// </summary>
16+
/// <param name="assemblyName">Assembly to load the types from.</param>
17+
protected SybaseAseClientDriverBase(string assemblyName) : base(
18+
"Sybase.Data.AseClient",
19+
assemblyName,
20+
"Sybase.Data.AseClient.AseConnection",
21+
"Sybase.Data.AseClient.AseCommand")
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Initializes a new instance of <see cref="SybaseAseClientDriverBase" /> with
27+
/// type names that are loaded from the specified assembly.
28+
/// </summary>
29+
/// <param name="providerInvariantName">The Invariant name of a provider.</param>
30+
/// <param name="assemblyName">Assembly to load the types from.</param>
31+
/// <param name="connectionTypeName">Connection type name.</param>
32+
/// <param name="commandTypeName">Command type name.</param>
33+
protected SybaseAseClientDriverBase(
34+
string providerInvariantName,
35+
string assemblyName,
36+
string connectionTypeName,
37+
string commandTypeName) : base(
38+
providerInvariantName,
39+
assemblyName,
40+
connectionTypeName,
41+
commandTypeName)
42+
{
43+
}
44+
45+
/// <inheritdoc />
46+
public override string NamedPrefix => "@";
47+
48+
/// <inheritdoc />
49+
public override bool UseNamedPrefixInParameter => true;
50+
51+
/// <inheritdoc />
52+
public override bool UseNamedPrefixInSql => true;
53+
}
54+
}

0 commit comments

Comments
 (0)