add sql types

This commit is contained in:
zongor 2024-05-27 20:19:37 -04:00
parent 15739c4d43
commit db2164f498
7 changed files with 164 additions and 137 deletions

View File

@ -1,34 +1,34 @@
typedef struct Entity { typedef struct Entity {
char Name[24]; char Name[24];
int Strength; unsigned char Strength;
int Endurance; unsigned char Endurance;
int Intelligence; unsigned char Intelligence;
int Wisdom; unsigned char Wisdom;
int Charisma; unsigned char Charisma;
int Faith; unsigned char Faith;
int Artisan; unsigned char Artisan;
int Metalworking; unsigned char Metalworking;
int Alchemy; unsigned char Alchemy;
int Engineering; unsigned char Engineering;
int Culinary; unsigned char Culinary;
int Ranged; unsigned char Ranged;
int Melee; unsigned char Melee;
int Acrobatics; unsigned char Acrobatics;
int Prestidigitation; unsigned char Prestidigitation;
int Language; unsigned char Language;
int Medicine; unsigned char Medicine;
int Thaumitology; unsigned char Thaumitology;
int Theology; unsigned char Theology;
int Fishing; unsigned char Fishing;
int Mining; unsigned char Mining;
int Survival; unsigned char Survival;
int Gardening; unsigned char Gardening;
int History; unsigned char History;
int Perception; unsigned char Perception;
int Luck; unsigned char Luck;
int Stealth; unsigned char Stealth;
int Attractiveness; unsigned char Attractiveness;
int Affluence; unsigned char Affluence;
int Notoriety; unsigned char Notoriety;
} Entity; } Entity;

View File

