Skip to content
Permalink
Browse files

JDBC-541 Default character set for database creation in FBManager

  • Loading branch information...
mrotteveel committed Dec 29, 2018
1 parent 42ea403 commit 5a8b1e78a1010d695a7836fbf224984776b3d796
@@ -1080,6 +1080,8 @@ arguments ([JDBC-562](http://tracker.firebirdsql.org/browse/JDBC-562))
- Upgraded antlr-runtime used for generated keys support from 4.7 to 4.7.2.
The grammar generated for version 4.7.2 should still run on 4.7, but we
suggest that you upgrade this dependency.
- Improvement: Added `FBManager.setDefaultCharacterSet` to set default
database character set during database creation ([JDBC-541](http://tracker.firebirdsql.org/browse/JDBC-541))

Removal of deprecated classes and packages
------------------------------------------
@@ -51,6 +51,7 @@
private String password;
private int dialect = ISCConstants.SQL_DIALECT_CURRENT;
private int pageSize = -1;
private String defaultCharacterSet;
private boolean forceCreate;
private boolean createOnStart;
private boolean dropOnStop;
@@ -202,6 +203,16 @@ public int getPageSize() {
return pageSize;
}

@Override
public void setDefaultCharacterSet(String firebirdCharsetName) {
this.defaultCharacterSet = firebirdCharsetName;
}

@Override
public String getDefaultCharacterSet() {
return defaultCharacterSet;
}

@Override
public boolean isCreateOnStart() {
return createOnStart;
@@ -273,6 +284,10 @@ public void createDatabase(String fileName, String user, String password) throws
connectionProperties.getExtraDatabaseParameters()
.addArgument(ISCConstants.isc_dpb_page_size, getPageSize());
}
if (getDefaultCharacterSet() != null) {
connectionProperties.getExtraDatabaseParameters()
.addArgument(ISCConstants.isc_dpb_set_db_charset, getDefaultCharacterSet());
}
try (FbDatabase db = dbFactory.connect(connectionProperties)) {
db.createDatabase();
}
@@ -113,6 +113,20 @@
*/
int getPageSize();

/**
* Set the default database character set.
*
* @param firebirdCharsetName
* Character set name, use Firebird names only; {@code null} will use Firebird default ({@code NONE}).
* Specifying an invalid name will result in an exception during database creation.
*/
void setDefaultCharacterSet(String firebirdCharsetName);

/**
* @return The default character set name, {@code null} means not set.
*/
String getDefaultCharacterSet();

/**
* mbean get-set pair for field createOnStart
* Get the value of createOnStart
@@ -1,5 +1,5 @@
/*
* Firebird Open Source J2ee connector - jdbc driver
* Firebird Open Source JavaEE Connector - JDBC Driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
@@ -12,7 +12,7 @@
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a CVS history command.
* can be obtained from a source control history command.
*
* All rights reserved.
*/
@@ -40,4 +40,14 @@ public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "X", bi);
}

/**
* Null-safe trim.
*
* @param stringToTrim String to trim
* @return result of {@code stringToTrim.trim()} (or {@code null} if {@code stringToTrim} was null
*/
public static String trim(String stringToTrim) {
return stringToTrim == null ? null : stringToTrim.trim();
}
}
@@ -18,14 +18,18 @@
*/
package org.firebirdsql.management;

import org.firebirdsql.common.StringHelper;
import org.firebirdsql.gds.ISCConstants;
import org.firebirdsql.gds.ng.FbDatabase;
import org.firebirdsql.jdbc.FBConnection;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import static org.firebirdsql.common.FBTestProperties.*;
import static org.firebirdsql.gds.VaxEncoding.iscVaxInteger;
@@ -185,4 +189,31 @@ public void testDialect1_dbCreatedWithRightDialect() throws Exception {
}
}
}

@Test
public void testCreate_withDefaultCharacterSet() throws Exception {
try (FBManager m = createFBManager()) {
m.setServer(DB_SERVER_URL);
m.setPort(DB_SERVER_PORT);
m.start();

// Adding .fdb suffix to prevent conflicts with other tests if drop fails
final String databasePath = getDatabasePath() + ".fdb";
try {
m.setDefaultCharacterSet("UTF8");
m.createDatabase(databasePath, DB_USER, DB_PASSWORD);

try (Connection connection = DriverManager.getConnection(getUrl() + ".fdb",
getDefaultPropertiesForConnection());
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select RDB$CHARACTER_SET_NAME from rdb$database")) {

assertTrue("expected a row", rs.next());
assertEquals("Unexpected default character set", "UTF8", StringHelper.trim(rs.getString(1)));
}
} finally {
m.dropDatabase(databasePath, DB_USER, DB_PASSWORD);
}
}
}
}

0 comments on commit 5a8b1e7

Please sign in to comment.
You can’t perform that action at this time.