Package org.geotools.jdbc

Examples of org.geotools.jdbc.JDBCDataStore


        params.put(USER.key, db.getProperty(USER.key));
        params.put(PASSWD.key, db.getProperty("password"));
        params.put(DBTYPE.key, dbtype);

        assertTrue(factory.canProcess(params));
        JDBCDataStore store = factory.createDataStore(params);
        assertNotNull(store);
        try {
            // check dialect
            assertTrue(store.getSQLDialect() instanceof OracleDialect);
            // force connection usage
            assertNotNull(store.getSchema(tname("ft1")));
        } finally {
            store.dispose();
        }
    }
View Full Code Here


    private static final String CREATE_DROP_TESTDB = "gt2_create_drop_testdb";

    @Override
    protected boolean isOnline() throws Exception {
        PostgisNGDataStoreFactory factory =  new PostgisNGDataStoreFactory();
        JDBCDataStore closer = new JDBCDataStore();
        Class.forName(factory.getDriverClassName());
       
        // get host and port
        String host = fixture.getProperty(HOST.key);
        String port = fixture.getProperty(PORT.key);
        String user = fixture.getProperty(USER.key);
        String password = fixture.getProperty(PASSWD.key);
        String url = "jdbc:postgresql" + "://" + host + ":" + port + "/template1";
       
        Connection cx = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            cx = DriverManager.getConnection(url, user, password);
            st = cx.createStatement();
            rs = st.executeQuery("select rolcreatedb from pg_authid where rolname = '" + user + "'");
            boolean canCreate = false;
            if(rs.next()) {
                canCreate = rs.getBoolean(1);
            }
            rs.close();
           
            if(!canCreate) {
                System.out.println("User " + user + " has no database creation options, skipping test");
                return false;
            }
           
            // creation options available, let's check if we have the postgis extension available then
            rs = st.executeQuery("select * from pg_available_extensions where name = 'postgis'");
            boolean hasPostgisExtension = false;
            if(rs.next()) {
                hasPostgisExtension = true;
            }
            rs.close();
           
            if(!hasPostgisExtension) {
                System.out.println("This version of postgresql has no postgis extension available (as in, one that can be used by the \"create extension\" command, skipping it");
                return false;
            }
           
            // drop the database if available
            rs = st.executeQuery("select * from pg_database where datname = 'create_drop_testdb'");
            boolean databaseExists = rs.next();
            rs.close();
            if(databaseExists) {
                st.execute("drop database " + CREATE_DROP_TESTDB);
            }
           
            return true;
        } catch(SQLException e) {
            System.out.println("Failed to check if the user has database creation privileges and postgis is an available extension");
            e.printStackTrace();
            return false;
        } finally {
            closer.closeSafe(rs);
            closer.closeSafe(cx);
            closer.closeSafe(st);
        }
    }
View Full Code Here

        // we can work with it
        assertTrue(factory.canProcess(params));
       
        // force database creation and check the store functions
        JDBCDataStore store = factory.createDataStore(params);
        assertNotNull(store);
        store.createSchema(DataUtilities.createType("test", "id:String,polygonProperty:Polygon:srid=32615"));
        store.getSchema("test");
       
        // now disconnect and drop
        store.dispose();
        factory.dropDatabase(params);
       
        // try to connect again, it must fail
        params.remove(PostgisNGDataStoreFactory.CREATE_DB_IF_MISSING.key);
        try {
            store = factory.createDataStore(params);
            store.getTypeNames();
            fail("This one should have failed, the database has just been dropped");
        } catch(Exception e) {
            // fine, it's what we expected
        }
       
View Full Code Here

    @Override
    protected void initializeDatabase() throws Exception {
        DataSource dataSource = getDataSource();
        Connection cx = dataSource.getConnection();
        try {
            PostGISDialect dialect = new PostGISDialect(new JDBCDataStore());
            postgisVersion = dialect.getVersion(cx);
            pgsqlVersion = dialect.getPostgreSQLVersion(cx);
        }
        finally {
            cx.close();
View Full Code Here

        params.put(PostgisNGDataStoreFactory.SIMPLIFY.key, false);
       
        params.put(DBTYPE.key, dbtype);

        assertTrue(factory.canProcess(params));
        JDBCDataStore store = factory.createDataStore(params);
        assertNotNull(store);
        try {
            // check dialect
            assertTrue(store.getSQLDialect() instanceof PostGISDialect);
            // force connection usage
            assertNotNull(store.getSchema(tname("ft1")));
        } finally {
            store.dispose();
        }
    }
View Full Code Here

        params.put(USER.key, db.getProperty(USER.key));
        params.put(PASSWD.key, db.getProperty(PASSWD.key));
       
        // force simplify off
        params.put(PostgisNGDataStoreFactory.SIMPLIFY.key, false);
        JDBCDataStore store = factory.createDataStore(params);
        assertNotNull(store);
        try {
            // check dialect
            PostGISDialect dialect = (PostGISDialect) store.getSQLDialect();
            assertFalse(dialect.isSimplifyEnabled());
            Set<Hints.Key> baseHints = new HashSet<Key>();
            dialect.addSupportedHints(baseHints);
            assertTrue(baseHints.isEmpty());
        } finally {
            store.dispose();
        }
    }
