对取数据时发现外键的空指针进行了增强

This commit is contained in:
Xu Chang 2022-12-01 18:53:57 +08:00
parent 0dc7ec0b98
commit 91174eb558
2 changed files with 48 additions and 54 deletions

View File

@ -50,26 +50,18 @@ var MysqlStore = /** @class */ (function (_super) {
if (i !== -1) { if (i !== -1) {
var attrHead = attr.slice(0, i); var attrHead = attr.slice(0, i);
var attrTail = attr.slice(i + 1); var attrTail = attr.slice(i + 1);
var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
(0, assert_1.default)(rel === 2 || typeof rel === 'string');
if (rel === 2) {
if (r.entity === attrHead) {
if (!r[attrHead]) { if (!r[attrHead]) {
r[attrHead] = {}; r[attrHead] = {};
} }
var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
if (rel === 2) {
resolveAttribute(attrHead, r[attrHead], attrTail, value); resolveAttribute(attrHead, r[attrHead], attrTail, value);
} }
}
else { else {
(0, assert_1.default)(typeof rel === 'string'); (0, assert_1.default)(typeof rel === 'string');
if (typeof r["".concat(attrHead, "Id")] === 'string') {
if (!r[attrHead]) {
r[attrHead] = {};
}
resolveAttribute(rel, r[attrHead], attrTail, value); resolveAttribute(rel, r[attrHead], attrTail, value);
} }
} }
}
else if (attributes[attr]) { else if (attributes[attr]) {
var type = attributes[attr].type; var type = attributes[attr].type;
switch (type) { switch (type) {
@ -140,23 +132,28 @@ var MysqlStore = /** @class */ (function (_super) {
_a)); _a));
} }
} }
function formalizeNullObject(r, e) { function removeNullObjects(r, e) {
var a2 = schema[e].attributes; (0, assert_1.default)(r.id && typeof r.id === 'string', "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\u53D1\u73B0id\u4E3A\u975E\u6CD5\u503C").concat(r.id, ",rowId\u662F").concat(r.id));
var allowFormalize = true;
for (var attr in r) { for (var attr in r) {
if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') { var rel = (0, relation_1.judgeRelation)(schema, e, attr);
if (formalizeNullObject(r[attr], a2[attr].ref)) { if (rel === 2) {
r[attr] = null; (0, assert_1.default)(r.entityId === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entityId\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entityId\u503C\u4E3A").concat(r.entityId, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
if (r.entityId === null) {
delete r[attr];
continue;
} }
else { (0, assert_1.default)(r.entity === attr, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entity\u503C\u4E0E\u8FDE\u63A5\u7684\u5916\u952E\u5BF9\u8C61\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entity\u503C\u4E3A").concat(r.entity, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u4E3A").concat(attr));
allowFormalize = false; removeNullObjects(r[attr], attr);
}
else if (typeof rel === 'string') {
(0, assert_1.default)(r["".concat(attr, "Id")] === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0\u5176\u5916\u952E\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176").concat(attr, "Id\u503C\u4E3A").concat(r["".concat(attr, "Id")], "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
if (r["".concat(attr, "Id")] === null) {
delete r[attr];
continue;
}
removeNullObjects(r[attr], rel);
} }
} }
else if (r[attr] !== null) {
allowFormalize = false;
}
}
return allowFormalize;
} }
function formSingleRow(r) { function formSingleRow(r) {
var result2 = {}; var result2 = {};
@ -164,7 +161,7 @@ var MysqlStore = /** @class */ (function (_super) {
var value = r[attr]; var value = r[attr];
resolveAttribute(entity, result2, attr, value); resolveAttribute(entity, result2, attr, value);
} }
formalizeNullObject(result2, entity); removeNullObjects(result2, entity);
return result2; return result2;
} }
if (result instanceof Array) { if (result instanceof Array) {

View File

@ -57,26 +57,18 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
if (i !== -1) { if (i !== -1) {
const attrHead = attr.slice(0, i); const attrHead = attr.slice(0, i);
const attrTail = attr.slice(i + 1); const attrTail = attr.slice(i + 1);
const rel = judgeRelation(schema, entity2, attrHead);
assert(rel === 2 || typeof rel === 'string');
if (rel === 2) {
if (r.entity === attrHead) {
if (!r[attrHead]) { if (!r[attrHead]) {
r[attrHead] = {}; r[attrHead] = {};
} }
const rel = judgeRelation(schema, entity2, attrHead);
if (rel === 2) {
resolveAttribute(attrHead, r[attrHead], attrTail, value); resolveAttribute(attrHead, r[attrHead], attrTail, value);
} }
}
else { else {
assert (typeof rel === 'string'); assert (typeof rel === 'string');
if (typeof r[`${attrHead}Id`] === 'string') {
if (!r[attrHead]) {
r[attrHead] = {};
}
resolveAttribute(rel, r[attrHead], attrTail, value); resolveAttribute(rel, r[attrHead], attrTail, value);
} }
} }
}
else if (attributes[attr]) { else if (attributes[attr]) {
const { type } = attributes[attr]; const { type } = attributes[attr];
switch (type) { switch (type) {
@ -149,24 +141,29 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
} }
function formalizeNullObject<E extends keyof ED>(r: Record<string, any>, e: E) { function removeNullObjects<E extends keyof ED>(r: Record<string, any>, e: E) {
const { attributes: a2 } = schema[e]; assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
let allowFormalize = true;
for (let attr in r) {
if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') {
if (formalizeNullObject(r[attr], a2[attr].ref!)) {
r[attr] = null;
}
else {
allowFormalize = false;
}
}
else if (r[attr] !== null) {
allowFormalize = false;
}
}
return allowFormalize; for (let attr in r) {
const rel = judgeRelation(schema, e, attr);
if (rel === 2) {
assert (r.entityId === r[attr].id, `对象${<string>e}取数据时发现entityId与连接的对象的主键不一致rowId是${r.id}其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`);
if (r.entityId === null) {
delete r[attr];
continue;
}
assert (r.entity === attr, `对象${<string>e}取数据时发现entity值与连接的外键对象不一致rowId是${r.id}其entity值为${r.entity},连接的对象为${attr}`);
removeNullObjects(r[attr], attr);
}
else if (typeof rel === 'string') {
assert (r[`${attr}Id`] === r[attr].id, `对象${<string>e}取数据时发现其外键与连接的对象的主键不一致rowId是${r.id},其${attr}Id值为${r[`${attr}Id`]},连接的对象的主键为${r[attr].id}`);
if (r[`${attr}Id`] === null) {
delete r[attr];
continue;
}
removeNullObjects(r[attr], rel);
}
}
} }
function formSingleRow(r: any): any { function formSingleRow(r: any): any {
@ -176,7 +173,7 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
resolveAttribute(entity, result2, attr, value); resolveAttribute(entity, result2, attr, value);
} }
formalizeNullObject(result2, entity); removeNullObjects(result2, entity);
return result2 as any; return result2 as any;
} }