(tableName, attributes, options)
| 52 | } |
| 53 | |
| 54 | createTableQuery(tableName, attributes, options) { |
| 55 | const query = class="st">'CREATE TABLE <%= table %> (<%= attributes %>)', |
| 56 | primaryKeys = [], |
| 57 | foreignKeys = {}, |
| 58 | attrStr = [], |
| 59 | commentTemplate = class="st">' -- <%= comment %>, ' + |
| 60 | class="st">'TableName = <%= table %>, ColumnName = <%= column %>;'; |
| 61 | |
| 62 | let commentStr = class="st">''; |
| 63 | |
| 64 | for (const attr in attributes) { |
| 65 | if (Object.prototype.hasOwnProperty.call(attributes, attr)) { |
| 66 | let dataType = attributes[attr]; |
| 67 | let match; |
| 68 | |
| 69 | if (dataType.includes(class="st">'COMMENT ')) { |
| 70 | const commentMatch = dataType.match(/^(.+) (COMMENT.*)$/); |
| 71 | if (commentMatch && commentMatch.length > 2) { |
| 72 | const commentText = commentMatch[2].replace(/COMMENT/, class="st">'').trim(); |
| 73 | commentStr += _.template(commentTemplate, this._templateSettings)({ |
| 74 | table: this.quoteIdentifier(tableName), |
| 75 | comment: this.escape(commentText), |
| 76 | column: this.quoteIdentifier(attr) |
| 77 | }); |
| 78 | class="cm">// remove comment related substring from dataType |
| 79 | dataType = commentMatch[1]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (_.includes(dataType, class="st">'PRIMARY KEY')) { |
| 84 | primaryKeys.push(attr); |
| 85 | |
| 86 | if (_.includes(dataType, class="st">'REFERENCES')) { |
| 87 | class="cm">// Db2 doesn't support inline REFERENCES declarations: move to the end |
| 88 | match = dataType.match(/^(.+) (REFERENCES.*)$/); |
| 89 | attrStr.push(`${ this.quoteIdentifier(attr) } ${ match[1].replace(/PRIMARY KEY/, class="st">'') }`); |
| 90 | foreignKeys[attr] = match[2]; |
| 91 | } else { |
| 92 | attrStr.push(`${ this.quoteIdentifier(attr) } ${ dataType.replace(/PRIMARY KEY/, class="st">'') }`); |
| 93 | } |
| 94 | } else if (_.includes(dataType, class="st">'REFERENCES')) { |
| 95 | class="cm">// Db2 doesn't support inline REFERENCES declarations: move to the end |
| 96 | match = dataType.match(/^(.+) (REFERENCES.*)$/); |
| 97 | attrStr.push(`${this.quoteIdentifier(attr)} ${match[1]}`); |
| 98 | foreignKeys[attr] = match[2]; |
| 99 | } else { |
| 100 | if (options && options.uniqueKeys) { |
| 101 | for (const ukey in options.uniqueKeys) { |
| 102 | if (options.uniqueKeys[ukey].fields.includes(attr) && |
| 103 | ! _.includes(dataType, class="st">'NOT NULL')) |
| 104 | { |
| 105 | dataType += class="st">' NOT NULL'; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | attrStr.push(`${this.quoteIdentifier(attr)} ${dataType}`); |
| 111 | } |
no test coverage detected