/** * Register GEOMETRY type and register spatial functions * @param connection Active H2 connection * @throws java.sql.SQLException */ public static void initSpatialExtension(Connection connection) throws SQLException { addSpatialFunctions(connection,""); registerGeometryType(connection); registerSpatialTables(connection); }
/** * Create java code to add function copy paste into * GeoSpatialFunctionsAddRemove to upload it * @param st SQL Statement * @param function Function instance * @param packagePrepend For OSGi environment only, use Bundle-SymbolicName:Bundle-Version: */ public static void registerFunction(Statement st,Function function,String packagePrepend) throws SQLException { registerFunction(st,function,packagePrepend,true); }
/** * Remove spatial type and functions from the current connection. * @param connection Active H2 connection with DROP ALIAS rights */ public static void disposeSpatialExtension(Connection connection) throws SQLException { Statement st = connection.createStatement(); for(Function function : getBuiltInsFunctions()) { unRegisterFunction(st,function); } unRegisterGeometryType(connection); } }
/** * Register all built-ins function * @param connection JDBC Connection * @param packagePrepend For OSGi environment only, use Bundle-SymbolicName:Bundle-Version: * @throws SQLException */ private static void addSpatialFunctions(Connection connection,String packagePrepend) throws SQLException { Statement st = connection.createStatement(); for(Function function : getBuiltInsFunctions()) { try { registerFunction(st,function,packagePrepend); } catch (SQLException ex) { // Catch to register other functions ex.printStackTrace(System.err); } } }
/** * Create a spatial database * @param dbName filename * @param initSpatial If true add spatial features to the database * @param h2Parameters Additional h2 parameters * @return Connection * @throws java.sql.SQLException * @throws java.lang.ClassNotFoundException */ public static Connection createSpatialDataBase(String dbName,boolean initSpatial, String h2Parameters )throws SQLException, ClassNotFoundException { String databasePath = initDBFile(dbName, h2Parameters); org.h2.Driver.load(); // Keep a connection alive to not close the DataBase on each unit test Connection connection = DriverManager.getConnection(databasePath, "sa", "sa"); Statement st = connection.createStatement(); //Create one row table for tests st.execute("CREATE TABLE dummy(id INTEGER);"); st.execute("INSERT INTO dummy values (1)"); // Init spatial ext if(initSpatial) { CreateSpatialExtension.initSpatialExtension(connection); } return connection; } /**
String functionAlias = getAlias(function); if(getBooleanProperty(function,ScalarFunction.PROP_DETERMINISTIC,false)) { deterministic = " DETERMINISTIC"; if(getBooleanProperty(function, ScalarFunction.PROP_NOBUFFER, false)) { nobuffer = " NOBUFFER"; String functionRemarks = getStringProperty(function, Function.PROP_REMARKS); if(!functionRemarks.isEmpty()) { PreparedStatement ps = st.getConnection().prepareStatement("COMMENT ON ALIAS "+functionAlias+" IS ?");
/** * Register geometry type in an OSGi environment * @param connection Active H2 connection * @throws SQLException */ public static void registerGeometryType(Connection connection) throws SQLException { Statement st = connection.createStatement(); for(DomainInfo domainInfo : getBuiltInsType()) { // Check for byte array first, to not throw an enigmatic error CastException st.execute("CREATE DOMAIN IF NOT EXISTS "+domainInfo.getDomainName()+" AS "+GEOMETRY_BASE_TYPE+"("+domainInfo.getGeometryTypeCode()+") CHECK (ST_GeometryTypeCode(VALUE) = "+domainInfo.getGeometryTypeCode()+");"); } }
@Override public void start(BundleContext bc) throws Exception { for(Function function : CreateSpatialExtension.getBuiltInsFunctions()) { bc.registerService(Function.class, function, null); if(function instanceof DriverFunction) { bc.registerService(DriverFunction.class, (DriverFunction) function, null); } } DataSourceTracker dataSourceTracker = new DataSourceTracker(bc); databaseTracker = new ServiceTracker<DataSource, FunctionTracker>(bc,DataSource.class,dataSourceTracker); databaseTracker.open(); }
/** * Register GEOMETRY type and register spatial functions * @param connection Active H2 connection * @param BundleSymbolicName OSGi Bundle symbolic name * @param BundleVersion OSGi Bundle version */ public static void initSpatialExtension(Connection connection, String BundleSymbolicName, String BundleVersion) throws SQLException { String packagePrepend = BundleSymbolicName+":"+BundleVersion+":"; addSpatialFunctions(connection,packagePrepend); registerGeometryType(connection); connection.commit(); }
/** * Release geometry type * @param connection Active h2 connection with DROP DOMAIN and DROP ALIAS rights */ public static void unRegisterGeometryType(Connection connection) throws SQLException { Statement st = connection.createStatement(); DomainInfo[] domainInfos = getBuiltInsType(); for(DomainInfo domainInfo : domainInfos) { st.execute("DROP DOMAIN IF EXISTS " + domainInfo.getDomainName()); } }
String orientation, Value arg3) throws SQLException { if (isColumnListConnection(connection)) { return prepareResultSet();
@Override public void removedService(ServiceReference<Function> reference, Function service) { // Do not unregister system functions (h2spatial functions) if(reference.getBundle().getBundleId() != context.getBundle().getBundleId()) { try { Connection connection = dataSource.getConnection(); try { CreateSpatialExtension.unRegisterFunction(connection.createStatement(), service); } finally { connection.close(); } } catch (SQLException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); } super.removedService(reference, service); } } }
/** * @param function Function instance * @return the function ALIAS, name of the function in SQL engine */ public static String getAlias(Function function) { String functionAlias = getStringProperty(function,Function.PROP_NAME); if(!functionAlias.isEmpty()) { return functionAlias; } return function.getClass().getSimpleName(); } /**
/** * Create a database and return a DataSource * @param dbName * @param initSpatial * @param h2Parameters * @return * @throws SQLException */ public static DataSource createDataSource(String dbName ,boolean initSpatial, String h2Parameters) throws SQLException { // Create H2 memory DataSource org.h2.Driver driver = org.h2.Driver.load(); OsgiDataSourceFactory dataSourceFactory = new OsgiDataSourceFactory(driver); Properties properties = new Properties(); String databasePath = initDBFile(dbName, h2Parameters); properties.setProperty(DataSourceFactory.JDBC_URL, databasePath); properties.setProperty(DataSourceFactory.JDBC_USER, "sa"); properties.setProperty(DataSourceFactory.JDBC_PASSWORD, "sa"); DataSource dataSource = dataSourceFactory.createDataSource(properties); // Init spatial ext if(initSpatial) { Connection connection = dataSource.getConnection(); try { CreateSpatialExtension.initSpatialExtension(connection); } finally { connection.close(); } } return dataSource; } private static String initDBFile( String dbName, String h2_PARAMETERS ) {
@Override public Function addingService(ServiceReference<Function> reference) { // Do not register system functions (h2spatial functions) because it should already be done through initialisation. if(reference.getBundle().getBundleId() != context.getBundle().getBundleId()) { Function function = super.addingService(reference); try { Connection connection = dataSource.getConnection(); try { CreateSpatialExtension.registerFunction(connection.createStatement(), function, ""); //bundle.getSymbolicName() + ":" + bundle.getVersion().toString() + ":" } finally { connection.close(); } } catch (SQLException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); } return function; } else { return super.addingService(reference); } }
Value arg4, Value arg5) throws SQLException { if (isColumnListConnection(connection)) { return prepareResultSet();
/** * Remove the specified function from the provided DataBase connection * @param st Active statement * @param function function to remove * @throws SQLException */ public static void unRegisterFunction(Statement st, Function function) throws SQLException { String functionAlias = getStringProperty(function, Function.PROP_NAME); if(functionAlias.isEmpty()) { functionAlias = function.getClass().getSimpleName(); } st.execute("DROP ALIAS IF EXISTS " + functionAlias); } /**
/** * @param connection Connection * @param inputTable Edges table produced by ST_Graph * @param orientation Orientation string * @param weight Weight * @param arg4 Destination string or destination table * @return Table with closest destination id and distance to closest * destination * @throws SQLException */ public static ResultSet getAccessibility(Connection connection, String inputTable, String orientation, String weight, String arg4) throws SQLException { if (isColumnListConnection(connection)) { return prepareResultSet(); } final KeyedGraph<VAccess, Edge> graph = prepareGraph(connection, inputTable, orientation, weight, VAccess.class, Edge.class); // Decide whether this is a destination string or a table string. if (GraphFunctionParser.isDestinationsString(arg4)) { return compute(graph, prepareDestSet(graph, GraphFunctionParser.parseDestinationsString(arg4))); } else { // arg4 is a destination table. return compute(graph, prepareDestSet(connection, graph, arg4)); } }
Value arg3, Value arg4) throws SQLException { if (isColumnListConnection(connection)) { return prepareResultSet();
final boolean containsGeomField = firstGeometryField != null; final SimpleResultSet output = prepareResultSet(containsGeomField); if (isColumnListConnection(connection)) { return output;