下面列出了怎么用org.hibernate.annotations.common.util.StringHelper的API类实例代码及写法,或者点击链接到github查看源代码。
/**
*
* @param meta
* @throws SQLException
*/
private void initColumns(DatabaseMetaData meta , boolean upcase) throws SQLException {
ResultSet rs = null;
try {
if (meta.storesUpperCaseIdentifiers()) {
rs = meta.getColumns(StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema), StringHelper
.toUpperCase(name), "%");
} else if (meta.storesLowerCaseIdentifiers()) {
rs = meta.getColumns(StringHelper.toLowerCase(catalog),
StringHelper.toLowerCase(schema), StringHelper
.toLowerCase(name), "%");
} else {
rs = meta.getColumns(catalog, schema, name, "%");
}
while (rs.next())
addColumn(rs , upcase);
}catch(Exception ex){
ex.printStackTrace();
}finally {
if (rs != null)
rs.close();
}
}
@Override
public void addTypeDefinition(TypeDefinition typeDefinition) {
if ( typeDefinition == null ) {
throw new IllegalArgumentException( "Type definition is null" );
}
// Need to register both by name and registration keys.
if ( !StringHelper.isEmpty( typeDefinition.getName() ) ) {
addTypeDefinition( typeDefinition.getName(), typeDefinition );
}
if ( typeDefinition.getRegistrationKeys() != null ) {
for ( String registrationKey : typeDefinition.getRegistrationKeys() ) {
addTypeDefinition( registrationKey, typeDefinition );
}
}
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
if ( isGenerated( dialect ) ) {
// Certain dialects (ex: HANA) don't support FKs as expected, but other constraints can still be created.
// If that's the case, hasAlterTable() will be true, but getAddForeignKeyConstraintString will return
// empty string. Prevent blank "alter table" statements.
String constraintString = sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema );
if ( !StringHelper.isEmpty( constraintString ) ) {
final String tableName = getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
return dialect.getAlterTableString( tableName ) + " " + constraintString;
}
}
return null;
}
@SuppressWarnings("SimplifiableIfStatement")
private boolean isEmpty(Object dialectReference) {
if ( dialectReference != null ) {
// the referenced value is not null
if ( dialectReference instanceof String ) {
// if it is a String, it might still be empty though...
return StringHelper.isEmpty( (String) dialectReference );
}
return false;
}
return true;
}
public SessionFactory findSessionFactory(String uuid, String name) {
SessionFactory sessionFactory = getSessionFactory( uuid );
if ( sessionFactory == null && StringHelper.isNotEmpty( name ) ) {
sessionFactory = getNamedSessionFactory( name );
}
return sessionFactory;
}
public Object stringToObject(String stringValue) {
if ( StringHelper.isEmpty( stringValue ) ) return null;
try {
return DateTools.stringToDate( stringValue );
}
catch (ParseException e) {
throw new SearchException( "Unable to parse into date: " + stringValue, e );
}
}
/**
*
* @param name
* @param schema
* @param catalog
* @param isQuoted
* @return
* @throws Exception
*/
public List<UKTableMetaData> loadTables(String name, String schema, String catalog,
boolean isQuoted) throws Exception {
boolean upcase = false ;
try {
if(properties!=null && properties.get("schema")!=null && schema==null){
schema = properties.get("upcase")!=null?((String)properties.get("schema")).toUpperCase():(String)properties.get("schema") ;
}
if(properties!=null && properties.get("upcase")!=null){
upcase = properties.get("upcase")!=null && properties.get("upcase").toString().toLowerCase().equals("true");
}
UKTableMetaData table = null;
Statement statement = null;
ResultSet rs = null ;
try {
if ((isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
rs = meta.getTables(catalog, schema, name, TYPES);
} else if ((isQuoted && meta.storesUpperCaseIdentifiers() && meta.storesUpperCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesUpperCaseIdentifiers())) {
rs = meta.getTables(StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema), StringHelper
.toUpperCase(name), TYPES);
} else if ((isQuoted && meta.storesLowerCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesLowerCaseIdentifiers())) {
rs = meta.getTables(StringHelper.toLowerCase(catalog),
StringHelper.toLowerCase(schema), StringHelper
.toLowerCase(name), TYPES);
}else if(schema!=null && schema.equals("hive")){
statement = this.connection.createStatement() ;
if(properties.get("database")!=null){
statement.execute("USE "+properties.get("database")) ;
}
rs = statement.executeQuery("SHOW TABLES") ;
} else {
rs = meta.getTables(catalog, schema, name, TYPES);
}
while (rs.next()) {
String tableName = null ;
if(schema!=null && schema.equals("hive")){
tableName = rs.getString("tab_name") ;
}else{
tableName = rs.getString("TABLE_NAME");
}
if(tableName.matches("[\\da-zA-Z_-\u4e00-\u9fa5]+")){
table = new UKTableMetaData(rs, meta, true , upcase , false , schema);
tables.add(table);
}
}
}catch(Exception ex){
ex.printStackTrace();
} finally {
if (rs != null){
rs.close();
}
if(statement!=null){
statement.close();
}
}
} catch (SQLException sqle) {
throw sqle;
}
return tables ;
}
/**
*
* @param name
* @param schema
* @param catalog
* @param isQuoted
* @return
* @throws Exception
*/
public UKTableMetaData loadTable(String name, String schema, String catalog,
boolean isQuoted) throws Exception {
UKTableMetaData table = null;
boolean upcase = false ;
try {
if(properties!=null && properties.get("schema")!=null && schema==null){
schema = (String)properties.get("schema") ;
}
if(properties!=null && properties.get("upcase")!=null){
upcase = properties.get("upcase")!=null && properties.get("upcase").toString().toLowerCase().equals("true");
}
ResultSet rs = null;
try {
if ((isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
rs = meta.getTables(catalog, schema, name, TYPES);
} else if ((isQuoted && meta.storesUpperCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesUpperCaseIdentifiers())) {
rs = meta.getTables(StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema), StringHelper
.toUpperCase(name), TYPES);
} else if ((isQuoted && meta.storesLowerCaseQuotedIdentifiers())
|| (!isQuoted && meta.storesLowerCaseIdentifiers())) {
rs = meta.getTables(StringHelper.toLowerCase(catalog),
StringHelper.toLowerCase(schema), StringHelper
.toLowerCase(name), TYPES);
} else {
rs = meta.getTables(catalog, schema, name, TYPES);
}
while (rs.next()) {
table = new UKTableMetaData(rs, meta, true , upcase , true , schema);
break ;
}
} finally {
if (rs != null)
rs.close();
}
} catch (SQLException sqle) {
sqle.printStackTrace() ;
throw sqle;
}
return table ;
}
public static String gen(String description) {
return StringHelper.generateAlias(description.replaceAll("\\(|\\)", ""), nextValue());
}