Updates
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import type { BUN_MARIADB_FieldSchemaType } from "../../types";
|
||||
import mapDataType from "./map-data-types";
|
||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||
|
||||
export default function buildColumnDefinition(
|
||||
field: BUN_MARIADB_FieldSchemaType,
|
||||
): string {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
|
||||
const parts: string[] = [MariaDBQuoteGen(field.fieldName)];
|
||||
parts.push(mapDataType(field));
|
||||
|
||||
if (field.autoIncrement) {
|
||||
parts.push("AUTO_INCREMENT");
|
||||
}
|
||||
|
||||
if (field.notNullValue || field.primaryKey || field.isVector) {
|
||||
if (!field.primaryKey) {
|
||||
parts.push("NOT NULL");
|
||||
}
|
||||
}
|
||||
|
||||
if (field.unique && !field.primaryKey) {
|
||||
parts.push("UNIQUE");
|
||||
}
|
||||
|
||||
if (field.defaultValue !== undefined) {
|
||||
if (typeof field.defaultValue === "string") {
|
||||
parts.push(`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
|
||||
} else {
|
||||
parts.push(`DEFAULT ${field.defaultValue}`);
|
||||
}
|
||||
} else if (field.defaultValueLiteral) {
|
||||
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
|
||||
}
|
||||
|
||||
if (field.onUpdate) {
|
||||
parts.push(`ON UPDATE ${field.onUpdate}`);
|
||||
} else if (field.onUpdateLiteral) {
|
||||
parts.push(`ON UPDATE ${field.onUpdateLiteral}`);
|
||||
}
|
||||
|
||||
return parts.join(" ");
|
||||
}
|
||||
Reference in New Issue
Block a user