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;
}
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();
}
}
};
}
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();
}
}
};
}
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();
}
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);
}
}
Aggregations