MCPcopy
hub / github.com/sequelize/sequelize / attributeToSQL

Method attributeToSQL

src/dialects/db2/query-generator.js:581–673  ·  view source on GitHub ↗
(attribute, options)

Source from the content-addressed store, hash-verified

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

Callers 2

addColumnQueryMethod · 0.95
attributesToSQLMethod · 0.95

Calls 5

quoteIdentifierMethod · 0.95
quoteTableMethod · 0.80
toSqlMethod · 0.65
toStringMethod · 0.65
escapeMethod · 0.45

Tested by

no test coverage detected