1
1
import { Cardinality } from "../../data/constants" ;
2
2
import { dbToTypes } from "../../data/datatypes" ;
3
3
import i18n from "../../i18n/i18n" ;
4
- import { escapeQuotes , parseDefault } from "../exportSQL/shared" ;
4
+ import { escapeQuotes } from "../exportSQL/shared" ;
5
+ import { isFunction , isKeyword } from "../utils" ;
6
+
7
+ function parseDefaultDbml ( field , database ) {
8
+ if ( isFunction ( field . default ) ) {
9
+ return `\`${ field . default } \`` ;
10
+ }
11
+
12
+ if ( isKeyword ( field . default ) || ! dbToTypes [ database ] [ field . type ] . hasQuotes ) {
13
+ return field . default ;
14
+ }
15
+
16
+ return `'${ escapeQuotes ( field . default ) } '` ;
17
+ }
5
18
6
19
function columnDefault ( field , database ) {
7
20
if ( ! field . default || field . default . trim ( ) === "" ) {
8
21
return "" ;
9
22
}
10
23
11
- return `default: ${ parseDefault ( field , database ) } ` ;
24
+ return `default: ${ parseDefaultDbml ( field , database ) } ` ;
12
25
}
13
26
14
27
function columnComment ( field ) {
@@ -27,7 +40,7 @@ function columnSettings(field, database) {
27
40
field . notNull && constraints . push ( "not null" ) ;
28
41
field . unique && constraints . push ( "unique" ) ;
29
42
constraints . push ( columnDefault ( field , database ) ) ;
30
- constraints . push ( columnComment ( field , database ) ) ;
43
+ constraints . push ( columnComment ( field ) ) ;
31
44
32
45
constraints = constraints . filter ( ( x ) => Boolean ( x ) ) ;
33
46
@@ -99,7 +112,7 @@ export function toDBML(diagram) {
99
112
. join ( "\n\n" ) } ${ enumDefinitions } ${ diagram . tables
100
113
. map (
101
114
( table ) =>
102
- `Table ${ table . name } {\n${ table . fields
115
+ `Table ${ table . name } [headercolor: ${ table . color } ] {\n${ table . fields
103
116
. map (
104
117
( field ) =>
105
118
`\t${ field . name } ${
@@ -119,7 +132,7 @@ export function toDBML(diagram) {
119
132
( index ) =>
120
133
`\t\t(${ index . fields . join ( ", " ) } ) [ name: '${
121
134
index . name
122
- } '${ index . unique ? " unique" : "" } ]`,
135
+ } '${ index . unique ? ", unique" : "" } ]`,
123
136
)
124
137
. join ( "\n" ) +
125
138
"\n\t}"
0 commit comments