fql-toolkit
Version:
121 lines (108 loc) • 3.69 kB
JavaScript
const buildStringFromCondition = (condition) => {
if (Array.isArray(condition)) {
const [operator, ...operands] = condition;
if (operator === "symb") {
return operands.join(".");
} else {
return operands.map(buildStringFromCondition).join(` ${operator} `);
}
} else if (typeof condition === 'string') {
return `'${condition}'`;
}
return condition;
};
const formatValue = (value) => {
if (typeof value === 'string') {
return `'${value}'`;
} else if (Array.isArray(value)) {
return "(" + value.map(formatValue).join(", ") + ")";
} else {
return value;
}
};
const getPathString = (path) => {
if (typeof path === 'string') {
return path;
}
return path.map(element => {
if (typeof element === 'string') {
return element;
} else if (Array.isArray(element)) {
return "(" + element.map(getPathString).join(", ") + ")";
}
}).join(".");
};
export default class FQLQueryBuilder {
static buildCreateFormQuery(formData) {
const { name, specs } = formData;
const specsString = specs.map(({ name, type, cardinality, path, unique, notNull, totally }) => {
if (type === 'reference') {
let spec = `${name} references ${cardinality[0] + ".." + cardinality[1]} ${getPathString(path)}`;
if (unique) { spec += " unique"; }
if (totally) { spec += " totally unique"; }
return spec;
} else {
let spec = `${name} ${type}`;
if (notNull) { spec += " not null"; }
if (unique) { spec += " unique"; }
return spec;
}
}).join(", ");
return `create form ${name}(${specsString})`;
}
static buildShowFormsQuery(forms) {
return forms.length !== 0 ? `show forms ${forms.join(" ")}` : "show forms";
}
static buildRemoveFormsQuery(forms) {
return forms.length !== 0 ? `remove forms ${forms.join(" ")}` : "remove forms";
}
static buildCreateNewQuery(formName, values) {
const formattedValues = values.map(formatValue).join(', ');
return `create ${formName}(${formattedValues})`;
}
static buildGetCaseQuery(formName, fields = [], condition = null) {
let result = `get ${formName}`;
if (fields.length !== 0) {
result += `(${fields.join(", ")})`;
}
if (condition) {
result += ` with ${buildStringFromCondition(condition)}`;
}
return result;
}
static buildRemoveCaseQuery(formName, condition = null) {
let result = `remove ${formName}`;
if (condition) {
result += ` with ${buildStringFromCondition(condition)}`;
}
return result;
}
static buildModifyCaseQuery(formName, values, condition = null) {
let result = `modify ${formName}(${values.map(formatValue).join(', ')})`;
if (condition) {
result += ` with ${buildStringFromCondition(condition)}`;
}
return result;
}
static buildDefineRuleQuery(ruleName, formName, fields, during, condition, action, dict) {
let result = `define rule ${ruleName}: on ${formName}`;
if (fields.length !== 0) {
result += `(${fields.join(", ")})`;
}
if (during.length !== 0) {
result += ` during (${during.join(" ")})`;
}
if (action !== "is unique") {
result += ` when ${buildStringFromCondition(condition)}`;
}
if (action === "set") {
result += ` set ${dict.map(({ key, value }) => `${key} = ${typeof value === 'string' ? `'${value}'` : value}`).join(", ")}`;
} else {
result += ` ${action}`;
}
return result;
}
static buildDefineConceptQuery(conceptName, entityName, condition) {
return `define concept ${conceptName} is ${entityName} with ${buildStringFromCondition(condition)}`;
}
}