View Full Code Here

        params.put(PORT.key, db.getProperty(PORT.key));
        params.put(USER.key, db.getProperty(USER.key));
        params.put(PASSWD.key, db.getProperty(PASSWD.key));
       
        // do not specify simplify, on by default
        JDBCDataStore store = factory.createDataStore(params);
        assertNotNull(store);
        try {
            // check dialect
            PostGISDialect dialect = (PostGISDialect) store.getSQLDialect();
            assertTrue(dialect.isSimplifyEnabled());
            Set<Hints.Key> baseHints = new HashSet<Key>();
            dialect.addSupportedHints(baseHints);
            assertFalse(baseHints.isEmpty());
            assertTrue(baseHints.contains(Hints.GEOMETRY_SIMPLIFICATION));
        } finally {
            store.dispose();
        }
    }
View Full Code Here

        e.setLastChange(new Date());

        //pass in teh feature entry to the datsatore as user data
        schema.getUserData().put(FeatureEntry.class, e);

        JDBCDataStore dataStore = dataStore();

        //create the feature table
        dataStore.createSchema(schema);

        //update the metadata tables
        //addGeoPackageContentsEntry(e);

        //update the entry
View Full Code Here

        return "jdbc:postgresql" + "://" + host + ":" + port + "/" + db;
    }
   
    protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
        DataSource ds = super.createDataSource(params, dialect);
        JDBCDataStore closer = new JDBCDataStore();

        if (Boolean.TRUE.equals(CREATE_DB_IF_MISSING.lookUp(params))) {
            // verify we can connect
            Connection cx = null;
            boolean canConnect = true;
            try {
                cx = ds.getConnection();
            } catch (SQLException e) {
                canConnect = false;
            } finally {
                closer.closeSafe(cx);
            }

            if (!canConnect) {
                // get the connection params
                String host = (String) HOST.lookUp(params);
                int port = (Integer) PORT.lookUp(params);
                String db = (String) DATABASE.lookUp(params);
                String user = (String) USER.lookUp(params);
                String password = (String) PASSWD.lookUp(params);

                Statement st = null;
                try {
                    // connect to template1 instead
                    String url = "jdbc:postgresql" + "://" + host + ":" + port + "/template1";
                    cx = getConnection(user, password, url);

                    // create the database

                    String createParams = (String) CREATE_PARAMS.lookUp(params);
                    String sql = "CREATE DATABASE \"" + db + "\" " + (createParams == null ? "" : createParams);
                    st = cx.createStatement();
                    st.execute(sql);
                } catch (SQLException e) {
                    throw new IOException("Failed to create the target database", e);
                } finally {
                    closer.closeSafe(st);
                    closer.closeSafe(cx);
                }

                // if we got here the database has been created, now verify it has the postgis
                // extensions
                // and eventually try to create them
                ResultSet rs = null;
                try {
                    String url = "jdbc:postgresql" + "://" + host + ":" + port + "/" + db;
                    cx = DriverManager.getConnection(url, user, password);

                    // check we have postgis
                    st = cx.createStatement();
                    try {
                        rs = st.executeQuery("select PostGIS_version()");
                        rs.close();
                    } catch (SQLException e) {
                        // not available eh? create it
                        st.execute("create extension postgis");
                    }
                } catch (SQLException e) {
                    throw new IOException("Failed to create the target database", e);
                } finally {
                    closer.closeSafe(st);
                    closer.closeSafe(cx);
                }

                // and finally re-create the connection pool
                ds = super.createDataSource(params, dialect);
            }
View Full Code Here

     * the user must have the necessary privileges
     * @param params
     * @throws IOException
     */
    public void dropDatabase(Map<String, Object> params) throws IOException {
        JDBCDataStore closer = new JDBCDataStore();
        // get the connection params
        String host = (String) HOST.lookUp(params);
        int port = (Integer) PORT.lookUp(params);
        String db = (String) DATABASE.lookUp(params);
        String user = (String) USER.lookUp(params);
        String password = (String) PASSWD.lookUp(params);

        Connection cx = null;
        Statement st = null;
        try {
            // connect to template1 instead
            String url = "jdbc:postgresql" + "://" + host + ":" + port + "/template1";
            cx = getConnection(user, password, url);

            // drop the database
            String sql = "DROP DATABASE \"" + db + "\"";
            st = cx.createStatement();
            st.execute(sql);
        } catch (SQLException e) {
            throw new IOException("Failed to drop the target database", e);
        } finally {
            closer.closeSafe(st);
            closer.closeSafe(cx);
        }

    }
View Full Code Here

TOP

Related Classes of org.geotools.jdbc.JDBCDataStore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.