Major version update. Major bugfixes. Security vulnerabilites fixed
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import mapDataType from "./map-data-types";
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
export default function buildColumnDefinition(field, is_vector) {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
// vec0 can't parse quoted column names
|
||||
const fieldName = is_vector
|
||||
? field.sideCar
|
||||
? `+${field.fieldName}`
|
||||
: `${field.fieldName}`
|
||||
: q(field.fieldName);
|
||||
const parts = [fieldName];
|
||||
// Data type mapping
|
||||
parts.push(mapDataType(field));
|
||||
// Primary key
|
||||
if (field.primaryKey) {
|
||||
parts.push("PRIMARY KEY");
|
||||
if (field.autoIncrement) {
|
||||
parts.push("AUTOINCREMENT");
|
||||
}
|
||||
}
|
||||
// Not null
|
||||
if (field.notNullValue && !field.primaryKey) {
|
||||
parts.push("NOT NULL");
|
||||
}
|
||||
// Unique
|
||||
if (field.unique && !field.primaryKey) {
|
||||
parts.push("UNIQUE");
|
||||
}
|
||||
if (field.vectorDistanceMetric) {
|
||||
parts.push(`distance_metric=${field.vectorDistanceMetric}`);
|
||||
}
|
||||
// Default value
|
||||
if (field.defaultValue !== undefined) {
|
||||
if (typeof field.defaultValue === "string") {
|
||||
parts.push(
|
||||
// Escape single quotes by doubling them to prevent SQL injection and wrap in single quotes
|
||||
`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
|
||||
}
|
||||
else {
|
||||
parts.push(`DEFAULT ${field.defaultValue}`);
|
||||
}
|
||||
}
|
||||
else if (field.defaultValueLiteral) {
|
||||
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
|
||||
}
|
||||
return parts.join(" ");
|
||||
}
|
||||
Reference in New Issue
Block a user