unique delete bug fix #17

Merged
jusax23 merged 1 commit from dev into main 2023-03-19 13:38:37 +01:00
6 changed files with 45 additions and 27 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "dblang",
"version": "0.7.0",
"version": "0.9.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "dblang",
"version": "0.7.0",
"version": "0.9.0",
"license": "UNLICENSED",
"dependencies": {
"mariadb": "^3.0.2",

View file

@ -1,6 +1,6 @@
{
"name": "dblang",
"version": "0.8.1",
"version": "0.9.0",
"description": "",
"main": "dist/db.js",
"types": "dist/db.d.ts",

View file

@ -104,13 +104,13 @@ export class Attribute {
[this]
))
}
if (ops.foreginKey != null) {
if (ops.foreignKey != null) {
table.addConstraint(new foreignConstraint(
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreginKey.link.name,
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreignKey.link.name,
[this],
[ops.foreginKey.link],
ops.foreginKey.onDelete,
ops.foreginKey.onUpdate
[ops.foreignKey.link],
ops.foreignKey.onDelete,
ops.foreignKey.onUpdate
));
}
}
@ -170,7 +170,8 @@ export class Table {
}));
}
addConstraint(c: Constraint) {
c.check(this);
let check = c.check(this);
if(typeof check == "string") throw new Error(check);
this.dbLangConstrains.push(c);
}
createAlias(name: string) {

View file

@ -1,6 +1,7 @@
import { Attribute, Table } from "./db.js";
import { Handler } from "./defaultHandler.js";
import { QueryBuilder } from "./query.js";
import { sha256 } from "./tools.js";
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js";
export class Datatype {
@ -110,6 +111,7 @@ export class checkConstraint implements Constraint {
checkQuery: BooleanModifier;
name: string;
constructor(name: string, check: BooleanModifier) {
if (name.length > 64) name = sha256(name);
this.name = name.toLowerCase();
this.checkQuery = check;
}
@ -125,6 +127,7 @@ export class uniqueConstraint implements Constraint {
name: string;
attrs: Attribute[];
constructor(name: string, attrs: Attribute[]) {
if (name.length > 64) name = sha256(name);
this.name = name.toLowerCase();
this.attrs = attrs;
}
@ -147,6 +150,7 @@ export class foreignConstraint implements Constraint {
onUpdate: onAction;
onDelete: onAction;
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
if (name.length > 64) name = sha256(name);
this.name = name.toLowerCase();
this.fromAttrs = from;
this.toAttrs = to;
@ -160,7 +164,7 @@ export class foreignConstraint implements Constraint {
let table = this.toAttrs[0].table;
for (let i = 0; i < this.toAttrs.length; i++) {
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
if (this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
if (!this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
}
for (let i = 0; i < this.fromAttrs.length; i++) {
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";

View file

@ -121,6 +121,21 @@ export class Handler {
return found == fromAttrs.length && found == toAttrs.length;
}
function freeForUpdate(attr: string, table: string, start = false) {
for (let i = 0; i < key.length; i++) {
const k = key[i];
if (
((k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
&& (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase())
|| (start && (k.TABLE_NAME ?? k.table_name) == table
&& (k.COLUMN_NAME ?? k.column_name).toLowerCase() == attr.toLowerCase() && (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name) != null)
) {
del.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
key.splice(i--, 1);
}
}
}
//delete check and unused/false(unique, foreign) constraints
for (let i = 0; i < constraints.length; i++) {
const c = constraints[i];
@ -130,7 +145,19 @@ export class Handler {
if (
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|| !checkUniqueIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
) del.appendEnding(handler.querys.removeUnique(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
) {
const uTName = (c.TABLE_NAME ?? c.table_name);
const uCName = (c.CONSTRAINT_NAME ?? c.constraint_name);
for (let j = 0; j < key.length; j++) {
if (
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == uCName
&& (key[j].TABLE_NAME ?? key[j].table_name) == uTName
) {
freeForUpdate((key[j].COLUMN_NAME ?? key[j].column_name), uTName, true);
}
}
del.appendEnding(handler.querys.removeUnique(handler, uTName, uCName));
}
} else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "FOREIGN KEY") {
if (
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
@ -143,19 +170,6 @@ export class Handler {
if (typeof tableData[allTables[i]] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
}
function freeForUpdate(attr: string, table: string) {
for (let i = 0; i < key.length; i++) {
const k = key[i];
if (
(k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
&& (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase()
) {
del.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
key.splice(i--, 1);
}
}
}
//create tables
for (let i = 0; i < db.tables.length; i++) {
const table = db.tables[i];
@ -205,7 +219,6 @@ export class Handler {
if (attrData != null) {
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
freeForUpdate(a.name, a.table.dbLangTableName);
console.log("206");
changePrimary = true;
}
}

View file

@ -20,7 +20,7 @@ export type attributeSettings = {
default?: primaryData,
notNull?: boolean
primaryKey?: boolean,
foreginKey?: {
foreignKey?: {
link: Attribute,
onDelete?: onAction,
onUpdate?: onAction
@ -35,7 +35,7 @@ export type extendedAttributeSettings = {
default?: primaryData,
notNull?: boolean
primaryKey?: boolean,
foreginKey?: {
foreignKey?: {
link: Attribute,
onDelete?: onAction,
onUpdate?: onAction