1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-27 07:42:10 +03:00
Files
postgres/src/interfaces/jdbc/org/postgresql/util/MessageTranslator.java
Barry Lind 0378a269f3 This set of changes applies a patch from KHO at redhat to add some SQLState
support to the jdbc driver.
That patch needed some work: it assumed the sqlcode in a server message was
fixed in its position, the patch lost the ability to pass exceptions, and the
patch missed a couple of places where server errors where being received.
In addition to fixing the above, I also added full support for the V3 protocol
error message syntax, I reversed the order of arguments in the PSQLException
constructor to more closely follow the constructors for SQLException, I changed
the new constructors that take PSQLState to take Object for additional
parameters as the old ones did.
Still todo are to add SQLState values to all existing exceptions thrown in the
driver and add support for parsing the V3 protocol format for notices.

 Modified Files:
 	jdbc/build.xml jdbc/org/postgresql/Driver.java.in
 	jdbc/org/postgresql/errors.properties
 	jdbc/org/postgresql/core/Encoding.java
 	jdbc/org/postgresql/core/PGStream.java
 	jdbc/org/postgresql/core/QueryExecutor.java
 	jdbc/org/postgresql/fastpath/Fastpath.java
 	jdbc/org/postgresql/jdbc1/AbstractJdbc1Connection.java
 	jdbc/org/postgresql/jdbc1/AbstractJdbc1ResultSet.java
 	jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java
 	jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
 	jdbc/org/postgresql/jdbc2/AbstractJdbc2Statement.java
 	jdbc/org/postgresql/util/MessageTranslator.java
 	jdbc/org/postgresql/util/PSQLException.java
2003-09-08 17:30:22 +00:00

98 lines
2.2 KiB
Java

/*-------------------------------------------------------------------------
*
* MessageTranslator.java
* A singleton class to translate JDBC driver messages in SQLException's.
*
* Copyright (c) 2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/util/Attic/MessageTranslator.java,v 1.5 2003/09/08 17:30:22 barry Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.util;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class MessageTranslator
{
// The singleton instance.
private static MessageTranslator instance = null;
private ResourceBundle bundle;
private MessageTranslator()
{
try
{
bundle = ResourceBundle.getBundle("org.postgresql.errors");
}
catch (MissingResourceException e)
{
// translation files have not been installed.
bundle = null;
}
}
// Synchronized, otherwise multiple threads may perform the test and
// assign to the singleton instance simultaneously.
private synchronized final static MessageTranslator getInstance()
{
if (instance == null)
{
instance = new MessageTranslator();
}
return instance;
}
public final static String translate(String id, Object[] args)
{
MessageTranslator translator = MessageTranslator.getInstance();
return translator._translate(id, args);
}
public final static String translate(String id, Object arg)
{
MessageTranslator translator = MessageTranslator.getInstance();
Object[] args = new Object[1];
args[0] = arg;
return translator._translate(id, args);
}
private final String _translate(String id, Object[] args)
{
String message;
if (bundle != null && id != null)
{
// Now look up a localized message. If one is not found, then use
// the supplied message instead.
try
{
message = bundle.getString(id);
}
catch (MissingResourceException e)
{
message = id;
}
}
else
{
message = id;
}
// Expand any arguments
if (args != null && message != null)
{
message = MessageFormat.format(message, args);
}
return message;
}
}