8000 Support row.getDouble and NUMERIC type. · juliombb/postgres-async-driver@e9fc76f · GitHub
[go: up one dir, main page]

Skip to content

Commit e9fc76f

Browse files
committed
Support row.getDouble and NUMERIC type.
1 parent 8dfa5f3 commit e9fc76f

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/main/java/com/github/pgasync/Row.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public interface Row {
6060

6161
BigDecimal getBigDecimal(String column);
6262

63+
Double getDouble(int index);
64+
65+
Double getDouble(String column);
66+
6367
Date getDate(int index);
6468

6569
Date getDate(String column);

src/main/java/com/github/pgasync/impl/PgRow.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public BigDecimal getBigDecimal(String column) {
135135
return dataConverter.toBigDecimal(pgColumn.type, data.getValue(pgColumn.index));
136136
}
137137

138+
@Override
139+
public Double getDouble(int index) {
140+
return dataConverter.toDouble(pgColumns[index].type, data.getValue(index));
141+
}
142+
143+
@Override
144+
public Double getDouble(String column) {
145+
PgColumn pgColumn = getColumn(column);
146+
return dataConverter.toDouble(pgColumn.type, data.getValue(pgColumn.index));
147+
}
148+
138149
@Override
139150
public Date getDate(int index) {
140151
return dataConverter.toDate(pgColumns[index].type, data.getValue(index));

src/main/java/com/github/pgasync/impl/conversion/DataConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public BigInteger toBigInteger(Oid oid, byte[] value) {
5050
public BigDecimal toBigDecimal(Oid oid, byte[] value) {
5151
return value == null ? null : NumericConversions.toBigDecimal(oid, value);
5252
}
53+
public Double toDouble(Oid oid, byte[] value) {
54+
return value == null ? null : NumericConversions.toDouble(oid, value);
55+
}
5356
public Date toDate(Oid oid, byte[] value) {
5457
return value == null ? null : TemporalConversions.toDate(oid, value);
5558
}

src/main/java/com/github/pgasync/impl/conversion/NumericConversions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,31 @@ static BigInteger toBigInteger(Oid oid, byte[] value) {
7272
static BigDecimal toBigDecimal(Oid oid, byte[] value) {
7373
switch (oid) {
7474
case UNSPECIFIED: // fallthrough
75+
case INT2: // fallthrough
76+
case INT4: // fallthrough
77+
case INT8: // fallthrough
78+
case NUMERIC: // fallthrough
7579
case FLOAT4: // fallthrough
7680
case FLOAT8:
7781
return new BigDecimal(new String(value, UTF_8));
7882
default:
7983
throw new SqlException("Unsupported conversion " + oid.name() + " -> BigDecimal");
8084
}
8185
}
86+
87+
static Double toDouble(Oid oid, byte[] value) {
88+
switch (oid) {
89+
case UNSPECIFIED: // fallthrough
90+
case INT2: // fallthrough
91+
case INT4: // fallthrough
92+
case INT8: // fallthrough
93+
case NUMERIC: // fallthrough
94+
case FLOAT4: // fallthrough
95+
case FLOAT8:
96+
return Double.valueOf(new String(value, UTF_8));
97+
default:
98+
throw new SqlException("Unsupported conversion " + oid.name() + " -> BigDecimal");
99+
}
100+
}
101+
82102
}

src/test/java/com/github/pgasync/impl/TypeConverterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ public void shouldConvertFloat4ToBigDecimal() {
141141
assertEquals(new BigDecimal("789.01"), dbr.query("select 789.01::FLOAT4 as sum").row(0).getBigDecimal("sum"));
142142
}
143143

144+
@Test
145+
public void shouldConvertNumericToBigDecimal() {
146+
assertEquals(new BigDecimal("1223423.01"), dbr.query("select 1223423.01::NUMERIC as sum").row(0).getBigDecimal("sum"));
147+
}
148+
149+
@Test
150+
public void shouldConvertFloat4ToDouble() {
151+
assertEquals((Double) 1223420.0, dbr.query("select 1223420.0::FLOAT4 as sum").row(0).getDouble("sum"));
152+
}
153+
154+
@Test
155+
public void shouldConvertNumericToDouble() {
156+
assertEquals((Double) 1223423.01, dbr.query("select 1223423.01::NUMERIC as sum").row(0).getDouble("sum"));
157+
}
158+
144159
@Test
145160
public void shouldConvertDateToDate() {
146161
assertEquals(new Date(millis(2014, 1, 31, 0, 0, 0, 0)),

0 commit comments

Comments
 (0)
0