Skip to content

Commit 2ed2162

Browse files
committed
fix: postgres driver from node.js
Fixed issue #1630
1 parent b79468c commit 2ed2162

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,25 @@ private void bindCommand() {
646646
final byte[] paramValue = new byte[(int) paramSize];
647647
channel.readBytes(paramValue);
648648

649-
portal.parameterValues.add(//
650-
PostgresType.deserialize(portal.parameterTypes.get(i), portal.parameterFormats.get(i), paramValue)//
651-
);
649+
// Determine format code according to PostgreSQL protocol:
650+
// - If paramFormatCount == 0: all parameters use text format (0)
651+
// - If paramFormatCount == 1: all parameters use that single format code
652+
// - Otherwise: each parameter uses its corresponding format code
653+
final int formatCode;
654+
if (portal.parameterFormats == null || portal.parameterFormats.isEmpty()) {
655+
formatCode = 0; // Default to text format
656+
} else if (portal.parameterFormats.size() == 1) {
657+
formatCode = portal.parameterFormats.get(0); // Single format for all
658+
} else {
659+
formatCode = portal.parameterFormats.get(i); // Per-parameter format
660+
}
661+
662+
// Determine type code - use UNSPECIFIED (0) if not declared in PARSE
663+
final long typeCode = (portal.parameterTypes != null && i < portal.parameterTypes.size())
664+
? portal.parameterTypes.get(i)
665+
: 0L; // UNSPECIFIED type
666+
667+
portal.parameterValues.add(PostgresType.deserialize(typeCode, formatCode, paramValue));
652668
}
653669
}
654670

postgresw/src/test/java/com/arcadedb/postgres/PostgresWJdbcIT.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,34 @@ void floatArrayPropertyRoundTrip() throws SQLException, ClassNotFoundException {
734734
}
735735
}
736736

737+
/**
738+
* Issue <a href="https://github.com/ArcadeData/arcadedb/issues/1630">...</a>
739+
* "Error on parsing bind message: null" when using node-postgres driver
740+
*
741+
* This test verifies that parameterized SELECT queries work correctly.
742+
*/
743+
@Test
744+
void selectWithParameterizedQuery() throws Exception {
745+
try (var conn = getConnection()) {
746+
try (var st = conn.createStatement()) {
747+
st.execute("CREATE DOCUMENT TYPE User");
748+
st.execute("INSERT INTO User SET id = 'test', name = 'Test User'");
749+
st.execute("INSERT INTO User SET id = 'other', name = 'Other User'");
750+
}
751+
752+
// This is similar to the node-postgres query: pool.query('select from User where id=$1',['test'])
753+
try (var pst = conn.prepareStatement("SELECT FROM User WHERE id = ?")) {
754+
pst.setString(1, "test");
755+
try (var rs = pst.executeQuery()) {
756+
assertThat(rs.next()).isTrue();
757+
assertThat(rs.getString("id")).isEqualTo("test");
758+
assertThat(rs.getString("name")).isEqualTo("Test User");
759+
assertThat(rs.next()).isFalse();
760+
}
761+
}
762+
}
763+
}
764+
737765
@Test
738766
void arrayOfFloatsPropertyRoundTrip() throws SQLException, ClassNotFoundException {
739767
try (var conn = getConnection()) {

0 commit comments

Comments
 (0)