-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support incoming binary values (#27)
* feat: add support incoming binary values * fix: use production endpoint by default * fix: do not generate db ids longer than 30 chars * fix: use production endpoint * fix: region of test instance did no longer support PG * test: try with prod endpoint Co-authored-by: Jim King <jsking@google.com>
- Loading branch information
Showing
18 changed files
with
377 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/google/cloud/spanner/pgadapter/parsers/NumericParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.cloud.spanner.pgadapter.parsers; | ||
|
||
import java.math.BigDecimal; | ||
import java.nio.charset.StandardCharsets; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import org.postgresql.util.ByteConverter; | ||
|
||
/** Translate from wire protocol to {@link Number}. */ | ||
public class NumericParser extends Parser<Number> { | ||
public NumericParser(ResultSet item, int position) throws SQLException { | ||
// This should be either a BigDecimal value or a Double.NaN. | ||
this.item = (Number) item.getObject(position); | ||
} | ||
|
||
public NumericParser(Object item) { | ||
this.item = (Number) item; | ||
} | ||
|
||
public NumericParser(byte[] item, FormatCode formatCode) { | ||
switch (formatCode) { | ||
case TEXT: | ||
String stringValue = new String(item); | ||
if (stringValue.equalsIgnoreCase("NaN")) { | ||
this.item = Double.NaN; | ||
} else { | ||
this.item = new BigDecimal(new String(item)); | ||
} | ||
break; | ||
case BINARY: | ||
this.item = ByteConverter.numeric(item, 0, item.length); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unsupported format: " + formatCode); | ||
} | ||
} | ||
|
||
@Override | ||
public Number getItem() { | ||
return this.item; | ||
} | ||
|
||
@Override | ||
protected String stringParse() { | ||
return Double.isNaN(this.item.doubleValue()) ? "NaN" : ((BigDecimal) this.item).toPlainString(); | ||
} | ||
|
||
@Override | ||
protected byte[] binaryParse() { | ||
if (Double.isNaN(this.item.doubleValue())) { | ||
return "NaN".getBytes(StandardCharsets.UTF_8); | ||
} | ||
return ByteConverter.numeric((BigDecimal) this.item); | ||
} | ||
} |
Oops, something went wrong.