@@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
2
2
import type { ColumnBaseConfig } from '~/column.ts' ;
3
3
import { entityKind } from '~/entity.ts' ;
4
4
import type { AnyMySqlTable } from '~/mysql-core/table.ts' ;
5
- import { getColumnNameAndConfig } from '~/utils.ts' ;
5
+ import { type Equal , getColumnNameAndConfig } from '~/utils.ts' ;
6
6
import { MySqlColumnBuilderWithAutoIncrement , MySqlColumnWithAutoIncrement } from './common.ts' ;
7
7
8
8
export type MySqlDecimalBuilderInitial < TName extends string > = MySqlDecimalBuilder < {
@@ -46,6 +46,134 @@ export class MySqlDecimal<T extends ColumnBaseConfig<'string', 'MySqlDecimal'>>
46
46
readonly scale : number | undefined = this . config . scale ;
47
47
readonly unsigned : boolean | undefined = this . config . unsigned ;
48
48
49
+ override mapFromDriverValue ( value : unknown ) : string {
50
+ if ( typeof value === 'string' ) return value ;
51
+
52
+ return String ( value ) ;
53
+ }
54
+
55
+ getSQLType ( ) : string {
56
+ let type = '' ;
57
+ if ( this . precision !== undefined && this . scale !== undefined ) {
58
+ type += `decimal(${ this . precision } ,${ this . scale } )` ;
59
+ } else if ( this . precision === undefined ) {
60
+ type += 'decimal' ;
61
+ } else {
62
+ type += `decimal(${ this . precision } )` ;
63
+ }
64
+ type = type === 'decimal(10,0)' || type === 'decimal(10)' ? 'decimal' : type ;
65
+ return this . unsigned ? `${ type } unsigned` : type ;
66
+ }
67
+ }
68
+
69
+ export type MySqlDecimalNumberBuilderInitial < TName extends string > = MySqlDecimalNumberBuilder < {
70
+ name : TName ;
71
+ dataType : 'number' ;
72
+ columnType : 'MySqlDecimalNumber' ;
73
+ data : number ;
74
+ driverParam : string ;
75
+ enumValues : undefined ;
76
+ } > ;
77
+
78
+ export class MySqlDecimalNumberBuilder <
79
+ T extends ColumnBuilderBaseConfig < 'number' , 'MySqlDecimalNumber' > ,
80
+ > extends MySqlColumnBuilderWithAutoIncrement < T , MySqlDecimalConfig > {
81
+ static override readonly [ entityKind ] : string = 'MySqlDecimalNumberBuilder' ;
82
+
83
+ constructor ( name : T [ 'name' ] , config : MySqlDecimalConfig | undefined ) {
84
+ super ( name , 'number' , 'MySqlDecimalNumber' ) ;
85
+ this . config . precision = config ?. precision ;
86
+ this . config . scale = config ?. scale ;
87
+ this . config . unsigned = config ?. unsigned ;
88
+ }
89
+
90
+ /** @internal */
91
+ override build < TTableName extends string > (
92
+ table : AnyMySqlTable < { name : TTableName } > ,
93
+ ) : MySqlDecimalNumber < MakeColumnConfig < T , TTableName > > {
94
+ return new MySqlDecimalNumber < MakeColumnConfig < T , TTableName > > (
95
+ table ,
96
+ this . config as ColumnBuilderRuntimeConfig < any , any > ,
97
+ ) ;
98
+ }
99
+ }
100
+
101
+ export class MySqlDecimalNumber < T extends ColumnBaseConfig < 'number' , 'MySqlDecimalNumber' > >
102
+ extends MySqlColumnWithAutoIncrement < T , MySqlDecimalConfig >
103
+ {
104
+ static override readonly [ entityKind ] : string = 'MySqlDecimalNumber' ;
105
+
106
+ readonly precision : number | undefined = this . config . precision ;
107
+ readonly scale : number | undefined = this . config . scale ;
108
+ readonly unsigned : boolean | undefined = this . config . unsigned ;
109
+
110
+ override mapFromDriverValue ( value : unknown ) : number {
111
+ if ( typeof value === 'number' ) return value ;
112
+
113
+ return Number ( value ) ;
114
+ }
115
+
116
+ override mapToDriverValue = String ;
117
+
118
+ getSQLType ( ) : string {
119
+ let type = '' ;
120
+ if ( this . precision !== undefined && this . scale !== undefined ) {
121
+ type += `decimal(${ this . precision } ,${ this . scale } )` ;
122
+ } else if ( this . precision === undefined ) {
123
+ type += 'decimal' ;
124
+ } else {
125
+ type += `decimal(${ this . precision } )` ;
126
+ }
127
+ type = type === 'decimal(10,0)' || type === 'decimal(10)' ? 'decimal' : type ;
128
+ return this . unsigned ? `${ type } unsigned` : type ;
129
+ }
130
+ }
131
+
132
+ export type MySqlDecimalBigIntBuilderInitial < TName extends string > = MySqlDecimalBigIntBuilder < {
133
+ name : TName ;
134
+ dataType : 'bigint' ;
135
+ columnType : 'MySqlDecimalBigInt' ;
136
+ data : bigint ;
137
+ driverParam : string ;
138
+ enumValues : undefined ;
139
+ } > ;
140
+
141
+ export class MySqlDecimalBigIntBuilder <
142
+ T extends ColumnBuilderBaseConfig < 'bigint' , 'MySqlDecimalBigInt' > ,
143
+ > extends MySqlColumnBuilderWithAutoIncrement < T , MySqlDecimalConfig > {
144
+ static override readonly [ entityKind ] : string = 'MySqlDecimalBigIntBuilder' ;
145
+
146
+ constructor ( name : T [ 'name' ] , config : MySqlDecimalConfig | undefined ) {
147
+ super ( name , 'bigint' , 'MySqlDecimalBigInt' ) ;
148
+ this . config . precision = config ?. precision ;
149
+ this . config . scale = config ?. scale ;
150
+ this . config . unsigned = config ?. unsigned ;
151
+ }
152
+
153
+ /** @internal */
154
+ override build < TTableName extends string > (
155
+ table : AnyMySqlTable < { name : TTableName } > ,
156
+ ) : MySqlDecimalBigInt < MakeColumnConfig < T , TTableName > > {
157
+ return new MySqlDecimalBigInt < MakeColumnConfig < T , TTableName > > (
158
+ table ,
159
+ this . config as ColumnBuilderRuntimeConfig < any , any > ,
160
+ ) ;
161
+ }
162
+ }
163
+
164
+ export class MySqlDecimalBigInt < T extends ColumnBaseConfig < 'bigint' , 'MySqlDecimalBigInt' > >
165
+ extends MySqlColumnWithAutoIncrement < T , MySqlDecimalConfig >
166
+ {
167
+ static override readonly [ entityKind ] : string = 'MySqlDecimalBigInt' ;
168
+
169
+ readonly precision : number | undefined = this . config . precision ;
170
+ readonly scale : number | undefined = this . config . scale ;
171
+ readonly unsigned : boolean | undefined = this . config . unsigned ;
172
+
173
+ override mapFromDriverValue = BigInt ;
174
+
175
+ override mapToDriverValue = String ;
176
+
49
177
getSQLType ( ) : string {
50
178
let type = '' ;
51
179
if ( this . precision !== undefined && this . scale !== undefined ) {
@@ -60,21 +188,31 @@ export class MySqlDecimal<T extends ColumnBaseConfig<'string', 'MySqlDecimal'>>
60
188
}
61
189
}
62
190
63
- export interface MySqlDecimalConfig {
191
+ export interface MySqlDecimalConfig < T extends 'string' | 'number' | 'bigint' = 'string' | 'number' | 'bigint' > {
64
192
precision ?: number ;
65
193
scale ?: number ;
66
194
unsigned ?: boolean ;
195
+ mode ?: T ;
67
196
}
68
197
69
198
export function decimal ( ) : MySqlDecimalBuilderInitial < '' > ;
70
- export function decimal (
71
- config : MySqlDecimalConfig ,
72
- ) : MySqlDecimalBuilderInitial < '' > ;
73
- export function decimal < TName extends string > (
199
+ export function decimal < TMode extends 'string' | 'number' | 'bigint' > (
200
+ config : MySqlDecimalConfig < TMode > ,
201
+ ) : Equal < TMode , 'number' > extends true ? MySqlDecimalNumberBuilderInitial < '' >
202
+ : Equal < TMode , 'bigint' > extends true ? MySqlDecimalBigIntBuilderInitial < '' >
203
+ : MySqlDecimalBuilderInitial < '' > ;
204
+ export function decimal < TName extends string , TMode extends 'string' | 'number' | 'bigint' > (
74
205
name : TName ,
75
- config ?: MySqlDecimalConfig ,
76
- ) : MySqlDecimalBuilderInitial < TName > ;
206
+ config ?: MySqlDecimalConfig < TMode > ,
207
+ ) : Equal < TMode , 'number' > extends true ? MySqlDecimalNumberBuilderInitial < TName >
208
+ : Equal < TMode , 'bigint' > extends true ? MySqlDecimalBigIntBuilderInitial < TName >
209
+ : MySqlDecimalBuilderInitial < TName > ;
77
210
export function decimal ( a ?: string | MySqlDecimalConfig , b : MySqlDecimalConfig = { } ) {
78
211
const { name, config } = getColumnNameAndConfig < MySqlDecimalConfig > ( a , b ) ;
79
- return new MySqlDecimalBuilder ( name , config ) ;
212
+ const mode = config ?. mode ;
213
+ return mode === 'number'
214
+ ? new MySqlDecimalNumberBuilder ( name , config )
215
+ : mode === 'bigint'
216
+ ? new MySqlDecimalBigIntBuilder ( name , config )
217
+ : new MySqlDecimalBuilder ( name , config ) ;
80
218
}
0 commit comments