2
2
3
3
import java .time .LocalDateTime ;
4
4
import java .util .List ;
5
+ import java .util .concurrent .ConcurrentHashMap ;
5
6
import org .hibernate .boot .model .FunctionContributions ;
6
7
import org .hibernate .boot .model .TypeContributions ;
7
8
import org .hibernate .dialect .Dialect ;
51
52
import static org .hibernate .type .SqlTypes .VARBINARY ;
52
53
import static org .hibernate .type .SqlTypes .VARCHAR ;
53
54
import org .hibernate .type .StandardBasicTypes ;
55
+ import org .hibernate .type .descriptor .jdbc .JdbcType ;
56
+ import org .hibernate .type .descriptor .jdbc .spi .JdbcTypeRegistry ;
54
57
import org .hibernate .type .descriptor .sql .internal .DdlTypeImpl ;
55
58
import org .hibernate .type .descriptor .sql .spi .DdlTypeRegistry ;
59
+ import tech .ydb .hibernate .dialect .code .YdbJdbcCode ;
60
+ import static tech .ydb .hibernate .dialect .code .YdbJdbcCode .DECIMAL_SHIFT ;
56
61
import tech .ydb .hibernate .dialect .exporter .EmptyExporter ;
57
62
import tech .ydb .hibernate .dialect .exporter .YdbIndexExporter ;
58
63
import tech .ydb .hibernate .dialect .hint .IndexQueryHintHandler ;
59
64
import tech .ydb .hibernate .dialect .hint .QueryHintHandler ;
60
65
import tech .ydb .hibernate .dialect .hint .ScanQueryHintHandler ;
61
66
import tech .ydb .hibernate .dialect .translator .YdbSqlAstTranslatorFactory ;
67
+ import tech .ydb .hibernate .dialect .types .BigDecimalJavaType ;
68
+ import tech .ydb .hibernate .dialect .types .DecimalJdbcType ;
62
69
import tech .ydb .hibernate .dialect .types .InstantJavaType ;
63
70
import tech .ydb .hibernate .dialect .types .InstantJdbcType ;
64
71
import tech .ydb .hibernate .dialect .types .LocalDateJavaType ;
65
72
import tech .ydb .hibernate .dialect .types .LocalDateJdbcType ;
66
73
import tech .ydb .hibernate .dialect .types .LocalDateTimeJavaType ;
67
74
import tech .ydb .hibernate .dialect .types .LocalDateTimeJdbcType ;
68
75
import static tech .ydb .hibernate .dialect .types .LocalDateTimeJdbcType .JDBC_TYPE_DATETIME_CODE ;
76
+ import tech .ydb .hibernate .dialect .types .Uint8JdbcType ;
69
77
70
78
/**
71
79
* @author Kirill Kurdyukov
72
80
*/
73
81
public class YdbDialect extends Dialect {
74
-
75
82
private static final Exporter <ForeignKey > FOREIGN_KEY_EMPTY_EXPORTER = new EmptyExporter <>();
76
83
private static final Exporter <Constraint > UNIQUE_KEY_EMPTY_EXPORTER = new EmptyExporter <>();
77
84
private static final List <QueryHintHandler > QUERY_HINT_HANDLERS = List .of (
78
85
IndexQueryHintHandler .INSTANCE ,
79
86
ScanQueryHintHandler .INSTANCE
80
87
);
88
+ private static final ConcurrentHashMap <Integer , DecimalJdbcType > DECIMAL_JDBC_TYPE_CACHE = new ConcurrentHashMap <>();
81
89
82
90
public YdbDialect (DialectResolutionInfo dialectResolutionInfo ) {
83
91
super (dialectResolutionInfo );
@@ -93,7 +101,7 @@ protected String columnType(int sqlTypeCode) {
93
101
case BIGINT -> "Int64" ;
94
102
case REAL , FLOAT -> "Float" ;
95
103
case DOUBLE -> "Double" ;
96
- case NUMERIC , DECIMAL -> "Decimal (22,9 )" ; // Fixed
104
+ case NUMERIC , DECIMAL -> "Decimal($p, $s )" ;
97
105
case DATE -> "Date" ;
98
106
case JDBC_TYPE_DATETIME_CODE -> "Datetime" ;
99
107
case TIME_WITH_TIMEZONE -> "TzDateTime" ;
@@ -117,6 +125,14 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
117
125
typeContributions .contributeJdbcType (LocalDateJdbcType .INSTANCE );
118
126
typeContributions .contributeJavaType (InstantJavaType .INSTANCE );
119
127
typeContributions .contributeJdbcType (InstantJdbcType .INSTANCE );
128
+ typeContributions .contributeJdbcType (new DecimalJdbcType (YdbJdbcCode .DECIMAL_22_9 ));
129
+ typeContributions .contributeJdbcType (new DecimalJdbcType (YdbJdbcCode .DECIMAL_31_9 ));
130
+ typeContributions .contributeJdbcType (new DecimalJdbcType (YdbJdbcCode .DECIMAL_35_0 ));
131
+ typeContributions .contributeJdbcType (new DecimalJdbcType (YdbJdbcCode .DECIMAL_35_9 ));
132
+
133
+ // custom jdbc codec
134
+ typeContributions .contributeJdbcType (Uint8JdbcType .INSTANCE );
135
+ typeContributions .contributeJavaType (BigDecimalJavaType .INSTANCE_22_9 );
120
136
}
121
137
122
138
@ Override
@@ -125,7 +141,33 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
125
141
126
142
final DdlTypeRegistry ddlTypeRegistry = typeContributions .getTypeConfiguration ().getDdlTypeRegistry ();
127
143
128
- ddlTypeRegistry .addDescriptor (new DdlTypeImpl (JDBC_TYPE_DATETIME_CODE , "Datetime" , "Datetime" , this ));
144
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .DATETIME , "Datetime" , "Datetime" , this ));
145
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .UINT8 , "Uint8" , "Uint8" , this ));
146
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .DECIMAL_22_9 , "Decimal(22, 9)" , "Decimal(22, 9)" , this ));
147
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .DECIMAL_31_9 , "Decimal(31, 9)" , "Decimal(31, 9)" , this ));
148
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .DECIMAL_35_0 , "Decimal(35, 0)" , "Decimal(35, 0)" , this ));
149
+ ddlTypeRegistry .addDescriptor (new DdlTypeImpl (YdbJdbcCode .DECIMAL_35_9 , "Decimal(35, 9)" , "Decimal(35, 9)" , this ));
150
+ }
151
+
152
+ @ Override
153
+ public JdbcType resolveSqlTypeDescriptor (
154
+ String columnTypeName ,
155
+ int jdbcTypeCode ,
156
+ int precision ,
157
+ int scale ,
158
+ JdbcTypeRegistry jdbcTypeRegistry ) {
159
+ if ((jdbcTypeCode == NUMERIC || jdbcTypeCode == DECIMAL ) && (precision != 0 || scale != 0 )) {
160
+ int sqlCode = DECIMAL_SHIFT + (precision << 6 ) + scale ;
161
+
162
+ return DECIMAL_JDBC_TYPE_CACHE .computeIfAbsent (sqlCode , DecimalJdbcType ::new );
163
+ }
164
+
165
+ return super .resolveSqlTypeDescriptor (columnTypeName , jdbcTypeCode , precision , scale , jdbcTypeRegistry );
166
+ }
167
+
168
+ @ Override
169
+ public int getDefaultDecimalPrecision () {
170
+ return 22 ;
129
171
}
130
172
131
173
@ Override
@@ -139,11 +181,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
139
181
140
182
functionContributions .getFunctionRegistry ().register (
141
183
"current_time" ,
142
- new CurrentFunction (
143
- "current_time" ,
144
- currentTime (),
145
- localDateTimeType
146
- )
184
+ new CurrentFunction ("current_time" , currentTime (), localDateTimeType )
147
185
);
148
186
}
149
187
0 commit comments