@@ -40,7 +40,7 @@ module.exports = (baseProvider, options, app) => {
40
40
const { wrapIfNotExistSchema, wrapIfNotExistDatabase, wrapIfNotExistTable, wrapIfNotExistView } =
41
41
require ( './helpers/ifNotExistStatementHelper' ) ( app ) ;
42
42
const { getPartitionedTables, getCreateViewData } = require ( './helpers/viewHelper' ) ( app ) ;
43
- const { getFullTableName, escapeSpecialCharacters } = require ( './utils/general' ) ( _ ) ;
43
+ const { getFullTableName, escapeSpecialCharacters, wrapInBracketsIfNecessary } = require ( './utils/general' ) ( _ ) ;
44
44
45
45
const terminator = getTerminator ( options ) ;
46
46
@@ -177,6 +177,14 @@ module.exports = (baseProvider, options, app) => {
177
177
: fullTableStatement ;
178
178
} ,
179
179
180
+ createComputedColumn ( { name, computedExpression, persisted } ) {
181
+ return assignTemplates ( templates . computedColumnDefinition , {
182
+ name,
183
+ expression : wrapInBracketsIfNecessary ( computedExpression ) ,
184
+ persisted : persisted ? ' PERSISTED' : '' ,
185
+ } ) ;
186
+ } ,
187
+
180
188
convertColumnDefinition ( columnDefinition ) {
181
189
const type = hasType ( columnDefinition . type )
182
190
? _ . toUpper ( columnDefinition . type )
@@ -207,19 +215,28 @@ module.exports = (baseProvider, options, app) => {
207
215
columnDefinition . isHidden ,
208
216
) ;
209
217
210
- const statement = assignTemplates ( templates . columnDefinition , {
211
- name : columnDefinition . name ,
212
- type : decorateType ( type , columnDefinition ) ,
213
- primary_key : primaryKey + unique ,
214
- not_null : notNull ,
215
- default : defaultValue ,
216
- sparse,
217
- maskedWithFunction,
218
- encryptedWith,
219
- terminator,
220
- temporalTableTime,
221
- ...identityContainer ,
222
- } ) ;
218
+ const { name, persisted, computed, computedExpression } = columnDefinition ;
219
+
220
+ const statement =
221
+ computed && computedExpression
222
+ ? this . createComputedColumn ( {
223
+ name,
224
+ computedExpression,
225
+ persisted,
226
+ } )
227
+ : assignTemplates ( templates . columnDefinition , {
228
+ name,
229
+ type : decorateType ( type , columnDefinition ) ,
230
+ primary_key : primaryKey + unique ,
231
+ not_null : notNull ,
232
+ default : defaultValue ,
233
+ sparse,
234
+ maskedWithFunction,
235
+ encryptedWith,
236
+ terminator,
237
+ temporalTableTime,
238
+ ...identityContainer ,
239
+ } ) ;
223
240
224
241
return commentIfDeactivated ( statement , { isActivated : columnDefinition . isActivated } ) ;
225
242
} ,
@@ -464,6 +481,9 @@ module.exports = (baseProvider, options, app) => {
464
481
encryption,
465
482
hasMaxLength : columnDefinition . hasMaxLength || jsonSchema . type === 'jsonObject' ,
466
483
comment : jsonSchema . description ,
484
+ computed : jsonSchema . computed ,
485
+ computedExpression : jsonSchema . computedExpression ,
486
+ persisted : jsonSchema . persisted ,
467
487
...( canHaveIdentity ( jsonSchema . mode ) && {
468
488
identity : {
469
489
seed : Number ( _ . get ( jsonSchema , 'identity.identitySeed' , 0 ) ) ,
@@ -730,6 +750,22 @@ module.exports = (baseProvider, options, app) => {
730
750
} ) ;
731
751
} ,
732
752
753
+ alterComputedColumn ( fullTableName , columnName , columnDefinition ) {
754
+ const { computedExpression, persisted } = columnDefinition ;
755
+
756
+ const command = assignTemplates ( templates . alterComputedColumn , {
757
+ name : columnName ,
758
+ expression : wrapInBracketsIfNecessary ( computedExpression ) ,
759
+ persisted : persisted ? ' PERSISTED' : '' ,
760
+ } ) ;
761
+
762
+ return assignTemplates ( templates . alterTable , {
763
+ tableName : fullTableName ,
764
+ command,
765
+ terminator,
766
+ } ) ;
767
+ } ,
768
+
733
769
dropView ( fullViewName ) {
734
770
return assignTemplates ( templates . dropView , {
735
771
name : fullViewName ,
0 commit comments