@@ -69,7 +69,9 @@ public PostgreSQLDialect()
69
69
RegisterFunction ( "mod" , new SQLFunctionTemplate ( NHibernateUtil . Int32 , "((?1) % (?2))" ) ) ;
70
70
71
71
RegisterFunction ( "sign" , new StandardSQLFunction ( "sign" , NHibernateUtil . Int32 ) ) ;
72
- RegisterFunction ( "round" , new RoundFunction ( ) ) ;
72
+ RegisterFunction ( "round" , new RoundFunction ( false ) ) ;
73
+ RegisterFunction ( "truncate" , new RoundFunction ( true ) ) ;
74
+ RegisterFunction ( "trunc" , new RoundFunction ( true ) ) ;
73
75
74
76
// Trigonometric functions.
75
77
RegisterFunction ( "acos" , new StandardSQLFunction ( "acos" , NHibernateUtil . Double ) ) ;
@@ -322,12 +324,34 @@ public override string CurrentTimestampSelectString
322
324
private class RoundFunction : ISQLFunction
323
325
{
324
326
private static readonly ISQLFunction Round = new StandardSQLFunction ( "round" ) ;
327
+ private static readonly ISQLFunction Truncate = new StandardSQLFunction ( "trunc" ) ;
325
328
326
- // PostgreSQL round with two arguments only accepts decimal as input, thus the cast.
329
+ // PostgreSQL round/trunc with two arguments only accepts decimal as input, thus the cast.
327
330
// It also yields only decimal, but for emulating similar behavior to other databases, we need
328
331
// to have it converted to the original input type, which will be done by NHibernate thanks to
329
332
// not specifying the function type.
330
333
private static readonly ISQLFunction RoundWith2Params = new SQLFunctionTemplate ( null , "round(cast(?1 as numeric), ?2)" ) ;
334
+ private static readonly ISQLFunction TruncateWith2Params = new SQLFunctionTemplate ( null , "trunc(cast(?1 as numeric), ?2)" ) ;
335
+
336
+ private readonly ISQLFunction _singleParamFunction ;
337
+ private readonly ISQLFunction _twoParamFunction ;
338
+ private readonly string _name ;
339
+
340
+ public RoundFunction ( bool truncate )
341
+ {
342
+ if ( truncate )
343
+ {
344
+ _singleParamFunction = Truncate ;
345
+ _twoParamFunction = TruncateWith2Params ;
346
+ _name = "truncate" ;
347
+ }
348
+ else
349
+ {
350
+ _singleParamFunction = Round ;
351
+ _twoParamFunction = RoundWith2Params ;
352
+ _name = "round" ;
353
+ }
354
+ }
331
355
332
356
public IType ReturnType ( IType columnType , IMapping mapping ) => columnType ;
333
357
@@ -337,10 +361,10 @@ private class RoundFunction : ISQLFunction
337
361
338
362
public SqlString Render ( IList args , ISessionFactoryImplementor factory )
339
363
{
340
- return args . Count == 2 ? RoundWith2Params . Render ( args , factory ) : Round . Render ( args , factory ) ;
364
+ return args . Count == 2 ? _twoParamFunction . Render ( args , factory ) : _singleParamFunction . Render ( args , factory ) ;
341
365
}
342
366
343
- public override string ToString ( ) => "round" ;
367
+ public override string ToString ( ) => _name ;
344
368
}
345
369
}
346
370
}
0 commit comments