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

WIP: rework TypeInfoCache #3062

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/core/Oid.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,30 @@ public class Oid {
public static final int XML_ARRAY = 143;
public static final int POINT = 600;
public static final int POINT_ARRAY = 1017;
public static final int HSTORE = 16388;
public static final int HSTORE_ARRAY = 16393;
public static final int BOX = 603;
public static final int BOX_ARRAY = 1020;
public static final int JSONB = 3802;
public static final int JSONB_ARRAY = 3807;
public static final int JSON = 114;
public static final int JSON_ARRAY = 199;
public static final int REF_CURSOR = 1790;
public static final int REF_CURSOR_ARRAY = 2201;
public static final int REFCURSOR = 1790;
public static final int REFCURSOR_ARRAY = 2201;
// backward compatibility
public static final int REF_CURSOR = REFCURSOR;
// backward compatibility
public static final int REF_CURSOR_ARRAY = REFCURSOR_ARRAY;
public static final int LINE = 628;
public static final int LINE_ARRAY = 629;
public static final int LSEG = 601;
public static final int LSEG_ARRAY = 1018;
public static final int PATH = 602;
public static final int PATH_ARRAY = 1019;
public static final int POLYGON = 604;
public static final int POLYGON_ARRAY = 1027;
public static final int CIRCLE = 718;
public static final int CIRCLE_ARRAY = 719;
public static final int CIDR = 650;
public static final int INET = 869;
public static final int MACADDR = 829;
Expand Down
20 changes: 8 additions & 12 deletions pgjdbc/src/main/java/org/postgresql/core/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import java.util.Iterator;

public interface TypeInfo {
void addCoreType(String pgTypeName, Integer oid, Integer sqlType, String javaClass,
Integer arrayOid);

void addDataType(String type, Class<? extends PGobject> klass) throws SQLException;

/**
Expand All @@ -36,7 +33,7 @@ void addCoreType(String pgTypeName, Integer oid, Integer sqlType, String javaCla
*/
int getSQLType(String pgTypeName) throws SQLException;

int getJavaArrayType(String className) throws SQLException;
int getJavaArrayType(Class<?> className) throws SQLException;

/**
* Look up the oid for a given postgresql type name. This is the inverse of
Expand All @@ -56,6 +53,7 @@ void addCoreType(String pgTypeName, Integer oid, Integer sqlType, String javaCla
* @return the server type name for that OID or null if unknown
* @throws SQLException if an error occurs when retrieving PG type
*/
// TODO: @Deprecated
@Nullable String getPGType(int oid) throws SQLException;

/**
Expand Down Expand Up @@ -85,8 +83,6 @@ void addCoreType(String pgTypeName, Integer oid, Integer sqlType, String javaCla
*/
char getArrayDelimiter(int oid) throws SQLException;

Iterator<String> getPGTypeNamesWithSQLTypes();

Iterator<Integer> getPGTypeOidsWithSQLTypes();

@Nullable Class<? extends PGobject> getPGobject(String type);
Expand All @@ -95,17 +91,17 @@ void addCoreType(String pgTypeName, Integer oid, Integer sqlType, String javaCla

@Nullable String getTypeForAlias(String alias);

int getPrecision(int oid, int typmod);
int getPrecision(int oid, int typmod) throws SQLException;

int getScale(int oid, int typmod);
int getScale(int oid, int typmod) throws SQLException;

boolean isCaseSensitive(int oid);
boolean isCaseSensitive(int oid) throws SQLException;

boolean isSigned(int oid);
boolean isSigned(int oid) throws SQLException;

int getDisplaySize(int oid, int typmod);
int getDisplaySize(int oid, int typmod) throws SQLException;

int getMaximumPrecision(int oid);
int getMaximumPrecision(int oid) throws SQLException;

boolean requiresQuoting(int oid) throws SQLException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,8 @@ protected void processResults(ResultHandler handler, int flags, boolean adaptive
// Handle status.
String status = receiveCommandStatus();
if (isFlushCacheOnDeallocate()
&& (status.startsWith("DEALLOCATE ALL") || status.startsWith("DISCARD ALL"))) {
&& (status.startsWith("DEALLOCATE ALL") || status.startsWith("DISCARD ALL")
|| status.startsWith("CREATE ") || status.endsWith("DROP "))) {
deallocateEpoch++;
Copy link
Member

Choose a reason for hiding this comment

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

ends with ?

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be status.startsWith("DROP ") ?

}

Expand Down
8 changes: 8 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public PgArray(BaseConnection connection, int oid, byte @Nullable [] fieldBytes)
this.fieldBytes = fieldBytes;
}

/**
* Returns {@code oid} of the array type
* @return array type oid
*/
public int getOid() {
return oid;
}

private BaseConnection getConnection() {
return castNonNull(connection);
}
Expand Down
17 changes: 0 additions & 17 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.sql.Types;

Check failure on line 72 in pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java

View workflow job for this annotation

GitHub Actions / Code style

Remove 1 line: 72..72
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -350,11 +350,6 @@
this.logServerErrorDetail = PGProperty.LOG_SERVER_ERROR_DETAIL.getBoolean(info);
this.disableColumnSanitiser = PGProperty.DISABLE_COLUMN_SANITISER.getBoolean(info);

if (haveMinimumServerVersion(ServerVersion.v8_3)) {
typeCache.addCoreType("uuid", Oid.UUID, Types.OTHER, "java.util.UUID", Oid.UUID_ARRAY);
typeCache.addCoreType("xml", Oid.XML, Types.SQLXML, "java.sql.SQLXML", Oid.XML_ARRAY);
}

this.clientInfo = new Properties();
if (haveMinimumServerVersion(ServerVersion.v9_0)) {
String appName = PGProperty.APPLICATION_NAME.getOrDefault(info);
Expand Down Expand Up @@ -799,18 +794,6 @@

// This initialises the objectTypes hash map
private void initObjectTypes(Properties info) throws SQLException {
// Add in the types that come packaged with the driver.
// These can be overridden later if desired.
addDataType("box", org.postgresql.geometric.PGbox.class);
addDataType("circle", org.postgresql.geometric.PGcircle.class);
addDataType("line", org.postgresql.geometric.PGline.class);
addDataType("lseg", org.postgresql.geometric.PGlseg.class);
addDataType("path", org.postgresql.geometric.PGpath.class);
addDataType("point", org.postgresql.geometric.PGpoint.class);
addDataType("polygon", org.postgresql.geometric.PGpolygon.class);
addDataType("money", org.postgresql.util.PGmoney.class);
addDataType("interval", org.postgresql.util.PGInterval.class);

Enumeration<?> e = info.propertyNames();
while (e.hasMoreElements()) {
String propertyName = (String) e.nextElement();
Expand Down
25 changes: 15 additions & 10 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ private Class<?> getArrayType(Class<?> type) {
} else {
if (oid == Oid.UNSPECIFIED) {
Class<?> arrayType = getArrayType(in.getClass());
oid = typeInfo.getJavaArrayType(arrayType.getName());
oid = typeInfo.getJavaArrayType(arrayType);
if (oid == Oid.UNSPECIFIED) {
throw new SQLFeatureNotSupportedException();
}
Expand Down Expand Up @@ -1170,15 +1170,16 @@ public void setArray(int i, java.sql.@Nullable Array x) throws SQLException {
return;
}

// This only works for Array implementations that return a valid array
// literal from Array.toString(), such as the implementation we return
// from ResultSet.getArray(). Eventually we need a proper implementation
// here that works for any Array implementation.
String typename = x.getBaseTypeName();
int oid = connection.getTypeInfo().getPGArrayType(typename);
if (oid == Oid.UNSPECIFIED) {
throw new PSQLException(GT.tr("Unknown type {0}.", typename),
PSQLState.INVALID_PARAMETER_TYPE);
int oid;
if (x instanceof PgArray) {
oid = ((PgArray) x).getOid();
} else {
String typename = x.getBaseTypeName();
oid = connection.getTypeInfo().getPGArrayType(typename);
if (oid == Oid.UNSPECIFIED) {
throw new PSQLException(GT.tr("Unknown type {0}.", typename),
PSQLState.INVALID_PARAMETER_TYPE);
}
}

if (x instanceof PgArray) {
Expand All @@ -1190,6 +1191,10 @@ public void setArray(int i, java.sql.@Nullable Array x) throws SQLException {
}
}

// This only works for Array implementations that return a valid array
// literal from Array.toString(), such as the implementation we return
// from ResultSet.getArray(). Eventually we need a proper implementation
// here that works for any Array implementation.
setString(i, x.toString(), oid);
}

Expand Down
2 changes: 1 addition & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3111,7 +3111,7 @@ private void initSqlType(Field field) throws SQLException {
TypeInfo typeInfo = connection.getTypeInfo();
int oid = field.getOID();
String pgType = castNonNull(typeInfo.getPGType(oid));
int sqlType = typeInfo.getSQLType(pgType);
int sqlType = typeInfo.getSQLType(oid);
field.setSQLType(sqlType);
field.setPGType(pgType);
}
Expand Down