@@ -18,6 +18,41 @@ namespace Xtensive.Sql.Drivers.PostgreSql.v8_0
1818{
1919 internal class Translator : SqlTranslator
2020 {
21+ protected class SqlFunctionTypeTranslations
22+ {
23+ private readonly string [ ] translations ;
24+
25+ public void Add ( in SqlFunctionType enumValue , in string value )
26+ {
27+ var index = ( int ) enumValue ;
28+ if ( translations [ index ] != null ) {
29+ throw new InvalidOperationException ( $ "Translation for '{ enumValue } ' is already defined") ;
30+ }
31+ translations [ index ] = value ;
32+ }
33+
34+ public void AddOrOverride ( in SqlFunctionType enumValue , in string value )
35+ {
36+ var index = ( int ) enumValue ;
37+ translations [ index ] = value ;
38+ }
39+
40+ public string Get ( in SqlFunctionType enumValue )
41+ {
42+ var index = ( int ) enumValue ;
43+ return translations [ index ] ;
44+ }
45+
46+ public SqlFunctionTypeTranslations ( )
47+ {
48+ // this is still fast but keeps reference to remind the reader
49+ // to keep array size in accordance with max enum value
50+ translations = new string [ ( int ) SqlFunctionType . RoundDoubleToZero ] ;
51+ }
52+ }
53+
54+ protected readonly SqlFunctionTypeTranslations FunctionTypeTranslations = new ( ) ;
55+
2156 /// <inheritdoc/>
2257 public override string DateTimeFormatString => @"\'yyyyMMdd HHmmss.ffffff\''::timestamp(6)'" ;
2358
@@ -43,6 +78,52 @@ public override void Initialize()
4378 DoubleNumberFormat . NaNSymbol = "'Nan'::float8" ;
4479 DoubleNumberFormat . NegativeInfinitySymbol = "'-Infinity'::float8" ;
4580 DoubleNumberFormat . PositiveInfinitySymbol = "'Infinity'::float8" ;
81+
82+ InitFunctionTypeTranslations ( ) ;
83+ }
84+
85+ protected virtual void InitFunctionTypeTranslations ( )
86+ {
87+ FunctionTypeTranslations . Add ( SqlFunctionType . User , "current_user" ) ;
88+ FunctionTypeTranslations . Add ( SqlFunctionType . CurrentUser , "current_user" ) ;
89+ FunctionTypeTranslations . Add ( SqlFunctionType . SessionUser , "session_user" ) ;
90+ FunctionTypeTranslations . Add ( SqlFunctionType . NullIf , "nullif" ) ;
91+ FunctionTypeTranslations . Add ( SqlFunctionType . Coalesce , "coalesce" ) ;
92+ FunctionTypeTranslations . Add ( SqlFunctionType . BinaryLength , "length" ) ;
93+
94+ FunctionTypeTranslations . Add ( SqlFunctionType . CurrentDate , "date_trunc('day', current_timestamp)" ) ;
95+ FunctionTypeTranslations . Add ( SqlFunctionType . CurrentTimeStamp , "current_timestamp" ) ;
96+ FunctionTypeTranslations . Add ( SqlFunctionType . IntervalNegate , "-" ) ;
97+
98+ FunctionTypeTranslations . Add ( SqlFunctionType . CharLength , "char_length" ) ;
99+ FunctionTypeTranslations . Add ( SqlFunctionType . Lower , "lower" ) ;
100+ FunctionTypeTranslations . Add ( SqlFunctionType . Position , "position" ) ;
101+ FunctionTypeTranslations . Add ( SqlFunctionType . Substring , "substring" ) ;
102+ FunctionTypeTranslations . Add ( SqlFunctionType . Upper , "upper" ) ;
103+ FunctionTypeTranslations . Add ( SqlFunctionType . Concat , "textcat" ) ;
104+
105+ FunctionTypeTranslations . Add ( SqlFunctionType . Abs , "abs" ) ;
106+ FunctionTypeTranslations . Add ( SqlFunctionType . Acos , "acos" ) ;
107+ FunctionTypeTranslations . Add ( SqlFunctionType . Asin , "asin" ) ;
108+ FunctionTypeTranslations . Add ( SqlFunctionType . Atan , "atan" ) ;
109+ FunctionTypeTranslations . Add ( SqlFunctionType . Atan2 , "atan2" ) ;
110+ FunctionTypeTranslations . Add ( SqlFunctionType . Ceiling , "ceil" ) ;
111+ FunctionTypeTranslations . Add ( SqlFunctionType . Cos , "cos" ) ;
112+ FunctionTypeTranslations . Add ( SqlFunctionType . Cot , "cot" ) ;
113+ FunctionTypeTranslations . Add ( SqlFunctionType . Degrees , "degrees" ) ;
114+ FunctionTypeTranslations . Add ( SqlFunctionType . Exp , "exp" ) ;
115+ FunctionTypeTranslations . Add ( SqlFunctionType . Floor , "floor" ) ;
116+ FunctionTypeTranslations . Add ( SqlFunctionType . Log , "ln" ) ;
117+ FunctionTypeTranslations . Add ( SqlFunctionType . Log10 , "log" ) ;
118+ FunctionTypeTranslations . Add ( SqlFunctionType . Pi , "pi" ) ;
119+ FunctionTypeTranslations . Add ( SqlFunctionType . Power , "power" ) ;
120+ FunctionTypeTranslations . Add ( SqlFunctionType . Radians , "radians" ) ;
121+ FunctionTypeTranslations . Add ( SqlFunctionType . Rand , "random" ) ;
122+ FunctionTypeTranslations . Add ( SqlFunctionType . Round , "round" ) ;
123+ FunctionTypeTranslations . Add ( SqlFunctionType . Truncate , "trunc" ) ;
124+ FunctionTypeTranslations . Add ( SqlFunctionType . Sign , "sign" ) ;
125+ FunctionTypeTranslations . Add ( SqlFunctionType . Sqrt , "sqrt" ) ;
126+ FunctionTypeTranslations . Add ( SqlFunctionType . Tan , "tan" ) ;
46127 }
47128
48129 /// <inheritdoc/>
@@ -69,67 +150,32 @@ protected override void TranslateChar(IOutput output, char ch)
69150 /// <inheritdoc/>
70151 public override void Translate ( IOutput output , SqlFunctionType type )
71152 {
72- switch ( type ) {
73- case SqlFunctionType . SystemUser : return ;
74- case SqlFunctionType . User :
75- case SqlFunctionType . CurrentUser :
76- _ = output . Append ( "current_user" ) ;
77- break ;
78- case SqlFunctionType . SessionUser : _ = output . Append ( "session_user" ) ; break ;
79- case SqlFunctionType . NullIf : _ = output . Append ( "nullif" ) ; break ;
80- case SqlFunctionType . Coalesce : _ = output . Append ( "coalesce" ) ; break ;
81- case SqlFunctionType . BinaryLength : _ = output . Append ( "length" ) ; break ;
82- //datetime/timespan
83- case SqlFunctionType . CurrentDate : _ = output . Append ( "date_trunc('day', current_timestamp)" ) ; break ;
84- case SqlFunctionType . CurrentTimeStamp : _ = output . Append ( "current_timestamp" ) ; break ;
85- case SqlFunctionType . IntervalNegate : _ = output . Append ( "-" ) ; break ;
86- //string
87- case SqlFunctionType . CharLength : _ = output . Append ( "char_length" ) ; break ;
88- case SqlFunctionType . Lower : _ = output . Append ( "lower" ) ; break ;
89- case SqlFunctionType . Position : _ = output . Append ( "position" ) ; break ;
90- case SqlFunctionType . Substring : _ = output . Append ( "substring" ) ; break ;
91- case SqlFunctionType . Upper : _ = output . Append ( "upper" ) ; break ;
92- case SqlFunctionType . Concat : _ = output . Append ( "textcat" ) ; break ;
93- //math
94- case SqlFunctionType . Abs : _ = output . Append ( "abs" ) ; break ;
95- case SqlFunctionType . Acos : _ = output . Append ( "acos" ) ; break ;
96- case SqlFunctionType . Asin : _ = output . Append ( "asin" ) ; break ;
97- case SqlFunctionType . Atan : _ = output . Append ( "atan" ) ; break ;
98- case SqlFunctionType . Atan2 : _ = output . Append ( "atan2" ) ; break ;
99- case SqlFunctionType . Ceiling : _ = output . Append ( "ceil" ) ; break ;
100- case SqlFunctionType . Cos : _ = output . Append ( "cos" ) ; break ;
101- case SqlFunctionType . Cot : _ = output . Append ( "cot" ) ; break ;
102- case SqlFunctionType . Degrees : _ = output . Append ( "degrees" ) ; break ;
103- case SqlFunctionType . Exp : _ = output . Append ( "exp" ) ; break ;
104- case SqlFunctionType . Floor : _ = output . Append ( "floor" ) ; break ;
105- case SqlFunctionType . Log : _ = output . Append ( "ln" ) ; break ;
106- case SqlFunctionType . Log10 : _ = output . Append ( "log" ) ; break ;
107- case SqlFunctionType . Pi : _ = output . Append ( "pi" ) ; break ;
108- case SqlFunctionType . Power : _ = output . Append ( "power" ) ; break ;
109- case SqlFunctionType . Radians : _ = output . Append ( "radians" ) ; break ;
110- case SqlFunctionType . Rand : _ = output . Append ( "random" ) ; break ;
111- case SqlFunctionType . Round : _ = output . Append ( "round" ) ; break ;
112- case SqlFunctionType . Truncate : _ = output . Append ( "trunc" ) ; break ;
113- case SqlFunctionType . Sign : _ = output . Append ( "sign" ) ; break ;
114- case SqlFunctionType . Sqrt : _ = output . Append ( "sqrt" ) ; break ;
115- case SqlFunctionType . Tan : _ = output . Append ( "tan" ) ; break ;
116- default : base . Translate ( output , type ) ; break ;
117- } ;
153+ if ( type == SqlFunctionType . SystemUser ) {
154+ return ;
155+ }
156+
157+ var value = FunctionTypeTranslations . Get ( type ) ;
158+ if ( value != null )
159+ _ = output . Append ( value ) ;
160+ else {
161+ base . Translate ( output , type ) ;
162+ }
118163 }
119164
120165 /// <inheritdoc/>
121166 public override string TranslateToString ( SqlFunctionType type )
122167 {
123- return type switch {
124- SqlFunctionType . User or SqlFunctionType . CurrentUser => "current_user" ,
125- //string
126- SqlFunctionType . CharLength => "char_length" ,
127- //math
128- SqlFunctionType . Abs => "abs" ,
129- SqlFunctionType . Rand => "random" ,
130- SqlFunctionType . Round => "round" ,
131- _ => base . TranslateToString ( type ) ,
132- } ;
168+ if ( type == SqlFunctionType . SystemUser ) {
169+ return string . Empty ;
170+ }
171+
172+ var value = FunctionTypeTranslations . Get ( type ) ;
173+ if ( value != null ) {
174+ return value ;
175+ }
176+ else {
177+ return base . TranslateToString ( type ) ;
178+ }
133179 }
134180
135181 /// <inheritdoc/>
0 commit comments