support pinyin search
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package db.migration.timiserver;
|
||||
|
||||
import com.imyeyu.api.util.PinyinUtil;
|
||||
import org.flywaydb.core.api.migration.BaseJavaMigration;
|
||||
import org.flywaydb.core.api.migration.Context;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
/// 回填拼音搜索字段
|
||||
///
|
||||
/// @author Codex
|
||||
/// @since 2026-08-21
|
||||
public class V37__backfill_pinyin_search extends BaseJavaMigration {
|
||||
|
||||
private static final int BATCH_SIZE = 500;
|
||||
|
||||
@Override
|
||||
public void migrate(Context context) throws Exception {
|
||||
backfillMultilingual(context.getConnection());
|
||||
backfillCustomer(context.getConnection());
|
||||
}
|
||||
|
||||
private void backfillMultilingual(Connection connection) throws Exception {
|
||||
try (PreparedStatement select = connection.prepareStatement("""
|
||||
SELECT `id`, `zh_cn`
|
||||
FROM `multilingual`
|
||||
WHERE `zh_cn` IS NOT NULL
|
||||
AND (`zh_cn_pinyin_full` IS NULL OR `zh_cn_pinyin_initial` IS NULL OR `zh_cn_pinyin_mixed` IS NULL)
|
||||
""");
|
||||
PreparedStatement update = connection.prepareStatement("""
|
||||
UPDATE `multilingual`
|
||||
SET `zh_cn_pinyin_full` = ?, `zh_cn_pinyin_initial` = ?, `zh_cn_pinyin_mixed` = ?
|
||||
WHERE `id` = ?
|
||||
""")) {
|
||||
try (ResultSet resultSet = select.executeQuery()) {
|
||||
int batchSize = 0;
|
||||
while (resultSet.next()) {
|
||||
addMultilingualBatch(update, resultSet.getString("id"), resultSet.getString("zh_cn"));
|
||||
batchSize++;
|
||||
if (batchSize >= BATCH_SIZE) {
|
||||
update.executeBatch();
|
||||
batchSize = 0;
|
||||
}
|
||||
}
|
||||
if (batchSize > 0) {
|
||||
update.executeBatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void backfillCustomer(Connection connection) throws Exception {
|
||||
try (PreparedStatement select = connection.prepareStatement("""
|
||||
SELECT `id`, `name`
|
||||
FROM `gao_customer`
|
||||
WHERE `name` IS NOT NULL
|
||||
AND (`name_pinyin_full` IS NULL OR `name_pinyin_initial` IS NULL OR `name_pinyin_mixed` IS NULL)
|
||||
""");
|
||||
PreparedStatement update = connection.prepareStatement("""
|
||||
UPDATE `gao_customer`
|
||||
SET `name_pinyin_full` = ?, `name_pinyin_initial` = ?, `name_pinyin_mixed` = ?
|
||||
WHERE `id` = ?
|
||||
""")) {
|
||||
try (ResultSet resultSet = select.executeQuery()) {
|
||||
int batchSize = 0;
|
||||
while (resultSet.next()) {
|
||||
addCustomerBatch(update, resultSet.getString("id"), resultSet.getString("name"));
|
||||
batchSize++;
|
||||
if (batchSize >= BATCH_SIZE) {
|
||||
update.executeBatch();
|
||||
batchSize = 0;
|
||||
}
|
||||
}
|
||||
if (batchSize > 0) {
|
||||
update.executeBatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addMultilingualBatch(PreparedStatement update, String id, String value) throws Exception {
|
||||
update.setString(1, PinyinUtil.full(value));
|
||||
update.setString(2, PinyinUtil.initial(value));
|
||||
update.setString(3, PinyinUtil.mixed(value));
|
||||
update.setString(4, id);
|
||||
update.addBatch();
|
||||
}
|
||||
|
||||
private void addCustomerBatch(PreparedStatement update, String id, String value) throws Exception {
|
||||
update.setString(1, PinyinUtil.full(value));
|
||||
update.setString(2, PinyinUtil.initial(value));
|
||||
update.setString(3, PinyinUtil.mixed(value));
|
||||
update.setString(4, id);
|
||||
update.addBatch();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user