Search in sources :

Example 1 with ADb

use of org.hortonmachine.dbs.compat.ADb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getSwitchDatabaseAction.

public Action getSwitchDatabaseAction(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer) {
    if (isNosql) {
        return new AbstractAction("Switch database") {

            @Override
            public void actionPerformed(ActionEvent e) {
                INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                List<String> databasesNames = db.getDatabasesNames();
                String selectedName = GuiUtilities.showComboDialog(databaseViewer, "Select database", "Select the database to switch to", databasesNames.toArray(new String[0]), db.getDbName());
                if (selectedName != null && !selectedName.equals(db.getDbName())) {
                    ConnectionData connectionData = db.getConnectionData();
                    connectionData.connectionLabel = selectedName;
                    connectionData.connectionUrl = db.getDbEngineUrl() + "/" + selectedName;
                    databaseViewer.openDatabase(connectionData, false);
                }
            }
        };
    } else if (databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGIS || databaseViewer.currentConnectedSqlDatabase.getType() == EDb.POSTGRES) {
        return new AbstractAction("Switch database") {

            @Override
            public void actionPerformed(ActionEvent e) {
                ADb db = databaseViewer.currentConnectedSqlDatabase;
                try {
                    List<String> databasesNames = PGDb.getDatabases(db);
                    ConnectionData connectionData = db.getConnectionData();
                    int lastSlash = connectionData.connectionUrl.lastIndexOf('/');
                    String engineUrl = connectionData.connectionUrl.substring(0, lastSlash);
                    String dbName = connectionData.connectionUrl.substring(lastSlash + 1);
                    String selectedName = GuiUtilities.showComboDialog(databaseViewer, "Select database", "Select the database to switch to", databasesNames.toArray(new String[0]), dbName);
                    if (selectedName != null && !selectedName.equals(dbName)) {
                        connectionData.connectionUrl = engineUrl + "/" + selectedName;
                        connectionData.connectionLabel = selectedName;
                        databaseViewer.openDatabase(connectionData, false);
                    }
                } catch (Exception ex) {
                    GuiUtilities.handleError(databaseViewer, ex);
                    Logger.INSTANCE.e("Error", ex);
                }
            }
        };
    }
    return null;
}
Also used : ActionEvent(java.awt.event.ActionEvent) List(java.util.List) ArrayList(java.util.ArrayList) AbstractAction(javax.swing.AbstractAction) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) ConnectionData(org.hortonmachine.dbs.compat.ConnectionData) IOException(java.io.IOException) ADb(org.hortonmachine.dbs.compat.ADb)

Example 2 with ADb

use of org.hortonmachine.dbs.compat.ADb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getSaveConnectionAction.

