Given an IPv6 or IPv4 network address as a string, returns a binary string that represents the numeric value of the address in network byte order (big endian). Because numeric-format IPv6 addresses require more bytes than the largest integer type, the representation returned by this function has theVARBINARY
data type:VARBINARY(16)
for IPv6 addresses andVARBINARY(4)
for IPv4 addresses. If the argument is not a valid address,INET6_ATON()
returnsNULL
.
public class Events extends TableImpl<EventsRecord> {
public static final Events EVENTS = new Events();
// other stuff...
public final TableField<EventsRecord, byte[]> IP_ADDRESS = createField("ip_address", org.jooq.impl.SQLDataType.VARBINARY(16), this, "");
}
DSL.condition("{0} = INET6_ATON({1})", EVENTS.IP_ADDRESS, "4.2.2.1")
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/c3c8b7a9-bdc7-41f9-a6d3-7312b7f529cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public class Events extends TableImpl<EventsRecord> {
public static final Events EVENTS = new Events();
// other stuff...
public final TableField<EventsRecord, String> IP_ADDRESS = createField("ip_address", org.jooq.impl.SQLDataType.VARBINARY(16),this, "", new Inet6AtonBinding());
}
package com.example;
import java.sql.*;
import org.jooq.*;
import java.util.Objects;
public class Inet6AtonBinding implements Binding<byte[], String> {
@Override
public Converter<byte[], String> converter() {
return new Converter<byte[], String>() {
@Override
public String from(byte[] databaseObject) {
return databaseObject.toString();
}
@Override
public byte[] to(String userObject) {
return userObject.getBytes();
}
@Override
public Class<byte[]> fromType() {
return byte[].class;
}
@Override
public Class<String> toType() {
return String.class;
}
};
}
// Rendering a bind variable for the binding context's value and casting it to the json type
@Override
public void sql(BindingSQLContext<String> ctx) throws SQLException {
ctx.render().sql("INET6_ATON(?)");
}
// Registering VARCHAR types for JDBC CallableStatement OUT parameters
@Override
public void register(BindingRegisterContext<String> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
// Converting the String to a String value and setting that on a JDBC PreparedStatement
@Override
public void set(BindingSetStatementContext<String> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}
// Getting a String value from a JDBC ResultSet and converting that to a String
@Override
public void get(BindingGetResultSetContext<String> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getBytes(ctx.index()));
}
// Getting a String value from a JDBC CallableStatement and converting that to a String
@Override
public void get(BindingGetStatementContext<String> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getBytes(ctx.index()));
}
// Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)
@Override
public void set(BindingSetSQLOutputContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
// Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)
@Override
public void get(BindingGetSQLInputContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
<database>
<forcedTypes>
<forcedType>
<userType>java.lang.String</userType>
<binding>com.example.Inet6AtonBinding</binding>
<expression>.*\.(ip_address)</expression>
</forcedType>
</forcedTypes>
</database>
where ( `events`.`ip_address` = INET6_ATON(?))
For the record, I've created a feature request to think about this particular data type conversion case a bit more thoroughly:It's great that the existing Binding can already accomodate 80% of your use-case, but it would be much better if you could automatically transform your columns using INET6_NTOA as well, if you're projecting them.
On Mon, Jun 24, 2019 at 8:58 AM Lukas Eder <luka...@gmail.com> wrote:
Hi John,You can write your own custom data type Binding and re-implement the sql() method to produce the correct SQL string ("INET6_ATON(?)") for your bind variables. That would make it work for writing to the database using this function (both with insert/updates, and with queries):Using a binding, you can also make sure that your client facing data type is not byte[], but a more reasonable format, e.g. String, or whatever IP Address data type you have.In order to read the type, however, you'd have to convert the byte[] to your type in the client, or call a MySQL function explicitly.I hope this helps,Lukas
On Mon, Jun 24, 2019 at 1:15 AM John Bai <jthe...@gmail.com> wrote:
I have an ip_address column with a VARBINARY(16) data type in a mysql table. I want to store IP addresses in that column using the INET6_ATON built-in function provided by mysql. I also want to lookup records by IP address using that built-in function as well (SQL example: WHERE ip_address = INET6_ATON('4.2.2.1')"). Here's the docs for that function:--Given an IPv6 or IPv4 network address as a string, returns a binary string that represents the numeric value of the address in network byte order (big endian). Because numeric-format IPv6 addresses require more bytes than the largest integer type, the representation returned by this function has theVARBINARY
data type:VARBINARY(16)
for IPv6 addresses andVARBINARY(4)
for IPv4 addresses. If the argument is not a valid address,INET6_ATON()
returnsNULL
.Here's a snippet of my generated jOOQ for the table and ip_address column:
public class Events extends TableImpl<EventsRecord> {
public static final Events EVENTS = new Events();
// other stuff...
public final TableField<EventsRecord, byte[]> IP_ADDRESS = createField("ip_address", org.jooq.impl.SQLDataType.VARBINARY(16), this, "");
}Q1. How can I insert/update records using INET6_ATON('4.2.2.1') as a value for the IP_ADDRESS field?I have a new EventsRecord instance via EventsRecord eventsRecord = dslContext.newRecord(EVENTS). There's existing code using this object to set values for all the fields. How can I set a value "INET6_ATON('4.2.2.1')" on my ip_address field? eventsRecord.setIpAddress() method accepts byte[] only, so I can't pass "INET6_ATON('4.2.2.1')" as a value.Q2. How do I lookup records using INET6_ATON('4.2.2.1') as an equals value for the IP_ADDRESS field?I tried using EVENTS.IP_ADDRESS.eq("INET6_ATON('4.2.2.1')") but that doesn't work because the field's eq method expects a byte[] value. I couldn't figure out from reading the docs if it was possible to get around this byte[] data type constraint, so I ended up doing this instead and it worked. Is there a way I can do this better?
DSL.condition("{0} = INET6_ATON({1})", EVENTS.IP_ADDRESS, "4.2.2.1")
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq...@googlegroups.com.
When I pretty print the SQL from a select with a .where(EVENTS.IP_ADDRESS.eq("4.2.2.1")) following these instructions, I see this:
where (`events`.`ip_address` = INET6_ATON(?))What do I need to change in order to get this working? Thanks!
@Override
public void set(BindingSetStatementContext<String> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}
@Override
public void set(BindingSetStatementContext<String> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), ctx.value());
}
package com.example;
import java.sql.*;
import org.jooq.*;
public class Inet6AtonBinding implements Binding<byte[], String> {
@Override
public Converter<byte[], String> converter() {
return new Converter<byte[], String>() {
@Override
public String from(byte[] bytes) {
return new String(bytes);
}
@Override
public byte[] to(String string) {
return string.getBytes();
}
@Override
public Class<byte[]> fromType() {
return byte[].class;
}
@Override
public Class<String> toType() {
return String.class;
}
};
}
@Override
public void sql(BindingSQLContext<String> ctx) throws SQLException {
ctx.render().sql("INET6_ATON(?)");
}
@Override
public void register(BindingRegisterContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetStatementContext<String> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), ctx.value());
}
@Override
public void get(BindingGetResultSetContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(BindingGetStatementContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetSQLOutputContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(BindingGetSQLInputContext<String> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/1aa3f828-5db3-479a-bd84-02c80818922e%40googlegroups.com.