fix: postgres的char类型在查询结果中会自动填充空格,需要特殊处理,以保证行为和mysql一致

This commit is contained in:
Pan Qiancheng 2025-12-30 16:19:08 +08:00
parent 05f58945c4
commit 27f8fb16c5
2 changed files with 37 additions and 3 deletions

View File

@ -176,13 +176,31 @@ class PostgreSQLStore extends CascadeStore_1.CascadeStore {
} }
break; break;
} }
// TODO: 这里和mysql统一行为ref类型的字符串去除前后空格
case "char":
case "ref": {
if (value) {
(0, assert_1.default)(typeof value === 'string');
r[attr] = value.trim();
}
else {
r[attr] = value;
}
break;
}
default: { default: {
r[attr] = value; r[attr] = value;
} }
} }
} }
else { else {
r[attr] = value; // TODO: 这里和mysql统一行为id字段为char类型时去除后面的空格
if (value && typeof value === 'string' && attr === 'id') {
r[attr] = value.trim();
}
else {
r[attr] = value;
}
} }
} }
else { else {

View File

@ -243,12 +243,28 @@ export class PostgreSQLStore<
} }
break; break;
} }
// TODO: 这里和mysql统一行为ref类型的字符串去除前后空格
case "char":
case "ref": {
if (value) {
assert(typeof value === 'string');
r[attr] = value.trim();
} else {
r[attr] = value;
}
break;
}
default: { default: {
r[attr] = value; r[attr] = value;
} }
} }
} else { } else {
r[attr] = value; // TODO: 这里和mysql统一行为id字段为char类型时去除后面的空格
if (value && typeof value === 'string' && attr === 'id') {
r[attr] = value.trim();
} else {
r[attr] = value;
}
} }
} else { } else {
assign(r, { [attr]: value }); assign(r, { [attr]: value });
@ -305,7 +321,7 @@ export class PostgreSQLStore<
option?: PostgreSQLSelectOption option?: PostgreSQLSelectOption
): Promise<Partial<ED[T]['Schema']>[]> { ): Promise<Partial<ED[T]['Schema']>[]> {
const sql = this.translator.translateSelect(entity, selection, option); const sql = this.translator.translateSelect(entity, selection, option);
console.log('selection:', JSON.stringify(selection, null , 2), 'Select SQL:', sql); console.log('selection:', JSON.stringify(selection, null, 2), 'Select SQL:', sql);
const result = await this.connector.exec(sql, context.getCurrentTxnId()); const result = await this.connector.exec(sql, context.getCurrentTxnId());
return this.formResult(entity, result[0]); return this.formResult(entity, result[0]);
} }