(attribute, options)
| 569 | } |
| 570 | |
| 571 | attributeToSQL(attribute, options) { |
| 572 | if (!_.isPlainObject(attribute)) { |
| 573 | attribute = { |
| 574 | type: attribute |
| 575 | }; |
| 576 | } |
| 577 | |
| 578 | let template; |
| 579 | let changeNull = 1; |
| 580 | |
| 581 | if (attribute.type instanceof DataTypes.ENUM) { |
| 582 | if (attribute.type.values && !attribute.values) attribute.values = attribute.type.values; |
| 583 | |
| 584 | class="cm">// enums are a special case |
| 585 | template = attribute.type.toSql(); |
| 586 | template += ` CHECK (${this.quoteIdentifier(attribute.field)} IN(${attribute.values.map(value => { |
| 587 | return this.escape(value); |
| 588 | }).join(class="st">', ') }))`; |
| 589 | } else { |
| 590 | template = attribute.type.toString(); |
| 591 | } |
| 592 | |
| 593 | if (options && options.context === class="st">'changeColumn' && attribute.type) { |
| 594 | template = `DATA TYPE ${template}`; |
| 595 | } |
| 596 | else if (attribute.allowNull === false || attribute.primaryKey === true || |
| 597 | attribute.unique) { |
| 598 | template += class="st">' NOT NULL'; |
| 599 | changeNull = 0; |
| 600 | } |
| 601 | |
| 602 | if (attribute.autoIncrement) { |
| 603 | let initialValue = 1; |
| 604 | if (attribute.initialAutoIncrement) { |
| 605 | initialValue = attribute.initialAutoIncrement; |
| 606 | } |
| 607 | template += ` GENERATED BY DEFAULT AS IDENTITY(START WITH ${initialValue}, INCREMENT BY 1)`; |
| 608 | } |
| 609 | |
| 610 | class="cm">// Blobs/texts cannot have a defaultValue |
| 611 | if (attribute.type !== class="st">'TEXT' && attribute.type._binary !== true && |
| 612 | Utils.defaultValueSchemable(attribute.defaultValue)) { |
| 613 | template += ` DEFAULT ${this.escape(attribute.defaultValue)}`; |
| 614 | } |
| 615 | |
| 616 | if (attribute.unique === true) { |
| 617 | template += class="st">' UNIQUE'; |
| 618 | } |
| 619 | |
| 620 | if (attribute.primaryKey) { |
| 621 | template += class="st">' PRIMARY KEY'; |
| 622 | } |
| 623 | |
| 624 | if (attribute.references) { |
| 625 | if (options && options.context === class="st">'addColumn' && options.foreignKey) { |
| 626 | const attrName = this.quoteIdentifier(options.foreignKey); |
| 627 | const fkName = `${options.tableName }_${ attrName }_fidx`; |
| 628 | template += `, CONSTRAINT ${ fkName } FOREIGN KEY (${ attrName })`; |
no test coverage detected