当前位置: 首页>>代码示例>>Java>>正文


Java PSQLException.getServerErrorMessage方法代码示例

本文整理汇总了Java中org.postgresql.util.PSQLException.getServerErrorMessage方法的典型用法代码示例。如果您正苦于以下问题:Java PSQLException.getServerErrorMessage方法的具体用法?Java PSQLException.getServerErrorMessage怎么用?Java PSQLException.getServerErrorMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.postgresql.util.PSQLException的用法示例。


在下文中一共展示了PSQLException.getServerErrorMessage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getRefTableFromForeignKeyPsqlException

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
private String getRefTableFromForeignKeyPsqlException(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	Matcher messageMatcher = Pattern.compile(
			"update or delete on table \"(.*)\" violates foreign key constraint \"(.*)\" on table \"(.*)\"")
									.matcher(serverErrorMessage.getMessage());
	if (!messageMatcher.matches())
	{
		LOG.error("Error translating postgres exception: ", pSqlException);
		throw new RuntimeException("Error translating exception", pSqlException);
	}
	return messageMatcher.group(1);
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:14,代码来源:PostgreSqlExceptionTranslator.java

示例2: translateCheckConstraintViolation

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateCheckConstraintViolation(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	String tableName = serverErrorMessage.getTable();
	String constraintName = serverErrorMessage.getConstraint();
	// constraint name: <tableName>_<columnName>_chk
	String columnName = constraintName.substring(tableName.length() + 1, constraintName.length() - 4);
	ConstraintViolation constraintViolation = new ConstraintViolation(
			format("Unknown enum value for attribute '%s' of entity '%s'.", getAttributeName(tableName, columnName),
					getEntityTypeName(tableName)), null);
	return new MolgenisValidationException(singleton(constraintViolation));
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:19,代码来源:PostgreSqlExceptionTranslator.java

示例3: translateUndefinedColumnException

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
static MolgenisValidationException translateUndefinedColumnException(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	String message = serverErrorMessage.getMessage(); // FIXME exposes internal message
	ConstraintViolation constraintViolation = new ConstraintViolation(message);
	return new MolgenisValidationException(singleton(constraintViolation));
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:14,代码来源:PostgreSqlExceptionTranslator.java

示例4: wrapIfNeededOrReturnNull

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
@Override
public DBException wrapIfNeededOrReturnNull(final Throwable t)
{
	final Boolean referencingTableHasDLMLevel;

	if (DBException.isSQLState(t, PG_SQLSTATE_Referencing_Record_Has_Wrong_DLM_Level))
	{
		referencingTableHasDLMLevel = true;
	}
	else if (DBException.isSQLState(t, PG_SQLSTATE_Referencing_Table_Has_No_DLM_LEvel))
	{
		referencingTableHasDLMLevel = false;
	}
	else
	{
		return null;
	}

	//
	// parse the exception detail and extract the infos
	final SQLException sqlException = DBException.extractSQLExceptionOrNull(t);
	Check.errorUnless(sqlException instanceof PSQLException, "exception={} needs to be a PSQLExcetion", sqlException);

	final PSQLException psqlException = (PSQLException)sqlException;
	final ServerErrorMessage serverErrorMessage = psqlException.getServerErrorMessage();
	Check.errorIf(serverErrorMessage == null, "ServerErrorMessage of PSQLException={} may not be null", psqlException);

	final String detail = serverErrorMessage.getDetail();
	Check.errorIf(Check.isEmpty(detail, true), "DETAIL ServerErrorMessage={} from of PSQLException={} may not be null", serverErrorMessage, psqlException);

	final String[] infos = extractInfos(detail);

	//
	// the the "real" tables and column from the extracted lowercase infos
	final IADTableDAO adTableDAO = Services.get(IADTableDAO.class);

	final I_AD_Table referencedTable = adTableDAO.retrieveTable(infos[0]);
	Check.errorIf(referencedTable == null, "Unable to retrieve an AD_Table for referencedTable name={}", infos[0]);

	final I_AD_Table referencingTable = adTableDAO.retrieveTable(infos[2]);
	Check.errorIf(referencingTable == null, "Unable to retrieve an AD_Table for referencingTable name={}", infos[2]);

	final I_AD_Column referencingColumn = adTableDAO.retrieveColumn(referencingTable.getTableName(), infos[3]);
	Check.errorIf(referencingTable == null, "Unable to retrieve an AD_Column for referencingTable name={} and referencingColumn name={}", infos[2], infos[3]);

	final DLMReferenceException ex = new DLMReferenceException(t,
			TableRecordIdDescriptor.of(
					referencingTable.getTableName(),
					referencingColumn.getColumnName(),
					referencedTable.getTableName()),
			referencingTableHasDLMLevel);
	ex.setParameter("referencedRecordId", Integer.parseInt(infos[1]));
	ex.appendParametersToMessage();
	return ex;
}
 
开发者ID:metasfresh,项目名称:metasfresh,代码行数:56,代码来源:DLMReferenceExceptionWrapper.java

示例5: createPg4jException

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Examine an Exception and use the exceptionConverter to return either a
 * Cl4pgException, or a more specific sub-class of Cl4pgException.
 *
 * @param e
 * @param sql
 * @return
 */
private Cl4pgException createPg4jException(Exception e,
                                           String sql) {
    if (e instanceof PSQLException) {
        PSQLException psqle = (PSQLException) e;
        Cl4pgPgSqlException pe = new Cl4pgPgSqlException(psqle.getServerErrorMessage(),
                "ROLLED BACK. Exception while trying to run this sql statement:\n" + sql,
                e);
        return exceptionConverter.convert(pe);
    } else if (e instanceof SQLException) {
        return new Cl4pgSqlException("ROLLED BACK. Exception while trying to run this sql statement:\n" + sql, e);
    } else {
        return new Cl4pgException("ROLLED BACK. Exception while trying to run this sql statement:\n" + sql, e);
    }
}
 
开发者ID:manniwood,项目名称:cl4pg,代码行数:23,代码来源:PgSession.java

示例6: translateDependentObjectsStillExist

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateDependentObjectsStillExist(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	String detail = serverErrorMessage.getDetail();
	Matcher matcher = Pattern.compile("constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?")
							 .matcher(detail);

	Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>();
	while (matcher.find())
	{
		String tableName = matcher.group(2);
		String dependentTableName = matcher.group(3);

		String entityTypeName = getEntityTypeName(tableName);
		String dependentEntityTypeName = getEntityTypeName(dependentTableName);
		Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent(dependentEntityTypeName,
				k -> new LinkedHashSet<>());
		dependentTableNames.add(entityTypeName);
	}

	if (entityTypeDependencyMap.isEmpty()) // no matches
	{
		LOG.error("Error translating postgres exception: ", pSqlException);
		throw new RuntimeException("Error translating exception", pSqlException);
	}

	Set<ConstraintViolation> constraintViolations = entityTypeDependencyMap.entrySet().stream().map(entry ->
	{
		String message;
		if (entry.getValue().size() == 1)
		{
			message = format("Cannot delete entity '%s' because entity '%s' depends on it.", entry.getKey(),
					entry.getValue().iterator().next());
		}
		else
		{
			message = format("Cannot delete entity '%s' because entities '%s' depend on it.", entry.getKey(),
					entry.getValue().stream().collect(joining(", ")));
		}
		return new ConstraintViolation(message, null);
	}).collect(toCollection(LinkedHashSet::new));

	return new MolgenisValidationException(constraintViolations);
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:51,代码来源:PostgreSqlExceptionTranslator.java

示例7: translateInvalidIntegerException

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
static MolgenisValidationException translateInvalidIntegerException(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	String message = serverErrorMessage.getMessage();
	Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"").matcher(message);
	boolean matches = matcher.matches();
	if (!matches)
	{
		throw new RuntimeException("Error translating exception", pSqlException);
	}
	String postgreSqlType = matcher.group(1);

	// convert PostgreSQL data type to attribute type:
	String type;
	switch (postgreSqlType)
	{
		case "boolean":
			type = BOOL.toString();
			break;
		case "date":
			type = DATE.toString();
			break;
		case "timestamp with time zone":
			type = DATE_TIME.toString();
			break;
		case "double precision":
			type = DECIMAL.toString();
			break;
		case "integer":
			type = INT.toString() + " or " + LONG.toString();
			break;
		default:
			type = postgreSqlType;
			break;
	}
	String value = matcher.group(2);

	ConstraintViolation constraintViolation = new ConstraintViolation(
			format("Value [%s] of this entity attribute is not of type [%s].", value, type), null);
	return new MolgenisValidationException(singleton(constraintViolation));
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:48,代码来源:PostgreSqlExceptionTranslator.java

示例8: translateForeignKeyViolation

import org.postgresql.util.PSQLException; //导入方法依赖的package包/类
/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateForeignKeyViolation(PSQLException pSqlException)
{
	ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
	String tableName = serverErrorMessage.getTable();
	String detailMessage = serverErrorMessage.getDetail();
	Matcher m = Pattern.compile("\\((.*?)\\)").matcher(detailMessage);
	if (!m.find())
	{
		LOG.error("Error translating postgres exception: ", pSqlException);
		throw new RuntimeException("Error translating exception", pSqlException);
	}
	String colName = m.group(1);
	if (!m.find())
	{
		LOG.error("Error translating postgres exception: ", pSqlException);
		throw new RuntimeException("Error translating exception", pSqlException);
	}
	String value = m.group(1);

	String constraintViolationMessageTemplate;
	String attrName;
	if (detailMessage.contains("still referenced from"))
	{
		// ERROR: update or delete on table "x" violates foreign key constraint "y" on table "z"
		// Detail: Key (k)=(v) is still referenced from table "x".
		constraintViolationMessageTemplate = "Value '%s' for attribute '%s' is referenced by entity '%s'.";
		String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException);
		attrName = getAttributeName(refTableName, colName);
	}
	else
	{
		// ERROR: insert or update on table "x" violates foreign key constraint "y"
		// Detail: Key (k)=(v) is not present in table "z".
		constraintViolationMessageTemplate = "Unknown xref value '%s' for attribute '%s' of entity '%s'.";
		attrName = getAttributeName(tableName, colName);
	}
	ConstraintViolation constraintViolation = new ConstraintViolation(
			format(constraintViolationMessageTemplate, value, attrName, getEntityTypeName(tableName)), null);
	return new MolgenisValidationException(singleton(constraintViolation));
}
 
开发者ID:molgenis,项目名称:molgenis,代码行数:47,代码来源:PostgreSqlExceptionTranslator.java


注:本文中的org.postgresql.util.PSQLException.getServerErrorMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。