(diagram)
| 37 | } |
| 38 | |
| 39 | export function toMSSQL(diagram) { |
| 40 | const tablesSql = diagram.tables |
| 41 | .map((table) => { |
| 42 | const fieldsSql = table.fields |
| 43 | .map((field) => { |
| 44 | const typeMetaData = dbToTypes[DB.MSSQL][field.type.toUpperCase()]; |
| 45 | const isSized = typeMetaData.isSized || typeMetaData.hasPrecision; |
| 46 | |
| 47 | return `\t[${field.name}] ${field.type}${field.size && isSized ? `(${field.size})` : ""}${ |
| 48 | field.notNull ? " NOT NULL" : "" |
| 49 | }${field.increment ? " IDENTITY" : ""}${ |
| 50 | field.unique ? " UNIQUE" : "" |
| 51 | }${ |
| 52 | field.default !== "" |
| 53 | ? ` DEFAULT ${parseDefault(field, diagram.database)}` |
| 54 | : "" |
| 55 | }${ |
| 56 | field.check === "" || |
| 57 | !dbToTypes[diagram.database][field.type].hasCheck |
| 58 | ? "" |
| 59 | : ` CHECK(${field.check})` |
| 60 | }`; |
| 61 | }) |
| 62 | .join(",\n"); |
| 63 | |
| 64 | const primaryKeys = table.fields.filter((f) => f.primary); |
| 65 | const primaryKeySql = |
| 66 | primaryKeys.length > 0 |
| 67 | ? `,\n\tPRIMARY KEY(${primaryKeys |
| 68 | .map((f) => `[${f.name}]`) |
| 69 | .join(", ")})` |
| 70 | : ""; |
| 71 | |
| 72 | const uniqueSql = uniqueConstraintClause(table, (s) => `[${s}]`); |
| 73 | |
| 74 | const createTableSql = `CREATE TABLE [${table.name}] (\n${fieldsSql}${primaryKeySql}${uniqueSql}\n);\nGO\n`; |
| 75 | |
| 76 | const tableCommentSql = generateAddExtendedPropertySQL( |
| 77 | table.comment, |
| 78 | table.name, |
| 79 | ); |
| 80 | |
| 81 | const columnCommentsSql = table.fields |
| 82 | .map((field) => |
| 83 | generateAddExtendedPropertySQL(field.comment, table.name, field.name), |
| 84 | ) |
| 85 | .join(""); |
| 86 | |
| 87 | const indicesSql = table.indices |
| 88 | .map( |
| 89 | (i) => |
| 90 | `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX [${ |
| 91 | i.name |
| 92 | }]\nON [${table.name}] (${i.fields |
| 93 | .map((f) => `[${f}]`) |
| 94 | .join(", ")});\nGO\n`, |
| 95 | ) |
| 96 | .join(""); |
no test coverage detected