public Action getSaveConnectionAction(DatabaseViewer databaseViewer) {
    return new AbstractAction("Save Connection") {

        @SuppressWarnings("unchecked")
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                ConnectionData connectionData = null;
                if (databaseViewer.currentConnectedSqlDatabase != null) {
                    ADb db = databaseViewer.currentConnectedSqlDatabase;
                    connectionData = db.getConnectionData();
                } else if (databaseViewer.currentConnectedNosqlDatabase != null) {
                    INosqlDb db = databaseViewer.currentConnectedNosqlDatabase;
                    connectionData = db.getConnectionData();
                }
                String newName = GuiUtilities.showInputDialog(databaseViewer, "Enter a name for the saved connection", "db connection " + new DateTime().toString(HMConstants.dateTimeFormatterYYYYMMDDHHMMSS));
                connectionData.connectionLabel = newName;
                byte[] savedDbs = PreferencesHandler.getPreference(HM_SAVED_DATABASES, new byte[0]);
                List<ConnectionData> connectionDataList = new ArrayList<>();
                try {
                    connectionDataList = (List<ConnectionData>) convertFromBytes(savedDbs);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                connectionDataList.add(connectionData);
                byte[] inBytes = convertObjectToBytes(connectionDataList);
                PreferencesHandler.setPreference(HM_SAVED_DATABASES, inBytes);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) AbstractAction(javax.swing.AbstractAction) ConnectionData(org.hortonmachine.dbs.compat.ConnectionData) INosqlDb(org.hortonmachine.dbs.nosql.INosqlDb) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) ADb(org.hortonmachine.dbs.compat.ADb)

Example 3 with ADb

use of org.hortonmachine.dbs.compat.ADb in project hortonmachine by TheHortonMachine.

the class SqlTemplatesAndActions method getGenerateInsertExportAction.

public Action getGenerateInsertExportAction(TableLevel table, DatabaseViewer spatialiteViewer) {
    if (isNosql) {
        return null;
    }
    return new AbstractAction("Generate insert sql statements") {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                DefaultGuiBridgeImpl gBridge = new DefaultGuiBridgeImpl();
                File[] saveFiles = gBridge.showSaveFileDialog("Save sql file", PreferencesHandler.getLastFile(), null);
                if (saveFiles != null && saveFiles.length > 0) {
                    try {
                        PreferencesHandler.setLastPath(saveFiles[0].getAbsolutePath());
                    } catch (Exception e1) {
                        logger.insertError("SqlTemplatesAndActions", "ERROR", e1);
                    }
                } else {
                    return;
                }
                File saveFile = saveFiles[0];
                ADb db = spatialiteViewer.currentConnectedSqlDatabase;
                QueryResult result = db.getTableRecordsMapFromRawSql("select * from " + table.tableName, -1);
                List<String> colNames = result.names;
                List<String> types = result.types;
                List<Object[]> data = result.data;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.size(); i++) {
                    Object[] record = data.get(i);
                    sb.append("INSERT INTO ").append(table.tableName).append("(");
                    StringBuilder namesSb = new StringBuilder();
                    StringBuilder valuesSb = new StringBuilder();
                    boolean firstDone = false;
                    for (int j = 0; j < record.length; j++) {
                        if (record[j] != null) {
                            String name = colNames.get(j);
                            String type = types.get(j);
                            EDataType etype = EDataType.getType4Name(type);
                            if (etype == EDataType.BLOB || etype == EDataType.GEOMETRY) {
                                continue;
                            } else {
                                if (firstDone) {
                                    namesSb.append(",");
                                    valuesSb.append(",");
                                }
                                namesSb.append(name);
                                String value = record[j].toString();
                                value = ADb.escapeSql(value);
                                if (etype == EDataType.TEXT) {
                                    valuesSb.append("'").append(value).append("'");
                                } else {
                                    valuesSb.append(value);
                                }
                                firstDone = true;
                            }
                        }
                    }
                    sb.append(namesSb.toString());
                    sb.append(") VALUES (");
                    sb.append(valuesSb.toString());
                    sb.append(");\n");
                }
                FileUtilities.writeFile(sb.toString(), saveFile);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
}
Also used : ActionEvent(java.awt.event.ActionEvent) EDataType(org.hortonmachine.dbs.datatypes.EDataType) DefaultGuiBridgeImpl(org.hortonmachine.gui.utils.DefaultGuiBridgeImpl) IOException(java.io.IOException) QueryResult(org.hortonmachine.dbs.compat.objects.QueryResult) AbstractAction(javax.swing.AbstractAction) File(java.io.File) ADb(org.hortonmachine.dbs.compat.ADb)

Example 4 with ADb

use of org.hortonmachine.dbs.compat.ADb in project hortonmachine by TheHortonMachine.

the class TestMbtiles method testWriting.

@Test
public void testWriting() throws Exception {
    String tempDir = System.getProperty("java.io.tmpdir");
    String dbPath = tempDir + File.separator + "jgt-dbs-testmbtiles.mbtiles" + DB_TYPE.getExtensionOnCreation();
    TestUtilities.deletePrevious(tempDir, dbPath, DB_TYPE);
    MBTilesDb fromMdb = new MBTilesDb(testDb);
    Envelope fromBounds = fromMdb.getBounds();
    try (ADb toDb = EDb.H2.getDb()) {
        toDb.open(dbPath);
        MBTilesDb toMdb = new MBTilesDb(toDb);
        toMdb.createTables(false);
        toMdb.fillMetadata((float) fromBounds.getMaxY(), (float) fromBounds.getMinY(), (float) fromBounds.getMinX(), (float) fromBounds.getMaxX(), fromMdb.getName(), fromMdb.getImageFormat(), fromMdb.getMinZoom(), fromMdb.getMaxZoom());
        List<Integer> availableZoomLevels = fromMdb.getAvailableZoomLevels();
        for (int zoomLevel : availableZoomLevels) {
            int[] boundsInTileIndex = fromMdb.getBoundsInTileIndex(zoomLevel);
            for (int col = boundsInTileIndex[0]; col <= boundsInTileIndex[1]; col++) {
                for (int row = boundsInTileIndex[2]; row <= boundsInTileIndex[3]; row++) {
                    byte[] tileData = fromMdb.getTile(col, row, zoomLevel);
                    toMdb.addTile(col, row, zoomLevel, tileData);
                }
            }
        }
        toMdb.createIndexes();
    }
    try (ADb toDb = EDb.H2.getDb()) {
        toDb.open(dbPath);
        testTestDb(toDb);
    }
    new File(dbPath + "." + DB_TYPE.getExtension()).delete();
}
Also used : MBTilesDb(org.hortonmachine.dbs.mbtiles.MBTilesDb) Envelope(org.locationtech.jts.geom.Envelope) File(java.io.File) ADb(org.hortonmachine.dbs.compat.ADb) Test(org.junit.Test)

Example 5 with ADb

use of org.hortonmachine.dbs.compat.ADb in project hortonmachine by TheHortonMachine.

the class TestDbsPwdMain method testRightPassword.

@Test
public void testRightPassword() throws Exception {
    String tempDir = System.getProperty("java.io.tmpdir");
    String dbPath = tempDir + File.separator + "jgt-dbs-testdbspwdmain4" + DB_TYPE.getExtensionOnCreation();
    TestUtilities.deletePrevious(tempDir, dbPath, DB_TYPE);
    try (ADb db = DB_TYPE.getDb()) {
        db.setCredentials("testuser", "testpwd");
        db.open(dbPath);
    }
    try (ADb db = DB_TYPE.getDb()) {
        db.setCredentials("testuser", "testpwd");
        db.open(dbPath);
    }
}
Also used : ADb(org.hortonmachine.dbs.compat.ADb) Test(org.junit.Test)

Aggregations

ADb (org.hortonmachine.dbs.compat.ADb)8 Test (org.junit.Test)5 ActionEvent (java.awt.event.ActionEvent)3 IOException (java.io.IOException)3 AbstractAction (javax.swing.AbstractAction)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ConnectionData (org.hortonmachine.dbs.compat.ConnectionData)2 INosqlDb (org.hortonmachine.dbs.nosql.INosqlDb)2 List (java.util.List)1 QueryResult (org.hortonmachine.dbs.compat.objects.QueryResult)1 EDataType (org.hortonmachine.dbs.datatypes.EDataType)1 MBTilesDb (org.hortonmachine.dbs.mbtiles.MBTilesDb)1 DefaultGuiBridgeImpl (org.hortonmachine.gui.utils.DefaultGuiBridgeImpl)1 DateTime (org.joda.time.DateTime)1 Envelope (org.locationtech.jts.geom.Envelope)1