diff --git a/tools/js-class-c-struct-transpiler/test/testgen.js b/tools/js-class-c-struct-transpiler/test/testgen.js new file mode 100755 index 0000000..42c234d --- /dev/null +++ b/tools/js-class-c-struct-transpiler/test/testgen.js @@ -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}`); diff --git a/tools/js-class-c-struct-transpiler/test/testgen.sh b/tools/js-class-c-struct-transpiler/test/testgen.sh deleted file mode 100755 index c78f5a9..0000000 --- a/tools/js-class-c-struct-transpiler/test/testgen.sh +++ /dev/null @@ -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; diff --git a/tools/js-class-c-struct-transpiler/transpile.js b/tools/js-class-c-struct-transpiler/transpile.js index e6ee930..ec0f5d6 100644 --- a/tools/js-class-c-struct-transpiler/transpile.js +++ b/tools/js-class-c-struct-transpiler/transpile.js @@ -120,7 +120,7 @@ for (const type of Object.keys(schema)) { let jsData = ""; let foreignKeys = ""; 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} {`; jsData += `class ${type} {`; for (const prop of Object.keys(props)) { @@ -175,11 +175,10 @@ for (const type of Object.keys(schema)) { if (!importStatements.includes(importS)) { importStatements += importS; } - const foreignKey = `${propType.toLowerCase()}_id`; - const localKey = `${prop.toLowerCase()}_${foreignKey}`; + const localKey = `${prop.toLowerCase()}_id`; 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};`; break; case "array":