Returns SQL statements to create a table with all its columns, primary key, indexes, and checks.
(table: DatabaseTable, alter?: boolean)
| 963 | |
| 964 | /** Returns SQL statements to create a table with all its columns, primary key, indexes, and checks. */ |
| 965 | createTable(table: DatabaseTable, alter?: boolean): string[] { |
| 966 | let sql = `create table ${table.getQuotedName()} (`; |
| 967 | |
| 968 | const columns = table.getColumns(); |
| 969 | const lastColumn = columns[columns.length - 1].name; |
| 970 | |
| 971 | for (const column of columns) { |
| 972 | const col = this.createTableColumn(column, table); |
| 973 | |
| 974 | if (col) { |
| 975 | const comma = column.name === lastColumn ? '' : ', '; |
| 976 | sql += col + comma; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | const primaryKey = table.getPrimaryKey(); |
| 981 | const createPrimary = |
| 982 | !table.getColumns().some(c => c.autoincrement && c.primary) || this.hasNonDefaultPrimaryKeyName(table); |
| 983 | |
| 984 | if (createPrimary && primaryKey) { |
| 985 | const name = this.hasNonDefaultPrimaryKeyName(table) ? `constraint ${this.quote(primaryKey.keyName)} ` : ''; |
| 986 | sql += `, ${name}primary key (${primaryKey.columnNames.map(c => this.quote(c)).join(', ')})`; |
| 987 | } |
| 988 | |
| 989 | sql += ')'; |
| 990 | sql += this.finalizeTable( |
| 991 | table, |
| 992 | this.platform.getConfig().get('charset'), |
| 993 | this.platform.getConfig().get('collate'), |
| 994 | ); |
| 995 | |
| 996 | const ret: string[] = []; |
| 997 | this.append(ret, sql); |
| 998 | this.append(ret, this.appendComments(table)); |
| 999 | |
| 1000 | for (const index of table.getIndexes()) { |
| 1001 | this.append(ret, this.createIndex(index, table)); |
| 1002 | } |
| 1003 | |
| 1004 | if (!alter) { |
| 1005 | for (const check of table.getChecks()) { |
| 1006 | this.append(ret, this.createCheck(table, check)); |
| 1007 | } |
| 1008 | |
| 1009 | for (const trigger of table.getTriggers()) { |
| 1010 | this.append(ret, this.createTrigger(table, trigger)); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | alterTableComment(table: DatabaseTable, comment?: string): string { |
| 1018 | return `alter table ${table.getQuotedName()} comment = ${this.platform.quoteValue(comment ?? '')}`; |
nothing calls this directly
no test coverage detected