@abaplint/transpiler
Version:
79 lines • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SnowflakeDatabaseSchema = void 0;
const abaplint = require("@abaplint/core");
class SnowflakeDatabaseSchema {
reg;
constructor(reg) {
this.reg = reg;
}
buildVIEW(_view) {
return "Error: buildView, todo snowflake";
}
buildTABL(tabl) {
const type = tabl.parseType(this.reg);
if (!(type instanceof abaplint.BasicTypes.StructureType)) {
return "";
}
const fields = [];
const fieldsRaw = [];
for (const field of type.getComponents()) {
if (field.type instanceof abaplint.BasicTypes.StructureType) {
// is a GROUP NAME
continue;
}
fieldsRaw.push(field.name.toLowerCase());
fields.push("\"" + field.name.toLowerCase() + "\" " + this.toType(field.type, field.name, tabl.getName()));
}
// assumption: all transparent tables have primary keys
// add single quotes to field names to allow for keywords as field names
const key = ", PRIMARY KEY(" + tabl.listKeys(this.reg)
.filter(e => fieldsRaw.includes(e.toLowerCase()))
.map(e => "\"" + e.toLowerCase() + "\"").join(",") + ")";
return `CREATE TABLE "${tabl.getName().toLowerCase()}" (${fields.join(", ")}${key});\n`;
}
// https://docs.snowflake.com/en/sql-reference/collation
toType(type, fieldname, errorInfo) {
if (type instanceof abaplint.BasicTypes.CharacterType) {
return `NCHAR(${type.getLength()}) COLLATE 'rtrim'`;
}
else if (type instanceof abaplint.BasicTypes.TimeType) {
return `NCHAR(6)`;
}
else if (type instanceof abaplint.BasicTypes.DateType) {
return `NCHAR(8)`;
}
else if (type instanceof abaplint.BasicTypes.NumericType) {
// it will be fine, the runtime representation of numc is also text
return `NCHAR(${type.getLength()})`;
}
else if (type instanceof abaplint.BasicTypes.StringType) {
return `TEXT`;
}
else if (type instanceof abaplint.BasicTypes.XStringType) {
// it will be fine, the runtime representation of xstring is also text
return `TEXT`;
}
else if (type instanceof abaplint.BasicTypes.HexType) {
return `NCHAR(${type.getLength() * 2})`;
}
else if (type instanceof abaplint.BasicTypes.IntegerType) {
return `INT`;
}
else if (type instanceof abaplint.BasicTypes.FloatType
|| type instanceof abaplint.BasicTypes.FloatingPointType) {
return `REAL`;
}
else if (type instanceof abaplint.BasicTypes.PackedType) {
return `DECIMAL(${type.getLength()},${type.getDecimals()})`;
}
else if (type instanceof abaplint.BasicTypes.VoidType) {
throw `Type of ${errorInfo}-${fieldname} is VoidType(${type.getVoided()}), make sure the type is known, enable strict syntax checking`;
}
else {
throw "database_setup: " + errorInfo + "-" + fieldname + ", todo toType handle: " + type.constructor.name;
}
}
}
exports.SnowflakeDatabaseSchema = SnowflakeDatabaseSchema;
//# sourceMappingURL=snowflake_database_schema.js.map