Add returning statement

This commit is contained in:
zongor 2024-05-27 22:56:25 -04:00
parent 8ca89a09d2
commit 598a3eefe0
3 changed files with 14 additions and 12 deletions

View File

@ -0,0 +1,11 @@
import { $ } from "bun";
await $`bun run ../transpile.js -S schema.json -j out/ -c out/ -s out/`;
await $`rm -rf out/test.db`;
await $`sqlite3 out/test.db < out/types.sql`;
const vid1 = await $`echo 'INSERT INTO Vector3 (x, y, z) VALUES (-9.0, 9.0, 4.0) RETURNING id;' | sqlite3 out/test.db`.text();
const vid2 = await $`echo 'INSERT INTO Vector3 (x, y, z) VALUES (9.0, 9.0, 0.0) RETURNING id;' | sqlite3 out/test.db`.text();
const vid3 = await $`echo 'INSERT INTO Vector3 (x, y, z) VALUES (0.0, 1.0, 0.0) RETURNING id;' | sqlite3 out/test.db`.text();
const cid = await $`echo 'INSERT INTO Camera3D (position_id, target_id, up_id, fovy, projection) VALUES (${vid1}, ${vid2}, ${vid3}, 60.0, 0) RETURNING id;' | sqlite3 out/test.db`.text();
const eid = await $`echo "INSERT INTO Entity (Name, camera_id, Strength, Endurance, Intelligence, Wisdom, Charisma, Faith, Artisan, Metalworking, Alchemy, Engineering, Culinary, Ranged, Melee, Acrobatics, Prestidigitation, Language, Medicine, Thaumatology, Theology, Fishing, Mining, Survival, Gardening, History, Perception, Luck, Stealth, Attractiveness, Affluence, Notoriety) VALUES ('Zongor',${cid},1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,42,1,1,1,1) RETURNING id;" | sqlite3 out/test.db`.text();
console.log(`generated Entity=${eid} Camera3D=${cid} Vector3=${vid1} Vector3=${vid2} Vector3=${vid3}`);

View File

@ -1,8 +0,0 @@
bun run ../transpile.js -S schema.json -j out/ -c out/ -s out/;
rm -rf out/test.db;
sqlite3 out/test.db < out/types.sql;
echo 'INSERT INTO Vector3 (x, y, z) VALUES (-9.0, 9.0, 4.0);' | sqlite3 out/test.db;
echo 'INSERT INTO Vector3 (x, y, z) VALUES (9.0, 9.0, 0.0);' | sqlite3 out/test.db;
echo 'INSERT INTO Vector3 (x, y, z) VALUES (0.0, 1.0, 0.0);' | sqlite3 out/test.db;
echo 'INSERT INTO Camera3D (position_vector3_id, target_vector3_id, up_vector3_id, fovy, projection) VALUES (1, 2, 3, 60.0, 0);' | sqlite3 out/test.db;
echo "INSERT INTO Entity (Name, camera_camera3d_id, Strength, Endurance, Intelligence, Wisdom, Charisma, Faith, Artisan, Metalworking, Alchemy, Engineering, Culinary, Ranged, Melee, Acrobatics, Prestidigitation, Language, Medicine, Thaumatology, Theology, Fishing, Mining, Survival, Gardening, History, Perception, Luck, Stealth, Attractiveness, Affluence, Notoriety) VALUES ('Zongor',1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,42,1,1,1,1);" | sqlite3 out/test.db;

View File

@ -120,7 +120,7 @@ for (const type of Object.keys(schema)) {
let jsData = ""; let jsData = "";
let foreignKeys = ""; let foreignKeys = "";
const props = schema[type]; const props = schema[type];
sqlData += `CREATE TABLE ${type} (${type.toLowerCase()}_id INTEGER PRIMARY KEY AUTOINCREMENT`; sqlData += `CREATE TABLE ${type} (id INTEGER PRIMARY KEY AUTOINCREMENT`;
cData += `typedef struct ${type} {`; cData += `typedef struct ${type} {`;
jsData += `class ${type} {`; jsData += `class ${type} {`;
for (const prop of Object.keys(props)) { for (const prop of Object.keys(props)) {
@ -175,11 +175,10 @@ for (const type of Object.keys(schema)) {
if (!importStatements.includes(importS)) { if (!importStatements.includes(importS)) {
importStatements += importS; importStatements += importS;
} }
const foreignKey = `${propType.toLowerCase()}_id`; const localKey = `${prop.toLowerCase()}_id`;
const localKey = `${prop.toLowerCase()}_${foreignKey}`;
sqlData += `, ${localKey} INTEGER`; sqlData += `, ${localKey} INTEGER`;
foreignKeys += `\n, FOREIGN KEY(${localKey}) REFERENCES ${propType}(${foreignKey})` foreignKeys += `\n, FOREIGN KEY(${localKey}) REFERENCES ${propType}(id)`
cData += `\n\t\t${types[propType].c} ${prop};`; cData += `\n\t\t${types[propType].c} ${prop};`;
break; break;
case "array": case "array":