提交 86fba974 authored 作者: Thomas Mueller's avatar Thomas Mueller

Cleanup.

上级 a75bf79a
...@@ -543,7 +543,7 @@ public class JdbcStatement extends TraceObject implements Statement { ...@@ -543,7 +543,7 @@ public class JdbcStatement extends TraceObject implements Statement {
/** /**
* Check whether the statement was cancelled. * Check whether the statement was cancelled.
* *
* @return true if yes * @return true if yes
*/ */
public boolean wasCancelled() { public boolean wasCancelled() {
......
...@@ -262,7 +262,8 @@ public class PgServer implements Service { ...@@ -262,7 +262,8 @@ public class PgServer implements Service {
/** /**
* Get the thread with the given process id. * Get the thread with the given process id.
* *
* @param processId the process id
* @return the thread * @return the thread
*/ */
PgServerThread getThread(int processId) { PgServerThread getThread(int processId) {
...@@ -463,9 +464,10 @@ public class PgServer implements Service { ...@@ -463,9 +464,10 @@ public class PgServer implements Service {
* A fake wrapper around pg_get_expr(expr_text, relation_oid), in PostgreSQL * A fake wrapper around pg_get_expr(expr_text, relation_oid), in PostgreSQL
* it "decompiles the internal form of an expression, assuming that any vars * it "decompiles the internal form of an expression, assuming that any vars
* in it refer to the relation indicated by the second parameter". * in it refer to the relation indicated by the second parameter".
* *
* @param exprText the expression text * @param exprText the expression text
* @param relationOid the relation object id * @param relationOid the relation object id
* @return always null
*/ */
public static String getPgExpr(String exprText, int relationOid) { public static String getPgExpr(String exprText, int relationOid) {
return null; return null;
...@@ -475,12 +477,14 @@ public class PgServer implements Service { ...@@ -475,12 +477,14 @@ public class PgServer implements Service {
* Check if the current session has access to this table. * Check if the current session has access to this table.
* This method is called by the database. * This method is called by the database.
* *
* @param pgType the postgres type oid * @param conn the connection
* @param pgType the PostgreSQL type oid
* @param typeMod the type modifier (typically -1) * @param typeMod the type modifier (typically -1)
* @return A string name for the given type * @return the name of the given type
*/ */
public static String formatType(Connection conn, int pgType, int typeMod) throws SQLException { public static String formatType(Connection conn, int pgType, int typeMod) throws SQLException {
PreparedStatement prep = conn.prepareStatement("select typname from pg_catalog.pg_type where oid = ? and typtypmod = ?"); PreparedStatement prep = conn.prepareStatement(
"select typname from pg_catalog.pg_type where oid = ? and typtypmod = ?");
prep.setInt(1, pgType); prep.setInt(1, pgType);
prep.setInt(2, typeMod); prep.setInt(2, typeMod);
ResultSet rs = prep.executeQuery(); ResultSet rs = prep.executeQuery();
......
...@@ -155,7 +155,8 @@ public class PgServerThread implements Runnable { ...@@ -155,7 +155,8 @@ public class PgServerThread implements Runnable {
if (c != null && key == c.secret) { if (c != null && key == c.secret) {
c.cancelRequest(); c.cancelRequest();
} else { } else {
// According to http://www.postgresql.org/docs/9.1/static/protocol-flow.html#AEN91739, // According to
// http://www.postgresql.org/docs/9.1/static/protocol-flow.html#AEN91739,
// when canceling a request, if an invalid secret is provided then no exception // when canceling a request, if an invalid secret is provided then no exception
// should be sent back to the client. // should be sent back to the client.
server.trace("Invalid CancelRequest: pid=" + pid + ", key=" + key); server.trace("Invalid CancelRequest: pid=" + pid + ", key=" + key);
...@@ -548,7 +549,8 @@ public class PgServerThread implements Runnable { ...@@ -548,7 +549,8 @@ public class PgServerThread implements Runnable {
return clientEncoding; return clientEncoding;
} }
private void setParameter(PreparedStatement prep, int pgType, int i, int[] formatCodes) throws SQLException, IOException { private void setParameter(PreparedStatement prep,
int pgType, int i, int[] formatCodes) throws SQLException, IOException {
boolean text = (i >= formatCodes.length) || (formatCodes[i] == 0); boolean text = (i >= formatCodes.length) || (formatCodes[i] == 0);
int col = i + 1; int col = i + 1;
int paramLen = readInt(); int paramLen = readInt();
...@@ -711,7 +713,7 @@ public class PgServerThread implements Runnable { ...@@ -711,7 +713,7 @@ public class PgServerThread implements Runnable {
/** /**
* Check whether the given type should be formatted as text. * Check whether the given type should be formatted as text.
* *
* @return true for binary * @return true for binary
*/ */
private static boolean formatAsText(int pgType) { private static boolean formatAsText(int pgType) {
......
...@@ -675,14 +675,8 @@ public class Data { ...@@ -675,14 +675,8 @@ public class Data {
writeByte((byte) type); writeByte((byte) type);
byte[] b = v.getBytes(); byte[] b = v.getBytes();
int len = b.length; int len = b.length;
if (len < 32) { writeVarInt(len);
writeByte((byte) (BYTES_0_31 + len)); write(b, 0, len);
write(b, 0, b.length);
} else {
writeByte((byte) type);
writeVarInt(b.length);
write(b, 0, b.length);
}
break; break;
} }
default: default:
...@@ -854,6 +848,12 @@ public class Data { ...@@ -854,6 +848,12 @@ public class Data {
} }
return ValueResultSet.get(rs); return ValueResultSet.get(rs);
} }
case Value.GEOMETRY: {
int len = readVarInt();
byte[] b = DataUtils.newBytes(len);
read(b, 0, len);
return ValueGeometry.get(b);
}
default: default:
if (type >= INT_0_15 && type < INT_0_15 + 16) { if (type >= INT_0_15 && type < INT_0_15 + 16) {
return ValueInt.get(type - INT_0_15); return ValueInt.get(type - INT_0_15);
...@@ -1092,10 +1092,7 @@ public class Data { ...@@ -1092,10 +1092,7 @@ public class Data {
case Value.GEOMETRY: { case Value.GEOMETRY: {
byte[] b = v.getBytesNoCopy(); byte[] b = v.getBytesNoCopy();
int len = b.length; int len = b.length;
if (len < 32) { return 1 + getVarIntLen(len) + len;
return 1 + b.length;
}
return 1 + getVarIntLen(b.length) + b.length;
} }
default: default:
throw DbException.throwInternalError("type=" + v.getType()); throw DbException.throwInternalError("type=" + v.getType());
......
...@@ -155,7 +155,7 @@ public abstract class Value { ...@@ -155,7 +155,7 @@ public abstract class Value {
* The value type for string values with a fixed size. * The value type for string values with a fixed size.
*/ */
public static final int GEOMETRY = 22; public static final int GEOMETRY = 22;
/** /**
* The number of value types. * The number of value types.
*/ */
......
...@@ -9,6 +9,8 @@ package org.h2.value; ...@@ -9,6 +9,8 @@ package org.h2.value;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.StringUtils;
import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKBReader; import com.vividsolutions.jts.io.WKBReader;
...@@ -49,7 +51,7 @@ public class ValueGeometry extends Value { ...@@ -49,7 +51,7 @@ public class ValueGeometry extends Value {
public static ValueGeometry get(String s) { public static ValueGeometry get(String s) {
return (ValueGeometry) Value.cache(new ValueGeometry(fromWKT(s))); return (ValueGeometry) Value.cache(new ValueGeometry(fromWKT(s)));
} }
/** /**
* Get or create a geometry value for the given geometry. * Get or create a geometry value for the given geometry.
* *
...@@ -59,23 +61,41 @@ public class ValueGeometry extends Value { ...@@ -59,23 +61,41 @@ public class ValueGeometry extends Value {
public static ValueGeometry get(byte[] bytes) { public static ValueGeometry get(byte[] bytes) {
return (ValueGeometry) Value.cache(new ValueGeometry(fromWKB(bytes))); return (ValueGeometry) Value.cache(new ValueGeometry(fromWKB(bytes)));
} }
public Geometry getGeometry() { public Geometry getGeometry() {
return geometry; return geometry;
} }
/**
* Check whether two values intersect.
*
* @param r the second value
* @return true if they intersect
*/
public boolean intersects(ValueGeometry r) { public boolean intersects(ValueGeometry r) {
return geometry.intersects(r.getGeometry()); return geometry.intersects(r.getGeometry());
} }
/**
* Get the intersection of two values.
*
* @param r the second value
* @return the intersection
*/
public Value intersection(ValueGeometry r) { public Value intersection(ValueGeometry r) {
return get(this.geometry.intersection(r.geometry)); return get(geometry.intersection(r.geometry));
} }
/**
* Get the union of two values.
*
* @param r the second value
* @return the union
*/
public Value union(ValueGeometry r) { public Value union(ValueGeometry r) {
return get(this.geometry.union(r.geometry)); return get(geometry.union(r.geometry));
} }
@Override @Override
public int getType() { public int getType() {
return Value.GEOMETRY; return Value.GEOMETRY;
...@@ -83,13 +103,13 @@ public class ValueGeometry extends Value { ...@@ -83,13 +103,13 @@ public class ValueGeometry extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
return "'" + toWKT() + "'"; return StringUtils.quoteStringSQL(toWKT());
} }
@Override @Override
protected int compareSecure(Value v, CompareMode mode) { protected int compareSecure(Value v, CompareMode mode) {
Geometry g = ((ValueGeometry) v).geometry; Geometry g = ((ValueGeometry) v).geometry;
return this.geometry.compareTo(g); return geometry.compareTo(g);
} }
@Override @Override
...@@ -104,7 +124,7 @@ public class ValueGeometry extends Value { ...@@ -104,7 +124,7 @@ public class ValueGeometry extends Value {
@Override @Override
public int hashCode() { public int hashCode() {
return this.geometry.hashCode(); return geometry.hashCode();
} }
@Override @Override
...@@ -116,7 +136,7 @@ public class ValueGeometry extends Value { ...@@ -116,7 +136,7 @@ public class ValueGeometry extends Value {
public byte[] getBytes() { public byte[] getBytes() {
return toWKB(); return toWKB();
} }
@Override @Override
public void set(PreparedStatement prep, int parameterIndex) throws SQLException { public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
prep.setObject(parameterIndex, geometry); prep.setObject(parameterIndex, geometry);
...@@ -136,25 +156,32 @@ public class ValueGeometry extends Value { ...@@ -136,25 +156,32 @@ public class ValueGeometry extends Value {
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof ValueGeometry && geometry.equals(((ValueGeometry) other).geometry); return other instanceof ValueGeometry && geometry.equals(((ValueGeometry) other).geometry);
} }
/** /**
* Convert to Well-Known-Text format. * Convert the value to the Well-Known-Text format.
*
* @return the well-known-text
*/ */
public String toWKT() { public String toWKT() {
WKTWriter w = new WKTWriter(); WKTWriter w = new WKTWriter();
return w.write(this.geometry); return w.write(geometry);
} }
/** /**
* Convert to Well-Known-Binary format. * Convert to value to the Well-Known-Binary format.
*
* @return the well-known-binary
*/ */
public byte[] toWKB() { public byte[] toWKB() {
WKBWriter w = new WKBWriter(); WKBWriter w = new WKBWriter();
return w.write(this.geometry); return w.write(geometry);
} }
/** /**
* Convert from Well-Known-Text format. * Convert a Well-Known-Text to a Geometry object.
*
* @param s the well-known-text
* @return the Geometry object
*/ */
private static Geometry fromWKT(String s) { private static Geometry fromWKT(String s) {
WKTReader r = new WKTReader(); WKTReader r = new WKTReader();
...@@ -164,9 +191,12 @@ public class ValueGeometry extends Value { ...@@ -164,9 +191,12 @@ public class ValueGeometry extends Value {
throw DbException.convert(ex); throw DbException.convert(ex);
} }
} }
/** /**
* Convert from Well-Known-Binary format. * Convert a Well-Known-Binary to a Geometry object.
*
* @param s the well-known-binary
* @return the Geometry object
*/ */
private static Geometry fromWKB(byte[] bytes) { private static Geometry fromWKB(byte[] bytes) {
WKBReader r = new WKBReader(); WKBReader r = new WKBReader();
...@@ -176,4 +206,5 @@ public class ValueGeometry extends Value { ...@@ -176,4 +206,5 @@ public class ValueGeometry extends Value {
throw DbException.convert(ex); throw DbException.convert(ex);
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论