109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
/**
|
||
|
* Generate SQL text for Field
|
||
|
* ==============================================================================
|
||
|
* @param {object} params - Single object params
|
||
|
* @param {import("../../types").DSQL_FieldSchemaType} params.columnData - Field object
|
||
|
* @param {boolean} [params.primaryKeySet] - Table Name(slug)
|
||
|
*
|
||
|
* @returns {{ fieldEntryText: string, newPrimaryKeySet: boolean }}
|
||
|
*/
|
||
|
module.exports = function generateColumnDescription({
|
||
|
columnData,
|
||
|
primaryKeySet,
|
||
|
}) {
|
||
|
/**
|
||
|
* Format tableInfoArray
|
||
|
*
|
||
|
* @description Format tableInfoArray
|
||
|
*/
|
||
|
const {
|
||
|
fieldName,
|
||
|
dataType,
|
||
|
nullValue,
|
||
|
primaryKey,
|
||
|
autoIncrement,
|
||
|
defaultValue,
|
||
|
defaultValueLiteral,
|
||
|
foreignKey,
|
||
|
updatedField,
|
||
|
onUpdate,
|
||
|
onUpdateLiteral,
|
||
|
onDelete,
|
||
|
onDeleteLiteral,
|
||
|
defaultField,
|
||
|
encrypted,
|
||
|
json,
|
||
|
newTempField,
|
||
|
notNullValue,
|
||
|
originName,
|
||
|
plainText,
|
||
|
pattern,
|
||
|
patternFlags,
|
||
|
richText,
|
||
|
} = columnData;
|
||
|
|
||
|
let fieldEntryText = "";
|
||
|
|
||
|
fieldEntryText += `\`${fieldName}\` ${dataType}`;
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
// if (String(fieldEntryText).match(/ UUID$/)) {
|
||
|
// fieldEntryText += ` DEFAULT UUID()`;
|
||
|
// } else
|
||
|
if (nullValue) {
|
||
|
fieldEntryText += " DEFAULT NULL";
|
||
|
} else if (defaultValueLiteral) {
|
||
|
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
|
||
|
} else if (defaultValue) {
|
||
|
if (String(defaultValue).match(/uuid\(\)/i)) {
|
||
|
fieldEntryText += ` DEFAULT UUID()`;
|
||
|
} else {
|
||
|
fieldEntryText += ` DEFAULT '${defaultValue}'`;
|
||
|
}
|
||
|
} else if (notNullValue) {
|
||
|
fieldEntryText += ` NOT NULL`;
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
if (onUpdateLiteral) {
|
||
|
fieldEntryText += ` ON UPDATE ${onUpdateLiteral}`;
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
if (primaryKey && !primaryKeySet) {
|
||
|
fieldEntryText += " PRIMARY KEY";
|
||
|
primaryKeySet = true;
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
if (autoIncrement) {
|
||
|
fieldEntryText += " AUTO_INCREMENT";
|
||
|
primaryKeySet = true;
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
return { fieldEntryText, newPrimaryKeySet: primaryKeySet || false };
|
||
|
};
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|