From c9f76a07994d5f002ba14a86e98bd34d43b87733 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Fri, 24 Mar 2023 16:46:50 +0100 Subject: [PATCH] fixes --- package-lock.json | 4 ++-- package.json | 2 +- src/dbStructure.ts | 4 +++- src/defaultHandler.ts | 7 +++++++ src/query.ts | 13 +++++++++---- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index c53e173..a4951eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dblang", - "version": "0.9.0", + "version": "0.9.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dblang", - "version": "0.9.0", + "version": "0.9.2", "license": "UNLICENSED", "dependencies": { "mariadb": "^3.0.2", diff --git a/package.json b/package.json index 273ef6d..a3a4aa5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dblang", - "version": "0.9.0", + "version": "0.9.2", "description": "", "main": "dist/db.js", "types": "dist/db.d.ts", diff --git a/src/dbStructure.ts b/src/dbStructure.ts index 7f4c7cd..3c077db 100644 --- a/src/dbStructure.ts +++ b/src/dbStructure.ts @@ -132,10 +132,12 @@ export class uniqueConstraint implements Constraint { this.attrs = attrs; } check(table: Table): boolean | string { + let prim = true; for (let i = 0; i < this.attrs.length; i++) { - if (this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key"; + if (!this.attrs[i].ops.primaryKey) prim = false; if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table."; } + if (prim) return "Can not combine unique Constraint and primary key"; return false; } uses(attr: Attribute): boolean { diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 146bb4d..114d0f9 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -604,6 +604,13 @@ export class Handler { }, } + catcherrors = { + insert: (err: any) => { + if(err?.code == "ER_DUP_ENTRY") return {affectedRows: 0, insertId: -1}; + throw err; + } + } + responses = { readResponse: (resp: any) => new readResponse(resp), writeResponse: (resp: any) => new writeResponse(resp.affectedRows), diff --git a/src/query.ts b/src/query.ts index 9571692..c6df5d0 100644 --- a/src/query.ts +++ b/src/query.ts @@ -138,8 +138,13 @@ export class insertQuery { const handler = db.getHandler(); const builder = this.serialize(handler); const s = handler.builders.query(builder); - let readResp = await db.query(s, printQuery); - return db.getHandler().responses.insertResponse(readResp); + let readResp: any; + try { + readResp = await db.query(s, printQuery); + } catch (err) { + readResp = handler.catcherrors.insert(err); + } + return handler.responses.insertResponse(readResp); } } @@ -168,7 +173,7 @@ export class updateQuery { const builder = this.serialize(handler); const s = handler.builders.query(builder); let readResp = await db.query(s, printQuery); - return db.getHandler().responses.writeResponse(readResp); + return handler.responses.writeResponse(readResp); } } @@ -190,6 +195,6 @@ export class removeQuery { const builder = this.serialize(handler); const s = handler.builders.query(builder); let readResp = await db.query(s, printQuery); - return db.getHandler().responses.writeResponse(readResp); + return handler.responses.writeResponse(readResp); } } \ No newline at end of file -- 2.39.5