new(basic.gblib): TABLE keyword #375.

This commit is contained in:
Rodrigo Rodriguez 2023-10-02 16:22:51 -03:00
parent 0385cad21f
commit 0012fd0f89
3 changed files with 78 additions and 26 deletions

View file

@ -77,6 +77,7 @@
"@semantic-release/exec": "5.0.0", "@semantic-release/exec": "5.0.0",
"@semantic-release/git": "9.0.0", "@semantic-release/git": "9.0.0",
"@sendgrid/mail": "7.7.0", "@sendgrid/mail": "7.7.0",
"@sequelize/core": "7.0.0-alpha.29",
"@types/node": "18.11.9", "@types/node": "18.11.9",
"@types/validator": "13.7.10", "@types/validator": "13.7.10",
"adm-zip": "0.5.9", "adm-zip": "0.5.9",

View file

@ -53,6 +53,8 @@ import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
import { GuaribasUser } from '../../security.gbapp/models/index.js'; import { GuaribasUser } from '../../security.gbapp/models/index.js';
import { SystemKeywords } from './SystemKeywords.js'; import { SystemKeywords } from './SystemKeywords.js';
import lineReplace from 'line-replace'; import lineReplace from 'line-replace';
import { Sequelize, DataTypes } from '@sequelize/core';
import { table } from 'console';
/** /**
* @fileoverview Decision was to priorize security(isolation) and debugging, * @fileoverview Decision was to priorize security(isolation) and debugging,
@ -147,6 +149,7 @@ export class GBVMService extends GBService {
} }
// Hot swap for .vbs files. // Hot swap for .vbs files.
const fullFilename = urlJoin(folder, filename); const fullFilename = urlJoin(folder, filename);
if (process.env.DEV_HOTSWAP) { if (process.env.DEV_HOTSWAP) {
Fs.watchFile(fullFilename, async () => { Fs.watchFile(fullFilename, async () => {
@ -168,9 +171,26 @@ export class GBVMService extends GBService {
} else { } else {
await this.translateBASIC(mainName, fullFilename, min); await this.translateBASIC(mainName, fullFilename, min);
} }
// Syncronizes Database Objects with the ones returned from "Word".
const tablesFile = urlJoin(folder, 'tables.json');
if (Fs.existsSync(tablesFile)) {
const minBoot = GBServer.globals.minBoot;
GBLogEx.info(min, `BASIC: Reading tables and sync storage for ${min.botId}...`);
const tables = JSON.parse(Fs.readFileSync(tablesFile, 'utf8'));
tables.forEach(t => {
minBoot.core.sequelize.define(t.name, t.fields);
});
await minBoot.core.sequelize.sync();
}
const parsedCode: string = Fs.readFileSync(jsfile, 'utf8'); const parsedCode: string = Fs.readFileSync(jsfile, 'utf8');
min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode; min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode;
return filename; return filename;
} }
public async translateBASIC(mainName, filename: any, min: GBMinInstance) { public async translateBASIC(mainName, filename: any, min: GBMinInstance) {
@ -322,20 +342,25 @@ export class GBVMService extends GBService {
for (let i = 0; i < tasks.length; i++) { for (let i = 0; i < tasks.length; i++) {
const task = tasks[i]; const task = tasks[i];
if (task.kind === 'createTable') { if (task.kind === 'writeTableDefinition') {
const minBoot = GBServer.globals.minBoot;
let sql = `CREATE TABLE ${task.name}`; // Creates an empty object that will receive Sequelize fields.
let obj = {name: task.name};
obj['fields'] = [];
const fields = task.fields; const fields = task.fields;
fields.forEach(f => { fields.forEach(f => {
const size = f.size ? '(' + f.size + ')' : ''; let field = {};
const nullable = f.required ? 'NOT NULL' : 'NULL'; field[f.name] = f;
sql = `${sql}\n${f.name} ${f.kind} ${size} ${nullable}`
obj['fields'].push(field);
}); });
await minBoot.core.sequelize.query(sql); const jsonFile = `${task.name}.table.json`;
Fs.writeFileSync(jsonFile, JSON.stringify(obj));
} }
@ -429,26 +454,39 @@ export class GBVMService extends GBService {
let required = line.indexOf('*') !== -1; let required = line.indexOf('*') !== -1;
line = line.replace('*', ''); line = line.replace('*', '');
const field = /^\s*(\w+)\s*(\w+)(\((\d+)\))?/gim; const fieldRegExp = /^\s*(\w+)\s*(\w+)(\((\d+)\))?/gim;
let reg = field.exec(line); let reg = fieldRegExp.exec(line);
const t = reg[2]; const t = reg[2];
const n = reg[1];
let obj = { required: required, name: reg[1] }; let obj = {};
let field = {allowNull: !required };
obj[n] = field;
if (t === 'string') { const getTypeBasedOnCondition = (t) => {
obj['size'] = eval(reg[3]); switch (t) {
obj['kind'] = 'nvarchar'; case 'string':
} else if (t === 'key') { return DataTypes.STRING;
obj['kind'] = 'key'; case 'key':
} else if (t === 'integer') { return DataTypes.STRING; // Assuming key is a string data type
obj['kind'] = 'INT'; case 'integer':
} else if (t === 'double') { return DataTypes.INTEGER;
obj['kind'] = 'FLOAT'; case 'double':
} else if (t === 'date') { return DataTypes.FLOAT;
obj['kind'] = 'DATETIME'; case 'date':
} else if (t === 'boolean') { return DataTypes.DATE;
obj['kind'] = 'BIT'; case 'boolean':
return DataTypes.BOOLEAN;
default:
return DataTypes.STRING; // Default to string if the type is unknown
}
};
field['type'] = getTypeBasedOnCondition(t);
if (reg[3]){
field['size'] = getTypeBasedOnCondition(t);
} }
return obj; return obj;
@ -504,6 +542,7 @@ export class GBVMService extends GBService {
} }
// Inside BEGIN/END table pair containing FIELDS. // Inside BEGIN/END table pair containing FIELDS.
if (table) { if (table) {
const field = this.parseField(line); const field = this.parseField(line);
fields.push(field); fields.push(field);
@ -516,13 +555,13 @@ export class GBVMService extends GBService {
emmit = false; emmit = false;
} }
const endTableKeyword = /^\s*END TABLE"/gim; const endTableKeyword = /^\s*END TABLE"/gim;
let endTableReg = endTableKeyword.exec(line); let endTableReg = endTableKeyword.exec(line);
if (endTableReg) { if (endTableReg) {
tasks.push({ tasks.push({
kind: 'createTable', name: table, fields: fields kind: 'writeTableDefinition', name: table, fields: fields
}); });
fields = []; fields = [];

View file

@ -995,6 +995,18 @@ export class KeywordsExpressions {
} }
]; ];
keywords[i++] = [
/^\s*(save)(\s*)(.*)(.*)/gim,
($0, $1, $2, $3, $4) => {
$3 = $3.replace(/\'/g, '');
$3 = $3.replace(/\"/g, '');
$4 = $4.substr(2);
const fields = $4.split(',').
return `await sys.save({pid: pid, file: "${$3}", args: [${$4}]}, fields)`;
}
];
keywords[i++] = [ keywords[i++] = [
/^\s*set\s*(.*)/gim, /^\s*set\s*(.*)/gim,
($0, $1, $2) => { ($0, $1, $2) => {