Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加根据列全名解析表别名方法 #71

Merged
merged 3 commits into from
May 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.metadata.TableOrViewMetadata;
import org.hswebframework.ezorm.rdb.metadata.dialect.Dialect;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.NativeSql;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.TermFragmentBuilder;
Expand Down Expand Up @@ -115,4 +116,35 @@ protected PrepareSqlFragments appendPrepareOrNative(PrepareSqlFragments sql, Obj
}
return sql;
}


/**
* 根据字段全名获取表别名
*
* @param columnFullName 列全名
* @param dialect 数据库方言
* @return 表别名
*/
protected static Optional<String> parseTablePlainName(String columnFullName, Dialect dialect) {
if (columnFullName.contains(".")) {
String[] split = parsePlainName(columnFullName, dialect).split("\\.");
return Optional.of(split[0]);
}
return Optional.empty();
}

protected static String parsePlainName(String name, Dialect dialect) {
if (name == null || name.isEmpty()) {
return null;
}
char firstChar = name.charAt(0);

if (firstChar == '`' || firstChar == '"' || firstChar == '[' ||
name.startsWith(dialect.getQuoteStart())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dialect.getQuoteStart() 是多个字符时,截取可能不对。


return new String(name.toCharArray(), 1, name.length() - 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接substring?

}

return name;
}
}