@ -6,190 +6,190 @@ class Entity {
this._data.set(this._encoder.encode(v), 0); this._data.set(this._encoder.encode(v), 0);
} }
get Strength() { get Strength() {
return this._data.getInt32(24, true); return this._data.getUint8(24, true);
} }
set Strength(v) { set Strength(v) {
return this._data.setInt32(24, v, true); return this._data.setUint8(24, v, true);
} }
get Endurance() { get Endurance() {
return this._data.getInt32(28, true); return this._data.getUint8(25, true);
} }
set Endurance(v) { set Endurance(v) {
return this._data.setInt32(28, v, true); return this._data.setUint8(25, v, true);
} }
get Intelligence() { get Intelligence() {
return this._data.getInt32(32, true); return this._data.getUint8(26, true);
} }
set Intelligence(v) { set Intelligence(v) {
return this._data.setInt32(32, v, true); return this._data.setUint8(26, v, true);
} }
get Wisdom() { get Wisdom() {
return this._data.getInt32(36, true); return this._data.getUint8(27, true);
} }
set Wisdom(v) { set Wisdom(v) {
return this._data.setInt32(36, v, true); return this._data.setUint8(27, v, true);
} }
get Charisma() { get Charisma() {
return this._data.getInt32(40, true); return this._data.getUint8(28, true);
} }
set Charisma(v) { set Charisma(v) {
return this._data.setInt32(40, v, true); return this._data.setUint8(28, v, true);
} }
get Faith() { get Faith() {
return this._data.getInt32(44, true); return this._data.getUint8(29, true);
} }
set Faith(v) { set Faith(v) {
return this._data.setInt32(44, v, true); return this._data.setUint8(29, v, true);
} }
get Artisan() { get Artisan() {
return this._data.getInt32(48, true); return this._data.getUint8(30, true);
} }
set Artisan(v) { set Artisan(v) {
return this._data.setInt32(48, v, true); return this._data.setUint8(30, v, true);
} }
get Metalworking() { get Metalworking() {
return this._data.getInt32(52, true); return this._data.getUint8(31, true);
} }
set Metalworking(v) { set Metalworking(v) {
return this._data.setInt32(52, v, true); return this._data.setUint8(31, v, true);
} }
get Alchemy() { get Alchemy() {
return this._data.getInt32(56, true); return this._data.getUint8(32, true);
} }
set Alchemy(v) { set Alchemy(v) {
return this._data.setInt32(56, v, true); return this._data.setUint8(32, v, true);
} }
get Engineering() { get Engineering() {
return this._data.getInt32(60, true); return this._data.getUint8(33, true);
} }
set Engineering(v) { set Engineering(v) {
return this._data.setInt32(60, v, true); return this._data.setUint8(33, v, true);
} }
get Culinary() { get Culinary() {
return this._data.getInt32(64, true); return this._data.getUint8(34, true);
} }
set Culinary(v) { set Culinary(v) {
return this._data.setInt32(64, v, true); return this._data.setUint8(34, v, true);
} }
get Ranged() { get Ranged() {
return this._data.getInt32(68, true); return this._data.getUint8(35, true);
} }
set Ranged(v) { set Ranged(v) {
return this._data.setInt32(68, v, true); return this._data.setUint8(35, v, true);
} }
get Melee() { get Melee() {
return this._data.getInt32(72, true); return this._data.getUint8(36, true);
} }
set Melee(v) { set Melee(v) {
return this._data.setInt32(72, v, true); return this._data.setUint8(36, v, true);
} }
get Acrobatics() { get Acrobatics() {
return this._data.getInt32(76, true); return this._data.getUint8(37, true);
} }
set Acrobatics(v) { set Acrobatics(v) {
return this._data.setInt32(76, v, true); return this._data.setUint8(37, v, true);
} }
get Prestidigitation() { get Prestidigitation() {
return this._data.getInt32(80, true); return this._data.getUint8(38, true);
} }
set Prestidigitation(v) { set Prestidigitation(v) {
return this._data.setInt32(80, v, true); return this._data.setUint8(38, v, true);
} }
get Language() { get Language() {
return this._data.getInt32(84, true); return this._data.getUint8(39, true);
} }
set Language(v) { set Language(v) {
return this._data.setInt32(84, v, true); return this._data.setUint8(39, v, true);
} }
get Medicine() { get Medicine() {
return this._data.getInt32(88, true); return this._data.getUint8(40, true);
} }
set Medicine(v) { set Medicine(v) {
return this._data.setInt32(88, v, true); return this._data.setUint8(40, v, true);
} }
get Thaumitology() { get Thaumitology() {
return this._data.getInt32(92, true); return this._data.getUint8(41, true);
} }
set Thaumitology(v) { set Thaumitology(v) {
return this._data.setInt32(92, v, true); return this._data.setUint8(41, v, true);
} }
get Theology() { get Theology() {
return this._data.getInt32(96, true); return this._data.getUint8(42, true);
} }
set Theology(v) { set Theology(v) {
return this._data.setInt32(96, v, true); return this._data.setUint8(42, v, true);
} }
get Fishing() { get Fishing() {
return this._data.getInt32(100, true); return this._data.getUint8(43, true);
} }
set Fishing(v) { set Fishing(v) {
return this._data.setInt32(100, v, true); return this._data.setUint8(43, v, true);
} }
get Mining() { get Mining() {
return this._data.getInt32(104, true); return this._data.getUint8(44, true);
} }
set Mining(v) { set Mining(v) {
return this._data.setInt32(104, v, true); return this._data.setUint8(44, v, true);
} }
get Survival() { get Survival() {
return this._data.getInt32(108, true); return this._data.getUint8(45, true);
} }
set Survival(v) { set Survival(v) {
return this._data.setInt32(108, v, true); return this._data.setUint8(45, v, true);
} }
get Gardening() { get Gardening() {
return this._data.getInt32(112, true); return this._data.getUint8(46, true);
} }
set Gardening(v) { set Gardening(v) {
return this._data.setInt32(112, v, true); return this._data.setUint8(46, v, true);
} }
get History() { get History() {
return this._data.getInt32(116, true); return this._data.getUint8(47, true);
} }
set History(v) { set History(v) {
return this._data.setInt32(116, v, true); return this._data.setUint8(47, v, true);
} }
get Perception() { get Perception() {
return this._data.getInt32(120, true); return this._data.getUint8(48, true);
} }
set Perception(v) { set Perception(v) {
return this._data.setInt32(120, v, true); return this._data.setUint8(48, v, true);
} }
get Luck() { get Luck() {
return this._data.getInt32(124, true); return this._data.getUint8(49, true);
} }
set Luck(v) { set Luck(v) {
return this._data.setInt32(124, v, true); return this._data.setUint8(49, v, true);
} }
get Stealth() { get Stealth() {
return this._data.getInt32(128, true); return this._data.getUint8(50, true);
} }
set Stealth(v) { set Stealth(v) {
return this._data.setInt32(128, v, true); return this._data.setUint8(50, v, true);
} }
get Attractiveness() { get Attractiveness() {
return this._data.getInt32(132, true); return this._data.getUint8(51, true);
} }
set Attractiveness(v) { set Attractiveness(v) {
return this._data.setInt32(132, v, true); return this._data.setUint8(51, v, true);
} }
get Affluence() { get Affluence() {
return this._data.getInt32(136, true); return this._data.getUint8(52, true);
} }
set Affluence(v) { set Affluence(v) {
return this._data.setInt32(136, v, true); return this._data.setUint8(52, v, true);
} }
get Notoriety() { get Notoriety() {
return this._data.getInt32(140, true); return this._data.getUint8(53, true);
} }
set Notoriety(v) { set Notoriety(v) {
return this._data.setInt32(140, v, true); return this._data.setUint8(53, v, true);
} }
get bytes() { get bytes() {
return new Uint8Array(this._ptr); return new Uint8Array(this._ptr);
} }
constructor(init = {}, ptr = undefined) { constructor(init = {}, ptr = undefined) {
this._size = 144; this._size = 54;
this._ptr = ptr.buffer || new ArrayBuffer(this._size); this._ptr = ptr.buffer || new ArrayBuffer(this._size);
this._data = new DataView(this._ptr); this._data = new DataView(this._ptr);

View File

@ -1,2 +1,2 @@
#!/bin/bash #!/bin/bash
bun run ../tools/js-class-c-struct-transpiler/transpile.js -S schema.json -j components/ -c ../client/ bun run ../tools/js-class-c-struct-transpiler/transpile.js -S schema.json -j components/ -c ../client/ -s sql/

View File

@ -6,123 +6,123 @@
"size": "24" "size": "24"
}, },
"Strength": { "Strength": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Endurance": { "Endurance": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Intelligence": { "Intelligence": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Wisdom": { "Wisdom": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Charisma": { "Charisma": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Faith": { "Faith": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Artisan": { "Artisan": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Metalworking": { "Metalworking": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Alchemy": { "Alchemy": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Engineering": { "Engineering": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Culinary": { "Culinary": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Ranged": { "Ranged": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Melee": { "Melee": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Acrobatics": { "Acrobatics": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Prestidigitation": { "Prestidigitation": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Language": { "Language": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Medicine": { "Medicine": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Thaumitology": { "Thaumitology": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Theology": { "Theology": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Fishing": { "Fishing": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Mining": { "Mining": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Survival": { "Survival": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Gardening": { "Gardening": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"History": { "History": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Perception": { "Perception": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Luck": { "Luck": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Stealth": { "Stealth": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Attractiveness": { "Attractiveness": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Affluence": { "Affluence": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
}, },
"Notoriety": { "Notoriety": {
"type": "i32", "type": "u8",
"kind": "scalar" "kind": "scalar"
} }
} }

2
server/sql/types.sql Normal file
View File

@ -0,0 +1,2 @@
CREATE TABLE Entity (entity_id INTEGER PRIMARY KEY AUTOINCREMENT, string TEXT, Strength INTEGER, Endurance INTEGER, Intelligence INTEGER, Wisdom INTEGER, Charisma INTEGER, Faith INTEGER, Artisan INTEGER, Metalworking INTEGER, Alchemy INTEGER, Engineering INTEGER, Culinary INTEGER, Ranged INTEGER, Melee INTEGER, Acrobatics INTEGER, Prestidigitation INTEGER, Language INTEGER, Medicine INTEGER, Thaumitology INTEGER, Theology INTEGER, Fishing INTEGER, Mining INTEGER, Survival INTEGER, Gardening INTEGER, History INTEGER, Perception INTEGER, Luck INTEGER, Stealth INTEGER, Attractiveness INTEGER, Affluence INTEGER, Notoriety INTEGER);

View File

@ -1 +1 @@
bun run transpile.js -S schema.json -j out/ -c out/ bun run ../transpile.js -S schema.json -j out/ -c out/ -s out/

View File

@ -4,41 +4,59 @@ let types = {
u8: { u8: {
c: "unsigned char", c: "unsigned char",
js: "Uint8", js: "Uint8",
sql: "INTEGER",
size: 1, size: 1,
}, },
i8: { i8: {
c: "char", c: "char",
js: "Int8", js: "Int8",
sql: "INTEGER",
size: 1, size: 1,
}, },
u16: { u16: {
c: "unsigned short", c: "unsigned short",
js: "Uint16", js: "Uint16",
sql: "INTEGER",
size: 2, size: 2,
}, },
i16: { i16: {
c: "short", c: "short",
js: "Int16", js: "Int16",
sql: "INTEGER",
size: 2, size: 2,
}, },
u32: { u32: {
c: "unisgned int", c: "unisgned int",
js: "Uint32", js: "Uint32",
sql: "INTEGER",
size: 4, size: 4,
}, },
i32: { i32: {
c: "int", c: "int",
js: "Int32", js: "Int32",
sql: "INTEGER",
size: 4, size: 4,
}, },
i64: {
c: "long",
js: "BigInt64",
sql: "INTEGER"
},
u64: {
c: "unsigned long",
js: "BigUint64",
sql: "INTEGER"
},
f32: { f32: {
c: "float", c: "float",
js: "Float32", js: "Float32",
sql: "REAL",
size: 4, size: 4,
}, },
f64: { f64: {
c: "double", c: "double",
js: "Float64", js: "Float64",
sql: "REAL",
size: 8, size: 8,
}, },
}; };
@ -50,14 +68,18 @@ const { values } = parseArgs({
type: "string", type: "string",
short: "S", short: "S",
}, },
jsout: { javascript_out: {
type: "string", type: "string",
short: "j", short: "j",
}, },
cout: { c_out: {
type: "string", type: "string",
short: "c", short: "c",
}, },
sqlite_out: {
type: "string",
short: "s"
}
}, },
strict: true, strict: true,
allowPositionals: true, allowPositionals: true,
@ -88,6 +110,7 @@ function jsStructConstructor(size, containsString) {
const sFile = Bun.file(values.schema); const sFile = Bun.file(values.schema);
const schema = await sFile.json(); const schema = await sFile.json();
let cData = ""; let cData = "";
let sqlData = "";
for (const type of Object.keys(schema)) { for (const type of Object.keys(schema)) {
let containsString = false; let containsString = false;
@ -95,7 +118,9 @@ for (const type of Object.keys(schema)) {
let size = 0; let size = 0;
let importStatements = ""; let importStatements = "";
let jsData = ""; let jsData = "";
let foreignKeys = "";
const props = schema[type]; const props = schema[type];
sqlData += `CREATE TABLE ${type} (${type.toLowerCase()}_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)) {
@ -117,7 +142,7 @@ for (const type of Object.keys(schema)) {
set ${prop}(v) { set ${prop}(v) {
this._data.set(this._encoder.encode(v), ${parseInt(offset)}); this._data.set(this._encoder.encode(v), ${parseInt(offset)});
}`; }`;
sqlData += `, ${propType} TEXT`;
cData += ` cData += `
char ${prop}[${iSize}];`; char ${prop}[${iSize}];`;
break; break;
@ -132,7 +157,7 @@ for (const type of Object.keys(schema)) {
offset offset
)}, v, true); )}, v, true);
}`; }`;
sqlData += `, ${prop} ${types[propType].sql}`;
cData += ` cData += `
${types[propType].c} ${prop};`; ${types[propType].c} ${prop};`;
@ -150,7 +175,11 @@ 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()}_${foreignKey}`;
sqlData += `,${localKey} INTEGER`;
foreignKeys += `\nFOREIGN KEY(${localKey}) REFERENCES ${propType}(${foreignKey})`
cData += `\n\t\t${types[propType].c} ${prop};`; cData += `\n\t\t${types[propType].c} ${prop};`;
break; break;
case "array": case "array":
@ -174,18 +203,14 @@ for (const type of Object.keys(schema)) {
}; };
jsData += jsStructConstructor(size, containsString); jsData += jsStructConstructor(size, containsString);
jsData += ` jsData += `\n}\n\nexport default ${type}`;
} cData += `\n} ${type};\n\n`;
sqlData += `)${foreignKeys};\n\n`;
export default ${type}`;
cData += `
} ${type};
`;
await Bun.write( await Bun.write(
Bun.file(values.jsout + type + ".js"), Bun.file(values.javascript_out + type + ".js"),
importStatements + jsData importStatements + jsData
); );
} }
await Bun.write(Bun.file(values.cout + "types.h"), cData); await Bun.write(Bun.file(values.c_out + "types.h"), cData);
await Bun.write(Bun.file(values.sqlite_out + "types.sql"), sqlData);