Skip to content

Commit

Permalink
Pre release42.2.17 (#1908)
Browse files Browse the repository at this point in the history
* fix: avoid removal type annotations on "this" so the source archive is buildable

"this" type annotations are Java 8+, so we no longer need to remove them.

* fix: PgTokenizer was ignoring last empty token (#1882)

* fix: PgTokenizer was ignoring last empty token fixes #1881

* fix: handle smallserial correctly fixes #1897 (#1899)

* feat: add smallserial metadata (#899)

PostgreSQL 9.2 adds a SMALLSERIAL data type, this reports the correct metadata information when a column is a smallserial (int2 with sequence), similar to how a serial or bigserial data types are reported.

* fix:remove osgi from karaf fixes Issue #1891 (#1902)

* Change default of gssEncMode to ALLOW. PostgreSQL can deal with PREFER but there are cloud providers that did not implement the protocol properly. Using PREFER seems to cause more problems than it solves

Co-authored-by: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
Co-authored-by: Jorge Solorzano <jorsol@gmail.com>
  • Loading branch information
3 people committed Oct 6, 2020
1 parent 4185501 commit 722e790
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 22 deletions.
10 changes: 1 addition & 9 deletions pgjdbc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ dependencies {
shaded("com.ongres.scram:client")

implementation("org.checkerframework:checker-qual")

// https://github.com/lburgazzoli/gradle-karaf-plugin/issues/75
karafFeatures(platform(project(":bom")))
karafFeatures("org.osgi:org.osgi.core:${"org.osgi.core".v}")
karafFeatures("org.osgi:org.osgi.enterprise:${"org.osgi.enterprise".v}")

testImplementation("se.jiderhamn:classloader-leak-test-framework")
}

Expand Down Expand Up @@ -256,17 +250,15 @@ val hiddenAnnotation = Regex(
"GuardedBy|UnderInitialization|" +
"DefaultQualifier)(?:\\([^)]*\\))?")
val hiddenImports = Regex("import org.checkerframework")
val thisReferences = Regex("""\*/\s+(\w+\s+this\b,?)""")

val removeTypeAnnotations by tasks.registering(Sync::class) {
destinationDir = withoutAnnotations
inputs.property("regexpsUpdatedOn", "2020-08-29")
inputs.property("regexpsUpdatedOn", "2020-08-25")
from(projectDir) {
filteringCharset = `java.nio.charset`.StandardCharsets.UTF_8.name()
filter { x: String ->
x.replace(hiddenAnnotation, "/* $0 */")
.replace(hiddenImports, "// $0")
.replace(thisReferences, "*/ /* $1 */")
}
include("src/**")
}
Expand Down
2 changes: 1 addition & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/GSSEncMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static GSSEncMode of(Properties info) throws PSQLException {
String gssEncMode = PGProperty.GSS_ENC_MODE.get(info);
// If gssEncMode is not set, fallback to prefer
if (gssEncMode == null) {
return PREFER;
return ALLOW;
}

for (GSSEncMode mode : VALUES) {
Expand Down
24 changes: 14 additions & 10 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1604,15 +1604,13 @@ public ResultSet getColumns(@Nullable String catalog, @Nullable String schemaPat

String defval = rs.getString("adsrc");

if (defval != null) {
if (defval != null && defval.contains("nextval(") ) {
if ("int4".equals(pgType)) {
if (defval.contains("nextval(")) {
tuple[5] = connection.encodeString("serial"); // Type name == serial
}
tuple[5] = connection.encodeString("serial"); // Type name == serial
} else if ("int8".equals(pgType)) {
if (defval.contains("nextval(")) {
tuple[5] = connection.encodeString("bigserial"); // Type name == bigserial
}
tuple[5] = connection.encodeString("bigserial"); // Type name == bigserial
} else if ("int2".equals(pgType) && connection.haveMinimumServerVersion(ServerVersion.v9_2)) {
tuple[5] = connection.encodeString("smallserial"); // Type name == smallserial
}
}
String identity = rs.getString("attidentity");
Expand Down Expand Up @@ -2374,19 +2372,25 @@ public ResultSet getTypeInfo() throws SQLException {
tuple[17] = b10; // everything is base 10
v.add(new Tuple(tuple));

// add pseudo-type serial, bigserial
if (typname.equals("int4")) {
// add pseudo-type serial, bigserial, smallserial
if ("int4".equals(typname)) {
byte[] @Nullable [] tuple1 = tuple.clone();

tuple1[0] = connection.encodeString("serial");
tuple1[11] = bt;
v.add(new Tuple(tuple1));
} else if (typname.equals("int8")) {
} else if ("int8".equals(typname)) {
byte[] @Nullable [] tuple1 = tuple.clone();

tuple1[0] = connection.encodeString("bigserial");
tuple1[11] = bt;
v.add(new Tuple(tuple1));
} else if ("int2".equals(typname) && connection.haveMinimumServerVersion(ServerVersion.v9_2)) {
byte[] @Nullable [] tuple1 = tuple.clone();

tuple1[0] = connection.encodeString("smallserial");
tuple1[11] = bt;
v.add(new Tuple(tuple1));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ public String getColumnTypeName(int column) throws SQLException {
return "serial";
} else if ("int8".equals(type)) {
return "bigserial";
} else if ("int2".equals(type) && connection.haveMinimumServerVersion(ServerVersion.v9_2)) {
return "smallserial";
}
}

Expand Down
9 changes: 7 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/util/PGtokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public int tokenize(String string, char delim) {
int s;
boolean skipChar = false;
boolean nestedDoubleQuote = false;

char c = (char)0;
for (p = 0, s = 0; p < string.length(); p++) {
char c = string.charAt(p);
c = string.charAt(p);

// increase nesting if an open character is found
if (c == '(' || c == '[' || c == '<' || (!nestedDoubleQuote && !skipChar && c == '"')) {
Expand Down Expand Up @@ -94,6 +94,11 @@ public int tokenize(String string, char delim) {
tokens.add(string.substring(s));
}

// check for last token empty
if ( s == string.length() && c == delim) {
tokens.add("");
}

return tokens.size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,15 @@ public void testSerialColumns() throws SQLException {
assertEquals(rownum + 1, rs.getInt("ORDINAL_POSITION"));
if (rownum == 0) {
assertEquals("int4", rs.getString("TYPE_NAME"));

} else if (rownum == 1) {
assertEquals("serial", rs.getString("TYPE_NAME"));
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
} else if (rownum == 2) {
assertEquals("bigserial", rs.getString("TYPE_NAME"));
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
}

rownum++;
}
assertEquals(3, rownum);
Expand Down Expand Up @@ -1484,4 +1488,56 @@ public void testFunctionColumns() throws SQLException {

rs.close();
}

@Test
public void testSmallSerialColumns() throws SQLException {
org.junit.Assume.assumeTrue(TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_2));
TestUtil.createTable(con, "smallserial_test", "a smallserial");

DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, "smallserial_test", "a");
assertTrue(rs.next());
assertEquals("smallserial_test", rs.getString("TABLE_NAME"));
assertEquals("a", rs.getString("COLUMN_NAME"));
assertEquals(Types.SMALLINT, rs.getInt("DATA_TYPE"));
assertEquals("smallserial", rs.getString("TYPE_NAME"));
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
assertEquals("nextval('smallserial_test_a_seq'::regclass)", rs.getString("COLUMN_DEF"));
assertFalse(rs.next());
rs.close();

TestUtil.dropTable(con, "smallserial_test");
}

@Test
public void testSmallSerialSequenceLikeColumns() throws SQLException {
Statement stmt = con.createStatement();
// This is the equivalent of the smallserial, not the actual smallserial
stmt.execute("CREATE SEQUENCE smallserial_test_a_seq;\n"
+ "CREATE TABLE smallserial_test (\n"
+ " a smallint NOT NULL DEFAULT nextval('smallserial_test_a_seq')\n"
+ ");\n"
+ "ALTER SEQUENCE smallserial_test_a_seq OWNED BY smallserial_test.a;");

DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, "smallserial_test", "a");
assertTrue(rs.next());
assertEquals("smallserial_test", rs.getString("TABLE_NAME"));
assertEquals("a", rs.getString("COLUMN_NAME"));
assertEquals(Types.SMALLINT, rs.getInt("DATA_TYPE"));
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_2)) {
// in Pg 9.2+ it behaves like smallserial
assertEquals("smallserial", rs.getString("TYPE_NAME"));
} else {
assertEquals("int2", rs.getString("TYPE_NAME"));
}
assertTrue(rs.getBoolean("IS_AUTOINCREMENT"));
assertEquals("nextval('smallserial_test_a_seq'::regclass)", rs.getString("COLUMN_DEF"));
assertFalse(rs.next());
rs.close();

stmt.execute("DROP TABLE smallserial_test");
stmt.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,50 @@ private void assumePreparedStatementMetadataSupported() {
preferQueryMode.compareTo(PreferQueryMode.EXTENDED_FOR_PREPARED) >= 0);
}

@Test
public void testSmallSerialColumns() throws SQLException {
org.junit.Assume.assumeTrue(TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_2));
TestUtil.createTable(con, "smallserial_test", "a smallserial");

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a FROM smallserial_test");
ResultSetMetaData rsmd = rs.getMetaData();
assertTrue(rsmd.isAutoIncrement(1));
assertEquals("smallserial_test", rsmd.getTableName(1));
assertEquals("a", rsmd.getColumnName(1));
assertEquals(Types.SMALLINT, rsmd.getColumnType(1));
assertEquals("smallserial", rsmd.getColumnTypeName(1));
rs.close();

TestUtil.dropTable(con, "smallserial_test");
}

@Test
public void testSmallSerialSequenceLikeColumns() throws SQLException {
Statement stmt = con.createStatement();
// This is the equivalent of the smallserial, not the actual smallserial
stmt.execute("CREATE SEQUENCE smallserial_test_a_seq;\n"
+ "CREATE TABLE smallserial_test (\n"
+ " a smallint NOT NULL DEFAULT nextval('smallserial_test_a_seq')\n"
+ ");\n"
+ "ALTER SEQUENCE smallserial_test_a_seq OWNED BY smallserial_test.a;");

ResultSet rs = stmt.executeQuery("SELECT a FROM smallserial_test");
ResultSetMetaData rsmd = rs.getMetaData();
assertTrue(rsmd.isAutoIncrement(1));
assertEquals("smallserial_test", rsmd.getTableName(1));
assertEquals("a", rsmd.getColumnName(1));
assertEquals(Types.SMALLINT, rsmd.getColumnType(1));
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_2)) {
// in Pg 9.2+ it behaves like smallserial
assertEquals("smallserial", rsmd.getColumnTypeName(1));
} else {
assertEquals("int2", rsmd.getColumnTypeName(1));
}
rs.close();

stmt.execute("DROP TABLE smallserial_test");
stmt.close();
}

}
43 changes: 43 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/util/PGtokenizerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2017, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

class PGtokenizerTest {

@Test
void tokenize() {
PGtokenizer pGtokenizer = new PGtokenizer("1,2EC1830300027,1,,",',');
assertEquals(5,pGtokenizer.getSize());

}

@Test
void removePara() {
String string = PGtokenizer.removePara("(1,2EC1830300027,1,,)");
Assert.assertEquals("1,2EC1830300027,1,,", string);
}

@Test
void removeBox() {
assertTrue(true);
}

@Test
void removeAngle() {
assertTrue(true);
}

@Test
void removeCurlyBrace() {
assertTrue(true);
}
}

0 comments on commit 722e790

Please sign in to comment.