forked from BUAA-BDA/OpenHuFu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
2,049 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Postgis适配说明 | ||
Point类在com.hufudb.openhufu.data.storage.Point中定义 | ||
|
||
在proto传输过程中,Point类型以字符串类型Point(x y)形式传递 | ||
|
||
在生成SQL语句时,需要将Point类型翻译为ST_GeomFromText('Point(x y)', 4326),从而表示为geometry类型对象 |
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,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>openhufu-adapter</artifactId> | ||
<groupId>com.hufudb.openhufu</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>openhufu-adapter-postgis</artifactId> | ||
|
||
<properties> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.hufudb.openhufu</groupId> | ||
<artifactId>openhufu-owner</artifactId> | ||
<version>${project.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.postgis</groupId> | ||
<artifactId>postgis-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>report-aggregate</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>report-aggregate</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
32 changes: 32 additions & 0 deletions
32
...apter-postgis/src/main/java/com/hufudb/openhufu/owner/adapter/postgis/PostgisAdapter.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,32 @@ | ||
package com.hufudb.openhufu.owner.adapter.postgis; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
import java.sql.SQLException; | ||
import java.sql.ResultSet; | ||
import com.hufudb.openhufu.owner.adapter.AdapterTypeConverter; | ||
import com.hufudb.openhufu.owner.adapter.jdbc.JDBCAdapter; | ||
import com.hufudb.openhufu.data.storage.DataSet; | ||
import com.hufudb.openhufu.data.storage.EmptyDataSet; | ||
import com.hufudb.openhufu.expression.Translator; | ||
import com.hufudb.openhufu.data.schema.Schema; | ||
|
||
|
||
public class PostgisAdapter extends JDBCAdapter { | ||
PostgisAdapter(String catalog, Connection connection, Statement statement, | ||
AdapterTypeConverter converter, Translator translator) { | ||
super(catalog, connection, statement, converter, translator); | ||
} | ||
|
||
@Override | ||
protected DataSet executeSQL(String sql, Schema schema) { | ||
try { | ||
ResultSet rs = statement.executeQuery(sql); | ||
LOG.info("Execute {}", sql); | ||
return new PostgisResultDataSet(schema, rs); | ||
} catch (SQLException e) { | ||
LOG.error("Fail to execute SQL [{}]: {}", sql, e.getMessage()); | ||
return EmptyDataSet.INSTANCE; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ostgis/src/main/java/com/hufudb/openhufu/owner/adapter/postgis/PostgisAdapterFactory.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,43 @@ | ||
package com.hufudb.openhufu.owner.adapter.postgis; | ||
|
||
import com.hufudb.openhufu.common.enums.DataSourceType; | ||
import com.hufudb.openhufu.owner.adapter.AdapterConfig; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.Statement; | ||
import com.hufudb.openhufu.owner.adapter.AdapterFactory; | ||
import com.hufudb.openhufu.expression.BasicTranslator; | ||
import com.hufudb.openhufu.owner.adapter.Adapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class PostgisAdapterFactory implements AdapterFactory { | ||
|
||
static final Logger LOG = LoggerFactory.getLogger(PostgisAdapterFactory.class); | ||
|
||
public PostgisAdapterFactory() { | ||
try { | ||
Class.forName("org.postgresql.Driver"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public Adapter create(AdapterConfig config) { | ||
try { | ||
Connection connection = DriverManager.getConnection(config.url, config.user, config.passwd); | ||
Statement statement = connection.createStatement(); | ||
return new PostgisAdapter(config.catalog, connection, statement, new PostgisTypeConverter(), | ||
new BasicTranslator(getType().getType())); | ||
} catch (Exception e) { | ||
LOG.error("Fail to create csv adapter: {}", config.url, e); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public DataSourceType getType() { | ||
return DataSourceType.POSTGIS; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...postgis/src/main/java/com/hufudb/openhufu/owner/adapter/postgis/PostgisResultDataSet.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,91 @@ | ||
package com.hufudb.openhufu.owner.adapter.postgis; | ||
|
||
import java.sql.ResultSet; | ||
import java.util.List; | ||
import com.google.common.collect.ImmutableList; | ||
import com.hufudb.openhufu.data.schema.Schema; | ||
import com.hufudb.openhufu.data.storage.ResultDataSet; | ||
import com.hufudb.openhufu.proto.OpenHuFuData.ColumnDesc; | ||
import org.postgresql.util.PGobject; | ||
|
||
/** | ||
* PostGIS extension of ResultDataSet | ||
*/ | ||
public class PostgisResultDataSet extends ResultDataSet { | ||
|
||
public PostgisResultDataSet(Schema schema, ResultSet result) { | ||
super(schema, result); | ||
} | ||
|
||
@Override | ||
protected List<Getter> generateGetters() { | ||
ImmutableList.Builder<Getter> builder = ImmutableList.builder(); | ||
int i = 1; | ||
for (ColumnDesc col : schema.getColumnDescs()) { | ||
final int idx = i; | ||
switch (col.getType()) { | ||
case BLOB: | ||
builder.add(() -> { | ||
return result.getBytes(idx); | ||
}); | ||
break; | ||
case BOOLEAN: | ||
builder.add(() -> { | ||
return result.getBoolean(idx); | ||
}); | ||
break; | ||
case BYTE: | ||
case SHORT: | ||
case INT: | ||
builder.add(() -> { | ||
return result.getInt(idx); | ||
}); | ||
break; | ||
case DATE: | ||
builder.add(() -> { | ||
return result.getDate(idx); | ||
}); | ||
break; | ||
case TIME: | ||
builder.add(() -> { | ||
return result.getTime(idx); | ||
}); | ||
break; | ||
case TIMESTAMP: | ||
builder.add(() -> { | ||
return result.getTimestamp(idx); | ||
}); | ||
break; | ||
case LONG: | ||
builder.add(() -> { | ||
return result.getLong(idx); | ||
}); | ||
break; | ||
case STRING: | ||
builder.add(() -> { | ||
return result.getString(idx); | ||
}); | ||
break; | ||
case DOUBLE: | ||
builder.add(() -> { | ||
return result.getDouble(idx); | ||
}); | ||
break; | ||
case FLOAT: | ||
builder.add(() -> { | ||
return result.getFloat(idx); | ||
}); | ||
break; | ||
case GEOMETRY: | ||
builder.add(() -> { | ||
return PostgisUtils.fromPGPoint(((PGobject) (result.getObject(idx)))); | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
++i; | ||
} | ||
return builder.build(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...postgis/src/main/java/com/hufudb/openhufu/owner/adapter/postgis/PostgisTypeConverter.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,48 @@ | ||
package com.hufudb.openhufu.owner.adapter.postgis; | ||
|
||
import com.hufudb.openhufu.owner.adapter.AdapterTypeConverter; | ||
import com.hufudb.openhufu.proto.OpenHuFuData.ColumnType; | ||
|
||
public class PostgisTypeConverter extends AdapterTypeConverter { | ||
@Override | ||
public ColumnType convert(int type, String typeName) { | ||
switch (typeName) { | ||
case "real": | ||
case "float4": | ||
return ColumnType.FLOAT; | ||
case "float8": | ||
case "double precision": | ||
case "numeric": | ||
return ColumnType.DOUBLE; | ||
case "TINYINT": | ||
return ColumnType.BYTE; | ||
case "SMALLINT": | ||
return ColumnType.SHORT; | ||
case "int2": | ||
case "int4": | ||
return ColumnType.INT; | ||
case "oid": | ||
case "int8": | ||
return ColumnType.LONG; | ||
case "varchar": | ||
case "char": | ||
case "bpchar": | ||
case "text": | ||
case "name": | ||
return ColumnType.STRING; | ||
case "bit": | ||
case "bool": | ||
return ColumnType.BOOLEAN; | ||
case "date": | ||
return ColumnType.DATE; | ||
case "time": | ||
return ColumnType.TIME; | ||
case "timestamp": | ||
return ColumnType.TIMESTAMP; | ||
case "geometry": | ||
return ColumnType.GEOMETRY; | ||
default: | ||
return ColumnType.STRING; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...adapter-postgis/src/main/java/com/hufudb/openhufu/owner/adapter/postgis/PostgisUtils.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,23 @@ | ||
package com.hufudb.openhufu.owner.adapter.postgis; | ||
|
||
import java.sql.SQLException; | ||
import org.postgresql.util.PGobject; | ||
import org.postgis.GeometryBuilder; | ||
import com.hufudb.openhufu.data.storage.Point; | ||
|
||
public class PostgisUtils { | ||
private PostgisUtils() {} | ||
|
||
/** | ||
* convert PostGIS Point to Pair in java | ||
*/ | ||
public static Point fromPGPoint(PGobject pgpoint) { | ||
try { | ||
org.postgis.Point p = GeometryBuilder.geomFromString(pgpoint.getValue()).getPoint(0); | ||
return new Point(p.x, p.y); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...gis/src/main/resources/META-INF/services/com.hufudb.openhufu.owner.adapter.AdapterFactory
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 @@ | ||
com.hufudb.openhufu.owner.adapter.postgis.PostgisAdapterFactory |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
<modules> | ||
<module>adapter-csv</module> | ||
<module>adapter-postgis</module> | ||
</modules> | ||
|
||
<dependencies> | ||
|
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
Oops, something went wrong.