Package io.codemine.java.postgresql.jdbc
Interface Codec<A>
- Type Parameters:
A- the Java type handled by this codec
public interface Codec<A>
A codec for PostgreSQL types intended for use with pgJDBC. This interface provides methods for
binding values to prepared statements and decoding values from result sets.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Codec<io.codemine.java.postgresql.codecs.Bit> Codec for SQL bit.Codec for SQL boolean.static final Codec<io.codemine.java.postgresql.codecs.Box> Codec for SQL box.Codec for SQL bpchar (char(n)).static final Codec<io.codemine.java.postgresql.codecs.Bytea> Codec for SQL bytea (binary).Codec for SQL "char" (internal type).static final Codec<io.codemine.java.postgresql.codecs.Cidr> Codec for SQL cidr.static final Codec<io.codemine.java.postgresql.codecs.Circle> Codec for SQL circle.Codec for SQL citext.Codec for SQL date.Codec for SQL datemultirange.Codec for SQL daterange.Codec for SQL float4 (real).Codec for SQL float8 (double).static final Codec<io.codemine.java.postgresql.codecs.Hstore> Codec for SQL hstore.static final Codec<io.codemine.java.postgresql.codecs.Inet> Codec for SQL inet (IP address).Codec for SQL int2 (smallint).Codec for SQL int4 (integer).Codec for SQL int4multirange.Codec for SQL int4range.Codec for SQL int8 (bigint).Codec for SQL int8multirange.Codec for SQL int8range.static final Codec<io.codemine.java.postgresql.codecs.Interval> Codec for SQL interval.static final Codec<com.fasterxml.jackson.databind.JsonNode> Codec for SQL json (text).static final Codec<com.fasterxml.jackson.databind.JsonNode> Codec for SQL jsonb (binary JSON).static final Codec<io.codemine.java.postgresql.codecs.Line> Codec for SQL line.static final Codec<io.codemine.java.postgresql.codecs.Lseg> Codec for SQL lseg.static final Codec<io.codemine.java.postgresql.codecs.Ltree> Codec for SQL ltree.static final Codec<io.codemine.java.postgresql.codecs.Macaddr> Codec for SQL macaddr.static final Codec<io.codemine.java.postgresql.codecs.Macaddr8> Codec for SQL macaddr8.Codec for SQL money.static final Codec<BigDecimal> Codec for SQL numeric/decimal.static final Codec<io.codemine.java.postgresql.codecs.Multirange<BigDecimal>> Codec for SQL nummultirange.static final Codec<io.codemine.java.postgresql.codecs.Range<BigDecimal>> Codec for SQL numrange.Codec for PostgreSQL OID.static final Codec<io.codemine.java.postgresql.codecs.Path> Codec for SQL path.static final Codec<io.codemine.java.postgresql.codecs.Point> Codec for SQL point.static final Codec<io.codemine.java.postgresql.codecs.Polygon> Codec for SQL polygon.Codec for SQL text.Codec for SQL time.static final Codec<LocalDateTime> Codec for SQL timestamp without timezone.Codec for SQL timestamptz (timestamp with timezone).static final Codec<io.codemine.java.postgresql.codecs.Timetz> Codec for SQL timetz.static final Codec<io.codemine.java.postgresql.codecs.Multirange<LocalDateTime>> Codec for SQL tsmultirange.static final Codec<io.codemine.java.postgresql.codecs.Range<LocalDateTime>> Codec for SQL tsrange.Codec for SQL tstzmultirange.Codec for SQL tstzrange.static final Codec<io.codemine.java.postgresql.codecs.Tsvector> Codec for SQL tsvector.Codec for SQL uuid.static final Codec<io.codemine.java.postgresql.codecs.Bit> Codec for SQL varbit.Codec for SQL varchar. -
Method Summary
Modifier and TypeMethodDescriptionvoidbind(PreparedStatement ps, int index, A value) Binds a value to a prepared statement.static Codec<io.codemine.java.postgresql.codecs.Bit> bit(int n) Returns a codec for PostgreSQLbit(n)— a fixed-length bit string of exactlynbits.bpchar(int n) Returns a codec for PostgreSQLbpchar(n)— a fixed-length blank-padded character string of exactlyncharacters.static <Z> Codec<Z> composite(String schema, String name, Function<Object[], Z> construct, io.codemine.java.postgresql.codecs.CompositeCodec.Field<Z, ?>... fields) Creates a composite codec for any number of fields using an untyped vararg array constructor.default AdecodeNonNullable(ResultSet rs, int row, int col) Decodes a non-nullable value from the result set.decodeNullable(ResultSet rs, int row, int col) Decodes a nullable value from the result set.decodeOptional(ResultSet rs, int row, int col) Decodes an optional value from the result set.static <A> Codec<A> enumeration(String schema, String name, Map<A, String> valueToLabel) Returns a codec for PostgreSQL enum types.static <Z,A> io.codemine.java.postgresql.codecs.CompositeCodec.Field <Z, A> Creates a field descriptor for a composite type.inDim()Add one dimension of array nesting to this codec, returning a codec for the corresponding PostgreSQL array type.io.codemine.java.postgresql.codecs.Codec<A> Converts this codec to an agnostic codec that can be used with any PostgreSQL client library.static Codec<io.codemine.java.postgresql.codecs.Bit> varbit(int n) Returns a codec for PostgreSQLvarbit(n)— a variable-length bit string of at mostnbits.varchar(int n) Returns a codec for PostgreSQLvarchar(n)— a variable-length character string of at mostncharacters.
-
Field Details
-
INT4
Codec for SQL int4 (integer). -
TEXT
Codec for SQL text. -
INET
Codec for SQL inet (IP address). -
MACADDR
Codec for SQL macaddr. -
BOOL
Codec for SQL boolean. -
INT2
Codec for SQL int2 (smallint). -
INT8
Codec for SQL int8 (bigint). -
FLOAT4
Codec for SQL float4 (real). -
FLOAT8
Codec for SQL float8 (double). -
NUMERIC
Codec for SQL numeric/decimal. -
BYTEA
Codec for SQL bytea (binary). -
UUID
Codec for SQL uuid. -
JSON
Codec for SQL json (text). -
JSONB
Codec for SQL jsonb (binary JSON). -
VARCHAR
Codec for SQL varchar. -
BPCHAR
Codec for SQL bpchar (char(n)). -
CHAR
Codec for SQL "char" (internal type). -
OID
Codec for PostgreSQL OID. -
MONEY
Codec for SQL money. -
DATE
Codec for SQL date. -
TIME
Codec for SQL time. -
TIMETZ
Codec for SQL timetz. -
TIMESTAMP
Codec for SQL timestamp without timezone. -
TIMESTAMPTZ
Codec for SQL timestamptz (timestamp with timezone). -
INTERVAL
Codec for SQL interval. -
POINT
Codec for SQL point. -
LINE
Codec for SQL line. -
LSEG
Codec for SQL lseg. -
BOX
Codec for SQL box. -
PATH
Codec for SQL path. -
POLYGON
Codec for SQL polygon. -
CIRCLE
Codec for SQL circle. -
CIDR
Codec for SQL cidr. -
MACADDR8
Codec for SQL macaddr8. -
BIT
Codec for SQL bit. -
VARBIT
Codec for SQL varbit. -
CITEXT
Codec for SQL citext. -
TSVECTOR
Codec for SQL tsvector. -
HSTORE
Codec for SQL hstore. -
INT4RANGE
Codec for SQL int4range. -
INT8RANGE
Codec for SQL int8range. -
NUMRANGE
Codec for SQL numrange. -
TSRANGE
Codec for SQL tsrange. -
TSTZRANGE
Codec for SQL tstzrange. -
DATERANGE
Codec for SQL daterange. -
INT4MULTIRANGE
Codec for SQL int4multirange. -
INT8MULTIRANGE
Codec for SQL int8multirange. -
NUMMULTIRANGE
Codec for SQL nummultirange. -
TSMULTIRANGE
Codec for SQL tsmultirange. -
TSTZMULTIRANGE
Codec for SQL tstzmultirange. -
DATEMULTIRANGE
Codec for SQL datemultirange. -
LTREE
Codec for SQL ltree.
-
-
Method Details
-
bit
Returns a codec for PostgreSQLbit(n)— a fixed-length bit string of exactlynbits.If
n <= 0, this returns the unparameterizedBITcodec.- Parameters:
n- the number of bits- Returns:
- a codec for
bit(n)
-
varbit
Returns a codec for PostgreSQLvarbit(n)— a variable-length bit string of at mostnbits.If
n <= 0, this returns the unparameterizedVARBITcodec.- Parameters:
n- the maximum number of bits- Returns:
- a codec for
varbit(n)
-
varchar
Returns a codec for PostgreSQLvarchar(n)— a variable-length character string of at mostncharacters.If
n <= 0, this returns the unparameterizedVARCHARcodec.- Parameters:
n- the maximum length in characters- Returns:
- a codec for
varchar(n)
-
bpchar
Returns a codec for PostgreSQLbpchar(n)— a fixed-length blank-padded character string of exactlyncharacters.- Parameters:
n- the fixed length in characters- Returns:
- a codec for
bpchar(n)
-
enumeration
Returns a codec for PostgreSQL enum types.- Type Parameters:
A- the enum type- Parameters:
schema- the schema name. Empty string or null for the default schema (usually ")name- the enum type namevalueToLabel- a map of enum values to their corresponding labels- Returns:
- a
Codecfor the specified enum type
-
composite
@SafeVarargs static <Z> Codec<Z> composite(String schema, String name, Function<Object[], Z> construct, io.codemine.java.postgresql.codecs.CompositeCodec.Field<Z, ?>... fields) Creates a composite codec for any number of fields using an untyped vararg array constructor.The
constructfunction receives anObject[]whose elements correspond positionally to the supplied field descriptors.Note: this constructor is less safely typed than the arity-specific overloads. Callers are responsible for casting elements of the array to the correct types.
- Type Parameters:
Z- the composite type produced by the construct function- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- function that maps anObject[]of decoded field values toZfields- field descriptors in declaration order- Returns:
- a
Codecfor the composite type
-
field
static <Z,A> io.codemine.java.postgresql.codecs.CompositeCodec.Field<Z,A> field(String name, Codec<A> codec, Function<Z, A> getter) Creates a field descriptor for a composite type.- Type Parameters:
Z- the composite typeA- the field type- Parameters:
name- the field namecodec- the codec for the field typegetter- a function to extract the field value from the composite object- Returns:
- a field descriptor
-
toAgnostic
io.codemine.java.postgresql.codecs.Codec<A> toAgnostic()Converts this codec to an agnostic codec that can be used with any PostgreSQL client library.This is required for composite codecs, which need to delegate to the underlying agnostic codec for each field.
- Returns:
- an agnostic codec equivalent to this codec
-
bind
Binds a value to a prepared statement.- Parameters:
ps- the prepared statementindex- the parameter indexvalue- the value to bind- Throws:
SQLException- if a database access error occurs
-
decodeNullable
Decodes a nullable value from the result set.- Parameters:
rs- the result setrow- the row indexcol- the column index- Returns:
- the decoded value, or null if the value is SQL NULL
- Throws:
SQLException- if a database access error occurs
-
decodeNonNullable
Decodes a non-nullable value from the result set.- Parameters:
rs- the result setrow- the row indexcol- the column index- Returns:
- the decoded value
- Throws:
SQLException- if a database access error occurs or the value is null
-
decodeOptional
Decodes an optional value from the result set.- Parameters:
rs- the result setrow- the row indexcol- the column index- Returns:
- the decoded value wrapped in an
Optional, orOptional.empty()if the value is SQL NULL - Throws:
SQLException- if a database access error occurs
-
inDim
Add one dimension of array nesting to this codec, returning a codec for the corresponding PostgreSQL array type. For example, if this is a codec for typeA, the returned codec will be for typeA[]. This can be called repeatedly to create codecs for multi-dimensional arrays, e.g.A[][].- Returns:
- a codec for a one-dimension higher array of this codec's type
-