我有一个被调用的函数GET_TRANSLATION
,它可以根据提供的枚举值基于不同的选择查询返回不同的结果。这是完整的代码:
CREATE FUNCTION GET_TRANSLATION(preferredLanguageId BIGINT, backupLanguageId BIGINT, entityId BIGINT, translationSubject ENUM('CROP_NAME', 'CROP_VARIETY_NAME', 'JOURNAL_TITLE', 'SOIL_TYPE', 'COUNTRY', 'PROVINCE', 'PLACE'))
RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
CASE
WHEN translationSubject = 'CROP_NAME' THEN
RETURN (SELECT COALESCE((SELECT s.translatedName FROM CropNameTranslation s WHERE s.language_id = preferredLanguageId AND s.parent_id = entityId AND s.approved = TRUE LIMIT 1),
(SELECT s.translatedName FROM CropNameTranslation s WHERE s.language_id = backupLanguageId AND s.parent_id = entityId AND s.approved = TRUE LIMIT 1), (SELECT s.translatedName FROM CropNameTranslation s WHERE s.parent_id = entityId AND s.approved = TRUE LIMIT 1)));
WHEN translationSubject = 'CROP_VARIETY_NAME' THEN
RETURN (SELECT COALESCE((SELECT s.translatedName FROM CropVarietyName s WHERE s.language_id = preferredLanguageId AND s.parent_id = entityId AND s.approved = TRUE LIMIT 1),
(SELECT s.translatedName FROM CropVarietyName s WHERE s.language_id = backupLanguageId AND s.parent_id = entityId AND s.approved = TRUE LIMIT 1), (SELECT s.translatedName FROM CropVarietyName s WHERE s.parent_id = entityId AND s.approved = TRUE LIMIT 1)));
WHEN translationSubject = 'JOURNAL_TITLE' THEN
RETURN (SELECT COALESCE((SELECT s.title FROM JournalTitleAndContent s WHERE s.language_id = preferredLanguageId AND s.journal_id = entityId LIMIT 1),
(SELECT s.title FROM JournalTitleAndContent s WHERE s.language_id = backupLanguageId AND s.journal_id = entityId LIMIT 1), (SELECT s.title FROM JournalTitleAndContent s WHERE s.journal_id = entityId LIMIT 1)));
WHEN translationSubject = 'SOIL_TYPE' THEN
RETURN (SELECT COALESCE((SELECT s.translatedName FROM SoilTypeNameTranslation s WHERE s.language_id = preferredLanguageId AND s.soiltypedetails_id = entityId LIMIT 1),
(SELECT s.translatedName FROM SoilTypeNameTranslation s WHERE s.language_id = backupLanguageId AND s.soiltypedetails_id = entityId LIMIT 1), (SELECT s.translatedName FROM SoilTypeNameTranslation s WHERE s.soiltypedetails_id = entityId LIMIT 1)));
WHEN translationSubject = 'COUNTRY' THEN
RETURN (SELECT COALESCE((SELECT ct.translatedName FROM countryNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = preferredLanguageId LIMIT 1),
(SELECT ct.translatedName FROM countryNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = backupLanguageId LIMIT 1), (SELECT ct.translatedName FROM countryNameTranslation ct WHERE ct.parent_id = entityId LIMIT 1)));
WHEN translationSubject = 'PROVINCE' THEN
RETURN (SELECT COALESCE((SELECT ct.translatedName FROM provinceNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = preferredLanguageId LIMIT 1),
(SELECT ct.translatedName FROM provinceNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = backupLanguageId LIMIT 1), (SELECT ct.translatedName FROM provinceNameTranslation ct WHERE ct.parent_id = entityId LIMIT 1)));
WHEN translationSubject = 'PLACE' THEN
RETURN (SELECT COALESCE((SELECT ct.translatedName FROM placeNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = preferredLanguageId LIMIT 1),
(SELECT ct.translatedName FROM placeNameTranslation ct WHERE ct.parent_id = entityId AND ct.language_id = backupLanguageId LIMIT 1), (SELECT ct.translatedName FROM placeNameTranslation ct WHERE ct.parent_id = entityId LIMIT 1)));
ELSE SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid enumType!';
END CASE;
END
我在一个更大的选择查询中使用了这个函数。IESELECT c.scientificName, GET_TRANSLATION(@languageId, 1, j.soiltype_details_id, 'CROP') as soilType FROM journal j;
我错误地给它输入CROP
而不是CROP_NAME
. 现在它应该已经进入结构的else
一部分CASE
并返回错误消息invalid enumType
。但是,我真正得到的是错误Error Code: 1265. Data truncated for column 'translationSubject' at row 1
。
我的问题是;为什么它不触发我为无效枚举类型定义的错误,为什么它被translationSubject
视为“截断”的列?这个错误完全不合适,因为原因仅仅是一个错误的枚举值。
谁能向我解释这种行为?
谢谢
编辑:
我只是尝试将 SIGNAL 更改为 RESIGNAL 并删除 SQLSTATE 参数。此外,我添加了
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RESIGNAL;
END;
到 GET_TRANSLATION 但同样的错误仍然存在。
如果将参数更改为文本:
它会按照您的预期运行
如果使用枚举,则在计算函数体之前会发生错误