Skip to content

Commit

Permalink
HHH-14802 Update CockroachDB support for 6
Browse files Browse the repository at this point in the history
  • Loading branch information
maesenka committed Sep 21, 2021
1 parent bc44d65 commit 9316c16
Show file tree
Hide file tree
Showing 24 changed files with 155 additions and 707 deletions.
Expand Up @@ -192,7 +192,7 @@ The `GEOGRAPHY` type is not currently supported.
====

CockroachDB::
The dialect `CockroachDB202SpatialDialect` support the `GEOMETRY` type in CockroachDB v20.2 and later.
The dialect `CockroachDB202` support the `GEOMETRY` type in CockroachDB v20.2 and later.

[NOTE]
====
Expand Down
2 changes: 1 addition & 1 deletion gradle/databases.gradle
Expand Up @@ -148,7 +148,7 @@ ext {
'connection.init_sql' : ''
],
postgis : [
'db.dialect' : 'org.hibernate.dialect.PostgreSQLDialect',
'db.dialect' : 'org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect',
'jdbc.driver': 'org.postgresql.Driver',
'jdbc.user' : 'hibernate_orm_test',
'jdbc.pass' : 'hibernate_orm_test',
Expand Down
Expand Up @@ -87,6 +87,9 @@ public CockroachDialect(int version) {
registerColumnType( Types.NCLOB, "string" );

registerColumnType( Types.JAVA_OBJECT, "json" );

//register geometry type
registerColumnType( 5432, "geometry" );
}

@Override
Expand Down
Expand Up @@ -11,21 +11,24 @@
import java.util.Map;
import java.util.function.Function;

import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.dialect.cockroachdb.CockroachDbContributor;
import org.hibernate.spatial.dialect.postgis.PostgisDialectContributor;

class ContributorResolver {

private final static Map<Class<? extends Dialect>,
Function<ServiceRegistry, ContributorImplementor>> ContributorMap = new HashMap<>();
Function<ServiceRegistry, ContributorImplementor>> CONTRIBUTOR_MAP = new HashMap<>();


static {
//TypeContributorImplementor
ContributorMap.put( PostgreSQLDialect.class, PostgisDialectContributor::new );
CONTRIBUTOR_MAP.put( PostgreSQLDialect.class, PostgisDialectContributor::new );
CONTRIBUTOR_MAP.put( CockroachDialect.class, CockroachDbContributor::new );
}

private ContributorResolver() {
Expand All @@ -34,9 +37,9 @@ private ContributorResolver() {
static ContributorImplementor resolveSpatialtypeContributorImplementor(ServiceRegistry serviceRegistry) {
JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
Dialect dialect = jdbcServices.getDialect();
for ( Class<?> dialectClass : ContributorMap.keySet() ) {
for ( Class<?> dialectClass : CONTRIBUTOR_MAP.keySet() ) {
if ( dialectClass.isAssignableFrom( dialect.getClass() ) ) {
return ContributorMap.get( dialectClass ).apply( serviceRegistry );
return CONTRIBUTOR_MAP.get( dialectClass ).apply( serviceRegistry );
}
}
return null;
Expand Down

This file was deleted.

@@ -0,0 +1,62 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/

package org.hibernate.spatial.dialect.cockroachdb;

import org.hibernate.boot.model.TypeContributions;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spatial.FunctionKey;
import org.hibernate.spatial.GeolatteGeometryType;
import org.hibernate.spatial.HSMessageLogger;
import org.hibernate.spatial.JTSGeometryType;
import org.hibernate.spatial.contributor.ContributorImplementor;
import org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor;
import org.hibernate.spatial.dialect.postgis.PostgisSqmFunctionDescriptors;

public class CockroachDbContributor implements ContributorImplementor {

private final ServiceRegistry serviceRegistry;

public CockroachDbContributor(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}

@Override
public void contributeTypes(TypeContributions typeContributions) {
HSMessageLogger.LOGGER.typeContributions( this.getClass().getCanonicalName() );
typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) );
typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) );
}

@Override
public void contributeFunctions(SqmFunctionRegistry functionRegistry) {
HSMessageLogger.LOGGER.functionContributions( this.getClass().getCanonicalName() );
PostgisSqmFunctionDescriptors postgisFunctions = new PostgisSqmFunctionDescriptors( getServiceRegistry() );

postgisFunctions.asMap()
.forEach( (key, desc) -> {
if ( isUnsupported( key ) ) {
return;
}
functionRegistry.register( key.getName(), desc );
key.getAltName().ifPresent( altName -> functionRegistry.registerAlternateKey(
altName,
key.getName()
) );
} );
}

private boolean isUnsupported(FunctionKey key) {
return key.getName().equalsIgnoreCase( "st_union" );
}

@Override
public ServiceRegistry getServiceRegistry() {
return this.serviceRegistry;
}
}

This file was deleted.

Expand Up @@ -27,12 +27,8 @@ public PostgisDialectContributor(ServiceRegistry serviceRegistry) {

public void contributeTypes(TypeContributions typeContributions) {
HSMessageLogger.LOGGER.typeContributions( this.getClass().getCanonicalName() );
typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );
typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_1 ) );

//Isn't this redundant?
typeContributions.contributeJavaTypeDescriptor( GeolatteGeometryJavaTypeDescriptor.INSTANCE );
typeContributions.contributeJavaTypeDescriptor( JTSGeometryJavaTypeDescriptor.INSTANCE );
typeContributions.contributeType( new GeolatteGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) );
typeContributions.contributeType( new JTSGeometryType( PGGeometryTypeDescriptor.INSTANCE_WKB_2 ) );
}

@Override
Expand Down

0 comments on commit 9316c16

Please sign in to comment.